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