Optimize startup and scan.
[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     orientationLock: PageOrientation.LockPortrait
30
31     property bool initializing: true
32
33     Component.onCompleted: {
34         SettingsStorage.initialize();
35
36         var address = SettingsStorage.getSetting("address");
37         var port = SettingsStorage.getSetting("port");
38         if(address !== "Unknown" && port !== "Unknown"){
39             console.log("Loaded address " + address + " and port " + port + " from DB.")
40             addressField.text = address
41             portField.text = port
42         }
43
44         if(SettingsStorage.getSetting("A") === "Unknown"){
45             console.log("Initializing key bindings.")
46             setKeyBindingsToDefault()
47         }
48
49         loadKeyBindings()
50         initializing = false
51     }
52
53     function setKeyBindingsToDefault(){
54         console.log("Setting key bindings to default.")
55         SettingsStorage.setSetting("A", "a")
56         SettingsStorage.setSetting("B", "b")
57         SettingsStorage.setSetting("C", "c")
58         SettingsStorage.setSetting("D", "d")
59
60         SettingsStorage.setSetting("Up", "Up")
61         SettingsStorage.setSetting("Down", "Down")
62         SettingsStorage.setSetting("Left", "Left")
63         SettingsStorage.setSetting("Right", "Right")
64     }
65
66     function loadKeyBindings(){
67         console.log("Loading key bindings.")
68         btConn.keyBindingA = SettingsStorage.getSetting("A")
69         btConn.keyBindingB = SettingsStorage.getSetting("B")
70         btConn.keyBindingC = SettingsStorage.getSetting("C")
71         btConn.keyBindingD = SettingsStorage.getSetting("D")
72
73         btConn.keyBindingUp = SettingsStorage.getSetting("Up")
74         btConn.keyBindingDown = SettingsStorage.getSetting("Down")
75         btConn.keyBindingLeft = SettingsStorage.getSetting("Left")
76         btConn.keyBindingRight = SettingsStorage.getSetting("Right")
77     }
78
79     states: [
80         State {
81             name: "active"
82             PropertyChanges {
83                 target: cursorRectangle
84                 x: moveArea.x + (moveArea.width * 0.5) + btConn.x - (cursorRectangle.width * 0.5)
85                 y: moveArea.y + (moveArea.height * 0.5) + btConn.y - (cursorRectangle.height * 0.5)
86             }
87             PropertyChanges {
88                 target: labelA
89                 color: btConn.a ? "red" : "blue"
90             }
91             PropertyChanges {
92                 target: labelB
93                 color: btConn.b ? "red" : "blue"
94             }
95             PropertyChanges {
96                 target: labelC
97                 color: btConn.c ? "red" : "blue"
98             }
99             PropertyChanges {
100                 target: labelD
101                 color: btConn.d ? "red" : "blue"
102             }
103         },
104         State {
105             name: "inactive"
106             PropertyChanges {
107                 target: cursorRectangle
108                 x: moveArea.x + (moveArea.width * 0.5) - (cursorRectangle.width * 0.5)
109                 y: moveArea.y + (moveArea.height * 0.5) - (cursorRectangle.height * 0.5)
110             }
111             PropertyChanges {
112                 target: labelA
113                 color: "blue"
114             }
115             PropertyChanges {
116                 target: labelB
117                 color: "blue"
118             }
119             PropertyChanges {
120                 target: labelC
121                 color: "blue"
122             }
123             PropertyChanges {
124                 target: labelD
125                 color: "blue"
126             }
127         },
128         State {
129             name: "connecting"
130             PropertyChanges {
131                 target: scanButton
132                 enabled: false
133             }
134             PropertyChanges {
135                 target: addressField
136                 enabled: false
137             }
138             PropertyChanges {
139                 target: portField
140                 enabled: false
141             }
142             PropertyChanges {
143                 target: connectButton
144                 enabled: false
145             }
146             PropertyChanges {
147                 target: disconnectButton
148                 enabled: false
149             }
150             PropertyChanges {
151                 target: infoText
152                 text: "Connecting..."
153             }
154         },
155         State {
156             name: "disconnected"
157             PropertyChanges {
158                 target: scanButton
159                 enabled: true
160             }
161             PropertyChanges {
162                 target: addressField
163                 enabled: true
164             }
165             PropertyChanges {
166                 target: portField
167                 enabled: true
168             }
169             PropertyChanges {
170                 target: connectButton
171                 enabled: true
172             }
173             PropertyChanges {
174                 target: disconnectButton
175                 enabled: false
176             }
177             PropertyChanges {
178                 target: infoText
179                 text: "Press \"Connect\" to connect to the device."
180             }
181         }
182     ]
183
184     Connections {
185         target: platformWindow
186
187         onActiveChanged: {
188             if(platformWindow.active){
189                 state = "active"
190             }else{
191                 state = "inactive"
192             }
193         }
194     }
195
196     Item {
197         id: headerItem
198         anchors{top: parent.top; left: parent.left; right: parent.right}
199         height: header.height
200         z: 1
201
202         Image {
203             id: header
204             height: 72
205             source: "image://theme/color8-meegotouch-view-header-fixed"
206             anchors.fill: parent
207
208             Text {
209                 text: "QZeeControl"
210                 color: "white"
211                 font.family: "Nokia Pure Text Light"
212                 font.pixelSize: 32
213                 anchors.left: parent.left
214                 anchors.leftMargin: 20
215                 anchors.verticalCenter: parent.verticalCenter
216             }
217         }
218     }
219
220     Flickable {
221         anchors{top: headerItem.bottom; bottom: parent.bottom; left: parent.left; right: parent.right}
222         contentHeight: contentColumn.height
223
224             Column{
225                 id: contentColumn
226                 spacing: 10
227                 anchors{top: parent.top; left: parent.left; right: parent.right; topMargin: 10}
228
229             Button{
230                 id: scanButton
231                 enabled: true
232
233                 anchors.horizontalCenter: parent.horizontalCenter
234                 text: "Scan"
235
236                 onClicked: {
237                     btDiscovery.discovery = true
238                 }
239             }
240
241             Row{
242                 id: addressRow
243                 anchors.horizontalCenter: parent.horizontalCenter
244                 spacing: 5
245
246                 TextField{
247                     id: addressField
248                     text: "No device found yet."
249
250                     onTextChanged: {
251                         if(initializing)
252                             return
253
254                         if(text === "No device found yet.")
255                             return
256
257                         connectButton.enabled = true
258                         infoText.text = "Press \"Connect\" to connect to the device."
259                         console.log("Storing address in DB: " + text)
260                         SettingsStorage.setSetting("address", text)
261                     }
262                 }
263                 TextField{
264                     id: portField
265                     text: "na"
266                     width: 60
267                     validator: IntValidator{}
268
269                     onTextChanged: {
270                         if(initializing)
271                             return
272
273                         if(text === "na")
274                             return
275
276                         console.log("Storing port in DB: " + text)
277                         SettingsStorage.setSetting("port", text)
278                     }
279                 }
280             }
281
282             Label {
283                 id: infoText
284                 width: parent.width
285
286                 text: "Please scan for a device first."
287                 horizontalAlignment: Text.AlignHCenter
288                 wrapMode: Text.WordWrap
289             }
290
291             Button{
292                 id: connectButton
293                 anchors.horizontalCenter: parent.horizontalCenter
294                 enabled: false
295
296                 text: "Connect"
297
298                 onClicked: {
299                     mainPage.state = "connecting"
300                     btConn.connect(addressField.text, parseInt(portField.text))
301                 }
302             }
303
304             Button{
305                 id: disconnectButton
306                 anchors.horizontalCenter: parent.horizontalCenter
307
308                 text: "Disconnect"
309
310                 onClicked: {
311                     btConn.disconnect()
312                 }
313             }
314
315             Row{
316                 id: buttonRow
317                 anchors.horizontalCenter: parent.horizontalCenter
318
319                 spacing: 20
320
321                 Label{
322                     id: labelA
323                     text: "A"
324                     color: btConn.a ? "red" : "blue"
325                 }
326                 Label{
327                     id: labelB
328                     text: "B"
329                     color: btConn.b ? "red" : "blue"
330                 }
331                 Label{
332                     id: labelC
333                     text: "C"
334                     color: btConn.c ? "red" : "blue"
335                 }
336                 Label{
337                     id: labelD
338                     text: "D"
339                     color: btConn.d ? "red" : "blue"
340                 }
341             }
342
343             Item{
344                 id: testArea
345                 anchors.horizontalCenter: parent.horizontalCenter
346                 height: moveArea.height
347                 width: moveArea.width
348
349                 Rectangle{
350                     id: moveArea
351                     color: "gray"
352
353                     width: 256
354                     height: 256
355                 }
356
357                 Rectangle{
358                     id: cursorRectangle
359                     width: 10
360                     height: 10
361                     color: "red"
362
363                     x: moveArea.x + (moveArea.width * 0.5) + btConn.x - (cursorRectangle.width * 0.5)
364                     y: moveArea.y + (moveArea.height * 0.5) + btConn.y - (cursorRectangle.height * 0.5)
365                 }
366             }
367         }
368     }
369
370     BluetoothDiscoveryModel{
371         id: btDiscovery
372
373         discovery: false
374         minimalDiscovery: true
375
376         onDiscoveryChanged: {
377             if(discovery){
378                 infoText.text = "Scanning for a device..."
379                 scanButton.enabled = false
380                 connectButton.enabled = false
381                 disconnectButton.enabled = false
382                 addressField.enabled = false
383                 portField.enabled = false
384             }else{
385                 scanButton.enabled = true
386                 disconnectButton.enabled = false
387                 addressField.enabled = true
388                 portField.enabled = true
389
390                 if(addressField.text !== "No device found yet." && portField.text !== "na"){
391                     infoText.text = "Device found. You can now connect."
392                     connectButton.enabled = true
393                 }
394             }
395         }
396
397         onNewServiceDiscovered: {
398             console.log("Service " + service.serviceName + " found on "
399                         + service.deviceName + " at address " + service.deviceAddress
400                         + " on port " + service.servicePort + ".")
401             if(service.serviceName === "Zeemote"){
402                 addressField.text = service.deviceAddress
403                 portField.text = service.servicePort
404                 discovery = false
405                 console.log("Found device. Stopped further discovery.")
406             }
407         }
408     }
409
410     BtConnector{
411         id: btConn
412
413         threshold: 50
414
415         property string keyBindingA
416         property string keyBindingB
417         property string keyBindingC
418         property string keyBindingD
419
420         property string keyBindingUp
421         property string keyBindingDown
422         property string keyBindingLeft
423         property string keyBindingRight
424
425         onConnected: {
426             disconnectButton.enabled = true
427             infoText.text = "Connected. Have fun."
428         }
429
430         onDisconnected: {
431             mainPage.state = "disconnected"
432         }
433
434 //        onStickMoved: {
435 //            console.log("Stick moved. x: " + x + " y: " + y)
436 //        }
437
438 //        onButtonsChanged: {
439 //            console.log("Buttons changed. A: " + a + " B: " + b + " C: " + c + " D: " + d)
440 //        }
441
442         onAChanged: {
443 //            console.log("A changed to: " + val)
444             xtstAdapter.sendKey(keyBindingA, val);
445         }
446         onBChanged: {
447 //            console.log("B changed to: " + val)
448             xtstAdapter.sendKey(keyBindingB, val);
449         }
450         onCChanged: {
451 //            console.log("C changed to: " + val)
452             xtstAdapter.sendKey(keyBindingC, val);
453         }
454         onDChanged: {
455 //            console.log("D changed to: " + val)
456             xtstAdapter.sendKey(keyBindingD, val);
457         }
458
459         onUpChanged: xtstAdapter.sendKey(keyBindingUp, val)
460         onDownChanged: xtstAdapter.sendKey(keyBindingDown, val)
461         onLeftChanged: xtstAdapter.sendKey(keyBindingLeft, val)
462         onRightChanged: xtstAdapter.sendKey(keyBindingRight, val)
463
464 //        onXChanged: {
465 //            if(val > joystickThreshold){
466 //                xtstAdapter.sendKey("Right", true);
467 //            }else if(val < -joystickThreshold){
468 //                xtstAdapter.sendKey("Left", true);
469 //            }else{
470 //                xtstAdapter.sendKey("Right", false);
471 //                xtstAdapter.sendKey("Left", false);
472 //            }
473 //        }
474
475 //        onYChanged: {
476 //            if(val > joystickThreshold){
477 //                xtstAdapter.sendKey("Down", true);
478 //            }else if(val < -joystickThreshold){
479 //                xtstAdapter.sendKey("Up", true);
480 //            }else{
481 //                xtstAdapter.sendKey("Down", false);
482 //                xtstAdapter.sendKey("Up", false);
483 //            }
484 //        }
485     }
486
487     XtstAdapter{
488         id: xtstAdapter
489     }
490 }