4140b9e4d19e40ab527d057ab6a92a8726170bad
[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                     width: 280
277
278                     onTextChanged: {
279                         if(mainPage.initializing)
280                             return
281
282                         if(text === "No Zeemote found yet.")
283                             return
284
285                         updateConnectAndScanButton();
286
287                         console.log("Storing address in DB: " + text)
288                         SettingsStorage.setSetting("address", text)
289                     }
290                 }
291                 TextField{
292                     id: portField
293                     text: "na"
294                     width: 60
295                     validator: IntValidator{}
296
297                     onTextChanged: {
298                         if(mainPage.initializing)
299                             return
300
301                         if(text === "na")
302                             return
303
304                         console.log("Storing port in DB: " + text)
305                         SettingsStorage.setSetting("port", text)
306                     }
307                 }
308             }
309
310             Label {
311                 id: infoText
312                 width: parent.width
313
314                 horizontalAlignment: Text.AlignHCenter
315                 wrapMode: Text.WordWrap
316             }
317
318             Button{
319                 id: connectButton
320                 anchors.horizontalCenter: parent.horizontalCenter
321                 enabled: false
322
323                 text: "Connect"
324
325                 onClicked: {
326                     mainPage.state = "connecting"
327                     btConn.connect(addressField.text, parseInt(portField.text))
328                 }
329             }
330
331             Button{
332                 id: disconnectButton
333                 anchors.horizontalCenter: parent.horizontalCenter
334
335                 text: "Disconnect"
336
337                 onClicked: {
338                     btConn.disconnect()
339                 }
340             }
341
342             Row{
343                 id: buttonRow
344                 anchors.horizontalCenter: parent.horizontalCenter
345
346                 spacing: 20
347
348                 Label{
349                     id: labelA
350                     text: "A"
351                     color: btConn.a ? "red" : "blue"
352                 }
353                 Label{
354                     id: labelB
355                     text: "B"
356                     color: btConn.b ? "red" : "blue"
357                 }
358                 Label{
359                     id: labelC
360                     text: "C"
361                     color: btConn.c ? "red" : "blue"
362                 }
363                 Label{
364                     id: labelD
365                     text: "D"
366                     color: btConn.d ? "red" : "blue"
367                 }
368             }
369
370             Item{
371                 id: testArea
372                 anchors.horizontalCenter: parent.horizontalCenter
373                 height: moveArea.height
374                 width: moveArea.width
375
376                 Rectangle{
377                     id: moveArea
378                     color: "gray"
379
380                     width: 256
381                     height: 256
382                 }
383
384                 Rectangle{
385                     id: cursorRectangle
386                     width: 10
387                     height: 10
388                     color: "red"
389
390                     x: moveArea.x + (moveArea.width * 0.5) + btConn.x - (cursorRectangle.width * 0.5)
391                     y: moveArea.y + (moveArea.height * 0.5) + btConn.y - (cursorRectangle.height * 0.5)
392                 }
393             }
394         }
395     }
396
397     DeviceInfo{
398         id: deviceInfo
399
400         monitorBluetoothStateChanges: true
401
402         onBluetoothStateChanged: {
403             updateConnectAndScanButton()
404         }
405     }
406
407     BluetoothDiscoveryModel{
408         id: btDiscovery
409
410         discovery: false
411         minimalDiscovery: true
412
413         onDiscoveryChanged: {
414             if(discovery){
415                 infoText.text = "Scanning for a Zeemote..."
416                 scanButton.enabled = false
417                 connectButton.enabled = false
418                 disconnectButton.enabled = false
419                 addressField.enabled = false
420                 portField.enabled = false
421             }else{
422                 scanButton.enabled = true
423                 disconnectButton.enabled = false
424                 addressField.enabled = true
425                 portField.enabled = true
426
427                 if(addressField.text !== "No Zeemote found yet." && portField.text !== "na"){
428                     infoText.text = "Zeemote found. To enable remote control please press \"Connect\" when ready."
429                     connectButton.enabled = true
430                 }
431             }
432         }
433
434         onNewServiceDiscovered: {
435             console.log("Service " + service.serviceName + " found on "
436                         + service.deviceName + " at address " + service.deviceAddress
437                         + " on port " + service.servicePort + ".")
438             if(service.serviceName === "Zeemote"){
439                 addressField.text = service.deviceAddress
440                 portField.text = service.servicePort
441                 discovery = false
442                 console.log("Found Zeemote. Stopped further discovery.")
443             }
444         }
445     }
446
447     BtConnector{
448         id: btConn
449
450         threshold: 50
451
452         property string keyBindingA
453         property string keyBindingB
454         property string keyBindingC
455         property string keyBindingD
456
457         property string keyBindingUp
458         property string keyBindingDown
459         property string keyBindingLeft
460         property string keyBindingRight
461
462         onConnected: {
463             disconnectButton.enabled = true
464             infoText.text = "Connected. Have fun."
465         }
466
467         onDisconnected: {
468             mainPage.state = "disconnected"
469         }
470
471         //        onStickMoved: {
472         //            console.log("Stick moved. x: " + x + " y: " + y)
473         //        }
474
475         //        onButtonsChanged: {
476         //            console.log("Buttons changed. A: " + a + " B: " + b + " C: " + c + " D: " + d)
477         //        }
478
479         onAChanged: {
480             //            console.log("A changed to: " + val)
481             xtstAdapter.sendKey(keyBindingA, val);
482         }
483         onBChanged: {
484             //            console.log("B changed to: " + val)
485             xtstAdapter.sendKey(keyBindingB, val);
486         }
487         onCChanged: {
488             //            console.log("C changed to: " + val)
489             xtstAdapter.sendKey(keyBindingC, val);
490         }
491         onDChanged: {
492             //            console.log("D changed to: " + val)
493             xtstAdapter.sendKey(keyBindingD, val);
494         }
495
496         onUpChanged: xtstAdapter.sendKey(keyBindingUp, val)
497         onDownChanged: xtstAdapter.sendKey(keyBindingDown, val)
498         onLeftChanged: xtstAdapter.sendKey(keyBindingLeft, val)
499         onRightChanged: xtstAdapter.sendKey(keyBindingRight, val)
500
501         //        onXChanged: {
502         //            if(val > joystickThreshold){
503         //                xtstAdapter.sendKey("Right", true);
504         //            }else if(val < -joystickThreshold){
505         //                xtstAdapter.sendKey("Left", true);
506         //            }else{
507         //                xtstAdapter.sendKey("Right", false);
508         //                xtstAdapter.sendKey("Left", false);
509         //            }
510         //        }
511
512         //        onYChanged: {
513         //            if(val > joystickThreshold){
514         //                xtstAdapter.sendKey("Down", true);
515         //            }else if(val < -joystickThreshold){
516         //                xtstAdapter.sendKey("Up", true);
517         //            }else{
518         //                xtstAdapter.sendKey("Down", false);
519         //                xtstAdapter.sendKey("Up", false);
520         //            }
521         //        }
522     }
523
524     XtstAdapter{
525         id: xtstAdapter
526     }
527 }