Don't set close to false when we deactivate ProximitySensor
[harmattan/cameraplus] / qml / CameraView.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 Viewfinder {
28     id: viewfinder
29     property bool pressed: focusReticle.locked || preview.animationRunning
30         || (loader.item ? loader.item.pressed : false)
31     property int policyMode: loader.item ? loader.item.policyMode : CameraResources.None
32
33     camera: cam
34     cameraConfig: cam.cameraConfig
35     renderingEnabled: mainView.currentItem == viewfinder
36
37     Component.onDestruction: cam.stop()
38
39     GridLines {
40         x: viewfinder.renderArea.x
41         y: viewfinder.renderArea.y
42         width: viewfinder.renderArea.width
43         height: viewfinder.renderArea.height
44         visible: settings.gridEnabled
45     }
46
47     PhoneProfile {
48         id: phoneProfile
49     }
50
51     Camera {
52         id: cam
53         sounds: Sounds {
54             id: sounds
55             mute: !settings.soundEnabled || phoneProfile.isSilent
56             volume: volumeControl.fullVolume ? Sounds.VolumeHigh : Sounds.VolumeLow
57             imageCaptureStart: platformSettings.imageCaptureStartedSound
58             imageCaptureEnd: platformSettings.imageCaptureEndedSound
59             videoRecordingStart: platformSettings.videoRecordingStartedSound
60             videoRecordingEnd: platformSettings.videoRecordingEndedSound
61             autoFocusAcquired: platformSettings.autoFocusAcquiredSound
62         }
63
64         onFocusChanged: focus.value = Focus.ContinuousNormal
65         onRoiChanged: roi.normalize = false
66
67         onRunningChanged: {
68             if (!cam.running) {
69                 mountProtector.unlockAll()
70             }
71         }
72
73         onError: {
74             if (pipelineManager.error) {
75                 // Ignore any subsequent errors.
76                 // Killing pulseaudio while recording will lead to an
77                 // infinite supply of errors which will break the UI
78                 // if we show a banner for each.
79                 return
80             }
81
82             pipelineManager.error = true
83             if (loader.item) {
84                 loader.item.cameraError()
85             }
86
87             console.log("Camera error (" + code + "): " + message + " " + debug)
88             showError(qsTr("Camera error. Please restart the application."))
89             // We cannot stop camera here. Seems there is a race condition somewhere
90             // which leads to a freeze if we do so.
91         }
92
93     }
94
95     SoundVolumeControl {
96         id: volumeControl
97     }
98
99     BatteryInfo {
100         id: batteryMonitor
101         active: cam.running
102
103         function check() {
104             if (!checkBattery()) {
105                 loader.item.batteryLow()
106             }
107         }
108
109         onChargingChanged: {
110             batteryMonitor.check()
111         }
112
113         onCriticalChanged: {
114             batteryMonitor.check()
115         }
116     }
117
118     PreviewImage {
119         id: preview
120     }
121
122     FocusReticle {
123         id: focusReticle
124         cam: cam
125         videoResolution: viewfinder.videoResolution
126         renderArea: viewfinder.renderArea
127
128         visible: loader.item != null && loader.item.controlsVisible &&
129             cam.autoFocus.canFocus(cam.scene.value)
130         cafStatus: cam ? cam.autoFocus.cafStatus : -1
131         status: cam ? cam.autoFocus.status : -1
132     }
133
134     Loader {
135         id: loader
136         property string src: cam.mode == Camera.VideoMode ? "VideoOverlay.qml" : "ImageOverlay.qml"
137         anchors.fill: parent
138         source: Qt.resolvedUrl(src)
139     }
140
141     Connections {
142         target: loader.item
143         onPreviewAvailable: preview.setPreview(uri)
144     }
145
146     Binding {
147         target: loader.item
148         property: "cam"
149         value: cam
150         when: loader.item != null
151     }
152
153     Binding {
154         target: loader.item
155         property: "animationRunning"
156         value: preview.animationRunning
157         when: loader.item != null
158     }
159
160     /* Camera bindings */
161     Binding {
162         target: cam.flash
163         property: "value"
164         when: cam.mode == Camera.ImageMode && !root.deviceChangeInProgress
165         value: settings.imageFlashMode
166     }
167
168     Binding {
169         target: settings
170         property: "imageFlashMode"
171         when: cam.mode == Camera.ImageMode && !root.deviceChangeInProgress
172         value: cam.flash.value
173     }
174
175     Binding {
176         target: cam.scene
177         property: "value"
178         when: cam.mode == Camera.VideoMode && !root.deviceChangeInProgress
179         value: settings.videoSceneMode
180     }
181
182     Binding {
183         target: cam.scene
184         property: "value"
185         when: cam.mode == Camera.ImageMode && !root.deviceChangeInProgress
186         value: settings.imageSceneMode
187     }
188
189     Binding {
190         target: cam.evComp
191         property: "value"
192         when: cam.mode == Camera.ImageMode && !root.deviceChangeInProgress
193         value: settings.imageEvComp
194     }
195
196     Binding {
197         target: cam.evComp
198         property: "value"
199         when: cam.mode == Camera.VideoMode && !root.deviceChangeInProgress
200         value: settings.videoEvComp
201     }
202
203     Binding {
204         target: settings
205         property: "imageEvComp"
206         when: cam.mode == Camera.ImageMode && !root.deviceChangeInProgress
207         value: cam.evComp.value
208     }
209
210     Binding {
211         target: settings
212         property: "videoEvComp"
213         when: cam.mode == Camera.VideoMode && !root.deviceChangeInProgress
214         value: cam.evComp.value
215     }
216
217     Binding {
218         target: cam.whiteBalance
219         property: "value"
220         when: cam.mode == Camera.ImageMode && !root.deviceChangeInProgress
221         value: settings.imageWhiteBalance
222     }
223
224     Binding {
225         target: cam.whiteBalance
226         property: "value"
227         when: cam.mode == Camera.VideoMode && !root.deviceChangeInProgress
228         value: settings.videoWhiteBalance
229     }
230
231     Binding {
232         target: cam.colorTone
233         property: "value"
234         when: cam.mode == Camera.ImageMode && !root.deviceChangeInProgress
235         value: settings.imageColorFilter
236     }
237
238     Binding {
239         target: cam.colorTone
240         property: "value"
241         when: cam.mode == Camera.VideoMode && !root.deviceChangeInProgress
242         value: settings.videoColorFilter
243     }
244
245     Binding {
246         target: cam.iso
247         property: "value"
248         when: cam.mode == Camera.ImageMode && !root.deviceChangeInProgress
249         value: settings.imageIso
250     }
251
252     Binding {
253         target: settings
254         property: "imageIso"
255         when: cam.mode == Camera.ImageMode && !root.deviceChangeInProgress
256         value: cam.iso.value
257     }
258
259     Binding {
260         target: cam.videoMute
261         property: "enabled"
262         value: settings.videoMuted
263     }
264
265     Binding {
266         target: cam.roi
267         property: "enabled"
268         value: settings.faceDetectionEnabled && !focusReticle.pressed && !focusReticle.touchMode && cam.mode == Camera.ImageMode
269     }
270
271     function policyLost() {
272         if (loader.item) {
273             loader.item.policyLost()
274         }
275     }
276
277     function checkBattery() {
278         // We are fine if we are connected to the charger:
279         if (batteryMonitor.charging) {
280             return true
281         }
282
283         // If we have enough battery then we are fine:
284         if (!batteryMonitor.critical) {
285             return true
286         }
287
288         return false
289     }
290
291     function cameraDeviceChanged() {
292         if (loader.item) {
293             loader.item.cameraDeviceChanged()
294         }
295     }
296 }