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