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