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