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