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