Fixed VideoPlayerPage.qml failure to set cameraConfig
[harmattan/cameraplus] / qml / PipelineManager.qml
index 3d4d7ab..0ef0a68 100644 (file)
@@ -3,7 +3,7 @@
 /*!
  * This file is part of CameraPlus.
  *
- * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
+ * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-import QtQuick 1.1
+import QtQuick 2.0
 import QtCamera 1.0
 import CameraPlus 1.0
 
 Item {
-        id: handler
+    id: handler
 
-        property bool showStandBy: state != "on"
+    property bool showStandBy: state != "on"
 
-        property alias acquired: policy.acquired
-        property alias hijacked: policy.hijacked
+    property alias acquired: policy.acquired
+    property alias hijacked: policy.hijacked
+    property alias scaleAcquired: policy.scaleAcquired
 
-        property Camera camera: null
-        property Item currentPage: pageStack.currentPage
+    property Camera camera: null
+    property Item currentItem
+    property bool error: false
+    property int currentPolicyMode: CameraResources.None
 
-        onCurrentPageChanged: {
-                if (state == "on" || state == "policyLost") {
-                        startCamera();
-                }
+    onCurrentItemChanged: {
+        if (state == "on" || state == "policyLost") {
+            startCamera()
         }
-
-        CameraResources {
-                id: policy
+    }
+
+    Connections {
+        target: currentItem
+        onPolicyModeChanged: {
+            if (state == "on" || state == "policyLost") {
+                startCamera()
+            }
         }
-
-        function startCamera() {
-                if (!policy.acquire(currentPage.policyMode)) {
-                        console.log("Failed to acquire policy resources");
-                        return;
-                }
-
-                if (!camera.start()) {
-                        showError(qsTr("Failed to start camera. Please restart the application."));
-                }
+    }
+
+    CameraResources {
+        id: policy
+    }
+
+    function startCamera() {
+        if (error) {
+            return
+        } else if ((currentPolicyMode == currentItem.policyMode) && camera.running) {
+            return
+        } else if (!policy.acquire(currentItem.policyMode)) {
+            console.log("Failed to acquire policy resources")
+            return
+        } else if (currentItem.policyMode == CameraResources.Player) {
+            currentPolicyMode = CameraResources.Player
+            camera.stop(true)
+        } else if (!camera.start()) {
+            showError(qsTr("Failed to start camera. Please restart the application."))
+        } else {
+            currentPolicyMode = currentItem.policyMode
         }
+    }
 
-        function stopCamera() {
-                if (camera.stop(false)) {
-                        policy.acquire(CameraResources.None);
-                }
+    function stopCamera() {
+        if (camera.stop(false)) {
+            policy.acquire(CameraResources.None)
+            currentPolicyMode = CameraResources.None
+            error = false
         }
+    }
 
-        function forceStopCamera() {
-                // We don't release resources here so we can get them back
-                // when they become available
-                camera.stop(true);
-        }
+    function forceStopCamera() {
+        // We don't release resources here so we can get them back
+        // when they become available
+        currentItem.policyLost()
+        camera.stop(true)
+        currentPolicyMode = CameraResources.None
+        error = false
+    }
 
-        state: "off"
+    state: "off"
 
-//        onStateChanged: console.log("New state " + handler.state);
+//    onStateChanged: console.log("New state " + handler.state);
 
-        states: [
+    states: [
         State {
-                name: "on"
-                when: Qt.application.active && currentPage && currentPage.needsPipeline && !policy.hijacked
+            name: "on"
+            when: Qt.application.active && currentItem && currentItem.policyMode != CameraResources.None && !policy.hijacked
         },
         State {
-                name: "off"
-                when: (!Qt.application.active && camera.idle) || (currentPage && !currentPage.needsPipeline)
+            name: "off"
+            when: (!Qt.application.active && camera.idle) || (currentItem && currentItem.policyMode == CameraResources.None && camera.idle)
         },
         State {
-                name: "policyLost"
-                when: policy.hijacked
+            name: "policyLost"
+            when: policy.hijacked
         },
         State {
-                name: "error"
+            name: "error"
         }
-        ]
+    ]
 
-        transitions: [
+    transitions: [
         Transition {
-                to: "off"
-                ScriptAction {
-                        script: stopCamera();
-                }
+            to: "off"
+            ScriptAction {
+                script: stopCamera()
+            }
         },
         Transition {
-                from: "off"
-                to: "on"
-                ScriptAction {
-                        script: handler.startCamera();
-                }
+            from: "off"
+            to: "on"
+            ScriptAction {
+                script: handler.startCamera()
+            }
         },
-
         Transition {
-                from: "on"
-                to: "policyLost"
-                ScriptAction {
-                        script: forceStopCamera();
-                }
+            from: "on"
+            to: "policyLost"
+            ScriptAction {
+                script: forceStopCamera()
+            }
         },
-
         Transition {
-                from: "policyLost"
-                to: "off"
-                ScriptAction {
-                        script: stopCamera();
-                }
+            from: "policyLost"
+            to: "off"
+            ScriptAction {
+                script: stopCamera()
+            }
         },
         Transition {
-                from: "policyLost"
-                to: "on"
-                ScriptAction {
-                        script: startCamera();
-                }
+            from: "policyLost"
+            to: "on"
+            ScriptAction {
+                script: startCamera()
+            }
         }
-        ]
+    ]
 }