Code cleanup
[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: fade out transition
117                 id: standby
118                 color: "black"
119                 anchors.fill: parent
120                 visible: !Qt.application.active || !cam.running || !resourcePolicy.acquired
121                 Image {
122                         source: "image://theme/icon-l-camera-standby"
123                         anchors.centerIn: parent
124                 }
125         }
126
127         property Camera cam: null
128         property bool controlsVisible: cam.running && !standby.visible
129
130         anchors.fill: parent
131
132         property alias previewAnimationRunning: preview.animationRunning
133
134         function setPreview(image) {
135                 preview.setPreview(image);
136         }
137
138         ModeButton {
139                 anchors.bottom: parent.bottom
140                 anchors.right: parent.right
141                 anchors.rightMargin: 20
142                 anchors.bottomMargin: 20
143                 visible: controlsVisible
144         }
145
146         PreviewImage {
147                 id: preview
148         }
149
150         ZoomSlider {
151                 id: zoom
152                 camera: cam
153                 anchors.top: parent.top
154                 anchors.topMargin: 0
155                 anchors.horizontalCenter: parent.horizontalCenter
156                 visible: controlsVisible
157         }
158
159         function checkDiskSpace() {
160                 return fileSystem.hasFreeSpace(fileNaming.path);
161         }
162
163         function checkBattery() {
164                 // We are fine if we are connected to the charger:
165                 if (batteryMonitor.chargingState == BatteryInfo.Charging) {
166                         return true;
167                 }
168
169                 // If we have enough battery then we are fine:
170                 if (batteryMonitor.batteryStatus > BatteryInfo.BatteryCritical) {
171                         return true;
172                 }
173
174                 return false;
175         }
176
177         BatteryInfo {
178                 id: batteryMonitor
179                 monitorChargingStateChanges: cam.running
180                 monitorBatteryStatusChanges: cam.running
181
182                 onChargingStateChanged: {
183                         if (!checkBattery()) {
184                                 parent.batteryLow();
185                         }
186                 }
187
188                 onBatteryStatusChanged:  {
189                         if (!checkBattery()) {
190                                 parent.batteryLow();
191                         }
192                 }
193         }
194 }