Kill data.js and move all icons and functions to CameraTheme
[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 // TODO: qrc:/qml/PostCaptureView.qml:104:5: QML CameraToolBar: Binding loop detected for property "height"
28 Item {
29     id: postCaptureView
30
31     property Camera camera: null
32     property bool pressed: view.currentItem ? view.currentItem.playing : false
33     property int policyMode: view.currentItem && view.currentItem.playing ?
34         CameraResources.Player : settings.mode == Camera.VideoMode ? CameraResources.Video :
35         CameraResources.Image
36     property bool available: view.currentItem ? view.currentItem.itemData.available : false
37
38     Connections {
39         target: view.currentItem
40         onPlayingChanged: {
41             if (view.currentItem.playing) {
42                 hideTimer.running = false
43             }
44         }
45     }
46
47     ShareHelper {
48         id: share
49         settings: platformSettings
50     }
51
52     GalleryHelper {
53         id: gallery
54         settings: platformSettings
55     }
56
57     ListView {
58         id: view
59         anchors.fill: parent
60         snapMode: ListView.SnapOneItem
61         cacheBuffer: height * 3
62         model: postCaptureModel
63         highlightRangeMode: ListView.StrictlyEnforceRange
64         interactive: view.currentItem && view.currentItem.playing ? false : true
65
66         delegate: PostCaptureItem {
67             width: view.width
68             height: view.height
69             onClicked: hideTimer.running = !hideTimer.running
70         }
71
72         onFlickingChanged: {
73             if (flicking && hideTimer.running) {
74                 restartTimer()
75             }
76         }
77     }
78
79     property variant postCaptureModel: postCaptureModelLoader.item ?
80         postCaptureModelLoader.item.model : null
81     property bool loadModel: mainView.currentIndex == 2 && Qt.application.active
82
83     Loader {
84         id: postCaptureModelLoader
85         sourceComponent: loadModel ? postCaptureModelComponent : undefined
86     }
87
88     // We have to do it that way because Loader does not support non-visual elements.
89     Component {
90         id: postCaptureModelComponent
91
92         Item {
93             property alias model: postCaptureModel
94
95             PostCaptureModel {
96                 id: postCaptureModel
97                 manufacturer: deviceInfo.manufacturer
98                 model: deviceInfo.model
99                 Component.onCompleted: reload()
100                 onError: {
101                     console.log("Error populating model " + msg)
102                     showError(qsTr("Failed to load captures"))
103                 }
104             }
105         }
106     }
107
108     Timer {
109         id: hideTimer
110         running: false
111         interval: 3000
112     }
113
114     CameraToolBar {
115         id: toolBar
116         expanded: true
117         hideBack: true
118         anchors.bottom: parent.bottom
119         anchors.bottomMargin: show ? 20 : -1 * (height + 20)
120         anchors.left: parent.left
121         anchors.leftMargin: 20
122         anchors.right: parent.right
123         anchors.rightMargin: 20
124         opacity: 0.8
125
126         property bool show: deleteDialog.isOpen || deleteDialog.isOpening ||
127             hideTimer.running || menu.isOpen || menu.isOpening ||
128             (view.currentItem && view.currentItem.error) && !view.currentItem.playing
129
130         Behavior on anchors.bottomMargin {
131             PropertyAnimation { duration: view.currentItem && view.currentItem.playing ? 0 : 200 }
132         }
133
134         tools: CameraToolBarTools {
135             CameraToolIcon {
136                 iconId: !available ? cameraTheme.favoriteDisabledIconId : view.currentItem.itemData.favorite ? cameraTheme.favoriteMarkIconId : cameraTheme.favoriteUnmarkIconId
137                 onClicked: {
138                     addOrRemoveFavorite()
139                     restartTimer()
140                 }
141             }
142
143             CameraToolIcon {
144                 iconId: available ? cameraTheme.shareEnabledIconId : cameraTheme.shareDisabledIconId
145                 onClicked: {
146                     shareCurrentItem()
147                     restartTimer()
148                 }
149             }
150
151             CameraToolIcon {
152                 iconId: available ? cameraTheme.deleteEnabledIconId : cameraTheme.deleteDisabledIconId
153                 onClicked: {
154                     deleteCurrentItem()
155                     restartTimer()
156                 }
157             }
158
159             CameraToolIcon {
160                 iconId: cameraTheme.menuIconId
161                 onClicked: {
162                     menu.open()
163                     restartTimer()
164                 }
165             }
166         }
167     }
168
169     CameraQueryDialog {
170         id: deleteDialog
171         titleText: qsTr("Delete item?");
172         acceptButtonText: qsTr("Yes");
173         rejectButtonText: qsTr("No");
174
175         onStatusChanged: restartTimer()
176
177         onAccepted: {
178             if (!remove.remove(view.currentItem.itemData.url)) {
179                 showError(qsTr("Failed to delete item"))
180             } else {
181                 postCaptureModel.remove(view.currentItem.itemData);
182             }
183         }
184
185         DeleteHelper {
186             id: remove
187         }
188     }
189
190     CameraMenu {
191         id: menu
192         onStatusChanged: restartTimer()
193
194         actions: [
195             CameraMenuAction {
196                 text: qsTr("Captures in gallery")
197                 onClicked: launchGallery()
198             },
199             CameraMenuAction {
200                 // TODO: this is not working...
201                 text: qsTr("View in gallery")
202                 enabled: available
203                 onClicked: showInGallery()
204             }
205         ]
206     }
207
208     Rectangle {
209         opacity: toolBar.opacity
210         anchors.top: parent.top
211         anchors.topMargin: toolBar.show ? 20 : -1 * (height + 20)
212         anchors.left: parent.left
213         anchors.leftMargin: 20
214         anchors.right: parent.right
215         anchors.rightMargin: 20
216         visible: toolBar.visible
217         height: screen.isPortrait ? toolBar.height * 2 : toolBar.height
218         color: toolBar.color
219         border.color: toolBar.border.color
220         radius: toolBar.radius
221
222         Behavior on anchors.topMargin {
223             PropertyAnimation { duration: view.currentItem && view.currentItem.playing ? 0 : 200 }
224         }
225
226         Flow {
227             width: parent.width - 40
228             x: 20
229             height: parent.height
230
231             CameraLabel {
232                 text: view.currentItem ? view.currentItem.itemData.title : ""
233                 width: parent.width / 2
234                 height: parent.height
235                 font.bold: true
236                 verticalAlignment: Text.AlignVCenter
237                 horizontalAlignment: Text.AlignLeft
238             }
239
240             CameraLabel {
241                 text: view.currentItem ? view.currentItem.itemData.created : ""
242                 width: parent.width / 2
243                 height: parent.height
244                 font.bold: true
245                 verticalAlignment: Text.AlignVCenter
246                 horizontalAlignment: Text.AlignRight
247             }
248         }
249     }
250
251     function launchGallery() {
252         if (!gallery.launch()) {
253             showError(qsTr("Failed to launch gallery"))
254         }
255     }
256
257     function showInGallery() {
258         if (!available) {
259             return
260         }
261
262         if (!gallery.show(view.currentItem.itemUrl)) {
263             showError(qsTr("Failed to launch gallery"))
264         }
265     }
266
267     function deleteCurrentItem() {
268         if (!available) {
269             return
270         }
271
272         deleteDialog.message = view.currentItem.itemData.fileName
273         deleteDialog.open()
274     }
275
276     function shareCurrentItem() {
277         if (!available) {
278             return
279         }
280
281         if (!share.share(view.currentItem.itemData.url)) {
282             showError(qsTr("Failed to launch share service"))
283         }
284     }
285
286     function addOrRemoveFavorite() {
287         if (!available) {
288             return
289         }
290
291         if (view.currentItem.itemData.favorite) {
292             if (!trackerStore.removeFromFavorites(view.currentItem.itemData.url)) {
293                 showError(qsTr("Failed to remove favorite"))
294             } else {
295                 view.currentItem.itemData.favorite = false
296             }
297         } else {
298             if (!trackerStore.addToFavorites(view.currentItem.itemData.url)) {
299                 showError(qsTr("Failed to add favorite"))
300             } else {
301                 view.currentItem.itemData.favorite = true
302             }
303         }
304     }
305
306     function restartTimer() {
307         hideTimer.restart()
308     }
309
310     function policyLost() {
311         if (view.currentItem && view.currentItem.playing) {
312             view.currentItem.stopPlayback()
313         }
314     }
315 }