Rework UI.
[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 qzeecontrol 1.0
24
25 Page {
26     tools: commonTools
27
28     Button{
29         id: scanButton
30         anchors{bottom: addressRow.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
31         enabled: true
32
33         text: "Scan"
34
35         onClicked: {
36             btDiscovery.discovery = true
37         }
38     }
39
40     Row{
41         id: addressRow
42         spacing: 5
43         anchors{bottom: infoText.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
44
45         TextField{
46             id: addressField
47             text: "No device found yet."
48
49             onTextChanged: {
50                 connectButton.enabled = true
51                 infoText.text = "Press \"Connect\" to connect to the device."
52             }
53         }
54         TextField{
55             id: portField
56             text: "-"
57             width: 60
58             validator: IntValidator{}
59         }
60     }
61
62     Label {
63         id: infoText
64         anchors{bottom: connectButton.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
65         width: parent.width
66
67         text: "Please scan for a device first."
68         horizontalAlignment: Text.AlignHCenter
69         wrapMode: Text.WordWrap
70     }
71
72     Button{
73         id: connectButton
74         anchors{bottom: disconnectButton.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
75         enabled: false
76
77         text: "Connect"
78
79         onClicked: {
80             enabled = false
81             btConn.connect(addressField.text, parseInt(portField.text))
82         }
83     }
84
85     Button{
86         id: disconnectButton
87         anchors{bottom: buttonRow.top; bottomMargin: 10; horizontalCenter: parent.horizontalCenter}
88
89         text: "Disconnect"
90
91         onClicked: {
92             btConn.disconnect()
93
94         }
95     }
96
97     Row{
98         id: buttonRow
99         anchors.centerIn: parent
100         spacing: 20
101
102         Label{
103             id: labelA
104             text: "A"
105             color: btConn.a ? "red" : "blue"
106         }
107         Label{
108             id: labelB
109             text: "B"
110             color: btConn.b ? "red" : "blue"
111         }
112         Label{
113             id: labelC
114             text: "C"
115             color: btConn.c ? "red" : "blue"
116         }
117         Label{
118             id: labelD
119             text: "D"
120             color: btConn.d ? "red" : "blue"
121         }
122     }
123
124     Rectangle{
125         id: moveArea
126         anchors{top: buttonRow.bottom; topMargin: 10; horizontalCenter: parent.horizontalCenter}
127         color: "gray"
128
129         width: 256
130         height: 256
131     }
132
133     Rectangle{
134         id: cursorRectangle
135         width: 10
136         height: 10
137         color: "red"
138
139         x: moveArea.x + (moveArea.width * 0.5) + btConn.x - (cursorRectangle.width * 0.5)
140         y: moveArea.y + (moveArea.height * 0.5) + btConn.y - (cursorRectangle.height * 0.5)
141     }
142
143     BluetoothDiscoveryModel{
144         id: btDiscovery
145
146         discovery: false
147         minimalDiscovery: true
148
149         onDiscoveryChanged: {
150             if(discovery){
151                 infoText.text = "Scanning for devices..."
152                 scanButton.enabled = false
153                 connectButton.enabled = false
154                 disconnectButton.enabled = false
155             }else{
156                 scanButton.enabled = true
157                 disconnectButton.enabled = false
158             }
159         }
160
161         onNewServiceDiscovered: {
162             console.log("Service " + service.serviceName + " found on "
163                         + service.deviceName + " at address " + service.deviceAddress
164                         + " on port " + service.servicePort + ".")
165             if(service.serviceName === "Zeemote"){
166                 addressField.text = service.deviceAddress
167                 portField.text = service.servicePort
168             }
169         }
170     }
171
172     BtConnector{
173         id: btConn
174
175         property int joystickThreshold: 50
176
177         onConnected: {
178             disconnectButton.enabled = true
179             infoText.text = "Connected. Have fun."
180         }
181
182         onDisconnected: {
183             connectButton.enabled = true
184             disconnectButton.enabled = false
185             infoText.text = "Press \"Connect\" to connect to the device."
186         }
187
188 //        onStickMoved: {
189 //            console.log("Stick moved. x: " + x + " y: " + y)
190 //        }
191
192 //        onButtonsChanged: {
193 //            console.log("Buttons changed. A: " + a + " B: " + b + " C: " + c + " D: " + d)
194 //        }
195
196         onAChanged: {
197 //            console.log("A changed to: " + val)
198             xtstAdapter.sendKey("a", val);
199         }
200         onBChanged: {
201 //            console.log("B changed to: " + val)
202             xtstAdapter.sendKey("b", val);
203         }
204         onCChanged: {
205 //            console.log("C changed to: " + val)
206             xtstAdapter.sendKey("c", val);
207         }
208         onDChanged: {
209 //            console.log("D changed to: " + val)
210             xtstAdapter.sendKey("d", val);
211         }
212
213         onXChanged: {
214             if(val > joystickThreshold){
215                 xtstAdapter.sendKey("Right", true);
216             }else if(val < -joystickThreshold){
217                 xtstAdapter.sendKey("Left", true);
218             }else{
219                 xtstAdapter.sendKey("Right", false);
220                 xtstAdapter.sendKey("Left", false);
221             }
222         }
223
224         onYChanged: {
225             if(val > joystickThreshold){
226                 xtstAdapter.sendKey("Down", true);
227             }else if(val < -joystickThreshold){
228                 xtstAdapter.sendKey("Up", true);
229             }else{
230                 xtstAdapter.sendKey("Down", false);
231                 xtstAdapter.sendKey("Up", false);
232             }
233         }
234     }
235
236     XtstAdapter{
237         id: xtstAdapter
238     }
239 }