Optimize startup and scan.
[qzeecontrol] / qml / QZeeControl / MainPage.qml
index 8198d51..848babc 100644 (file)
+/*
+ *  Copyright 2012 Ruediger Gad
+ *
+ *  This file is part of QZeeControl.
+ *
+ *  QZeeControl is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  QZeeControl is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with QZeeControl.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 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.centerIn: parent
-        text: "Press to connect."
+    orientationLock: PageOrientation.LockPortrait
+
+    property bool initializing: true
+
+    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
+        }
+
+        if(SettingsStorage.getSetting("A") === "Unknown"){
+            console.log("Initializing key bindings.")
+            setKeyBindingsToDefault()
+        }
+
+        loadKeyBindings()
+        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")
     }
 
-    Button{
-        id: connectButton
+    function loadKeyBindings(){
+        console.log("Loading key bindings.")
+        btConn.keyBindingA = SettingsStorage.getSetting("A")
+        btConn.keyBindingB = SettingsStorage.getSetting("B")
+        btConn.keyBindingC = SettingsStorage.getSetting("C")
+        btConn.keyBindingD = SettingsStorage.getSetting("D")
+
+        btConn.keyBindingUp = SettingsStorage.getSetting("Up")
+        btConn.keyBindingDown = SettingsStorage.getSetting("Down")
+        btConn.keyBindingLeft = SettingsStorage.getSetting("Left")
+        btConn.keyBindingRight = SettingsStorage.getSetting("Right")
+    }
 
-        anchors {
-            horizontalCenter: parent.horizontalCenter
-            top: label.bottom
-            topMargin: 10
+    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"
+            }
+        },
+        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."
+            }
         }
+    ]
 
-        text: "Connect"
+    Connections {
+        target: platformWindow
 
-        onClicked: {
-            btDiscovery.discovery = true
+        onActiveChanged: {
+            if(platformWindow.active){
+                state = "active"
+            }else{
+                state = "inactive"
+            }
         }
     }
 
-    Button{
-        id: disconnectButton
+    Item {
+        id: headerItem
+        anchors{top: parent.top; left: parent.left; right: parent.right}
+        height: header.height
+        z: 1
+
+        Image {
+            id: header
+            height: 72
+            source: "image://theme/color8-meegotouch-view-header-fixed"
+            anchors.fill: parent
 
-        anchors {
-            horizontalCenter: parent.horizontalCenter
-            top: connectButton.bottom
-            topMargin: 10
+            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
+            }
         }
+    }
+
+    Flickable {
+        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}
+
+            Button{
+                id: scanButton
+                enabled: true
+
+                anchors.horizontalCenter: parent.horizontalCenter
+                text: "Scan"
+
+                onClicked: {
+                    btDiscovery.discovery = true
+                }
+            }
+
+            Row{
+                id: addressRow
+                anchors.horizontalCenter: parent.horizontalCenter
+                spacing: 5
 
-        text: "Disconnect"
+                TextField{
+                    id: addressField
+                    text: "No device found yet."
 
-        onClicked: {
-            btConn.disconnect()
+                    onTextChanged: {
+                        if(initializing)
+                            return
+
+                        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(initializing)
+                            return
+
+                        if(text === "na")
+                            return
+
+                        console.log("Storing port in DB: " + text)
+                        SettingsStorage.setSetting("port", text)
+                    }
+                }
+            }
+
+            Label {
+                id: infoText
+                width: parent.width
+
+                text: "Please scan for a device first."
+                horizontalAlignment: Text.AlignHCenter
+                wrapMode: Text.WordWrap
+            }
+
+            Button{
+                id: connectButton
+                anchors.horizontalCenter: parent.horizontalCenter
+                enabled: false
+
+                text: "Connect"
+
+                onClicked: {
+                    mainPage.state = "connecting"
+                    btConn.connect(addressField.text, parseInt(portField.text))
+                }
+            }
+
+            Button{
+                id: disconnectButton
+                anchors.horizontalCenter: parent.horizontalCenter
+
+                text: "Disconnect"
+
+                onClicked: {
+                    btConn.disconnect()
+                }
+            }
+
+            Row{
+                id: buttonRow
+                anchors.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"
+                }
+            }
+
+            Item{
+                id: testArea
+                anchors.horizontalCenter: parent.horizontalCenter
+                height: moveArea.height
+                width: moveArea.width
+
+                Rectangle{
+                    id: moveArea
+                    color: "gray"
+
+                    width: 256
+                    height: 256
+                }
+
+                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)
+                }
+            }
         }
     }
 
@@ -52,61 +375,116 @@ Page {
 
         onDiscoveryChanged: {
             if(discovery){
-                label.text = "Scanning for devices..."
+                infoText.text = "Scanning for a device..."
+                scanButton.enabled = false
                 connectButton.enabled = false
                 disconnectButton.enabled = false
+                addressField.enabled = false
+                portField.enabled = false
             }else{
-                connectButton.enabled = true
+                scanButton.enabled = true
                 disconnectButton.enabled = false
+                addressField.enabled = true
+                portField.enabled = true
+
+                if(addressField.text !== "No device found yet." && portField.text !== "na"){
+                    infoText.text = "Device found. You can now connect."
+                    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
+                discovery = false
+                console.log("Found device. Stopped further discovery.")
+            }
         }
     }
 
-    /*
-    BluetoothSocket{
-        id: btSocket
+    BtConnector{
+        id: btConn
 
-        onDataAvailable: {
-            console.log("Data available: " + stringData.charCodeAt(0) + data)
-        }
+        threshold: 50
 
-        onServiceChanged: {
-            console.log("Service changed. Connecting...")
-            connected = true
-        }
+        property string keyBindingA
+        property string keyBindingB
+        property string keyBindingC
+        property string keyBindingD
 
-        onConnectedChanged: {
-            console.log("Connected.")
-        }
-    }*/
-
-    BtConnector{
-        id: btConn
+        property string keyBindingUp
+        property string keyBindingDown
+        property string keyBindingLeft
+        property string keyBindingRight
 
         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."
+            mainPage.state = "disconnected"
         }
 
-        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)
+            xtstAdapter.sendKey(keyBindingA, val);
+        }
+        onBChanged: {
+//            console.log("B changed to: " + val)
+            xtstAdapter.sendKey(keyBindingB, val);
+        }
+        onCChanged: {
+//            console.log("C changed to: " + val)
+            xtstAdapter.sendKey(keyBindingC, val);
+        }
+        onDChanged: {
+//            console.log("D changed to: " + val)
+            xtstAdapter.sendKey(keyBindingD, val);
         }
+
+        onUpChanged: xtstAdapter.sendKey(keyBindingUp, val)
+        onDownChanged: xtstAdapter.sendKey(keyBindingDown, val)
+        onLeftChanged: xtstAdapter.sendKey(keyBindingLeft, val)
+        onRightChanged: xtstAdapter.sendKey(keyBindingRight, 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{
+        id: xtstAdapter
     }
 }