Add some minor sanity check.
[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         zeeRemoteControl.keyBindingA = SettingsStorage.getSetting("A")
72         zeeRemoteControl.keyBindingB = SettingsStorage.getSetting("B")
73         zeeRemoteControl.keyBindingC = SettingsStorage.getSetting("C")
74         zeeRemoteControl.keyBindingD = SettingsStorage.getSetting("D")
75
76         zeeRemoteControl.keyBindingUp = SettingsStorage.getSetting("Up")
77         zeeRemoteControl.keyBindingDown = SettingsStorage.getSetting("Down")
78         zeeRemoteControl.keyBindingLeft = SettingsStorage.getSetting("Left")
79         zeeRemoteControl.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     states: [
106         State {
107             name: "active"
108             PropertyChanges {
109                 target: cursorRectangle
110                 x: moveArea.x + (moveArea.width * 0.5) + zeeRemoteControl.x - (cursorRectangle.width * 0.5)
111                 y: moveArea.y + (moveArea.height * 0.5) + zeeRemoteControl.y - (cursorRectangle.height * 0.5)
112             }
113             PropertyChanges {
114                 target: labelA
115                 color: zeeRemoteControl.a ? "red" : "blue"
116             }
117             PropertyChanges {
118                 target: labelB
119                 color: zeeRemoteControl.b ? "red" : "blue"
120             }
121             PropertyChanges {
122                 target: labelC
123                 color: zeeRemoteControl.c ? "red" : "blue"
124             }
125             PropertyChanges {
126                 target: labelD
127                 color: zeeRemoteControl.d ? "red" : "blue"
128             }
129         },
130         State {
131             name: "inactive"
132             PropertyChanges {
133                 target: cursorRectangle
134                 x: moveArea.x + (moveArea.width * 0.5) - (cursorRectangle.width * 0.5)
135                 y: moveArea.y + (moveArea.height * 0.5) - (cursorRectangle.height * 0.5)
136             }
137             PropertyChanges {
138                 target: labelA
139                 color: "blue"
140             }
141             PropertyChanges {
142                 target: labelB
143                 color: "blue"
144             }
145             PropertyChanges {
146                 target: labelC
147                 color: "blue"
148             }
149             PropertyChanges {
150                 target: labelD
151                 color: "blue"
152             }
153         },
154         State {
155             name: "connecting"
156             PropertyChanges {
157                 target: scanButton
158                 enabled: false
159             }
160             PropertyChanges {
161                 target: addressField
162                 enabled: false
163             }
164             PropertyChanges {
165                 target: portField
166                 enabled: false
167             }
168             PropertyChanges {
169                 target: connectButton
170                 enabled: false
171             }
172             PropertyChanges {
173                 target: disconnectButton
174                 enabled: false
175             }
176             PropertyChanges {
177                 target: infoText
178                 text: "Connecting..."
179             }
180         },
181         State {
182             name: "disconnected"
183             PropertyChanges {
184                 target: scanButton
185                 enabled: true
186             }
187             PropertyChanges {
188                 target: addressField
189                 enabled: true
190             }
191             PropertyChanges {
192                 target: portField
193                 enabled: true
194             }
195             PropertyChanges {
196                 target: connectButton
197                 enabled: true
198             }
199             PropertyChanges {
200                 target: disconnectButton
201                 enabled: false
202             }
203             PropertyChanges {
204                 target: infoText
205                 text: "To enable remote control please press \"Connect\" when ready."
206             }
207         }
208     ]
209
210     Connections {
211         target: platformWindow
212
213         onActiveChanged: {
214             if(platformWindow.active){
215                 state = "active"
216             }else{
217                 state = "inactive"
218             }
219         }
220     }
221
222     Item {
223         id: headerItem
224         anchors{top: parent.top; left: parent.left; right: parent.right}
225         height: header.height
226         z: 1
227
228         Image {
229             id: header
230             height: 72
231             source: "image://theme/color8-meegotouch-view-header-fixed"
232             anchors.fill: parent
233
234             Text {
235                 text: "QZeeControl"
236                 color: "white"
237                 font.family: "Nokia Pure Text Light"
238                 font.pixelSize: 32
239                 anchors.left: parent.left
240                 anchors.leftMargin: 20
241                 anchors.verticalCenter: parent.verticalCenter
242             }
243         }
244     }
245
246     Flickable {
247         anchors{top: headerItem.bottom; bottom: parent.bottom; left: parent.left; right: parent.right}
248         contentHeight: contentColumn.height
249
250         Column{
251             id: contentColumn
252             spacing: 10
253             anchors{top: parent.top; left: parent.left; right: parent.right; topMargin: 10}
254
255             Button{
256                 id: scanButton
257                 enabled: false
258
259                 anchors.horizontalCenter: parent.horizontalCenter
260                 text: "Scan"
261
262                 onClicked: {
263                     btDiscovery.discovery = true
264                 }
265             }
266
267             Row{
268                 id: addressRow
269                 anchors.horizontalCenter: parent.horizontalCenter
270                 spacing: 5
271
272                 TextField{
273                     id: addressField
274                     text: "No Zeemote found yet."
275                     width: 280
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                     zeeRemoteControl.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                     zeeRemoteControl.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: zeeRemoteControl.a ? "red" : "blue"
351                 }
352                 Label{
353                     id: labelB
354                     text: "B"
355                     color: zeeRemoteControl.b ? "red" : "blue"
356                 }
357                 Label{
358                     id: labelC
359                     text: "C"
360                     color: zeeRemoteControl.c ? "red" : "blue"
361                 }
362                 Label{
363                     id: labelD
364                     text: "D"
365                     color: zeeRemoteControl.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) + zeeRemoteControl.x - (cursorRectangle.width * 0.5)
390                     y: moveArea.y + (moveArea.height * 0.5) + zeeRemoteControl.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(initializing)
414                 return
415
416             if(discovery){
417                 infoText.text = "Scanning for a Zeemote..."
418                 scanButton.enabled = false
419                 connectButton.enabled = false
420                 disconnectButton.enabled = false
421                 addressField.enabled = false
422                 portField.enabled = false
423             }else{
424                 scanButton.enabled = true
425                 disconnectButton.enabled = false
426                 addressField.enabled = true
427                 portField.enabled = true
428
429                 if(addressField.text !== "No Zeemote found yet." && portField.text !== "na"){
430                     infoText.text = "Zeemote found. To enable remote control please press \"Connect\" when ready."
431                     connectButton.enabled = true
432                 }
433             }
434         }
435
436         onNewServiceDiscovered: {
437             console.log("Service " + service.serviceName + " found on "
438                         + service.deviceName + " at address " + service.deviceAddress
439                         + " on port " + service.servicePort + ".")
440             if(service.serviceName === "Zeemote"){
441                 addressField.text = service.deviceAddress
442                 portField.text = service.servicePort
443                 discovery = false
444                 console.log("Found Zeemote. Stopped further discovery.")
445             }
446         }
447     }
448
449     ZeeRemoteControl{
450         id: zeeRemoteControl
451
452         threshold: 50
453
454         onConnected: {
455             disconnectButton.enabled = true
456             infoText.text = "Connected. Have fun."
457         }
458         onDisconnected: mainPage.state = "disconnected"
459     }
460
461     XtstAdapter{
462         id: xtstAdapter
463     }
464 }