Don't disable GPS when we are obscured by location-ui or conndlgs
[harmattan/cameraplus] / qml / VideoOverlay.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 Item {
28     id: overlay
29     property bool recording: false
30
31     property Camera cam
32     property bool animationRunning: false
33     property int policyMode: recording == true ? CameraResources.Recording : CameraResources.Video
34     property bool controlsVisible: !animationRunning && cam != null && cam.running
35         && dimmer.opacity == 0.0 && !cameraMode.busy
36     property bool pressed: overlay.recording || capture.pressed ||
37         zoomSlider.pressed || modeButton.pressed
38     property bool inhibitDim: recording
39
40     signal previewAvailable(string uri)
41
42     anchors.fill: parent
43
44     VideoMode {
45         id: videoMode
46         camera: cam
47         onPreviewAvailable: overlay.previewAvailable(preview)
48     }
49
50     ZoomSlider {
51         id: zoomSlider
52         camera: cam
53         anchors.top: parent.top
54         anchors.topMargin: 0
55         anchors.horizontalCenter: parent.horizontalCenter
56         visible: controlsVisible
57     }
58
59     ModeButton {
60         id: modeButton
61         anchors.bottom: parent.bottom
62         anchors.right: parent.right
63         anchors.rightMargin: 20
64         anchors.bottomMargin: 20
65         visible: controlsVisible && !overlay.recording
66     }
67
68     ZoomCaptureButton {
69         id: zoomCapture
70     }
71
72     CaptureControl {
73         id: captureControl
74         capturePressed: capture.pressed
75         zoomPressed: zoomCapture.zoomPressed
76         proximityClosed: proximitySensor.close
77         onStartCapture: overlay.toggleRecording()
78     }
79
80     CaptureCancel {
81         anchors.fill: parent
82         enabled: captureControl.showCancelBanner
83         onPressed: captureControl.canceled = true
84     }
85
86     CaptureButton {
87         id: capture
88         anchors.right: parent.right
89         anchors.rightMargin: 20
90         anchors.verticalCenter: parent.verticalCenter
91         iconSource: overlay.recording ? cameraTheme.captureButtonRecordingIconId : cameraTheme.captureButtonVideoIconId
92         width: 75
93         height: 75
94         opacity: 0.5
95
96         visible: controlsVisible
97
98         onExited: {
99             if (mouseX <= 0 || mouseY <= 0 || mouseX > width || mouseY > height) {
100                 // Release outside the button:
101                 captureControl.canceled = true
102             }
103         }
104     }
105
106     CameraToolBar {
107         id: toolBar
108         anchors.bottom: parent.bottom
109         anchors.bottomMargin: 20
110         anchors.left: parent.left
111         anchors.leftMargin: 20
112         opacity: 0.5
113         targetWidth: parent.width - (anchors.leftMargin * 2) - (66 * 1.5)
114         visible: controlsVisible
115         expanded: settings.showToolBar
116         onExpandedChanged: settings.showToolBar = expanded;
117
118         tools: CameraToolBarTools {
119             VideoTorchButton {
120                 camera: cam
121                 visible: !overlay.cam.quirks.hasQuirk(Quirks.NoVideoTorch)
122             }
123
124             VideoSceneButton {
125                 property bool hide: (overlay.recording && overlay.cam.quirks.hasQuirk(Quirks.NoSceneModeChangeDuringRecording)) || overlay.cam.quirks.hasQuirk(Quirks.NoNightSceneMode)
126                 visible: !hide
127                 onClicked: toolBar.push(tools)
128             }
129
130             VideoEvCompButton {
131                 onClicked: toolBar.push(tools)
132             }
133
134             VideoWhiteBalanceButton {
135                 onClicked: toolBar.push(tools)
136             }
137
138             VideoColorFilterButton {
139                 onClicked: toolBar.push(tools)
140             }
141
142             VideoMuteButton {
143             }
144         }
145     }
146
147     Rectangle {
148         anchors.top: parent.top
149         anchors.topMargin: 20
150         anchors.left: parent.left
151         anchors.leftMargin: 20
152         width: 48
153         height: col.height
154         color: "black"
155         border.color: "gray"
156         radius: 20
157         opacity: 0.5
158         visible: controlsVisible
159
160         Column {
161             id: col
162             width: parent.width
163             spacing: 5
164
165             Indicator {
166                 id: resolutionIndicator
167                 source: cameraTheme.videoIcon(settings.videoAspectRatio,
168                     settings.videoResolution, settings.device)
169             }
170
171             Indicator {
172                 id: wbIndicator
173                 source: visible ? cameraTheme.whiteBalanceIcon(settings.videoWhiteBalance) : ""
174                 visible: settings.videoWhiteBalance != WhiteBalance.Auto && !toolBar.expanded
175             }
176
177             Indicator {
178                 id: cfIndicator
179                 source: visible ? cameraTheme.colorFilterIcon(settings.videoColorFilter) : ""
180                 visible: settings.videoColorFilter != ColorTone.Normal && !toolBar.expanded
181             }
182
183             Indicator {
184                 id: sceneIndicator
185                 visible: settings.videoSceneMode != Scene.Auto && (!toolBar.expanded || overlay.recording)
186                 source: visible ? cameraTheme.videoSceneModeIcon(settings.videoSceneMode) : ""
187             }
188
189             Indicator {
190                 id: gpsIndicator
191                 visible: settings.useGps
192                 source: cameraTheme.gpsIndicatorIcon
193
194                 PropertyAnimation on opacity  {
195                     easing.type: Easing.OutSine
196                     loops: Animation.Infinite
197                     from: 0.2
198                     to: 1.0
199                     duration: 1000
200                     running: settings.useGps && !positionSource.position.longitudeValid
201                     alwaysRunToEnd: true
202                 }
203             }
204         }
205     }
206
207     Connections {
208         target: Qt.application
209         onActiveChanged: {
210             if (!Qt.application.active && overlay.recording) {
211                 overlay.stopRecording()
212             }
213         }
214     }
215
216     Timer {
217         id: recordingDuration
218         property int duration: 0
219         running: overlay.recording
220         interval: 1000
221         repeat: true
222
223         onTriggered: {
224             duration = duration + 1
225
226             if (duration == 3600) {
227                 overlay.stopRecording()
228                 showError(qsTr("Maximum recording time reached."))
229             } else if (!fileSystem.hasFreeSpace(fileNaming.temporaryVideoPath)) {
230                 page.stopRecording()
231                 showError(qsTr("Not enough space to continue recording."))
232             }
233
234         }
235     }
236
237     RecordingDurationLabel {
238         visible: overlay.recording
239         duration: recordingDuration.duration
240     }
241
242     function resetToolBar() {
243         if (toolBar.depth() > 1) {
244             toolBar.pop()
245         }
246     }
247
248     function doStartRecording() {
249         if (!overlay.recording) {
250             return
251         }
252
253         if (!pipelineManager.acquired || pipelineManager.hijacked) {
254             showError(qsTr("Failed to acquire needed resources."))
255             overlay.recording = false
256             return
257         }
258
259         metaData.setMetaData()
260
261         if (!mountProtector.lock(fileNaming.temporaryVideoPath)) {
262             showError(qsTr("Failed to lock temporary videos directory."))
263             overlay.recording = false
264             return
265         }
266
267         if (!mountProtector.lock(fileNaming.videoPath)) {
268             showError(qsTr("Failed to lock videos directory."))
269             overlay.recording = false
270             mountProtector.unlockAll()
271             return
272         }
273
274         var file = fileNaming.videoFileName()
275         var tmpFile = fileNaming.temporaryVideoFileName()
276
277         if (!videoMode.startRecording(file, tmpFile)) {
278             showError(qsTr("Failed to record video. Please restart the camera."))
279             mountProtector.unlockAll()
280             overlay.recording = false
281             return
282         }
283
284         trackerStore.storeVideo(file);
285
286         resetToolBar()
287     }
288
289     function startRecording() {
290         if (!fileSystem.available) {
291             showError(qsTr("Camera cannot record videos in mass storage mode."))
292         } else if (!checkBattery()) {
293             showError(qsTr("Not enough battery to record video."))
294         } else if (!fileSystem.hasFreeSpace(fileNaming.videoPath) || !fileSystem.hasFreeSpace(fileNaming.temporaryVideoPath)) {
295             showError(qsTr("Not enough space to record video."))
296         } else {
297             recordingDuration.duration = 0
298             overlay.recording = true
299             doStartRecording()
300         }
301     }
302
303     function stopRecording() {
304         videoMode.stopRecording(true)
305         mountProtector.unlockAll()
306         overlay.recording = false
307     }
308
309     function toggleRecording() {
310         if (overlay.recording) {
311             overlay.stopRecording()
312         } else {
313             overlay.startRecording()
314         }
315     }
316
317     function cameraError() {
318         overlay.stopRecording()
319     }
320
321     function policyLost() {
322         overlay.stopRecording()
323     }
324
325     function batteryLow() {
326         if (!overlay.recording) {
327             return
328         }
329
330         if (!checkBattery()) {
331             overlay.stopRecording()
332             showError(qsTr("Not enough battery to record video."))
333         }
334     }
335
336     function cameraDeviceChanged() {
337         resetToolBar()
338     }
339 }