38f32df739001513127b5387287b8134d67ab01c
[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     ]
155
156     Connections {
157         target: platformWindow
158
159         onActiveChanged: {
160             if(platformWindow.active){
161                 state = "active"
162             }else{
163                 state = "inactive"
164             }
165         }
166     }
167
168     Item {
169         id: headerItem
170         anchors{top: parent.top; left: parent.left; right: parent.right}
171         height: header.height
172         z: 1
173
174         Image {
175             id: header
176             height: 72
177             source: "image://theme/color8-meegotouch-view-header-fixed"
178             anchors.fill: parent
179
180             Text {
181                 text: "QZeeControl"
182                 color: "white"
183                 font.family: "Nokia Pure Text Light"
184                 font.pixelSize: 32
185                 anchors.left: parent.left
186                 anchors.leftMargin: 20
187                 anchors.verticalCenter: parent.verticalCenter
188             }
189         }
190     }
191
192     Flickable {
193         anchors{top: headerItem.bottom; bottom: parent.bottom; left: parent.left; right: parent.right}
194         contentHeight: contentColumn.height
195
196         Column{
197             id: contentColumn
198             spacing: 10
199             anchors{top: parent.top; left: parent.left; right: parent.right; topMargin: 10}
200
201             Button{
202                 id: scanButton
203                 enabled: false
204
205                 anchors.horizontalCenter: parent.horizontalCenter
206                 text: "Scan"
207
208                 onClicked: {
209                     btDiscovery.discovery = true
210                 }
211             }
212
213             Row{
214                 id: addressRow
215                 anchors.horizontalCenter: parent.horizontalCenter
216                 spacing: 5
217
218                 TextField{
219                     id: addressField
220                     text: "No Zeemote found yet."
221                     width: 280
222
223                     onTextChanged: {
224                         if(mainPage.initializing)
225                             return
226
227                         if(text === "No Zeemote found yet.")
228                             return
229
230                         updateConnectAndScanButton();
231
232                         console.log("Storing address in DB: " + text)
233                         SettingsStorage.setSetting("address", text)
234                     }
235                 }
236                 TextField{
237                     id: portField
238                     text: "na"
239                     width: 60
240                     validator: IntValidator{}
241
242                     onTextChanged: {
243                         if(mainPage.initializing)
244                             return
245
246                         if(text === "na")
247                             return
248
249                         console.log("Storing port in DB: " + text)
250                         SettingsStorage.setSetting("port", text)
251                     }
252                 }
253             }
254
255             Label {
256                 id: infoText
257                 width: parent.width
258
259                 horizontalAlignment: Text.AlignHCenter
260                 wrapMode: Text.WordWrap
261             }
262
263             Button{
264                 id: connectButton
265                 anchors.horizontalCenter: parent.horizontalCenter
266                 enabled: false
267
268                 text: "Connect"
269
270                 onClicked: {
271                     scanButton.enabled = false
272                     addressField.enabled = false
273                     portField.enabled = false
274                     connectButton.enabled = false
275                     disconnectButton.enabled = false
276                     infoText.text = "Connecting..."
277
278                     zeeRemoteControl.connect(addressField.text, parseInt(portField.text))
279                 }
280             }
281
282             Button{
283                 id: disconnectButton
284                 anchors.horizontalCenter: parent.horizontalCenter
285
286                 text: "Disconnect"
287                 enabled: false
288
289                 onClicked: {
290                     zeeRemoteControl.disconnect()
291                 }
292             }
293
294             Row{
295                 id: buttonRow
296                 anchors.horizontalCenter: parent.horizontalCenter
297
298                 spacing: 20
299
300                 Label{
301                     id: labelA
302                     text: "A"
303                     color: zeeRemoteControl.a ? "red" : "blue"
304                 }
305                 Label{
306                     id: labelB
307                     text: "B"
308                     color: zeeRemoteControl.b ? "red" : "blue"
309                 }
310                 Label{
311                     id: labelC
312                     text: "C"
313                     color: zeeRemoteControl.c ? "red" : "blue"
314                 }
315                 Label{
316                     id: labelD
317                     text: "D"
318                     color: zeeRemoteControl.d ? "red" : "blue"
319                 }
320             }
321
322             Item{
323                 id: testArea
324                 anchors.horizontalCenter: parent.horizontalCenter
325                 height: moveArea.height
326                 width: moveArea.width
327
328                 Rectangle{
329                     id: moveArea
330                     color: "gray"
331
332                     width: 256
333                     height: 256
334                 }
335
336                 Rectangle{
337                     id: cursorRectangle
338                     width: 10
339                     height: 10
340                     color: "red"
341
342                     x: moveArea.x + (moveArea.width * 0.5) + zeeRemoteControl.x - (cursorRectangle.width * 0.5)
343                     y: moveArea.y + (moveArea.height * 0.5) + zeeRemoteControl.y - (cursorRectangle.height * 0.5)
344                 }
345             }
346         }
347     }
348
349     DeviceInfo{
350         id: deviceInfo
351
352         monitorBluetoothStateChanges: true
353
354         onBluetoothStateChanged: {
355             updateConnectAndScanButton()
356         }
357     }
358
359     BluetoothDiscoveryModel{
360         id: btDiscovery
361
362         discovery: false
363         minimalDiscovery: true
364
365         onDiscoveryChanged: {
366             if(initializing)
367                 return
368
369             if(discovery){
370                 infoText.text = "Scanning for a Zeemote..."
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 Zeemote found yet." && portField.text !== "na"){
383                     infoText.text = "Zeemote found. To enable remote control please press \"Connect\" when ready."
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                 discovery = false
397                 console.log("Found Zeemote. Stopped further discovery.")
398             }
399         }
400     }
401
402     ZeeRemoteControl{
403         id: zeeRemoteControl
404
405         threshold: 50
406
407         onConnected: {
408             disconnectButton.enabled = true
409             infoText.text = "Connected. Have fun."
410         }
411         onDisconnected: {
412             scanButton.enabled = true
413             addressField.enabled = true
414             portField.enabled = true
415             connectButton.enabled = true
416             disconnectButton.enabled = false
417             infoText.text = "To enable remote control please press \"Connect\" when ready."
418         }
419     }
420
421     XtstAdapter{
422         id: xtstAdapter
423     }
424 }