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