d4e6d87346d05e4919e690990fe4a458f1999b1b
[harmattan/cameraplus] / qml / RecordingPage.qml
1 // -*- qml -*-
2
3 /*!
4  * This file is part of CameraPlus.
5  *
6  * Copyright (C) 2012 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 1.1
24 import com.nokia.meego 1.1
25 import QtCamera 1.0
26 import CameraPlus 1.0
27 import "data.js" as Data
28
29 // TODO: on error ?
30 // TODO: resources lost?
31 // TODO: closing camera in the middle of recording will hang camera
32 CameraPage {
33         id: page
34         modesVisible: false
35
36         Component.onCompleted: startRecording();
37         Component.onDestruction: videoMode.stopRecording();
38
39         function startRecording() {
40                 if (!resourcePolicy.acquired || resourcePolicy.hijacked) {
41                         pageStack.pop(undefined, true);
42                         return;
43                 }
44
45                 metaData.setMetaData();
46
47                 if (!mountProtector.lock()) {
48                         showError(qsTr("Failed to lock images directory."));
49                         pageStack.pop(undefined, true);
50                         return;
51                 }
52
53                 var file = fileNaming.videoFileName();
54                 var tmpFile = fileNaming.temporaryVideoFileName();
55
56                 if (!videoMode.startRecording(file, tmpFile)) {
57                         showError(qsTr("Failed to record video. Please restart the camera."));
58                         pageStack.pop(undefined, true);
59                         mountProtector.unlock();
60                 }
61         }
62
63         function stopRecording() {
64                 mountProtector.unlock();
65                 pageStack.pop(undefined, true);
66         }
67
68         policyMode: CameraResources.Recording
69
70         controlsVisible: cam.running && videoMode.recording && !cameraMode.animationRunning && !previewAnimationRunning && !standbyWidget.visible
71
72         orientationLock: PageOrientation.LockLandscape
73
74         DisplayState {
75                 inhibitDim: true
76         }
77
78         onBatteryLow: {
79                 if (!checkBattery()) {
80                         page.stopRecording();
81                         showError(qsTr("Not enough battery to record video."));
82                 }
83         }
84
85         Button {
86                 id: recording
87                 anchors.right: parent.right
88                 anchors.rightMargin: 20
89                 anchors.verticalCenter: parent.verticalCenter
90                 iconSource: "image://theme/icon-m-camera-video-record"
91                 width: 75
92                 height: 75
93                 opacity: 0.5
94
95                 onClicked: page.stopRecording();
96                 visible: controlsVisible
97         }
98
99         Connections {
100                 target: Qt.application
101                 onActiveChanged: {
102                         if (!Qt.application.active) {
103                                 page.stopRecording();
104                         }
105                 }
106         }
107
108         VideoMode {
109                 id: videoMode
110                 camera: cam
111         }
112
113         VideoTorchButton {
114                 id: torch
115                 camera: cam
116                 visible: controlsVisible
117                 anchors.top: parent.top
118                 anchors.left: parent.left
119                 anchors.topMargin: 20
120                 anchors.leftMargin: 20
121                 opacity: 0.5
122         }
123
124         VideoEvCompButton {
125                 id: evComp
126                 visible: controlsVisible
127                 anchors.top: torch.bottom
128                 anchors.left: parent.left
129                 anchors.topMargin: 10
130                 anchors.leftMargin: 20
131         }
132
133         Rectangle {
134                 anchors.left: parent.left
135                 anchors.bottom: parent.bottom
136                 anchors.leftMargin: 20
137                 anchors.bottomMargin: 20
138
139                 visible: controlsVisible
140
141                 color: "black"
142                 opacity: 0.5
143                 width: 100
144                 height: 30
145
146                 Timer {
147                         id: recordingDuration
148
149                         property int duration: 0
150
151                         running: videoMode.recording
152                         interval: 1000
153                         repeat: true
154
155                         onTriggered: {
156                                 duration = duration + 1;
157                                 if (duration == 3600) {
158                                         page.stopRecording();
159                                         showError(qsTr("Maximum recording time reached."));
160                                 }
161                                 else if (!checkDiskSpace()) {
162                                         page.stopRecording();
163                                         showError(qsTr("Not enough space to continue recording."));
164                                 }
165                         }
166                 }
167
168                 Image {
169                         id: recordingIcon
170                         source: "image://theme/icon-m-camera-ongoing-recording"
171                         width: 20
172                         height: 20
173                         anchors.verticalCenter: parent.verticalCenter
174                         anchors.left: parent.left
175                         anchors.leftMargin: 5
176                         sourceSize.width: 20
177                         sourceSize.height: 20
178                 }
179
180                 Label {
181                         function formatDuration(dur) {
182                                 var secs = parseInt(recordingDuration.duration);
183                                 var minutes = Math.floor(secs / 60);
184                                 var seconds = secs - (minutes * 60);
185
186                                 var date = new Date();
187                                 date.setSeconds(seconds);
188                                 date.setMinutes(minutes);
189                                 return Qt.formatTime(date, "mm:ss");
190                         }
191
192                         id: durationLabel
193                         text: formatDuration(recordingDuration.duration);
194                         anchors.left: recordingIcon.right
195                         anchors.leftMargin: 5
196                 }
197         }
198 }