Convert TextSwitch to a platform specific component and rename it to CameraTextSwitch
[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 2.0
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     property int currentPolicyMode: CameraResources.None
40
41     onCurrentItemChanged: {
42         if (state == "on" || state == "policyLost") {
43             startCamera()
44         }
45     }
46
47     Connections {
48         target: currentItem
49         onPolicyModeChanged: {
50             if (state == "on" || state == "policyLost") {
51                 startCamera()
52             }
53         }
54     }
55
56     CameraResources {
57         id: policy
58     }
59
60     function startCamera() {
61         if (error) {
62             return
63         } else if ((currentPolicyMode == currentItem.policyMode) && camera.running) {
64             return
65         } else if (!policy.acquire(currentItem.policyMode)) {
66             console.log("Failed to acquire policy resources")
67             return
68         } else if (currentItem.policyMode == CameraResources.Player) {
69             currentPolicyMode = CameraResources.Player
70             camera.stop(true)
71         } else if (!camera.start()) {
72             showError(qsTr("Failed to start camera. Please restart the application."))
73         } else {
74             currentPolicyMode = currentItem.policyMode
75         }
76     }
77
78     function stopCamera() {
79         if (camera.stop(false)) {
80             policy.acquire(CameraResources.None)
81             currentPolicyMode = CameraResources.None
82             error = false
83         }
84     }
85
86     function forceStopCamera() {
87         // We don't release resources here so we can get them back
88         // when they become available
89         currentItem.policyLost()
90         camera.stop(true)
91         currentPolicyMode = CameraResources.None
92         error = false
93     }
94
95     state: "off"
96
97 //    onStateChanged: console.log("New state " + handler.state);
98
99     states: [
100         State {
101             name: "on"
102             when: Qt.application.active && currentItem && currentItem.policyMode != CameraResources.None && !policy.hijacked
103         },
104         State {
105             name: "off"
106             when: (!Qt.application.active && camera.idle) || (currentItem && currentItem.policyMode == CameraResources.None && camera.idle)
107         },
108         State {
109             name: "policyLost"
110             when: policy.hijacked
111         },
112         State {
113             name: "error"
114         }
115     ]
116
117     transitions: [
118         Transition {
119             to: "off"
120             ScriptAction {
121                 script: stopCamera()
122             }
123         },
124         Transition {
125             from: "off"
126             to: "on"
127             ScriptAction {
128                 script: handler.startCamera()
129             }
130         },
131         Transition {
132             from: "on"
133             to: "policyLost"
134             ScriptAction {
135                 script: forceStopCamera()
136             }
137         },
138         Transition {
139             from: "policyLost"
140             to: "off"
141             ScriptAction {
142                 script: stopCamera()
143             }
144         },
145         Transition {
146             from: "policyLost"
147             to: "on"
148             ScriptAction {
149                 script: startCamera()
150             }
151         }
152     ]
153 }