indented
[harmattan/cameraplus] / qml / PostCaptureView.qml
1 // -*- qml -*-
2
3 /*!
4  * This file is part of CameraPlus.
5  *
6  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 import QtQuick 2.0
24 import CameraPlus 1.0
25 import QtCamera 1.0
26
27 Item {
28     property Camera camera: null
29     property bool pressed: view.currentItem ? view.currentItem.playing : false
30     property int policyMode: view.currentItem && view.currentItem.playing ?
31         CameraResources.Player : settings.mode == Camera.VideoMode ? CameraResources.Video :
32         CameraResources.Image
33     property bool available: view.currentItem ? view.currentItem.itemData.available : false
34
35     ShareHelper {
36         id: share
37         settings: platformSettings
38     }
39
40     GalleryHelper {
41         id: gallery
42         settings: platformSettings
43     }
44
45     ListView {
46         id: view
47         anchors.fill: parent
48         snapMode: ListView.SnapOneItem
49         cacheBuffer: height * 3
50         model: postCaptureModel
51         highlightRangeMode: ListView.StrictlyEnforceRange
52         interactive: view.currentItem && view.currentItem.playing ? false : true
53
54         delegate: PostCaptureItem {
55             width: view.width
56             height: view.height
57             onClicked: hideTimer.running = !hideTimer.running
58         }
59
60         onFlickingChanged: {
61             if (flicking && hideTimer.running) {
62                 restartTimer()
63             }
64         }
65     }
66
67     property variant postCaptureModel: postCaptureModelLoader.item ?
68         postCaptureModelLoader.item.model : null
69     property bool loadModel: mainView.currentIndex == 2 && Qt.application.active
70
71     Loader {
72         id: postCaptureModelLoader
73         sourceComponent: loadModel ? postCaptureModelComponent : undefined
74     }
75
76     // We have to do it that way because Loader does not support non-visual elements.
77     Component {
78         id: postCaptureModelComponent
79
80         Item {
81             property alias model: postCaptureModel
82
83             PostCaptureModel {
84                 id: postCaptureModel
85                 manufacturer: deviceInfo.manufacturer
86                 model: deviceInfo.model
87                 Component.onCompleted: reload()
88                 onError: {
89                     console.log("Error populating model " + msg)
90                     showError(qsTr("Failed to load captures"))
91                 }
92             }
93         }
94     }
95
96     Timer {
97         id: hideTimer
98         running: false
99         interval: 3000
100     }
101
102     CameraToolBar {
103         id: toolBar
104         expanded: true
105         hideBack: true
106         anchors.bottom: parent.bottom
107         anchors.bottomMargin: show ? 20 : -1 * (height + 20)
108         anchors.left: parent.left
109         anchors.leftMargin: 20
110         opacity: 0.8
111
112         property bool show: deleteDialog.isOpen || deleteDialog.isOpening ||
113             hideTimer.running || menu.isOpen || menu.isOpening ||
114             (view.currentItem && view.currentItem.error) && !view.currentItem.playing
115
116         Behavior on anchors.bottomMargin {
117             PropertyAnimation { duration: 200; }
118         }
119
120         items: [
121             CameraToolIcon {
122                 iconId: !available ? "icon-m-toolbar-favorite-mark-dimmed-white" : view.currentItem.itemData.favorite ? "icon-m-toolbar-favorite-mark-white" : "icon-m-toolbar-favorite-unmark-white"
123                 onClicked: {
124                     addOrRemoveFavorite()
125                     restartTimer()
126                 }
127             },
128             CameraToolIcon {
129                 iconId: available ? "icon-m-toolbar-share-white" : "icon-m-toolbar-share-dimmed-white"
130                 onClicked: {
131                     shareCurrentItem()
132                     restartTimer()
133                 }
134             },
135             CameraToolIcon {
136                 iconId: available ? "icon-m-toolbar-delete-white" : "icon-m-toolbar-delete-dimmed-white"
137                 onClicked: {
138                     deleteCurrentItem()
139                     restartTimer()
140                 }
141             },
142             CameraToolIcon {
143                 iconId: "icon-m-toolbar-view-menu-white"
144                 onClicked: {
145                     menu.open()
146                     restartTimer()
147                 }
148             }
149         ]
150     }
151
152     CameraQueryDialog {
153         id: deleteDialog
154         titleText: qsTr("Delete item?");
155         acceptButtonText: qsTr("Yes");
156         rejectButtonText: qsTr("No");
157
158         onStatusChanged: restartTimer()
159
160         onAccepted: {
161             if (!remove.remove(view.currentItem.itemData.url)) {
162                 showError(qsTr("Failed to delete item"))
163             } else {
164                 postCaptureModel.remove(view.currentItem.itemData);
165             }
166         }
167
168         DeleteHelper {
169             id: remove
170         }
171     }
172
173     CameraMenu {
174         id: menu
175         onStatusChanged: restartTimer()
176
177         actions: [
178             CameraMenuAction {
179                 text: qsTr("Captures in gallery")
180                 onClicked: launchGallery()
181             },
182             CameraMenuAction {
183                 // TODO: this is not working...
184                 text: qsTr("View in gallery")
185                 enabled: available
186                 onClicked: showInGallery()
187             }
188         ]
189     }
190
191     Rectangle {
192         opacity: toolBar.opacity
193         anchors.top: parent.top
194         anchors.topMargin: toolBar.show ? 20 : -1 * (height + 20)
195         anchors.left: parent.left
196         anchors.leftMargin: 20
197         anchors.right: parent.right
198         anchors.rightMargin: 20
199         visible: toolBar.visible
200         height: screen.isPortrait ? toolBar.height * 2 : toolBar.height
201         color: toolBar.color
202         border.color: toolBar.border.color
203         radius: toolBar.radius
204
205         Behavior on anchors.topMargin {
206             PropertyAnimation { duration: 200; }
207         }
208
209         Flow {
210             width: parent.width - 40
211             x: 20
212             height: parent.height
213
214             CameraLabel {
215                 text: view.currentItem ? view.currentItem.itemData.title : ""
216                 width: parent.width / 2
217                 height: parent.height
218                 font.bold: true
219                 verticalAlignment: Text.AlignVCenter
220                 horizontalAlignment: Text.AlignLeft
221             }
222
223             CameraLabel {
224                 text: view.currentItem ? view.currentItem.itemData.created : ""
225                 width: parent.width / 2
226                 height: parent.height
227                 font.bold: true
228                 verticalAlignment: Text.AlignVCenter
229                 horizontalAlignment: Text.AlignRight
230             }
231         }
232     }
233
234     function launchGallery() {
235         if (!gallery.launch()) {
236             showError(qsTr("Failed to launch gallery"))
237         }
238     }
239
240     function showInGallery() {
241         if (!available) {
242             return
243         }
244
245         if (!gallery.show(view.currentItem.itemUrl)) {
246             showError(qsTr("Failed to launch gallery"))
247         }
248     }
249
250     function deleteCurrentItem() {
251         if (!available) {
252             return
253         }
254
255         deleteDialog.message = view.currentItem.itemData.fileName
256         deleteDialog.open()
257     }
258
259     function shareCurrentItem() {
260         if (!available) {
261             return
262         }
263
264         if (!share.share(view.currentItem.itemData.url)) {
265             showError(qsTr("Failed to launch share service"))
266         }
267     }
268
269     function addOrRemoveFavorite() {
270         if (!available) {
271             return
272         }
273
274         if (view.currentItem.itemData.favorite) {
275             if (!trackerStore.removeFromFavorites(view.currentItem.itemData.url)) {
276                 showError(qsTr("Failed to remove favorite"))
277             } else {
278                 view.currentItem.itemData.favorite = false
279             }
280         } else {
281             if (!trackerStore.addToFavorites(view.currentItem.itemData.url)) {
282                 showError(qsTr("Failed to add favorite"))
283             } else {
284                 view.currentItem.itemData.favorite = true
285             }
286         }
287     }
288
289     function restartTimer() {
290         hideTimer.restart()
291     }
292
293     function policyLost() {
294         if (view.currentItem && view.currentItem.playing) {
295             view.currentItem.stopPlayback()
296         }
297     }
298 }