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