Fix button enabling when window is inactivated.
[qzeecontrol] / qml / QZeeControl / MainPage.qml
index 03f55c8..555c7f5 100644 (file)
 import QtQuick 1.1
 import com.nokia.meego 1.0
 import QtMobility.connectivity 1.2
+import QtMobility.systeminfo 1.2
 import "settingsstorage.js" as SettingsStorage
 import qzeecontrol 1.0
 
 Page {
+    id: mainPage
     tools: commonTools
 
     orientationLock: PageOrientation.LockPortrait
 
+    property bool initializing: true
+
     Component.onCompleted: {
         SettingsStorage.initialize();
 
@@ -38,6 +42,64 @@ Page {
             addressField.text = address
             portField.text = port
         }
+
+        if(SettingsStorage.getSetting("A") === "Unknown"){
+            console.log("Initializing key bindings.")
+            setKeyBindingsToDefault()
+        }
+
+        loadKeyBindings()
+        updateConnectAndScanButton()
+        initializing = false
+    }
+
+    function setKeyBindingsToDefault(){
+        console.log("Setting key bindings to default.")
+        SettingsStorage.setSetting("A", "a")
+        SettingsStorage.setSetting("B", "b")
+        SettingsStorage.setSetting("C", "c")
+        SettingsStorage.setSetting("D", "d")
+
+        SettingsStorage.setSetting("Up", "Up")
+        SettingsStorage.setSetting("Down", "Down")
+        SettingsStorage.setSetting("Left", "Left")
+        SettingsStorage.setSetting("Right", "Right")
+    }
+
+    function loadKeyBindings(){
+        console.log("Loading key bindings.")
+        zeeRemoteControl.keyBindingA = SettingsStorage.getSetting("A")
+        zeeRemoteControl.keyBindingB = SettingsStorage.getSetting("B")
+        zeeRemoteControl.keyBindingC = SettingsStorage.getSetting("C")
+        zeeRemoteControl.keyBindingD = SettingsStorage.getSetting("D")
+
+        zeeRemoteControl.keyBindingUp = SettingsStorage.getSetting("Up")
+        zeeRemoteControl.keyBindingDown = SettingsStorage.getSetting("Down")
+        zeeRemoteControl.keyBindingLeft = SettingsStorage.getSetting("Left")
+        zeeRemoteControl.keyBindingRight = SettingsStorage.getSetting("Right")
+    }
+
+    function updateConnectAndScanButton(){
+        if(!deviceInfo.currentBluetoothPowerState){
+            scanButton.enabled = false
+            connectButton.enabled = false
+
+            addressField.enabled = false
+            portField.enabled = false
+
+            infoText.text = "To get started please turn Bluetooth on."
+            return
+        }
+
+        scanButton.enabled = true
+
+        addressField.enabled = true
+        portField.enabled = true
+
+        connectButton.enabled = (addressField.text !== "No Zeemote found yet.")
+        infoText.text = (addressField.text !== "No Zeemote found yet.") ?
+                    "To enable remote control please press \"Connect\" when ready." :
+                    "Please scan for a Zeemote first."
     }
 
     states: [
@@ -45,24 +107,24 @@ Page {
             name: "active"
             PropertyChanges {
                 target: cursorRectangle
-                x: moveArea.x + (moveArea.width * 0.5) + btConn.x - (cursorRectangle.width * 0.5)
-                y: moveArea.y + (moveArea.height * 0.5) + btConn.y - (cursorRectangle.height * 0.5)
+                x: moveArea.x + (moveArea.width * 0.5) + zeeRemoteControl.x - (cursorRectangle.width * 0.5)
+                y: moveArea.y + (moveArea.height * 0.5) + zeeRemoteControl.y - (cursorRectangle.height * 0.5)
             }
             PropertyChanges {
                 target: labelA
-                color: btConn.a ? "red" : "blue"
+                color: zeeRemoteControl.a ? "red" : "blue"
             }
             PropertyChanges {
                 target: labelB
-                color: btConn.b ? "red" : "blue"
+                color: zeeRemoteControl.b ? "red" : "blue"
             }
             PropertyChanges {
                 target: labelC
-                color: btConn.c ? "red" : "blue"
+                color: zeeRemoteControl.c ? "red" : "blue"
             }
             PropertyChanges {
                 target: labelD
-                color: btConn.d ? "red" : "blue"
+                color: zeeRemoteControl.d ? "red" : "blue"
             }
         },
         State {
@@ -88,60 +150,6 @@ Page {
                 target: labelD
                 color: "blue"
             }
-        },
-        State {
-            name: "connecting"
-            PropertyChanges {
-                target: scanButton
-                enabled: false
-            }
-            PropertyChanges {
-                target: addressField
-                enabled: false
-            }
-            PropertyChanges {
-                target: portField
-                enabled: false
-            }
-            PropertyChanges {
-                target: connectButton
-                enabled: false
-            }
-            PropertyChanges {
-                target: disconnectButton
-                enabled: false
-            }
-            PropertyChanges {
-                target: infoText
-                text: "Connecting..."
-            }
-        },
-        State {
-            name: "disconnected"
-            PropertyChanges {
-                target: scanButton
-                enabled: true
-            }
-            PropertyChanges {
-                target: addressField
-                enabled: true
-            }
-            PropertyChanges {
-                target: portField
-                enabled: true
-            }
-            PropertyChanges {
-                target: connectButton
-                enabled: true
-            }
-            PropertyChanges {
-                target: disconnectButton
-                enabled: false
-            }
-            PropertyChanges {
-                target: infoText
-                text: "Press \"Connect\" to connect to the device."
-            }
         }
     ]
 
@@ -185,14 +193,14 @@ Page {
         anchors{top: headerItem.bottom; bottom: parent.bottom; left: parent.left; right: parent.right}
         contentHeight: contentColumn.height
 
-            Column{
-                id: contentColumn
-                spacing: 10
-                anchors{top: parent.top; left: parent.left; right: parent.right; topMargin: 10}
+        Column{
+            id: contentColumn
+            spacing: 10
+            anchors{top: parent.top; left: parent.left; right: parent.right; topMargin: 10}
 
             Button{
                 id: scanButton
-                enabled: true
+                enabled: false
 
                 anchors.horizontalCenter: parent.horizontalCenter
                 text: "Scan"
@@ -209,14 +217,18 @@ Page {
 
                 TextField{
                     id: addressField
-                    text: "No device found yet."
+                    text: "No Zeemote found yet."
+                    width: 280
 
                     onTextChanged: {
-                        if(text === "No device found yet.")
+                        if(mainPage.initializing)
+                            return
+
+                        if(text === "No Zeemote found yet.")
                             return
 
-                        connectButton.enabled = true
-                        infoText.text = "Press \"Connect\" to connect to the device."
+                        updateConnectAndScanButton();
+
                         console.log("Storing address in DB: " + text)
                         SettingsStorage.setSetting("address", text)
                     }
@@ -228,6 +240,9 @@ Page {
                     validator: IntValidator{}
 
                     onTextChanged: {
+                        if(mainPage.initializing)
+                            return
+
                         if(text === "na")
                             return
 
@@ -241,7 +256,6 @@ Page {
                 id: infoText
                 width: parent.width
 
-                text: "Please scan for a device first."
                 horizontalAlignment: Text.AlignHCenter
                 wrapMode: Text.WordWrap
             }
@@ -254,8 +268,14 @@ Page {
                 text: "Connect"
 
                 onClicked: {
-                    mainPage.state = "connecting"
-                    btConn.connect(addressField.text, parseInt(portField.text))
+                    scanButton.enabled = false
+                    addressField.enabled = false
+                    portField.enabled = false
+                    connectButton.enabled = false
+                    disconnectButton.enabled = false
+                    infoText.text = "Connecting..."
+
+                    zeeRemoteControl.connect(addressField.text, parseInt(portField.text))
                 }
             }
 
@@ -266,8 +286,7 @@ Page {
                 text: "Disconnect"
 
                 onClicked: {
-                    btConn.disconnect()
-
+                    zeeRemoteControl.disconnect()
                 }
             }
 
@@ -280,22 +299,22 @@ Page {
                 Label{
                     id: labelA
                     text: "A"
-                    color: btConn.a ? "red" : "blue"
+                    color: zeeRemoteControl.a ? "red" : "blue"
                 }
                 Label{
                     id: labelB
                     text: "B"
-                    color: btConn.b ? "red" : "blue"
+                    color: zeeRemoteControl.b ? "red" : "blue"
                 }
                 Label{
                     id: labelC
                     text: "C"
-                    color: btConn.c ? "red" : "blue"
+                    color: zeeRemoteControl.c ? "red" : "blue"
                 }
                 Label{
                     id: labelD
                     text: "D"
-                    color: btConn.d ? "red" : "blue"
+                    color: zeeRemoteControl.d ? "red" : "blue"
                 }
             }
 
@@ -319,13 +338,23 @@ Page {
                     height: 10
                     color: "red"
 
-                    x: moveArea.x + (moveArea.width * 0.5) + btConn.x - (cursorRectangle.width * 0.5)
-                    y: moveArea.y + (moveArea.height * 0.5) + btConn.y - (cursorRectangle.height * 0.5)
+                    x: moveArea.x + (moveArea.width * 0.5) + zeeRemoteControl.x - (cursorRectangle.width * 0.5)
+                    y: moveArea.y + (moveArea.height * 0.5) + zeeRemoteControl.y - (cursorRectangle.height * 0.5)
                 }
             }
         }
     }
 
+    DeviceInfo{
+        id: deviceInfo
+
+        monitorBluetoothStateChanges: true
+
+        onBluetoothStateChanged: {
+            updateConnectAndScanButton()
+        }
+    }
+
     BluetoothDiscoveryModel{
         id: btDiscovery
 
@@ -333,8 +362,11 @@ Page {
         minimalDiscovery: true
 
         onDiscoveryChanged: {
+            if(initializing)
+                return
+
             if(discovery){
-                infoText.text = "Scanning for a device..."
+                infoText.text = "Scanning for a Zeemote..."
                 scanButton.enabled = false
                 connectButton.enabled = false
                 disconnectButton.enabled = false
@@ -346,8 +378,8 @@ Page {
                 addressField.enabled = true
                 portField.enabled = true
 
-                if(addressField.text !== "No device found yet." && portField.text !== "na"){
-                    infoText.text = "Device found. You can now connect."
+                if(addressField.text !== "No Zeemote found yet." && portField.text !== "na"){
+                    infoText.text = "Zeemote found. To enable remote control please press \"Connect\" when ready."
                     connectButton.enabled = true
                 }
             }
@@ -360,12 +392,14 @@ Page {
             if(service.serviceName === "Zeemote"){
                 addressField.text = service.deviceAddress
                 portField.text = service.servicePort
+                discovery = false
+                console.log("Found Zeemote. Stopped further discovery.")
             }
         }
     }
 
-    BtConnector{
-        id: btConn
+    ZeeRemoteControl{
+        id: zeeRemoteControl
 
         threshold: 50
 
@@ -373,62 +407,14 @@ Page {
             disconnectButton.enabled = true
             infoText.text = "Connected. Have fun."
         }
-
         onDisconnected: {
-            mainPage.state = "disconnected"
-        }
-
-//        onStickMoved: {
-//            console.log("Stick moved. x: " + x + " y: " + y)
-//        }
-
-//        onButtonsChanged: {
-//            console.log("Buttons changed. A: " + a + " B: " + b + " C: " + c + " D: " + d)
-//        }
-
-        onAChanged: {
-//            console.log("A changed to: " + val)
-            xtstAdapter.sendKey("a", val);
+            scanButton.enabled = true
+            addressField.enabled = true
+            portField.enabled = true
+            connectButton.enabled = true
+            disconnectButton.enabled = false
+            infoText.text = "To enable remote control please press \"Connect\" when ready."
         }
-        onBChanged: {
-//            console.log("B changed to: " + val)
-            xtstAdapter.sendKey("b", val);
-        }
-        onCChanged: {
-//            console.log("C changed to: " + val)
-            xtstAdapter.sendKey("c", val);
-        }
-        onDChanged: {
-//            console.log("D changed to: " + val)
-            xtstAdapter.sendKey("d", val);
-        }
-
-        onUpChanged: xtstAdapter.sendKey("Up", val)
-        onDownChanged: xtstAdapter.sendKey("Down", val)
-        onLeftChanged: xtstAdapter.sendKey("Left", val)
-        onRightChanged: xtstAdapter.sendKey("Right", val)
-
-//        onXChanged: {
-//            if(val > joystickThreshold){
-//                xtstAdapter.sendKey("Right", true);
-//            }else if(val < -joystickThreshold){
-//                xtstAdapter.sendKey("Left", true);
-//            }else{
-//                xtstAdapter.sendKey("Right", false);
-//                xtstAdapter.sendKey("Left", false);
-//            }
-//        }
-
-//        onYChanged: {
-//            if(val > joystickThreshold){
-//                xtstAdapter.sendKey("Down", true);
-//            }else if(val < -joystickThreshold){
-//                xtstAdapter.sendKey("Up", true);
-//            }else{
-//                xtstAdapter.sendKey("Down", false);
-//                xtstAdapter.sendKey("Up", false);
-//            }
-//        }
     }
 
     XtstAdapter{