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