resetCamera() will set needed resolution after resetting camera.
[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             imageSettings.setImageResolution()
103         }
104
105         onImageResolutionChanged: {
106             imageSettings.setImageResolution()
107         }
108
109         onVideoResolutionChanged: {
110             videoSettings.setVideoResolution()
111         }
112     }
113
114     PipelineManager {
115         id: pipelineManager
116         camera: viewfinder.camera
117         currentItem: mainView.currentItem
118     }
119
120     function resetCamera(deviceId, mode) {
121         if (!viewfinder.camera.reset(deviceId, mode)) {
122             showError(qsTr("Failed to set camera device and mode. Please restart the application."))
123             return false
124         }
125
126         if (mode == Camera.ImageMode) {
127             imageSettings.setImageResolution()
128         }
129         else if (mode == Camera.VideoMode) {
130             videoSettings.setVideoResolution()
131         }
132
133         return true
134     }
135
136     function showError(msg) {
137         error.text = msg
138         error.show()
139     }
140
141     property alias dimmer: camDimmer
142     CameraPositionSource {
143         id: positionSource
144         active: settings.useGps
145         // TODO: we cannot bind to cam.running because camera will stop
146         // when the connection dialog pops up and we end up with an infinite loop
147         // active: cam.running && settings.useGps
148         onPositionChanged: geocode.search(position.coordinate.longitude, position.coordinate.latitude)
149     }
150
151     MetaData {
152         id: metaData
153         camera: viewfinder.camera
154         manufacturer: deviceInfo.manufacturer
155         model: deviceInfo.model
156         country: geocode.country
157         city: geocode.city
158         suburb: geocode.suburb
159         longitude: positionSource.longitude
160         longitudeValid: positionSource.longitudeValid && settings.useGps
161         latitude: positionSource.latitude
162         latitudeValid: positionSource.latitudeValid && settings.useGps
163         elevation: positionSource.altitude
164         elevationValid: positionSource.altitudeValid && settings.useGps
165         orientation: orientation.orientation
166         artist: settings.creatorName
167         captureDirection: compass.direction
168         captureDirectionValid: compass.directionValid
169         horizontalError: positionSource.horizontalAccuracy
170         horizontalErrorValid: positionSource.horizontalAccuracyValid && settings.useGps
171         dateTimeEnabled: true
172     }
173
174     CameraOrientation {
175         id: orientation
176         active: viewfinder.camera.running || (mainView.currentIndex == 2 && Qt.application.active)
177     }
178
179     CameraCompass {
180         id: compass
181         active: viewfinder.camera.running
182     }
183
184     ReverseGeocode {
185         id: geocode
186         active: viewfinder.camera.running && settings.useGps && settings.useGeotags
187     }
188
189     DeviceInfo {
190         id: deviceInfo
191     }
192
193     FSMonitor {
194         id: fileSystem
195     }
196
197     CameraInfoBanner {
198         id: error
199     }
200
201     FileNaming {
202         id: fileNaming
203         imageSuffix: viewfinder.camera.imageSuffix
204         videoSuffix: viewfinder.camera.videoSuffix
205         imagePath: platformSettings.imagePath
206         videoPath: platformSettings.videoPath
207         temporaryVideoPath: platformSettings.temporaryVideoPath
208     }
209
210     MountProtector {
211         id: mountProtector
212     }
213
214     TrackerStore {
215         id: trackerStore
216         active: viewfinder.camera.running
217         manufacturer: deviceInfo.manufacturer
218         model: deviceInfo.model
219     }
220
221     ImageSettings {
222         id: imageSettings
223         camera: viewfinder.camera
224
225         function setImageResolution() {
226             if (!imageSettings.setResolution(settings.imageAspectRatio, settings.imageResolution)) {
227                 showError(qsTr("Failed to set required resolution"))
228             }
229         }
230     }
231
232     VideoSettings {
233         id: videoSettings
234         camera: viewfinder.camera
235
236         function setVideoResolution() {
237             if (!videoSettings.setResolution(settings.videoAspectRatio, settings.videoResolution)) {
238                 showError(qsTr("Failed to set required resolution"))
239             }
240         }
241     }
242
243     ModeController {
244         id: cameraMode
245         cam: viewfinder.camera
246         dimmer: root.dimmer
247     }
248
249     Rectangle {
250         property bool dimmed: false
251         id: camDimmer
252         z: 1
253         anchors.fill: parent
254         opacity: dimmed ? 1.0 : 0.0
255         color: "black"
256         Behavior on opacity {
257             PropertyAnimation { duration: 150 }
258         }
259     }
260
261     DeviceKeys {
262         id: keys
263         active: Qt.application.active && pipelineManager.scaleAcquired
264         repeat: !settings.zoomAsShutter
265     }
266
267     Standby {
268         policyLost: pipelineManager.state == "policyLost"
269         show: !Qt.application.active || pipelineManager.showStandBy ||
270             (mainView.currentIndex == 1 && !viewfinder.camera.running)
271     }
272 }