a0620f0c8d15d7587eba745cd968339d15418f51
[harmattan/cameraplus] / qml / CameraPage.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 QtMobility.systeminfo 1.2
28
29 Page {
30         id: page
31
32         property alias standbyWidget: standby
33
34         property bool needsPipeline: true
35         property int policyMode: CameraResources.None
36
37         property Camera cam: null
38         property bool controlsVisible: cam.running && !standby.visible
39         property bool zoomVisible: true
40         property bool modesVisible: true
41         property bool standbyVisible: true
42
43         anchors.fill: parent
44
45         property alias previewAnimationRunning: preview.animationRunning
46
47         signal batteryLow
48
49         function updatePolicy() {
50                 if (!resourcePolicy.acquire(page.policyMode)) {
51                         cam.stop(true);
52                 }
53                 else {
54                         handlePipeline();
55                 }
56         }
57
58         Component.onCompleted: {
59                 if (Qt.application.active && needsPipeline) {
60                         updatePolicy();
61                 }
62                 else if (!needsPipeline) {
63                         cam.stop();
64                 }
65         }
66
67         onStatusChanged: {
68                 if (Qt.application.active && status == PageStatus.Activating) {
69                         updatePolicy();
70                 }
71         }
72 /*
73         onStatusChanged: {
74                 if (status == PageStatus.Active && !page.needsPipeline) {
75                         cam.stop();
76                 }
77         }
78 */
79
80         onPolicyModeChanged: {
81                 if (Qt.application.active) {
82                         updatePolicy();
83                 }
84         }
85
86         function handlePipeline() {
87                 var acquired = resourcePolicy.acquired;
88                 var hijacked = resourcePolicy.hijacked;
89
90                 if (hijacked) {
91                         cam.stop(true);
92                         showError(qsTr("Resources lost. Stopping camera."));
93                 }
94                 else if (!Qt.application.active && !acquired) {
95                         // I don't think this is needed
96                         cam.stop(true);
97                 }
98                 else if (!Qt.application.active) {
99                         cam.stop();
100                 }
101                 else if (acquired && page.needsPipeline && !cam.running) {
102                         if (!cam.start()) {
103                                 showError(qsTr("Failed to start camera. Please restart the application."));
104                         }
105                 }
106         }
107
108         Connections {
109                 target: resourcePolicy
110                 onHijackedChanged: handlePipeline();
111                 onAcquiredChanged: handlePipeline();
112         }
113
114         Connections {
115                 target: Qt.application
116                 onActiveChanged: {
117                         if (!Qt.application.active) {
118                                 // This is a noop if camera is not
119                                 // idle so calling it will not hurt
120                                 if (cam.stop()) {
121                                         resourcePolicy.acquire(CameraResources.None);
122                                 }
123                         }
124                         else if (page.needsPipeline) {
125                                 updatePolicy();
126                         }
127                 }
128         }
129
130         Connections {
131                 target: cam
132                 onIdleChanged: {
133                         if (cam.idle && !Qt.application.active) {
134                                 cam.stop();
135                                 resourcePolicy.acquire(CameraResources.None);
136                         }
137 /*
138                         else if (cam.idle && !page.needsPipeline) {
139                                 cam.stop();
140                         }
141 */
142                 }
143         }
144
145         Rectangle {
146                 // TODO: fade out transition
147                 // TODO: there is a toolbar visible on the first startup
148                 id: standby
149                 color: "black"
150                 anchors.fill: parent
151                 visible: standbyVisible && page.status == PageStatus.Active && (!Qt.application.active || !cam.running || !resourcePolicy.acquired)
152                 Image {
153                         source: "image://theme/icon-l-camera-standby"
154                         anchors.centerIn: parent
155                 }
156         }
157
158         function setPreview(image) {
159                 preview.setPreview(image);
160         }
161
162         ModeButton {
163                 anchors.bottom: parent.bottom
164                 anchors.right: parent.right
165                 anchors.rightMargin: 20
166                 anchors.bottomMargin: 20
167                 visible: controlsVisible && modesVisible
168         }
169
170         PreviewImage {
171                 id: preview
172         }
173
174         ZoomSlider {
175                 id: zoom
176                 camera: cam
177                 anchors.top: parent.top
178                 anchors.topMargin: 0
179                 anchors.horizontalCenter: parent.horizontalCenter
180                 visible: controlsVisible && zoomVisible
181         }
182
183         function checkDiskSpace() {
184                 return fileSystem.hasFreeSpace(fileNaming.path);
185         }
186
187         function checkBattery() {
188                 // We are fine if we are connected to the charger:
189                 if (batteryMonitor.chargingState == BatteryInfo.Charging) {
190                         return true;
191                 }
192
193                 // If we have enough battery then we are fine:
194                 if (batteryMonitor.batteryStatus > BatteryInfo.BatteryCritical) {
195                         return true;
196                 }
197
198                 return false;
199         }
200
201         BatteryInfo {
202                 id: batteryMonitor
203                 monitorChargingStateChanges: cam.running
204                 monitorBatteryStatusChanges: cam.running
205
206                 onChargingStateChanged: {
207                         if (!checkBattery()) {
208                                 parent.batteryLow();
209                         }
210                 }
211
212                 onBatteryStatusChanged:  {
213                         if (!checkBattery()) {
214                                 parent.batteryLow();
215                         }
216                 }
217         }
218 }