Use replacePage() for the initial page setting to prevent having 2 video pages
[harmattan/cameraplus] / qml / main.qml
1 // -*- qml -*-
2
3 /*!
4  * This file is part of CameraPlus.
5  *
6  * Copyright (C) 2012 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 1.1
24 import com.nokia.meego 1.1
25 import com.nokia.extras 1.1
26 import QtCamera 1.0
27 import CameraPlus 1.0
28 import QtMobility.systeminfo 1.2
29 import QtMobility.location 1.2
30
31 // TODO: postcapture
32 // TODO: flash not ready
33 // TODO: focus, caf, ...
34 // TODO: portrait/landscape
35 // TODO: record video in a hidden directory and then copy the video to avoid tracker indexing it.
36 // TODO: stop viewfinder in settings pages ?
37 // TODO: prevent going to mass storage while recording and capturing
38 // TODO: grid lines, face tracking, ambr
39 // TODO: complete settings pages
40 // TODO: stop camera properly when we get closed.
41 // TODO: select primary/secondary camera.
42 // TODO: disable debug builds.
43 // TODO: a way to get buffers to the application
44 // TODO: fcam like functionality (precise control over capture parameters).
45
46 PageStackWindow {
47         id: root
48
49         property alias dimmer: camDimmer
50
51         showStatusBar: false
52
53         Component.onCompleted: {
54                 theme.inverted = true;
55                 if (settings.mode == Camera.VideoMode) {
56                         // TODO: We will use replacePage for now.
57                         // If we use openPage() then we end up with 2 video pages
58                         // stacked on top of each other.
59                         // The first one is created when the camera mode gets
60                         // changed to video upon startup and this becomes the 2nd one.
61                         replacePage("VideoPage.qml");
62                 }
63                 else {
64                         replacePage("ImagePage.qml");
65                 }
66         }
67
68         function showError(msg) {
69                 error.text = msg;
70                 error.show();
71         }
72
73         PositionSource {
74                 // NOTE: The source will not reset the position when we lose the signal.
75                 // This shouldn't be a big problem as we are course enough.
76                 // If we ever need street level updates then this will be an issue.
77                 id: positionSource
78                 active: settings.useGps
79                 // TODO: we cannot bind to cam.running because camera will stop
80                 // when the connection dialog pops up and we end up with an infinite loop
81                 // active: cam.running && settings.useGps
82                 onPositionChanged: geocode.search(position.coordinate.longitude, position.coordinate.latitude);
83         }
84
85         MetaData {
86                 id: metaData
87                 camera: cam
88                 manufacturer: deviceInfo.manufacturer
89                 model: deviceInfo.model
90                 country: geocode.country
91                 city: geocode.city
92                 suburb: geocode.suburb
93                 longitude: positionSource.position.coordinate.longitude
94                 longitudeValid: positionSource.position.longitudeValid && settings.useGps
95                 latitude: positionSource.position.coordinate.latitude
96                 latitudeValid: positionSource.position.latitudeValid && settings.useGps
97                 elevation: positionSource.position.coordinate.altitude
98                 elevationValid: positionSource.position.altitudeValid && settings.useGps
99                 orientation: orientation.orientation
100                 artist: settings.creatorName
101                 captureDirection: compass.direction
102                 captureDirectionValid: compass.directionValid
103                 horizontalError: positionSource.position.horizontalAccuracy
104                 horizontalErrorValid: positionSource.position.horizontalAccuracyValid && settings.useGps
105                 dateTimeEnabled: true
106         }
107
108         Orientation {
109                 id: orientation
110                 active: cam.running
111         }
112
113         Compass {
114                 id: compass
115                 active: cam.running
116         }
117
118         ReverseGeocode {
119                 id: geocode
120                 active: cam.running && settings.useGps && settings.useGeotags
121         }
122
123         CameraResources {
124                 id: resourcePolicy
125                 onAcquiredChanged: {
126                         if (resourcePolicy.acquired) {
127                                 // TODO:
128                         }
129                         else {
130                                 // TODO: We need a way to force a stop.
131                         }
132                 }
133         }
134
135         DeviceInfo {
136                 id: deviceInfo
137         }
138
139         FSMonitor {
140                 id: fileSystem
141         }
142
143         InfoBanner {
144                 id: error
145         }
146
147         Settings {
148                 id: settings
149         }
150
151         FileNaming {
152                 id: fileNaming
153                 imageSuffix: cam.imageSuffix
154                 videoSuffix: cam.videoSuffix
155         }
156
157         function replacePage(file) {
158                 pageStack.replace(Qt.resolvedUrl(file), {cam: cam}, true);
159         }
160
161         function openFile(file) {
162                 pageStack.push(Qt.resolvedUrl(file), {cam: cam});
163         }
164
165         platformStyle: PageStackWindowStyle {
166                 // TODO: Hack
167                 background: " "
168         }
169
170         ImageSettings {
171                 id: imageSettings
172                 camera: cam
173                 function setImageResolution() {
174                         if (!imageSettings.setResolution(settings.imageAspectRatio, settings.imageResolution)) {
175                                 showError(qsTr("Failed to set required resolution"));
176                         }
177                 }
178
179                 onReadyChanged: {
180                         if (ready) {
181                                 imageSettings.setImageResolution();
182                         }
183                 }
184         }
185
186         VideoSettings {
187                 id: videoSettings
188                 camera: cam
189
190                 function setVideoResolution() {
191                         if (!videoSettings.setResolution(settings.videoAspectRatio, settings.videoResolution)) {
192                                 showError(qsTr("Failed to set required resolution"));
193                         }
194                 }
195
196                 onReadyChanged: {
197                         if (ready) {
198                                 videoSettings.setVideoResolution();
199                         }
200                 }
201         }
202
203         Connections {
204                 target: settings
205
206                 onImageAspectRatioChanged: {
207                         imageSettings.setImageResolution();
208                 }
209
210                 onImageResolutionChanged: {
211                         imageSettings.setImageResolution();
212                 }
213
214                 onVideoResolutionChanged: {
215                         videoSettings.setVideoResolution();
216                 }
217         }
218
219         Camera {
220 /*
221                 onDeviceIdChanged: {
222                         // TODO: is this needed ?
223                         if (platformWindow.active) {
224                                 cam.start();
225                         }
226                 }
227 */
228                 id: cam
229                 anchors.fill: parent
230
231                 onError: {
232                         console.log("Camera error (" + code + "): " + message + " " + debug);
233                         showError(qsTr("Camera error. Please restart the application."));
234                         cam.stop();
235                 }
236
237                 // TODO: hardcoding device id
238                 Component.onCompleted: { cam.deviceId = 0; mode = settings.mode; }
239                 Component.onDestruction: {
240                 console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
241                 }
242
243                 // TODO: Hack
244                 z: -1
245
246                 Rectangle {
247                         id: camDimmer
248                         z: 1
249                         anchors.fill: parent
250                         opacity: 0
251                         color: "black"
252                 }
253
254                 notifications: Sounds {
255                         id: sounds
256                         mute: !settings.soundEnabled
257                 }
258         }
259
260         Scene {
261                 id: sceneController
262                 camera: cam
263                 value: ready ? camera.mode == Camera.VideoMode ? settings.videoSceneMode : settings.imageSceneMode : 0
264         }
265
266         ColorTone {
267                 id: colorToneController
268                 camera: cam
269                 value: ready ? camera.mode == Camera.VideoMode ? settings.videoColorFilter : settings.imageColorFilter : 0
270         }
271
272         WhiteBalance {
273                 id: whiteBalanceController
274                 camera: cam
275                 value: ready ? camera.mode == Camera.VideoMode ? settings.videoWhiteBalance : settings.imageWhiteBalance : 0
276         }
277
278         ModeController {
279                 id: cameraMode
280                 cam: cam
281                 dimmer: root.dimmer
282         }
283
284         Iso {
285                 id: iso
286                 camera: cam
287                 value: ready ? settings.imageIso : 0
288         }
289
290         Connections {
291                 target: cam
292                 onModeChanged: {
293                         if (cam.mode == Camera.VideoMode) {
294                                 replacePage("VideoPage.qml");
295                         }
296                         else {
297                                 replacePage("ImagePage.qml");
298                         }
299                 }
300         }
301 }