Add DB for storing address and port. Add AboutDialog.
[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
43     states: [
44         State {
45             name: "active"
46             PropertyChanges {
47                 target: cursorRectangle
48                 x: moveArea.x + (moveArea.width * 0.5) + btConn.x - (cursorRectangle.width * 0.5)
49                 y: moveArea.y + (moveArea.height * 0.5) + btConn.y - (cursorRectangle.height * 0.5)
50             }
51             PropertyChanges {
52                 target: labelA
53                 color: btConn.a ? "red" : "blue"
54             }
55             PropertyChanges {
56                 target: labelB
57                 color: btConn.b ? "red" : "blue"
58             }
59             PropertyChanges {
60                 target: labelC
61                 color: btConn.c ? "red" : "blue"
62             }
63             PropertyChanges {
64                 target: labelD
65                 color: btConn.d ? "red" : "blue"
66             }
67         },
68         State {
69             name: "inactive"
70             PropertyChanges {
71                 target: cursorRectangle
72                 x: moveArea.x + (moveArea.width * 0.5) - (cursorRectangle.width * 0.5)
73                 y: moveArea.y + (moveArea.height * 0.5) - (cursorRectangle.height * 0.5)
74             }
75             PropertyChanges {
76                 target: labelA
77                 color: "blue"
78             }
79             PropertyChanges {
80                 target: labelB
81                 color: "blue"
82             }
83             PropertyChanges {
84                 target: labelC
85                 color: "blue"
86             }
87             PropertyChanges {
88                 target: labelD
89                 color: "blue"
90             }
91         }
92
93     ]
94
95     Connections {
96         target: platformWindow
97
98         onActiveChanged: {
99             if(platformWindow.active){
100                 state = "active"
101             }else{
102                 state = "inactive"
103             }
104         }
105     }
106
107     Item {
108         anchors.fill: parent
109
110         Image {
111             id: header
112             height: 72
113             source: "image://theme/color8-meegotouch-view-header-fixed"
114             anchors.left: parent.left
115             anchors.top: parent.top
116             anchors.right: parent.right
117
118             Text {
119                 text: "QZeeControl"
120                 color: "white"
121                 font.family: "Nokia Pure Text Light"
122                 font.pixelSize: 32
123                 anchors.left: parent.left
124                 anchors.leftMargin: 20
125                 anchors.verticalCenter: parent.verticalCenter
126             }
127         }
128
129         Button{
130             id: scanButton
131             anchors{bottom: addressRow.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
132             enabled: true
133
134             text: "Scan"
135
136             onClicked: {
137                 btDiscovery.discovery = true
138             }
139         }
140
141         Row{
142             id: addressRow
143             spacing: 5
144             anchors{bottom: infoText.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
145
146             TextField{
147                 id: addressField
148                 text: "No device found yet."
149
150                 onTextChanged: {
151                     if(text === "No device found yet.")
152                         return
153
154                     connectButton.enabled = true
155                     infoText.text = "Press \"Connect\" to connect to the device."
156                     console.log("Storing address in DB: " + text)
157                     SettingsStorage.setSetting("address", text)
158                 }
159             }
160             TextField{
161                 id: portField
162                 text: "na"
163                 width: 60
164                 validator: IntValidator{}
165
166                 onTextChanged: {
167                     if(text === "na")
168                         return
169
170                     console.log("Storing port in DB: " + text)
171                     SettingsStorage.setSetting("port", text)
172                 }
173             }
174         }
175
176         Label {
177             id: infoText
178             anchors{bottom: connectButton.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
179             width: parent.width
180
181             text: "Please scan for a device first."
182             horizontalAlignment: Text.AlignHCenter
183             wrapMode: Text.WordWrap
184         }
185
186         Button{
187             id: connectButton
188             anchors{bottom: disconnectButton.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
189             enabled: false
190
191             text: "Connect"
192
193             onClicked: {
194                 enabled = false
195                 btConn.connect(addressField.text, parseInt(portField.text))
196             }
197         }
198
199         Button{
200             id: disconnectButton
201             anchors.centerIn: parent
202
203             text: "Disconnect"
204
205             onClicked: {
206                 btConn.disconnect()
207
208             }
209         }
210
211         Row{
212             id: buttonRow
213             anchors{top: disconnectButton.bottom; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
214
215             spacing: 20
216
217             Label{
218                 id: labelA
219                 text: "A"
220                 color: btConn.a ? "red" : "blue"
221             }
222             Label{
223                 id: labelB
224                 text: "B"
225                 color: btConn.b ? "red" : "blue"
226             }
227             Label{
228                 id: labelC
229                 text: "C"
230                 color: btConn.c ? "red" : "blue"
231             }
232             Label{
233                 id: labelD
234                 text: "D"
235                 color: btConn.d ? "red" : "blue"
236             }
237         }
238
239         Rectangle{
240             id: moveArea
241             anchors{top: buttonRow.bottom; topMargin: 10; horizontalCenter: parent.horizontalCenter}
242             color: "gray"
243
244             width: 256
245             height: 256
246         }
247
248         Rectangle{
249             id: cursorRectangle
250             width: 10
251             height: 10
252             color: "red"
253
254             x: moveArea.x + (moveArea.width * 0.5) + btConn.x - (cursorRectangle.width * 0.5)
255             y: moveArea.y + (moveArea.height * 0.5) + btConn.y - (cursorRectangle.height * 0.5)
256         }
257     }
258
259     BluetoothDiscoveryModel{
260         id: btDiscovery
261
262         discovery: false
263         minimalDiscovery: true
264
265         onDiscoveryChanged: {
266             if(discovery){
267                 infoText.text = "Scanning for a device..."
268                 scanButton.enabled = false
269                 connectButton.enabled = false
270                 disconnectButton.enabled = false
271             }else{
272                 scanButton.enabled = true
273                 disconnectButton.enabled = false
274
275                 if(addressField.text !== "No device found yet." && portField.text !== "na")
276                     connectButton.enabled = true
277             }
278         }
279
280         onNewServiceDiscovered: {
281             console.log("Service " + service.serviceName + " found on "
282                         + service.deviceName + " at address " + service.deviceAddress
283                         + " on port " + service.servicePort + ".")
284             if(service.serviceName === "Zeemote"){
285                 addressField.text = service.deviceAddress
286                 portField.text = service.servicePort
287             }
288         }
289     }
290
291     BtConnector{
292         id: btConn
293
294         property int joystickThreshold: 50
295
296         onConnected: {
297             disconnectButton.enabled = true
298             infoText.text = "Connected. Have fun."
299         }
300
301         onDisconnected: {
302             connectButton.enabled = true
303             disconnectButton.enabled = false
304             infoText.text = "Press \"Connect\" to connect to the device."
305         }
306
307 //        onStickMoved: {
308 //            console.log("Stick moved. x: " + x + " y: " + y)
309 //        }
310
311 //        onButtonsChanged: {
312 //            console.log("Buttons changed. A: " + a + " B: " + b + " C: " + c + " D: " + d)
313 //        }
314
315         onAChanged: {
316 //            console.log("A changed to: " + val)
317             xtstAdapter.sendKey("a", val);
318         }
319         onBChanged: {
320 //            console.log("B changed to: " + val)
321             xtstAdapter.sendKey("b", val);
322         }
323         onCChanged: {
324 //            console.log("C changed to: " + val)
325             xtstAdapter.sendKey("c", val);
326         }
327         onDChanged: {
328 //            console.log("D changed to: " + val)
329             xtstAdapter.sendKey("d", val);
330         }
331
332         onXChanged: {
333             if(val > joystickThreshold){
334                 xtstAdapter.sendKey("Right", true);
335             }else if(val < -joystickThreshold){
336                 xtstAdapter.sendKey("Left", true);
337             }else{
338                 xtstAdapter.sendKey("Right", false);
339                 xtstAdapter.sendKey("Left", false);
340             }
341         }
342
343         onYChanged: {
344             if(val > joystickThreshold){
345                 xtstAdapter.sendKey("Down", true);
346             }else if(val < -joystickThreshold){
347                 xtstAdapter.sendKey("Up", true);
348             }else{
349                 xtstAdapter.sendKey("Down", false);
350                 xtstAdapter.sendKey("Up", false);
351             }
352         }
353     }
354
355     XtstAdapter{
356         id: xtstAdapter
357     }
358 }