Reworked VideoPlayerPage to use CameraToolBar
[harmattan/cameraplus] / qml / PipelineManager.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 QtCamera 1.0
25 import CameraPlus 1.0
26
27 // TODO: if a page doesn't need the pipeline then we will release its resources :(
28
29 Item {
30         id: handler
31
32         property bool showStandBy: state != "on"
33
34         property alias acquired: policy.acquired
35         property alias hijacked: policy.hijacked
36
37         property Camera camera: null
38         property Item currentPage: pageStack.currentPage
39         property bool error: false
40
41         onCurrentPageChanged: {
42                 if (state == "on" || state == "policyLost") {
43                         startCamera();
44                 }
45         }
46
47         CameraResources {
48                 id: policy
49         }
50
51         function startCamera() {
52                 if (error) {
53                         return;
54                 }
55
56                 if (!policy.acquire(currentPage.policyMode)) {
57                         console.log("Failed to acquire policy resources");
58                         return;
59                 }
60
61                 if (!camera.start()) {
62                         showError(qsTr("Failed to start camera. Please restart the application."));
63                 }
64         }
65
66         function stopCamera() {
67                 if (camera.stop(false)) {
68                         policy.acquire(CameraResources.None);
69                         error = false;
70                 }
71         }
72
73         function forceStopCamera() {
74                 // We don't release resources here so we can get them back
75                 // when they become available
76                 pageStack.currentPage.policyLost();
77                 camera.stop(true);
78                 error = false;
79         }
80
81         state: "off"
82
83 //        onStateChanged: console.log("New state " + handler.state);
84
85         states: [
86         State {
87                 name: "on"
88                 when: Qt.application.active && currentPage && currentPage.needsPipeline && !policy.hijacked
89         },
90         State {
91                 name: "off"
92                 when: (!Qt.application.active && camera.idle) || (currentPage && !currentPage.needsPipeline)
93         },
94         State {
95                 name: "policyLost"
96                 when: policy.hijacked
97         },
98         State {
99                 name: "error"
100         }
101         ]
102
103         transitions: [
104         Transition {
105                 to: "off"
106                 ScriptAction {
107                         script: stopCamera();
108                 }
109         },
110         Transition {
111                 from: "off"
112                 to: "on"
113                 ScriptAction {
114                         script: handler.startCamera();
115                 }
116         },
117
118         Transition {
119                 from: "on"
120                 to: "policyLost"
121                 ScriptAction {
122                         script: forceStopCamera();
123                 }
124         },
125
126         Transition {
127                 from: "policyLost"
128                 to: "off"
129                 ScriptAction {
130                         script: stopCamera();
131                 }
132         },
133         Transition {
134                 from: "policyLost"
135                 to: "on"
136                 ScriptAction {
137                         script: startCamera();
138                 }
139         }
140         ]
141 }