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