Hide unusable ui parts depending on device quirks
[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: !overlay.cam.quirks.hasQuirk(Quirks.NoVideoTorch)
108             }
109
110             VideoSceneButton {
111                 property bool hide: (overlay.recording && overlay.cam.quirks.hasQuirk(Quirks.NoSceneModeChangeDuringRecording)) || overlay.cam.quirks.hasQuirk(Quirks.NoNightSceneMode)
112                 visible: !hide
113                 onClicked: toolBar.push(tools)
114             }
115
116             VideoEvCompButton {
117                 onClicked: toolBar.push(tools)
118             }
119
120             VideoWhiteBalanceButton {
121                 onClicked: toolBar.push(tools)
122             }
123
124             VideoColorFilterButton {
125                 onClicked: toolBar.push(tools)
126             }
127
128             VideoMuteButton {
129             }
130         }
131     }
132
133     Rectangle {
134         anchors.top: parent.top
135         anchors.topMargin: 20
136         anchors.left: parent.left
137         anchors.leftMargin: 20
138         width: 48
139         height: col.height
140         color: "black"
141         border.color: "gray"
142         radius: 20
143         opacity: 0.5
144         visible: controlsVisible
145
146         Column {
147             id: col
148             width: parent.width
149             spacing: 5
150
151             Indicator {
152                 id: resolutionIndicator
153                 property string videoResolution: settings.device == 1 ? settings.secondaryVideoResolution : settings.primaryVideoResolution
154                 property string videoRatio: settings.device == 1 ? settings.secondaryVideoAspectRatio : settings.primaryVideoAspectRatio
155                 source: cameraTheme.videoIcon(videoRatio, 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 doStartRecording() {
234         if (!overlay.recording) {
235             return
236         }
237
238         if (!pipelineManager.acquired || pipelineManager.hijacked) {
239             showError(qsTr("Failed to acquire needed resources."))
240             overlay.recording = false
241             return
242         }
243
244         metaData.setMetaData()
245
246         if (!mountProtector.lock(fileNaming.temporaryVideoPath)) {
247             showError(qsTr("Failed to lock temporary videos directory."))
248             overlay.recording = false
249             return
250         }
251
252         if (!mountProtector.lock(fileNaming.videoPath)) {
253             showError(qsTr("Failed to lock videos directory."))
254             overlay.recording = false
255             mountProtector.unlockAll()
256             return
257         }
258
259         var file = fileNaming.videoFileName()
260         var tmpFile = fileNaming.temporaryVideoFileName()
261
262         if (!videoMode.startRecording(file, tmpFile)) {
263             showError(qsTr("Failed to record video. Please restart the camera."))
264             mountProtector.unlockAll()
265             overlay.recording = false
266             return
267         }
268
269         trackerStore.storeVideo(file);
270
271         if (toolBar.depth() > 1) {
272             toolBar.pop()
273         }
274     }
275
276     function startRecording() {
277         if (!fileSystem.available) {
278             showError(qsTr("Camera cannot record videos in mass storage mode."))
279         } else if (!checkBattery()) {
280             showError(qsTr("Not enough battery to record video."))
281         } else if (!fileSystem.hasFreeSpace(fileNaming.videoPath) || !fileSystem.hasFreeSpace(fileNaming.temporaryVideoPath)) {
282             showError(qsTr("Not enough space to record video."))
283         } else {
284             recordingDuration.duration = 0
285             overlay.recording = true
286             doStartRecording()
287         }
288     }
289
290     function stopRecording() {
291         videoMode.stopRecording(true)
292         mountProtector.unlockAll()
293         overlay.recording = false
294     }
295
296     function toggleRecording() {
297         if (overlay.recording) {
298             overlay.stopRecording()
299         } else {
300             overlay.startRecording()
301         }
302     }
303
304     function cameraError() {
305         overlay.stopRecording()
306     }
307
308     function policyLost() {
309         overlay.stopRecording()
310     }
311
312     function batteryLow() {
313         if (!overlay.recording) {
314             return
315         }
316
317         if (!checkBattery()) {
318             overlay.stopRecording()
319             showError(qsTr("Not enough battery to record video."))
320         }
321     }
322
323 }