First attempt at removing QtQuick 1.1 hardcoding from qml files.
[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_QT_QUICK@
24 import CameraPlus 1.0
25 import com.nokia.meego 1.1
26 import QtCamera 1.0
27
28 Item {
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
53         delegate: PostCaptureItem {
54             width: view.width
55             height: view.height
56             onClicked: hideTimer.running = !hideTimer.running
57         }
58
59         onFlickingChanged: {
60             if (flicking && hideTimer.running) {
61                 restartTimer()
62             }
63         }
64     }
65
66     property variant postCaptureModel: postCaptureModelLoader.item ?
67         postCaptureModelLoader.item.model : null
68     property bool loadModel: mainView.currentIndex == 2 && Qt.application.active
69
70     Loader {
71         id: postCaptureModelLoader
72         sourceComponent: loadModel ? postCaptureModelComponent : undefined
73     }
74
75     // We have to do it that way because Loader does not support non-visual elements.
76     Component {
77         id: postCaptureModelComponent
78
79         Item {
80             property alias model: postCaptureModel
81
82             PostCaptureModel {
83                 id: postCaptureModel
84                 manufacturer: deviceInfo.manufacturer
85                 model: deviceInfo.model
86                 Component.onCompleted: reload()
87                 onError: {
88                     console.log("Error populating model " + msg)
89                     showError(qsTr("Failed to load captures"))
90                 }
91             }
92         }
93     }
94
95     Timer {
96         id: hideTimer
97         running: false
98         interval: 3000
99     }
100
101     CameraToolBar {
102         id: toolBar
103         expanded: true
104         hideBack: true
105         anchors.bottom: parent.bottom
106         anchors.bottomMargin: show ? 20 : -1 * (height + 20)
107         anchors.left: parent.left
108         anchors.leftMargin: 20
109         opacity: 0.8
110
111         property bool show: deleteDialog.status == DialogStatus.Open ||
112             deleteDialog.status == DialogStatus.Opening ||
113             hideTimer.running || menu.status == DialogStatus.Open ||
114             menu.status == DialogStatus.Opening ||
115             (view.currentItem && view.currentItem.error) && !view.currentItem.playing
116
117         Behavior on anchors.bottomMargin {
118             PropertyAnimation { duration: 200; }
119         }
120
121         items: [
122             ToolIcon {
123                 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"
124                 onClicked: {
125                     addOrRemoveFavorite()
126                     restartTimer()
127                 }
128             },
129             ToolIcon {
130                 iconId: available ? "icon-m-toolbar-share-white" : "icon-m-toolbar-share-dimmed-white"
131                 onClicked: {
132                     shareCurrentItem()
133                     restartTimer()
134                 }
135             },
136             ToolIcon {
137                 iconId: available ? "icon-m-toolbar-delete-white" : "icon-m-toolbar-delete-dimmed-white"
138                 onClicked: {
139                     deleteCurrentItem()
140                     restartTimer()
141                 }
142             },
143             ToolIcon {
144                 iconId: "icon-m-toolbar-view-menu-white"
145                 onClicked: {
146                     menu.open()
147                     restartTimer()
148                 }
149             }
150         ]
151     }
152
153     QueryDialog {
154         id: deleteDialog
155         titleText: qsTr("Delete item?");
156         acceptButtonText: qsTr("Yes");
157         rejectButtonText: qsTr("No");
158
159         onStatusChanged: restartTimer()
160
161         onAccepted: {
162             if (!remove.remove(view.currentItem.itemData.url)) {
163                 showError(qsTr("Failed to delete item"))
164             } else {
165                 postCaptureModel.remove(view.currentItem.itemData);
166             }
167         }
168
169         DeleteHelper {
170             id: remove
171         }
172     }
173
174     Menu {
175         id: menu
176         onStatusChanged: restartTimer()
177
178         MenuLayout {
179             MenuItem {
180                 text: qsTr("Captures in gallery")
181                 onClicked: launchGallery()
182             }
183
184             MenuItem {
185                 text: qsTr("View in gallery")
186                 enabled: available
187                 onClicked: showInGallery()
188             }
189         }
190     }
191
192     Rectangle {
193         opacity: toolBar.opacity
194         anchors.top: parent.top
195         anchors.topMargin: toolBar.show ? 20 : -1 * (height + 20)
196         anchors.left: parent.left
197         anchors.leftMargin: 20
198         anchors.right: parent.right
199         anchors.rightMargin: 20
200         visible: toolBar.visible
201         height: screen.isPortrait ? toolBar.height * 2 : toolBar.height
202         color: toolBar.color
203         border.color: toolBar.border.color
204         radius: toolBar.radius
205
206         Behavior on anchors.topMargin {
207             PropertyAnimation { duration: 200; }
208         }
209
210         Flow {
211             width: parent.width - 40
212             x: 20
213             height: parent.height
214
215             Label {
216                 text: view.currentItem ? view.currentItem.itemData.title : ""
217                 width: parent.width / 2
218                 height: parent.height
219                 font.bold: true
220                 verticalAlignment: Text.AlignVCenter
221                 horizontalAlignment: Text.AlignLeft
222             }
223
224             Label {
225                 text: view.currentItem ? view.currentItem.itemData.created : ""
226                 width: parent.width / 2
227                 height: parent.height
228                 font.bold: true
229                 verticalAlignment: Text.AlignVCenter
230                 horizontalAlignment: Text.AlignRight
231             }
232         }
233     }
234
235     function launchGallery() {
236         if (!gallery.launch()) {
237             showError(qsTr("Failed to launch gallery"))
238         }
239     }
240
241     function showInGallery() {
242         if (!available) {
243             return
244         }
245
246         if (!gallery.show(view.currentItem.itemUrl)) {
247             showError(qsTr("Failed to launch gallery"))
248         }
249     }
250
251     function deleteCurrentItem() {
252         if (!available) {
253             return
254         }
255
256         deleteDialog.message = view.currentItem.itemData.fileName
257         deleteDialog.open()
258     }
259
260     function shareCurrentItem() {
261         if (!available) {
262             return
263         }
264
265         if (!share.share(view.currentItem.itemData.url)) {
266             showError(qsTr("Failed to launch share service"))
267         }
268     }
269
270     function addOrRemoveFavorite() {
271         if (!available) {
272             return
273         }
274
275         if (view.currentItem.itemData.favorite) {
276             if (!trackerStore.removeFromFavorites(view.currentItem.itemData.url)) {
277                 showError(qsTr("Failed to remove favorite"))
278             } else {
279                 view.currentItem.itemData.favorite = false
280             }
281         } else {
282             if (!trackerStore.addToFavorites(view.currentItem.itemData.url)) {
283                 showError(qsTr("Failed to add favorite"))
284             } else {
285                 view.currentItem.itemData.favorite = true
286             }
287         }
288     }
289
290     function restartTimer() {
291         hideTimer.restart()
292     }
293
294     function policyLost() {
295         if (view.currentItem && view.currentItem.playing) {
296             view.currentItem.stopPlayback()
297         }
298     }
299 }