d650b9d49a4518b7862c2bba2b2b0415e8ca3faa
[harmattan/cameraplus] / qml / MainPage.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 QtCamera 1.0
25 import CameraPlus 1.0
26
27 // TODO: flash not ready (battery low or flash not ready message)
28 // TODO: front camera
29
30 CameraPage {
31     id: root
32
33     CameraTheme {
34         id: cameraTheme
35     }
36
37     VisualItemModel {
38         id: mainModel
39
40         SettingsView {
41             camera: viewfinder.camera
42             width: mainView.width
43             height: mainView.height
44         }
45
46         CameraView {
47             id: viewfinder
48             width: mainView.width
49             height: mainView.height
50         }
51
52         PostCaptureView {
53             camera: viewfinder.camera
54             width: mainView.width
55             height: mainView.height
56         }
57     }
58
59     ListView {
60         id: mainView
61         LayoutMirroring.enabled: false
62         anchors.fill: parent
63         orientation: ListView.Horizontal
64         model: mainModel
65         snapMode: ListView.SnapOneItem
66         highlightRangeMode: ListView.StrictlyEnforceRange
67         boundsBehavior: Flickable.StopAtBounds
68         currentIndex: 1
69         interactive: !currentItem.pressed
70     }
71
72     Component.onCompleted: {
73         platformSettings.init()        
74         root.resetCamera(settings.device, settings.mode)
75     }
76
77     PlatformSettings {
78         id: platformSettings
79     }
80
81     Settings {
82         id: settings
83         onDeviceChanged: {
84             // Reset pipeline manager error
85             pipelineManager.error = false
86
87             if (root.resetCamera(settings.device, settings.mode)) {
88                 pipelineManager.startCamera()
89             }
90         }
91     }
92
93     PipelineManager {
94         id: pipelineManager
95         camera: viewfinder.camera
96         currentItem: mainView.currentItem
97     }
98
99     function resetCamera(deviceId, mode) {
100         if (!viewfinder.camera.reset(deviceId, mode)) {
101             showError(qsTr("Failed to set camera device and mode. Please restart the application."))
102             return false
103         }
104
105         return true
106     }
107
108     function showError(msg) {
109         error.text = msg
110         error.show()
111     }
112
113     property alias dimmer: camDimmer
114     CameraPositionSource {
115         id: positionSource
116         active: settings.useGps
117         // TODO: we cannot bind to cam.running because camera will stop
118         // when the connection dialog pops up and we end up with an infinite loop
119         // active: cam.running && settings.useGps
120         onPositionChanged: geocode.search(position.coordinate.longitude, position.coordinate.latitude)
121     }
122
123     MetaData {
124         id: metaData
125         camera: viewfinder.camera
126         manufacturer: deviceInfo.manufacturer
127         model: deviceInfo.model
128         country: geocode.country
129         city: geocode.city
130         suburb: geocode.suburb
131         longitude: positionSource.longitude
132         longitudeValid: positionSource.longitudeValid && settings.useGps
133         latitude: positionSource.latitude
134         latitudeValid: positionSource.latitudeValid && settings.useGps
135         elevation: positionSource.altitude
136         elevationValid: positionSource.altitudeValid && settings.useGps
137         orientation: orientation.orientation
138         artist: settings.creatorName
139         captureDirection: compass.direction
140         captureDirectionValid: compass.directionValid
141         horizontalError: positionSource.horizontalAccuracy
142         horizontalErrorValid: positionSource.horizontalAccuracyValid && settings.useGps
143         dateTimeEnabled: true
144     }
145
146     CameraOrientation {
147         id: orientation
148         active: viewfinder.camera.running || (mainView.currentIndex == 2 && Qt.application.active)
149     }
150
151     CameraCompass {
152         id: compass
153         active: viewfinder.camera.running
154     }
155
156     ReverseGeocode {
157         id: geocode
158         active: viewfinder.camera.running && settings.useGps && settings.useGeotags
159     }
160
161     DeviceInfo {
162         id: deviceInfo
163     }
164
165     FSMonitor {
166         id: fileSystem
167     }
168
169     CameraInfoBanner {
170         id: error
171     }
172
173     FileNaming {
174         id: fileNaming
175         imageSuffix: viewfinder.camera.imageSuffix
176         videoSuffix: viewfinder.camera.videoSuffix
177     }
178
179     MountProtector {
180         id: mountProtector
181         path: fileNaming.path
182     }
183
184     TrackerStore {
185         id: trackerStore
186         active: viewfinder.camera.running
187         manufacturer: deviceInfo.manufacturer
188         model: deviceInfo.model
189     }
190
191     function checkDiskSpace() {
192         return fileSystem.hasFreeSpace(fileNaming.path)
193     }
194
195     ImageSettings {
196         id: imageSettings
197         camera: viewfinder.camera
198
199         function setImageResolution() {
200             var aspectRatio = settings.device == 1 ? settings.secondaryImageAspectRatio : settings.primaryImageAspectRatio
201             var resolution = settings.device == 1 ? settings.secondaryImageResolution : settings.primaryImageResolution
202
203             if (!imageSettings.setResolution(aspectRatio, resolution)) {
204                 showError(qsTr("Failed to set required resolution"))
205             }
206         }
207
208         onReadyChanged: {
209             if (ready) {
210                 imageSettings.setImageResolution()
211             }
212         }
213     }
214
215     VideoSettings {
216         id: videoSettings
217         camera: viewfinder.camera
218
219         function setVideoResolution() {
220             var aspectRatio = settings.device == 1 ? settings.secondaryVideoAspectRatio : settings.primaryVideoAspectRatio
221             var resolution = settings.device == 1 ? settings.secondaryVideoResolution : settings.primaryVideoResolution
222
223             if (!videoSettings.setResolution(aspectRatio, resolution)) {
224                 showError(qsTr("Failed to set required resolution"))
225             }
226         }
227
228         onReadyChanged: {
229             if (ready) {
230                 videoSettings.setVideoResolution()
231             }
232         }
233     }
234
235     Connections {
236         target: settings
237
238         onPrimaryImageResolutionChanged: imageSettings.setImageResolution()
239         onPrimaryImageAspectRatioChanged: imageSettings.setImageResolution()
240         onSecondaryImageResolutionChanged: imageSettings.setImageResolution()
241         onSecondaryImageAspectRatioChanged: imageSettings.setImageResolution()
242
243         onPrimaryVideoResolutionChanged: videoSettings.setVideoResolution()
244         onPrimaryVideoAspectRatioChanged: videoSettings.setVideoResolution()
245         onSecondaryVideoResolutionChanged: videoSettings.setVideoResolution()
246         onSecondaryVideoAspectRatioChanged: videoSettings.setVideoResolution()
247     }
248
249     ModeController {
250         id: cameraMode
251         cam: viewfinder.camera
252         dimmer: root.dimmer
253     }
254
255     Rectangle {
256         property bool dimmed: false
257         id: camDimmer
258         z: 1
259         anchors.fill: parent
260         opacity: dimmed ? 1.0 : 0.0
261         color: "black"
262         Behavior on opacity {
263             PropertyAnimation { duration: 150 }
264         }
265     }
266
267     DeviceKeys {
268         id: keys
269         active: Qt.application.active && pipelineManager.scaleAcquired
270         repeat: !settings.zoomAsShutter
271     }
272
273     Standby {
274         policyLost: pipelineManager.state == "policyLost"
275         show: !Qt.application.active || pipelineManager.showStandBy ||
276             (mainView.currentIndex == 1 && !viewfinder.camera.running)
277     }
278 }