16d7f2392578c13b1b83d3361aefe377c23218a2
[qzeecontrol] / qml / QZeeControl / MainPage.qml
1 /*
2  *  Copyright 2012 Ruediger Gad
3  *
4  *  This file is part of QZeeControl.
5  *
6  *  QZeeControl is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  QZeeControl is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with QZeeControl.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 import QtQuick 1.1
21 import com.nokia.meego 1.0
22 import QtMobility.connectivity 1.2
23 import qzeecontrol 1.0
24
25 Page {
26     tools: commonTools
27
28     Label {
29         id: label
30         anchors{bottom: connectButton.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
31
32         text: "Press to connect."
33     }
34
35     Button{
36         id: connectButton
37         anchors{bottom: disconnectButton.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
38
39         text: "Connect"
40
41         onClicked: {
42             btDiscovery.discovery = true
43         }
44     }
45
46     Button{
47         id: disconnectButton
48         anchors{bottom: buttonRow.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
49
50         text: "Disconnect"
51
52         onClicked: {
53             btConn.disconnect()
54         }
55     }
56
57     Row{
58         id: buttonRow
59         anchors.centerIn: parent
60         spacing: 20
61
62         Label{
63             id: labelA
64             text: "A"
65             color: btConn.a ? "red" : "blue"
66         }
67         Label{
68             id: labelB
69             text: "B"
70             color: btConn.b ? "red" : "blue"
71         }
72         Label{
73             id: labelC
74             text: "C"
75             color: btConn.c ? "red" : "blue"
76         }
77         Label{
78             id: labelD
79             text: "D"
80             color: btConn.d ? "red" : "blue"
81         }
82     }
83
84     Rectangle{
85         id: moveArea
86         anchors{top: buttonRow.bottom; topMargin: 10; horizontalCenter: parent.horizontalCenter}
87         color: "gray"
88
89         width: 256
90         height: 256
91     }
92
93     Rectangle{
94         id: cursorRectangle
95         width: 10
96         height: 10
97         color: "red"
98
99         x: moveArea.x + (moveArea.width * 0.5) + btConn.x - (cursorRectangle.width * 0.5)
100         y: moveArea.y + (moveArea.height * 0.5) + btConn.y - (cursorRectangle.height * 0.5)
101     }
102
103     BluetoothDiscoveryModel{
104         id: btDiscovery
105
106         discovery: false
107         minimalDiscovery: true
108
109         onDiscoveryChanged: {
110             if(discovery){
111                 label.text = "Scanning for devices..."
112                 connectButton.enabled = false
113                 disconnectButton.enabled = false
114             }else{
115                 connectButton.enabled = true
116                 disconnectButton.enabled = false
117             }
118         }
119
120         onNewServiceDiscovered: {
121             console.log("Service " + service.serviceName + " found on " + service.deviceName + " at address " + service.deviceAddress + " on port " + service.servicePort + ".")
122             //btSocket.service = service
123             btConn.connect(service.deviceAddress, service.servicePort)
124         }
125     }
126
127     /*
128     BluetoothSocket{
129         id: btSocket
130
131         onDataAvailable: {
132             console.log("Data available: " + stringData.charCodeAt(0) + data)
133         }
134
135         onServiceChanged: {
136             console.log("Service changed. Connecting...")
137             connected = true
138         }
139
140         onConnectedChanged: {
141             console.log("Connected.")
142         }
143     }*/
144
145     BtConnector{
146         id: btConn
147
148         property int joystickThreshold: 50
149
150         onConnected: {
151             connectButton.enabled = false
152             disconnectButton.enabled = true
153             label.text = "Connected."
154         }
155
156         onDisconnected: {
157             connectButton.enabled = true
158             disconnectButton.enabled = false
159             label.text = "Press to connect."
160         }
161
162         onStickMoved: {
163             console.log("Stick moved. x: " + x + " y: " + y)
164         }
165
166         onButtonsChanged: {
167             console.log("Buttons changed. A: " + a + " B: " + b + " C: " + c + " D: " + d)
168         }
169
170         onAChanged: {
171             console.log("A changed to: " + val)
172             xtstAdapter.sendKey("a", val);
173         }
174         onBChanged: {
175             console.log("B changed to: " + val)
176             xtstAdapter.sendKey("b", val);
177         }
178         onCChanged: {
179             console.log("C changed to: " + val)
180             xtstAdapter.sendKey("c", val);
181         }
182         onDChanged: {
183             console.log("D changed to: " + val)
184             xtstAdapter.sendKey("d", val);
185         }
186
187         onXChanged: {
188             if(val > joystickThreshold){
189                 xtstAdapter.sendKey("Right", true);
190             }else if(val < -joystickThreshold){
191                 xtstAdapter.sendKey("Left", true);
192             }else{
193                 xtstAdapter.sendKey("Right", false);
194                 xtstAdapter.sendKey("Left", false);
195             }
196         }
197
198         onYChanged: {
199             if(val > joystickThreshold){
200                 xtstAdapter.sendKey("Down", true);
201             }else if(val < -joystickThreshold){
202                 xtstAdapter.sendKey("Up", true);
203             }else{
204                 xtstAdapter.sendKey("Down", false);
205                 xtstAdapter.sendKey("Up", false);
206             }
207         }
208     }
209
210     XtstAdapter{
211         id: xtstAdapter
212     }
213 }