Add properties for joystick directions.
[qzeecontrol] / qml / QZeeControl / MainPage.qml
index 58cf162..8a7f8c9 100644 (file)
 import QtQuick 1.1
 import com.nokia.meego 1.0
 import QtMobility.connectivity 1.2
+import "settingsstorage.js" as SettingsStorage
 import qzeecontrol 1.0
 
 Page {
     tools: commonTools
 
-    Label {
-        id: label
-        anchors{bottom: connectButton.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
+    orientationLock: PageOrientation.LockPortrait
 
-        text: "Press to connect."
+    Component.onCompleted: {
+        SettingsStorage.initialize();
+
+        var address = SettingsStorage.getSetting("address");
+        var port = SettingsStorage.getSetting("port");
+        if(address !== "Unknown" && port !== "Unknown"){
+            console.log("Loaded address " + address + " and port " + port + " from DB.")
+            addressField.text = address
+            portField.text = port
+        }
     }
 
-    Button{
-        id: connectButton
-        anchors{bottom: disconnectButton.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
+    states: [
+        State {
+            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)
+            }
+            PropertyChanges {
+                target: labelA
+                color: btConn.a ? "red" : "blue"
+            }
+            PropertyChanges {
+                target: labelB
+                color: btConn.b ? "red" : "blue"
+            }
+            PropertyChanges {
+                target: labelC
+                color: btConn.c ? "red" : "blue"
+            }
+            PropertyChanges {
+                target: labelD
+                color: btConn.d ? "red" : "blue"
+            }
+        },
+        State {
+            name: "inactive"
+            PropertyChanges {
+                target: cursorRectangle
+                x: moveArea.x + (moveArea.width * 0.5) - (cursorRectangle.width * 0.5)
+                y: moveArea.y + (moveArea.height * 0.5) - (cursorRectangle.height * 0.5)
+            }
+            PropertyChanges {
+                target: labelA
+                color: "blue"
+            }
+            PropertyChanges {
+                target: labelB
+                color: "blue"
+            }
+            PropertyChanges {
+                target: labelC
+                color: "blue"
+            }
+            PropertyChanges {
+                target: labelD
+                color: "blue"
+            }
+        }
 
-        text: "Connect"
+    ]
 
-        onClicked: {
-            btDiscovery.discovery = true
+    Connections {
+        target: platformWindow
+
+        onActiveChanged: {
+            if(platformWindow.active){
+                state = "active"
+            }else{
+                state = "inactive"
+            }
         }
     }
 
-    Button{
-        id: disconnectButton
-        anchors{bottom: buttonRow.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
+    Item {
+        anchors.fill: parent
+
+        Image {
+            id: header
+            height: 72
+            source: "image://theme/color8-meegotouch-view-header-fixed"
+            anchors.left: parent.left
+            anchors.top: parent.top
+            anchors.right: parent.right
+
+            Text {
+                text: "QZeeControl"
+                color: "white"
+                font.family: "Nokia Pure Text Light"
+                font.pixelSize: 32
+                anchors.left: parent.left
+                anchors.leftMargin: 20
+                anchors.verticalCenter: parent.verticalCenter
+            }
+        }
+
+        Button{
+            id: scanButton
+            anchors{bottom: addressRow.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
+            enabled: true
 
-        text: "Disconnect"
+            text: "Scan"
 
-        onClicked: {
-            btConn.disconnect()
+            onClicked: {
+                btDiscovery.discovery = true
+            }
         }
-    }
 
-    Row{
-        id: buttonRow
-        anchors.centerIn: parent
-        spacing: 20
+        Row{
+            id: addressRow
+            spacing: 5
+            anchors{bottom: infoText.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
+
+            TextField{
+                id: addressField
+                text: "No device found yet."
 
-        Label{
-            id: labelA
-            text: "A"
-            color: btConn.a ? "red" : "blue"
+                onTextChanged: {
+                    if(text === "No device found yet.")
+                        return
+
+                    connectButton.enabled = true
+                    infoText.text = "Press \"Connect\" to connect to the device."
+                    console.log("Storing address in DB: " + text)
+                    SettingsStorage.setSetting("address", text)
+                }
+            }
+            TextField{
+                id: portField
+                text: "na"
+                width: 60
+                validator: IntValidator{}
+
+                onTextChanged: {
+                    if(text === "na")
+                        return
+
+                    console.log("Storing port in DB: " + text)
+                    SettingsStorage.setSetting("port", text)
+                }
+            }
         }
-        Label{
-            id: labelB
-            text: "B"
-            color: btConn.b ? "red" : "blue"
+
+        Label {
+            id: infoText
+            anchors{bottom: connectButton.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
+            width: parent.width
+
+            text: "Please scan for a device first."
+            horizontalAlignment: Text.AlignHCenter
+            wrapMode: Text.WordWrap
+        }
+
+        Button{
+            id: connectButton
+            anchors{bottom: disconnectButton.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
+            enabled: false
+
+            text: "Connect"
+
+            onClicked: {
+                enabled = false
+                btConn.connect(addressField.text, parseInt(portField.text))
+            }
         }
-        Label{
-            id: labelC
-            text: "C"
-            color: btConn.c ? "red" : "blue"
+
+        Button{
+            id: disconnectButton
+            anchors.centerIn: parent
+
+            text: "Disconnect"
+
+            onClicked: {
+                btConn.disconnect()
+
+            }
         }
-        Label{
-            id: labelD
-            text: "D"
-            color: btConn.d ? "red" : "blue"
+
+        Row{
+            id: buttonRow
+            anchors{top: disconnectButton.bottom; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
+
+            spacing: 20
+
+            Label{
+                id: labelA
+                text: "A"
+                color: btConn.a ? "red" : "blue"
+            }
+            Label{
+                id: labelB
+                text: "B"
+                color: btConn.b ? "red" : "blue"
+            }
+            Label{
+                id: labelC
+                text: "C"
+                color: btConn.c ? "red" : "blue"
+            }
+            Label{
+                id: labelD
+                text: "D"
+                color: btConn.d ? "red" : "blue"
+            }
         }
-    }
 
-    Rectangle{
-        id: moveArea
-        anchors{top: buttonRow.bottom; topMargin: 10; horizontalCenter: parent.horizontalCenter}
-        color: "gray"
+        Rectangle{
+            id: moveArea
+            anchors{top: buttonRow.bottom; topMargin: 10; horizontalCenter: parent.horizontalCenter}
+            color: "gray"
 
-        width: 256
-        height: 256
-    }
+            width: 256
+            height: 256
+        }
 
-    Rectangle{
-        id: cursorRectangle
-        width: 10
-        height: 10
-        color: "red"
+        Rectangle{
+            id: cursorRectangle
+            width: 10
+            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) + btConn.x - (cursorRectangle.width * 0.5)
+            y: moveArea.y + (moveArea.height * 0.5) + btConn.y - (cursorRectangle.height * 0.5)
+        }
     }
 
     BluetoothDiscoveryModel{
@@ -108,105 +264,97 @@ Page {
 
         onDiscoveryChanged: {
             if(discovery){
-                label.text = "Scanning for devices..."
+                infoText.text = "Scanning for a device..."
+                scanButton.enabled = false
                 connectButton.enabled = false
                 disconnectButton.enabled = false
             }else{
-                connectButton.enabled = true
+                scanButton.enabled = true
                 disconnectButton.enabled = false
+
+                if(addressField.text !== "No device found yet." && portField.text !== "na")
+                    connectButton.enabled = true
             }
         }
 
         onNewServiceDiscovered: {
-            console.log("Service " + service.serviceName + " found on " + service.deviceName + " at address " + service.deviceAddress + " on port " + service.servicePort + ".")
-            //btSocket.service = service
-            btConn.connect(service.deviceAddress, service.servicePort)
+            console.log("Service " + service.serviceName + " found on "
+                        + service.deviceName + " at address " + service.deviceAddress
+                        + " on port " + service.servicePort + ".")
+            if(service.serviceName === "Zeemote"){
+                addressField.text = service.deviceAddress
+                portField.text = service.servicePort
+            }
         }
     }
 
-    /*
-    BluetoothSocket{
-        id: btSocket
-
-        onDataAvailable: {
-            console.log("Data available: " + stringData.charCodeAt(0) + data)
-        }
-
-        onServiceChanged: {
-            console.log("Service changed. Connecting...")
-            connected = true
-        }
-
-        onConnectedChanged: {
-            console.log("Connected.")
-        }
-    }*/
-
     BtConnector{
         id: btConn
 
-        property int joystickThreshold: 50
+        threshold: 50
 
         onConnected: {
-            connectButton.enabled = false
             disconnectButton.enabled = true
-            label.text = "Connected."
+            infoText.text = "Connected. Have fun."
         }
 
         onDisconnected: {
             connectButton.enabled = true
             disconnectButton.enabled = false
-            label.text = "Press to connect."
+            infoText.text = "Press \"Connect\" to connect to the device."
         }
 
-        onStickMoved: {
-            console.log("Stick moved. x: " + x + " y: " + y)
-        }
+//        onStickMoved: {
+//            console.log("Stick moved. x: " + x + " y: " + y)
+//        }
 
-        onButtonsChanged: {
-            console.log("Buttons changed. A: " + a + " B: " + b + " C: " + c + " D: " + d)
-        }
+//        onButtonsChanged: {
+//            console.log("Buttons changed. A: " + a + " B: " + b + " C: " + c + " D: " + d)
+//        }
 
         onAChanged: {
-            console.log("A changed to: " + val)
-            if(val){
-                xtstAdapter.sendKeyPress("a");
-            }
+//            console.log("A changed to: " + val)
+            xtstAdapter.sendKey("a", val);
         }
         onBChanged: {
-            console.log("B changed to: " + val)
-            if(val){
-                xtstAdapter.sendKeyPress("b");
-            }
+//            console.log("B changed to: " + val)
+            xtstAdapter.sendKey("b", val);
         }
         onCChanged: {
-            console.log("C changed to: " + val)
-            if(val){
-                xtstAdapter.sendKeyPress("c");
-            }
+//            console.log("C changed to: " + val)
+            xtstAdapter.sendKey("c", val);
         }
         onDChanged: {
-            console.log("D changed to: " + val)
-            if(val){
-                xtstAdapter.sendKeyPress("d");
-            }
-        }
-
-        onXChanged: {
-            if(val > joystickThreshold){
-                xtstAdapter.sendKeyPress("Right");
-            }else if(val < -joystickThreshold){
-                xtstAdapter.sendKeyPress("Left");
-            }
+//            console.log("D changed to: " + val)
+            xtstAdapter.sendKey("d", val);
         }
 
-        onYChanged: {
-            if(val > joystickThreshold){
-                xtstAdapter.sendKeyPress("Down");
-            }else if(val < -joystickThreshold){
-                xtstAdapter.sendKeyPress("Up");
-            }
-        }
+        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{