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