Set the video resolution when video aspect ratio gets changed
[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                 source: cameraTheme.videoIcon(settings.videoAspectRatio,
154                     settings.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 resetToolBar() {
233         if (toolBar.depth() > 1) {
234             toolBar.pop()
235         }
236     }
237
238     function doStartRecording() {
239         if (!overlay.recording) {
240             return
241         }
242
243         if (!pipelineManager.acquired || pipelineManager.hijacked) {
244             showError(qsTr("Failed to acquire needed resources."))
245             overlay.recording = false
246             return
247         }
248
249         metaData.setMetaData()
250
251         if (!mountProtector.lock(fileNaming.temporaryVideoPath)) {
252             showError(qsTr("Failed to lock temporary videos directory."))
253             overlay.recording = false
254             return
255         }
256
257         if (!mountProtector.lock(fileNaming.videoPath)) {
258             showError(qsTr("Failed to lock videos directory."))
259             overlay.recording = false
260             mountProtector.unlockAll()
261             return
262         }
263
264         var file = fileNaming.videoFileName()
265         var tmpFile = fileNaming.temporaryVideoFileName()
266
267         if (!videoMode.startRecording(file, tmpFile)) {
268             showError(qsTr("Failed to record video. Please restart the camera."))
269             mountProtector.unlockAll()
270             overlay.recording = false
271             return
272         }
273
274         trackerStore.storeVideo(file);
275
276         resetToolBar()
277     }
278
279     function startRecording() {
280         if (!fileSystem.available) {
281             showError(qsTr("Camera cannot record videos in mass storage mode."))
282         } else if (!checkBattery()) {
283             showError(qsTr("Not enough battery to record video."))
284         } else if (!fileSystem.hasFreeSpace(fileNaming.videoPath) || !fileSystem.hasFreeSpace(fileNaming.temporaryVideoPath)) {
285             showError(qsTr("Not enough space to record video."))
286         } else {
287             recordingDuration.duration = 0
288             overlay.recording = true
289             doStartRecording()
290         }
291     }
292
293     function stopRecording() {
294         videoMode.stopRecording(true)
295         mountProtector.unlockAll()
296         overlay.recording = false
297     }
298
299     function toggleRecording() {
300         if (overlay.recording) {
301             overlay.stopRecording()
302         } else {
303             overlay.startRecording()
304         }
305     }
306
307     function cameraError() {
308         overlay.stopRecording()
309     }
310
311     function policyLost() {
312         overlay.stopRecording()
313     }
314
315     function batteryLow() {
316         if (!overlay.recording) {
317             return
318         }
319
320         if (!checkBattery()) {
321             overlay.stopRecording()
322             showError(qsTr("Not enough battery to record video."))
323         }
324     }
325
326     function cameraDeviceChanged() {
327         resetToolBar()
328     }
329 }