Using Settings frim QML - does not work
[chessclock] / qml / NewGameDialogPage.qml
1 /**************************************************************************
2
3    Chess Clock
4
5    This file is part of Chess Clock software.
6
7    (This file) Copyright (c) Heli Hyvättinen 2011
8
9    Chess Clock is free software: you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation, either version 3 of the License, or
12    (at your option) any later version.
13
14    Chess Clock is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19
20 **************************************************************************/
21
22
23 import QtQuick 1.1
24 import com.meego 1.0
25 import QtQuick 1.0
26 import com.nokia.extras 1.0
27 import ChessClocks 1.0
28
29
30 Page
31 {
32     id: newGameDialogPage
33
34     orientationLock: PageOrientation.LockLandscape
35
36     property string name
37     property int timeControl //QML does not allow properties to be declared as enums...
38     property bool askAddition
39     property bool askTurnsPerAddition
40
41     property int test
42
43     Settings
44     {
45         id: settings
46     }
47     Component.onCompleted:
48     {
49         equalTimesSwitch = settings.isEqualTimes(timeControl)
50
51         var whiteInitial = settings.getInitialTime(timeControl,true)
52         whiteInitialTime.hours = whiteInitial/360
53         whiteInitialTime.minutes = (whiteIntial%360)/60
54         whiteInitialTime.seconds = whiteInitial%60
55
56         var blackInitial = settings.getInitialTime(timeControl,false)
57         blackInitialTime.hours = blackInitial/360
58         blackInitialTime.minutes = (blackInitial%360)/60
59         blackInitialTime.seconds = blackInitial%60
60
61         var whiteAdditional = settings.getAdditionalTime(timeControl,true)
62         whiteAdditionalTime.hours = whiteAdditional/360
63         whiteAdditionalTime.minutes = (whiteAdditional%360)/60
64         whiteAdditionalTime.seconds = whiteAdditional%60
65
66         var blackAdditional = settings.getAdditionalTime(timeControl,false)
67         blackAdditionalTime.hours = blackAdditional/360
68         blackAdditionalTime.minutes = (blackAdditional%360)/60
69         blackAdditionalTime.seconds = blackAdditional%60
70
71         whiteTurnsPerAddition.text = settings.getTurnsPerAddition(timeControl,true)
72         blackTurnsPerAddition.text = settings.getTurnsPerAddition(timeControl,false)
73     }
74
75     tools: ToolBarLayout
76     {
77         ToolIcon { iconId: "toolbar-back"; onClicked: pageStack.pop() }
78     }
79
80
81
82            Text
83             {
84                 id: title
85
86                 anchors.horizontalCenter: parent.horizontalCenter
87
88                 color:"white"
89                 font.pointSize: 26
90                 text: newGameDialogPage.name
91             }
92
93             Row
94             {
95                 id: rowRow
96                 spacing: 10
97
98                 anchors.top: title.bottom
99
100                 Text
101                 {
102 //                     width: rowRow.width - rowRow.spacing - switchComponent.width - whiteSmall.width - blackSmall.width
103 //                     height: switchComponent.height
104                     verticalAlignment: Text.AlignVCenter
105                     text: "Equal times"
106                     color: "white"
107                     font.pointSize: 26
108                 }
109
110                 Switch
111                 {
112                     id: equalTimesSwitch
113                     onCheckedChanged:
114                     {
115                         if (checked)
116                         {
117                             whiteSmall.anchors.horizontalCenter = undefined
118                             whiteSmall.anchors.right = whiteInitialTime.horizontalCenter
119
120                             blackSmall.anchors.horizontalCenter = undefined
121                             blackSmall.anchors.left = whiteInitialTime.horizontalCenter
122
123
124                         }
125
126                         else
127                         {
128
129                             whiteSmall.anchors.right = undefined
130                             whiteSmall.anchors.horizontalCenter = whiteInitialTime.horizontalCenter
131
132                             blackSmall.anchors.left = undefined
133                             blackSmall.anchors.horizontalCenter = blackInitialTime.horizontalCenter
134
135                         }
136
137                     }
138                 }
139             }
140
141
142
143
144             Image
145             {
146                 id: whiteSmall
147
148                 anchors.top: rowRow.bottom
149
150                 source: ":/rc/pic/white_small.png"
151
152                 Component.onCompleted:
153                 {
154                     if (equalTimesSwitch.checked)
155                         anchors.right = whiteInitialTime.horizontalCenter
156                     else
157                         anchors.horizontalCenter = whiteInitialTime.horizontalCenter
158                 }
159             }
160
161             Image
162             {
163
164                 id: blackSmall
165
166                 anchors.top: rowRow.bottom
167
168
169                 source: ":/rc/pic/black_small.png"
170
171                 Component.onCompleted:
172                 {
173                     if (equalTimesSwitch.checked)
174                         anchors.left = whiteInitialTime.horizontalCenter
175                     else
176                         anchors.horizontalCenter = blackInitialTime.horizontalCenter
177                 }
178             }
179
180             Text
181             {
182                 id: initialTimeText
183                 text: "Initial time"
184                 color: "white"
185                 font.pointSize: 26
186                 anchors.verticalCenter: whiteInitialTime.verticalCenter
187               }
188
189
190             TextField
191             {
192                 id: whiteInitialTime
193                 readOnly: true
194
195                 anchors.top: whiteSmall.bottom
196                 anchors.left: whiteTurnsPerAddition.left
197
198                 property int hours
199                 property int minutes
200                 property int seconds
201
202                 text: {hours + " h " + minutes + " min " + seconds + " s"}
203
204
205                 MouseArea
206                 {
207                     anchors.fill: parent
208                     onClicked: {timePicker.timeType = "initial";  timePicker.player = "white"; timePicker.open()}
209                 }
210             }
211
212
213
214             TextField
215             {
216                 id: blackInitialTime
217                 visible: !equalTimesSwitch.checked
218
219                 readOnly: true
220
221                 anchors.top: whiteSmall.bottom
222                 anchors.left:  blackTurnsPerAddition.left
223
224                 property int hours
225                 property int minutes
226                 property int seconds
227
228                 text: hours + " h " + minutes + " min " + seconds + " s"
229
230
231                 MouseArea
232                 {
233                     anchors.fill: parent
234                     onClicked: {timePicker.timeType = "initial";  timePicker.player = "black"; timePicker.open()}
235                 }
236             }
237
238
239
240             Text
241             {
242                 id: additionalTimeText
243
244                 anchors.verticalCenter: whiteAdditionalTime.verticalCenter
245
246                 text: "Additional time"
247                 color: "white"
248                 font.pointSize: 26
249                 visible: newGameDialogPage.askAddition
250 //                    anchors.top: initialTimeText.bottom
251 //                    anchors.horizontalCenter: parent.horizontalCenter
252             }
253
254
255
256             TextField
257             {
258                 id: whiteAdditionalTime
259                 visible:  newGameDialogPage.askAddition
260                 readOnly: true
261
262                 anchors.top: whiteInitialTime.bottom
263                 anchors.topMargin: 15
264   //              anchors.left: additionalTimeText.right
265                 anchors.left: whiteTurnsPerAddition.left
266                 property int hours
267                 property int minutes
268                 property int seconds
269
270                 text: hours + " h " + minutes + " min " + seconds + " s"
271
272
273                 MouseArea
274                 {
275                     anchors.fill: parent
276                     onClicked: {timePicker.timeType = "additional";  timePicker.player = "white"; timePicker.open()}
277                 }
278             }
279
280             TextField
281             {
282                 id: blackAdditionalTime
283                 visible: newGameDialogPage.askAddition && !equalTimesSwitch.checked
284                 readOnly: true
285
286                 anchors.top: whiteAdditionalTime.top
287                 anchors.left: blackTurnsPerAddition.left
288
289                 property int hours
290                 property int minutes
291                 property int seconds
292
293                 text: hours + " h " + minutes + " min " + seconds + " s"
294
295                 MouseArea
296                 {
297                     anchors.fill: parent
298                     onClicked: {timePicker.timeType = "additional";  timePicker.player = "black"; timePicker.open()}
299                 }
300
301
302             }
303
304             Text
305             {
306                 id: turnsPerAdditionText
307                 text:  "Turns per addition"
308                 color: "white"
309                 font.pointSize: 26
310                 visible: newGameDialogPage.askTurnsPerAddition
311                 anchors.verticalCenter: whiteTurnsPerAddition.verticalCenter
312             }
313
314             TextField
315             {
316                 id: whiteTurnsPerAddition
317                 visible: newGameDialogPage.askTurnsPerAddition
318                 readOnly: true;
319
320                 anchors.top: whiteAdditionalTime.bottom
321                 anchors.topMargin: 15
322                 anchors.left: turnsPerAdditionText.right
323                 anchors.leftMargin: 25
324
325                 text: "1"
326
327                 MouseArea
328                 {
329                     anchors.fill: parent
330                     onClicked: {turnsDialog.player = "white";  turnsDialog.open()}
331                 }
332
333             }
334
335             TextField
336             {
337                 id: blackTurnsPerAddition
338                 visible: newGameDialogPage.askTurnsPerAddition && !equalTimesSwitch.checked
339                 readOnly: true;
340
341                 anchors.top: whiteTurnsPerAddition.top
342                 anchors.left: whiteTurnsPerAddition.right
343                 anchors.leftMargin: 25
344
345                 text: "1"
346
347                 MouseArea
348                 {
349                     anchors.fill: parent
350                     onClicked: {turnsDialog.player = "black";  turnsDialog.open()}
351                 }
352
353             }
354
355
356             Button
357             {
358                 id: okButton
359                 text:  "Start game"
360
361                 anchors.top: whiteTurnsPerAddition.bottom
362                 anchors.topMargin: 15
363                 anchors.right: parent.right
364
365                 onClicked:
366                 {
367
368
369                 clocksPage.timeControl = timeControl
370
371
372                 clocksPage.whiteInitialTime = 60*60*1000*whiteInitialTime.hours+60*1000*whiteInitialTime.minutes+1000*whiteInitialTime.seconds
373                 clocksPage.whiteAdditionalTime = 60*60*1000*whiteAdditionalTime.hours+60*1000*whiteAdditionalTime.minutes+1000*whiteAdditionalTime.seconds
374                 clocksPage.whiteTurnsPerAddition = whiteTurnsPerAddition.text
375
376
377                 //Save settings for white
378                 settings.setInitialTime(timeControl,true,clocksPage.whiteInitialTime)
379                 settings.setAdditionalTime(timeControl,true,clocksPage.whiteAdditionalTime)
380                 settings.setTurnsPerAddition(timeControl,true,clocksPage.whiteTurnsPerAddition)
381
382                 if (equalTimesSwitch.checked)
383                 {
384                     //use same values for white and black
385                     clocksPage.blackInitialTime = 60*60*1000*whiteInitialTime.hours+60*1000*whiteInitialTime.minutes+1000*whiteInitialTime.seconds
386                     clocksPage.blackAdditionalTime = 60*60*1000*whiteAdditionalTime.hours+60*1000*whiteAdditionalTime.minutes+1000*whiteAdditionalTime.seconds
387                     clocksPage.blackTurnsPerAddition = whiteTurnsPerAddition.text
388
389                     //If black values in dialog are not used they are not saved to settings
390                 }
391                 else
392                 {
393                     clocksPage.blackInitialTime = 60*60*1000*blackInitialTime.hours+60*1000*blackInitialTime.minutes+1000*blackInitialTime.seconds
394                     clocksPage.blackAdditionalTime = 60*60*1000*blackAdditionalTime.hours+60*1000*blackAdditionalTime.minutes+1000*blackAdditionalTime.seconds
395                     clocksPage.blackTurnsPerAddition = blackTurnsPerAddition.text
396
397                     //Save settings for black
398                     settings.setInitialTime(timeControl,false,clocksPage.blackInitialTime)
399                     settings.setAdditionalTime(timeControl,false,clocksPage.blackAdditionalTime)
400                     settings.setTurnsPerAddition(timeControl,false,clocksPage.blackTurnsPerAddition)
401                 }
402
403
404
405
406                 pageStack.push(clocksPage)
407
408             }
409
410             }
411
412
413
414
415 TimePickerDialog
416 {
417     id: timePicker
418
419     property string timeType
420     property string player
421
422     titleText: "Choose " + timeType + " time for " + player
423     rejectButtonText: "Cancel"
424     acceptButtonText: "Ok"
425     hourMode: DateTime.TwentyFourHours
426     onAccepted:
427     {
428         if (timeType == "initial")
429         {
430             if (player == "white")
431             {
432                 whiteInitialTime.hours = hour
433                 whiteInitialTime.minutes = minute
434                 whiteInitialTime.seconds = second
435             }
436             else
437             {
438                 blackInitialTime.hours = hour
439                 blackInitialTime.minutes = minute
440                 blackInitialTime.seconds = second
441             }
442         }
443         else if (player == "white")
444             {
445                 whiteAdditionalTime.hours = hour
446                 whiteAdditionalTime.minutes = minute
447                 whiteAdditionalTime.seconds = second
448             }
449             else
450             {
451                 blackAdditionalTime.hours = hour
452                 blackAdditionalTime.minutes = minute
453                 blackAdditionalTime.seconds = second
454             }
455
456     }
457 }
458
459
460
461 TumblerColumn
462 {
463     id: turnsColumn
464     selectedIndex: 1
465     items: turnsList
466
467
468 }
469
470 ListModel
471 {
472     id: turnsList
473
474     Component.onCompleted:
475     {
476         for (var turn = 1; turn <= 99; turn++)
477               {
478                  turnsList.append({"value" : turn});
479               }
480
481     }
482
483 }
484
485
486 TumblerDialog
487 {
488     id: turnsDialog
489
490     property string player
491
492     titleText: "Choose turns per addition for " + player
493     acceptButtonText: "Ok"
494     rejectButtonText: "Cancel"
495
496     columns: [turnsColumn]
497
498     onAccepted:
499     {
500         if (player == "white")
501             whiteTurnsPerAddition.text = turnsColumn.selectedIndex+1
502         else if (player == "black")
503             blackTurnsPerAddition.text = turnsColumn.selectedIndex+1
504
505
506     }
507 }
508
509 }