harbour-books/app/qml/BooksShelfView.qml

337 lines
12 KiB
QML
Raw Normal View History

2015-06-28 14:22:35 +03:00
/*
2021-08-07 04:47:20 +03:00
Copyright (C) 2015-2021 Jolla Ltd.
Copyright (C) 2015-2021 Slava Monich <slava.monich@jolla.com>
2015-06-28 14:22:35 +03:00
2019-04-01 02:06:55 +03:00
You may use this file under the terms of BSD license as follows:
2015-06-28 14:22:35 +03:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
2019-04-01 02:06:55 +03:00
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
3. Neither the names of the copyright holders nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
2015-06-28 14:22:35 +03:00
*/
import QtQuick 2.0
import Sailfish.Silica 1.0
import harbour.books 1.0
2019-04-01 02:06:55 +03:00
import "harbour"
Item {
2015-06-28 14:22:35 +03:00
id: shelfView
property int shelfIndex
property bool singleStorage
property bool removableStorage
property bool editMode
property bool deleteAllRequest
property real cellWidth
property real cellHeight
property alias view: grid
property alias shelf: shelfModel
property alias device: shelfModel.device
property alias dummyItemIndex: shelfModel.dummyItemIndex
property alias bookCount: shelfModel.bookCount
property alias bookCountVisible: storageHeader.needed
2015-06-28 14:22:35 +03:00
signal openBook(var book)
signal dropItem(var mouseX, var mouseY)
signal startEditing()
signal stopEditing()
signal cancelDeleteAll()
signal scrollRight()
signal scrollLeft()
2021-08-07 04:47:20 +03:00
readonly property bool _haveBooks: shelfModel.count > 0
readonly property int _cellsPerRow: Math.floor(width/cellWidth)
2015-06-28 14:22:35 +03:00
readonly property int _remorseTimeout: 5000
2021-08-07 04:47:20 +03:00
readonly property bool _loading: shelfModel.loading || startAnimationTimer.running
2015-06-28 14:22:35 +03:00
property var _remorse
on_HaveBooksChanged: if (!_haveBooks) shelfView.stopEditing()
Shelf {
id: shelfModel
property bool needDummyItem: dragInProgress && dragItem.shelfIndex !== shelfView.shelfIndex
property bool _completed // Received Component.onCompleted
2015-06-28 14:22:35 +03:00
onNeedDummyItemChanged: if (needDummyItem) hasDummyItem = true
editMode: shelfView.editMode
onRelativePathChanged: longStartTimer.restart()
onPathChanged: {
if (Settings.currentStorage === device && _completed) {
Settings.currentFolder = path
}
}
onDeviceChanged: {
if (device === Settings.currentStorage) {
relativePath = Settings.relativePath
}
}
Component.onCompleted: _completed = true
}
BooksPathModel {
id: pathModel
path: shelfModel.relativePath
2015-06-28 14:22:35 +03:00
}
onEditModeChanged: {
dragItem.visible = false
shelf.cancelAllDeleteRequests()
dragArea.draggedItemIndex = -1
fadeAnimation.stop()
if (!editMode) {
dragArea.resetPressState()
dragScrollAnimation.stop()
if (_remorse) _remorse.cancel()
}
}
function doDeleteAll() {
if (deleteAllRequest) {
fadeAnimation.stop()
shelf.removeAll()
shelf.cancelAllDeleteRequests()
}
}
onDeleteAllRequestChanged: {
if (deleteAllRequest) {
if (!_remorse) _remorse = remorseComponent.createObject(shelfView)
fadeAnimation.restart()
//% "Deleting all books"
_remorse.execute(qsTrId("harbour-books-shelf-view-about_to_delete_all"),
2015-06-28 14:22:35 +03:00
doDeleteAll, _remorseTimeout)
} else if (_remorse) {
fadeAnimation.stop()
_remorse.cancel()
}
}
Component {
id: remorseComponent
RemorsePopup {
onCanceled: {
shelfView.cancelDeleteAll()
shelf.cancelAllDeleteRequests()
fadeAnimation.stop()
}
}
}
Connections {
target: dragItem
onAnimatingChanged: if (!dragItem.animating && !dragArea.pressed) dragArea.finishDrag()
}
BooksStorageHeader {
id: storageHeader
removable: removableStorage
count: shelfModel.bookCount
2015-06-28 14:22:35 +03:00
showCount: !_loading
enabled: grid.contentY > grid.minContentY || pathModel.count > 0
needed: !singleStorage || pathModel.count > 0
onClicked: {
if (!scrollToTopAnimation.running) {
if (grid.contentY > grid.minContentY) {
scrollToTopAnimation.start()
} else {
animationEnabled = true
shelfModel.relativePath = ""
}
}
}
}
NumberAnimation {
id: scrollToTopAnimation
target: grid
property: "contentY"
duration: 500
easing.type: Easing.InOutQuad
to: grid.minContentY
2015-06-28 14:22:35 +03:00
}
SilicaGridView {
id: grid
anchors {
top: storageHeader.bottom
2015-06-28 14:22:35 +03:00
left: parent.left
right: parent.right
bottom: parent.bottom
leftMargin: Math.floor((shelfView.width - _cellsPerRow * shelfView.cellWidth)/2)
2015-06-28 14:22:35 +03:00
}
model: shelfModel
interactive: !dragInProgress && !scrollToTopAnimation.running
2015-06-28 14:22:35 +03:00
clip: true
cellWidth: shelfView.cellWidth
cellHeight: shelfView.cellHeight
flickableDirection: Flickable.VerticalFlick
header: Column {
Repeater {
model: pathModel
BooksShelfTitle {
width: grid.width
text: model.name
editable: editMode && currentFolder
2015-11-30 00:18:09 +03:00
currentFolder: model.index === (pathModel.count-1)
onClicked: {
if (!currentFolder) {
2015-11-30 00:18:09 +03:00
console.log("switching to", model.path)
2015-11-30 18:57:27 +03:00
shelfView.stopEditing()
2015-11-30 00:18:09 +03:00
shelfModel.relativePath = model.path
}
}
2015-11-30 18:57:27 +03:00
onRename: {
console.log(to)
shelfModel.name = to
2015-11-30 18:57:27 +03:00
}
onStartEdit: shelfView.startEditing()
}
}
}
2015-06-28 14:22:35 +03:00
delegate: BooksShelfItem {
editMode: shelfView.editMode
dropped: dragItem.dropShelfIndex >= 0 &&
dragItem.dropShelfIndex === shelfView.shelfIndex &&
dragItem.dropItemIndex >= 0 &&
dragItem.dropItemIndex === model.index
dragged: dragItem.shelfIndex === shelfView.shelfIndex &&
dragArea.draggedItemIndex === model.index
visible: !model.dummy && !dragged && !dropped
pressed: model.accessible && (model.index === dragArea.pressedItemIndex) && (model.index !== dragArea.pressedDeleteItemIndex)
deleting: model.deleteRequested
deletingAll: shelfView.deleteAllRequest
deleteAllOpacity: grid.itemOpacity
width: shelfView.cellWidth
height: shelfView.cellHeight
book: model.book
name: model.name
copyingIn: model.copyingIn
copyingOut: model.copyingOut
copyProgress: model.copyProgress
2015-06-28 14:22:35 +03:00
remorseTimeout: _remorseTimeout
onScalingChanged: updateLastPressedItemScalingIndex()
onPressedChanged: updateLastPressedItemScalingIndex()
onDeleteMe: shelfView.shelf.remove(model.index)
function updateLastPressedItemScalingIndex() {
if (pressed && scaling) {
dragArea.lastPressedItemScalingIndex = model.index
} else if (dragArea.lastPressedItemScalingIndex === model.index) {
dragArea.lastPressedItemScalingIndex = -1
}
}
}
footer: BooksShelfFooter {
width: grid.width
height: visible ? Math.max(implicitHeight, (grid.height > grid.headerItem.height) ? (grid.height - grid.headerItem.height) : 0) : 0
allowBusyIndicator: !longStartTimer.running
footerState: _haveBooks ? 0 : _loading ? 1 : 2
visible: !_haveBooks
}
2015-06-28 14:22:35 +03:00
property real itemOpacity: 1
property real minContentY: -headerItem.height
2015-06-28 14:22:35 +03:00
moveDisplaced: Transition {
SmoothedAnimation { properties: "x,y"; duration: 150 }
}
removeDisplaced: Transition {
SmoothedAnimation { properties: "x,y"; duration: 150 }
}
NumberAnimation {
id: fadeAnimation
target: grid
property: "itemOpacity"
from: 1
to: 0
duration: _remorseTimeout
easing.type: Easing.OutCubic
}
onActiveFocusChanged: console.log("BooksShelfView.grid", activeFocus)
2015-06-28 14:22:35 +03:00
Behavior on y { SpringAnimation {} }
VerticalScrollDecorator {}
}
BooksDragArea {
id: dragArea
dragParent: storageView
gridView: grid
onDeleteItemAt: {
if (!shelfView.deleteAllRequest) {
shelfView.shelf.setDeleteRequested(index, true);
}
}
onDropItem: shelfView.dropItem(mouseX, mouseY)
onActiveFocusChanged: console.log("BooksShelfView.grid.dragArea", activeFocus)
Component.onCompleted: {
console.log("BooksDragArea created")
grid.focus = true
}
}
2015-06-28 14:22:35 +03:00
Timer {
id: longStartTimer
interval: 500
running: true
onTriggered: {
if (shelf.loading) {
console.log(shelfModel.path, "startup is taking too long")
startAnimationTimer.start()
}
}
}
Timer {
id: startAnimationTimer
interval: 2000
}
2015-07-12 17:11:55 +03:00
Loader {
id: leftSwipeHintLoader
anchors.fill: parent
2020-02-03 05:33:57 +03:00
active: BooksHints.storageLeftSwipe < MaximumHintCount || running
2015-07-12 17:11:55 +03:00
property bool running
sourceComponent: Component {
2019-04-01 02:06:55 +03:00
HarbourHorizontalSwipeHint {
hintEnabled: !_loading &&
2015-07-12 17:11:55 +03:00
storageView.visible &&
shelfIndex == storageListWatcher.currentIndex &&
(shelfIndex+1) < storageModel.count &&
2020-02-03 05:33:57 +03:00
BooksHints.storageLeftSwipe < MaximumHintCount
2015-07-12 17:11:55 +03:00
2019-04-01 02:06:55 +03:00
//% "Swipe left to see what's on the SD-card"
text: qsTrId("harbour-books-storage-view-swipe_left_hint")
swipeRight: false
2020-02-03 05:33:57 +03:00
onHintShown: BooksHints.storageLeftSwipe++
2015-07-12 17:11:55 +03:00
onHintRunningChanged: leftSwipeHintLoader.running = hintRunning
}
}
}
2015-06-28 14:22:35 +03:00
}