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