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