dc08d055509b27d25524165b7c71aeb01b74e981
[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 "settingsstorage.js" as SettingsStorage
24 import qzeecontrol 1.0
25
26 Page {
27     tools: commonTools
28
29     Component.onCompleted: {
30         SettingsStorage.initialize();
31
32         var address = SettingsStorage.getSetting("address");
33         var port = SettingsStorage.getSetting("port");
34         if(address !== "Unknown" && port !== "Unknown"){
35             console.log("Loaded address " + address + " and port " + port + " from DB.")
36             addressField.text = address
37             portField.text = port
38         }
39     }
40
41     states: [
42         State {
43             name: "active"
44             PropertyChanges {
45                 target: cursorRectangle
46                 x: moveArea.x + (moveArea.width * 0.5) + btConn.x - (cursorRectangle.width * 0.5)
47                 y: moveArea.y + (moveArea.height * 0.5) + btConn.y - (cursorRectangle.height * 0.5)
48             }
49             PropertyChanges {
50                 target: labelA
51                 color: btConn.a ? "red" : "blue"
52             }
53             PropertyChanges {
54                 target: labelB
55                 color: btConn.b ? "red" : "blue"
56             }
57             PropertyChanges {
58                 target: labelC
59                 color: btConn.c ? "red" : "blue"
60             }
61             PropertyChanges {
62                 target: labelD
63                 color: btConn.d ? "red" : "blue"
64             }
65         },
66         State {
67             name: "inactive"
68             PropertyChanges {
69                 target: cursorRectangle
70                 x: moveArea.x + (moveArea.width * 0.5) - (cursorRectangle.width * 0.5)
71                 y: moveArea.y + (moveArea.height * 0.5) - (cursorRectangle.height * 0.5)
72             }
73             PropertyChanges {
74                 target: labelA
75                 color: "blue"
76             }
77             PropertyChanges {
78                 target: labelB
79                 color: "blue"
80             }
81             PropertyChanges {
82                 target: labelC
83                 color: "blue"
84             }
85             PropertyChanges {
86                 target: labelD
87                 color: "blue"
88             }
89         }
90
91     ]
92
93     Connections {
94         target: platformWindow
95
96         onActiveChanged: {
97             if(platformWindow.active){
98                 state = "active"
99             }else{
100                 state = "inactive"
101             }
102         }
103     }
104
105     Button{
106         id: scanButton
107         anchors{bottom: addressRow.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
108         enabled: true
109
110         text: "Scan"
111
112         onClicked: {
113             btDiscovery.discovery = true
114         }
115     }
116
117     Row{
118         id: addressRow
119         spacing: 5
120         anchors{bottom: infoText.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
121
122         TextField{
123             id: addressField
124             text: "No device found yet."
125
126             onTextChanged: {
127                 if(text === "No device found yet.")
128                     return
129
130                 connectButton.enabled = true
131                 infoText.text = "Press \"Connect\" to connect to the device."
132                 console.log("Storing address in DB: " + text)
133                 SettingsStorage.setSetting("address", text)
134             }
135         }
136         TextField{
137             id: portField
138             text: "na"
139             width: 60
140             validator: IntValidator{}
141
142             onTextChanged: {
143                 if(text === "na")
144                     return
145
146                 console.log("Storing port in DB: " + text)
147                 SettingsStorage.setSetting("port", text)
148             }
149         }
150     }
151
152     Label {
153         id: infoText
154         anchors{bottom: connectButton.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
155         width: parent.width
156
157         text: "Please scan for a device first."
158         horizontalAlignment: Text.AlignHCenter
159         wrapMode: Text.WordWrap
160     }
161
162     Button{
163         id: connectButton
164         anchors{bottom: disconnectButton.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
165         enabled: false
166
167         text: "Connect"
168
169         onClicked: {
170             enabled = false
171             btConn.connect(addressField.text, parseInt(portField.text))
172         }
173     }
174
175     Button{
176         id: disconnectButton
177         anchors{bottom: buttonRow.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
178
179         text: "Disconnect"
180
181         onClicked: {
182             btConn.disconnect()
183
184         }
185     }
186
187     Row{
188         id: buttonRow
189         anchors.centerIn: parent
190         spacing: 20
191
192         Label{
193             id: labelA
194             text: "A"
195             color: btConn.a ? "red" : "blue"
196         }
197         Label{
198             id: labelB
199             text: "B"
200             color: btConn.b ? "red" : "blue"
201         }
202         Label{
203             id: labelC
204             text: "C"
205             color: btConn.c ? "red" : "blue"
206         }
207         Label{
208             id: labelD
209             text: "D"
210             color: btConn.d ? "red" : "blue"
211         }
212     }
213
214     Rectangle{
215         id: moveArea
216         anchors{top: buttonRow.bottom; topMargin: 10; horizontalCenter: parent.horizontalCenter}
217         color: "gray"
218
219         width: 256
220         height: 256
221     }
222
223     Rectangle{
224         id: cursorRectangle
225         width: 10
226         height: 10
227         color: "red"
228
229         x: moveArea.x + (moveArea.width * 0.5) + btConn.x - (cursorRectangle.width * 0.5)
230         y: moveArea.y + (moveArea.height * 0.5) + btConn.y - (cursorRectangle.height * 0.5)
231     }
232
233     BluetoothDiscoveryModel{
234         id: btDiscovery
235
236         discovery: false
237         minimalDiscovery: true
238
239         onDiscoveryChanged: {
240             if(discovery){
241                 infoText.text = "Scanning for a device..."
242                 scanButton.enabled = false
243                 connectButton.enabled = false
244                 disconnectButton.enabled = false
245             }else{
246                 scanButton.enabled = true
247                 disconnectButton.enabled = false
248
249                 if(addressField.text !== "No device found yet." && portField.text !== "na")
250                     connectButton.enabled = true
251             }
252         }
253
254         onNewServiceDiscovered: {
255             console.log("Service " + service.serviceName + " found on "
256                         + service.deviceName + " at address " + service.deviceAddress
257                         + " on port " + service.servicePort + ".")
258             if(service.serviceName === "Zeemote"){
259                 addressField.text = service.deviceAddress
260                 portField.text = service.servicePort
261             }
262         }
263     }
264
265     BtConnector{
266         id: btConn
267
268         property int joystickThreshold: 50
269
270         onConnected: {
271             disconnectButton.enabled = true
272             infoText.text = "Connected. Have fun."
273         }
274
275         onDisconnected: {
276             connectButton.enabled = true
277             disconnectButton.enabled = false
278             infoText.text = "Press \"Connect\" to connect to the device."
279         }
280
281 //        onStickMoved: {
282 //            console.log("Stick moved. x: " + x + " y: " + y)
283 //        }
284
285 //        onButtonsChanged: {
286 //            console.log("Buttons changed. A: " + a + " B: " + b + " C: " + c + " D: " + d)
287 //        }
288
289         onAChanged: {
290 //            console.log("A changed to: " + val)
291             xtstAdapter.sendKey("a", val);
292         }
293         onBChanged: {
294 //            console.log("B changed to: " + val)
295             xtstAdapter.sendKey("b", val);
296         }
297         onCChanged: {
298 //            console.log("C changed to: " + val)
299             xtstAdapter.sendKey("c", val);
300         }
301         onDChanged: {
302 //            console.log("D changed to: " + val)
303             xtstAdapter.sendKey("d", val);
304         }
305
306         onXChanged: {
307             if(val > joystickThreshold){
308                 xtstAdapter.sendKey("Right", true);
309             }else if(val < -joystickThreshold){
310                 xtstAdapter.sendKey("Left", true);
311             }else{
312                 xtstAdapter.sendKey("Right", false);
313                 xtstAdapter.sendKey("Left", false);
314             }
315         }
316
317         onYChanged: {
318             if(val > joystickThreshold){
319                 xtstAdapter.sendKey("Down", true);
320             }else if(val < -joystickThreshold){
321                 xtstAdapter.sendKey("Up", true);
322             }else{
323                 xtstAdapter.sendKey("Down", false);
324                 xtstAdapter.sendKey("Up", false);
325             }
326         }
327     }
328
329     XtstAdapter{
330         id: xtstAdapter
331     }
332 }