Reads the settings right
[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
38     //QML does not allow properties to be declared as enums...
39     //Initializing to invalid value to make sure picking *any* time control will trigger OnTimeControlChanged
40     property int timeControl: 1000
41
42
43     property bool askAddition
44     property bool askTurnsPerAddition
45
46     property int test
47
48     Settings
49     {
50         id: settings
51
52     }
53     onTimeControlChanged:
54     {
55         equalTimesSwitch.checked = settings.isEqualTimes(timeControl)
56
57         var whiteInitial = settings.getInitialTime(timeControl,true)
58         whiteInitialTime.hours = Qt.formatTime(whiteInitial,"h")
59         whiteInitialTime.minutes = Qt.formatTime(whiteInitial,"m")
60         whiteInitialTime.seconds = Qt.formatTime(whiteInitial,"s")
61
62         var blackInitial = settings.getInitialTime(timeControl,false)
63         blackInitialTime.hours = Qt.formatTime(blackInitial,"h")
64         blackInitialTime.minutes = Qt.formatTime(blackInitial,"m")
65         blackInitialTime.seconds = Qt.formatTime(blackInitial,"s")
66
67         var whiteAdditional = settings.getAdditionalTime(timeControl,true)
68         whiteAdditionalTime.hours = Qt.formatTime(whiteAdditional,"h")
69         whiteAdditionalTime.minutes = Qt.formatTime(whiteAdditional,"m")
70         whiteAdditionalTime.seconds = Qt.formatTime(whiteAdditional,"s")
71
72         var blackAdditional = settings.getAdditionalTime(timeControl,false)
73         blackAdditionalTime.hours = Qt.formatTime(blackAdditional,"h")
74         blackAdditionalTime.minutes = Qt.formatTime(blackAdditional,"m")
75         blackAdditionalTime.seconds = Qt.formatTime(blackAdditional,"s")
76
77
78         whiteTurnsPerAddition.text = settings.getTurnsPerAddition(timeControl,true)
79         blackTurnsPerAddition.text = settings.getTurnsPerAddition(timeControl,false)
80     }
81
82     tools: ToolBarLayout
83     {
84         ToolIcon { iconId: "toolbar-back"; onClicked: pageStack.pop() }
85     }
86
87
88
89            Text
90             {
91                 id: title
92
93                 anchors.horizontalCenter: parent.horizontalCenter
94
95                 color:"white"
96                 font.pointSize: 26
97                 text: newGameDialogPage.name
98             }
99
100             Row
101             {
102                 id: rowRow
103                 spacing: 10
104
105                 anchors.top: title.bottom
106
107                 Text
108                 {
109 //                     width: rowRow.width - rowRow.spacing - switchComponent.width - whiteSmall.width - blackSmall.width
110 //                     height: switchComponent.height
111                     verticalAlignment: Text.AlignVCenter
112                     text: "Equal times"
113                     color: "white"
114                     font.pointSize: 26
115                 }
116
117                 Switch
118                 {
119                     id: equalTimesSwitch
120                     onCheckedChanged:
121                     {
122                         if (checked)
123                         {
124                             whiteSmall.anchors.horizontalCenter = undefined
125                             whiteSmall.anchors.right = whiteInitialTime.horizontalCenter
126
127                             blackSmall.anchors.horizontalCenter = undefined
128                             blackSmall.anchors.left = whiteInitialTime.horizontalCenter
129
130
131                         }
132
133                         else
134                         {
135
136                             whiteSmall.anchors.right = undefined
137                             whiteSmall.anchors.horizontalCenter = whiteInitialTime.horizontalCenter
138
139                             blackSmall.anchors.left = undefined
140                             blackSmall.anchors.horizontalCenter = blackInitialTime.horizontalCenter
141
142                         }
143
144                     }
145                 }
146             }
147
148
149
150
151             Image
152             {
153                 id: whiteSmall
154
155                 anchors.top: rowRow.bottom
156
157                 source: ":/rc/pic/white_small.png"
158
159                 Component.onCompleted:
160                 {
161                     if (equalTimesSwitch.checked)
162                         anchors.right = whiteInitialTime.horizontalCenter
163                     else
164                         anchors.horizontalCenter = whiteInitialTime.horizontalCenter
165                 }
166             }
167
168             Image
169             {
170
171                 id: blackSmall
172
173                 anchors.top: rowRow.bottom
174
175
176                 source: ":/rc/pic/black_small.png"
177
178                 Component.onCompleted:
179                 {
180                     if (equalTimesSwitch.checked)
181                         anchors.left = whiteInitialTime.horizontalCenter
182                     else
183                         anchors.horizontalCenter = blackInitialTime.horizontalCenter
184                 }
185             }
186
187             Text
188             {
189                 id: initialTimeText
190                 text: "Initial time"
191                 color: "white"
192                 font.pointSize: 26
193                 anchors.verticalCenter: whiteInitialTime.verticalCenter
194               }
195
196
197             TextField
198             {
199                 id: whiteInitialTime
200                 readOnly: true
201
202                 anchors.top: whiteSmall.bottom
203                 anchors.left: whiteTurnsPerAddition.left
204
205                 property int hours
206                 property int minutes
207                 property int seconds
208
209                 text: {hours + " h " + minutes + " min " + seconds + " s"}
210
211
212                 MouseArea
213                 {
214                     anchors.fill: parent
215                     onClicked:
216                     {
217                         timePicker.timeType = "initial"
218                         timePicker.player = "white"
219                         timePicker.hour = parent.hours
220                         timePicker.minute = parent.minutes
221                         timePicker.second = parent.seconds
222                         timePicker.open()
223                     }
224                 }
225             }
226
227
228
229             TextField
230             {
231                 id: blackInitialTime
232                 visible: !equalTimesSwitch.checked
233
234                 readOnly: true
235
236                 anchors.top: whiteSmall.bottom
237                 anchors.left:  blackTurnsPerAddition.left
238
239                 property int hours
240                 property int minutes
241                 property int seconds
242
243                 text: hours + " h " + minutes + " min " + seconds + " s"
244
245
246                 MouseArea
247                 {
248                     anchors.fill: parent
249                     onClicked:
250                     {
251                         timePicker.timeType = "initial"
252                         timePicker.player = "black"
253                         timePicker.hour = parent.hours
254                         timePicker.minute = parent.minutes
255                         timePicker.second = parent.seconds
256                         timePicker.open()
257                     }
258                 }
259             }
260
261
262
263             Text
264             {
265                 id: additionalTimeText
266
267                 anchors.verticalCenter: whiteAdditionalTime.verticalCenter
268
269                 text: "Additional time"
270                 color: "white"
271                 font.pointSize: 26
272                 visible: newGameDialogPage.askAddition
273 //                    anchors.top: initialTimeText.bottom
274 //                    anchors.horizontalCenter: parent.horizontalCenter
275             }
276
277
278
279             TextField
280             {
281                 id: whiteAdditionalTime
282                 visible:  newGameDialogPage.askAddition
283                 readOnly: true
284
285                 anchors.top: whiteInitialTime.bottom
286                 anchors.topMargin: 15
287   //              anchors.left: additionalTimeText.right
288                 anchors.left: whiteTurnsPerAddition.left
289                 property int hours
290                 property int minutes
291                 property int seconds
292
293                 text: hours + " h " + minutes + " min " + seconds + " s"
294
295
296                 MouseArea
297                 {
298                     anchors.fill: parent
299                     onClicked:
300                     {
301                         timePicker.timeType = "additional"
302                         timePicker.player = "white"
303                         timePicker.hour = parent.hours
304                         timePicker.minute = parent.minutes
305                         timePicker.second = parent.seconds
306                         timePicker.open()
307                     }
308                 }
309             }
310
311             TextField
312             {
313                 id: blackAdditionalTime
314                 visible: newGameDialogPage.askAddition && !equalTimesSwitch.checked
315                 readOnly: true
316
317                 anchors.top: whiteAdditionalTime.top
318                 anchors.left: blackTurnsPerAddition.left
319
320                 property int hours
321                 property int minutes
322                 property int seconds
323
324                 text: hours + " h " + minutes + " min " + seconds + " s"
325
326                 MouseArea
327                 {
328                     anchors.fill: parent
329                     onClicked:
330                     {
331                         timePicker.timeType = "additional"
332                         timePicker.player = "black"
333                         timePicker.hour = parent.hours
334                         timePicker.minute = parent.minutes
335                         timePicker.second = parent.seconds
336                         timePicker.open()
337                     }
338                 }
339
340
341             }
342
343             Text
344             {
345                 id: turnsPerAdditionText
346                 text:  "Turns per addition"
347                 color: "white"
348                 font.pointSize: 26
349                 visible: newGameDialogPage.askTurnsPerAddition
350                 anchors.verticalCenter: whiteTurnsPerAddition.verticalCenter
351             }
352
353             TextField
354             {
355                 id: whiteTurnsPerAddition
356                 visible: newGameDialogPage.askTurnsPerAddition
357                 readOnly: true;
358
359                 anchors.top: whiteAdditionalTime.bottom
360                 anchors.topMargin: 15
361                 anchors.left: turnsPerAdditionText.right
362                 anchors.leftMargin: 25
363
364                 MouseArea
365                 {
366                     anchors.fill: parent
367                     onClicked:
368                     {
369                         turnsDialog.player = "white"
370                         turnsColumn.selectedIndex = parent.text-1
371                         turnsDialog.open()
372                     }
373                 }
374
375             }
376
377             TextField
378             {
379                 id: blackTurnsPerAddition
380                 visible: newGameDialogPage.askTurnsPerAddition && !equalTimesSwitch.checked
381                 readOnly: true;
382
383                 anchors.top: whiteTurnsPerAddition.top
384                 anchors.left: whiteTurnsPerAddition.right
385                 anchors.leftMargin: 25
386
387                 MouseArea
388                 {
389                     anchors.fill: parent
390                     onClicked:
391                     {
392                         turnsDialog.player = "black"
393                         turnsColumn.selectedIndex = parent.text-1
394                         turnsDialog.open()
395                     }
396                 }
397
398             }
399
400
401             Button
402             {
403                 id: okButton
404                 text:  "Start game"
405
406                 anchors.top: whiteTurnsPerAddition.bottom
407                 anchors.topMargin: 15
408                 anchors.right: parent.right
409
410                 onClicked:
411                 {
412
413
414                 clocksPage.timeControl = timeControl
415
416
417                 clocksPage.whiteInitialTime = 60*60*1000*whiteInitialTime.hours+60*1000*whiteInitialTime.minutes+1000*whiteInitialTime.seconds
418                 clocksPage.whiteAdditionalTime = 60*60*1000*whiteAdditionalTime.hours+60*1000*whiteAdditionalTime.minutes+1000*whiteAdditionalTime.seconds
419                 clocksPage.whiteTurnsPerAddition = whiteTurnsPerAddition.text
420
421
422
423                 //Save settings for white
424                 settings.setInitialTime(timeControl,true,whiteInitialTime.hours,whiteInitialTime.minutes,whiteInitialTime.seconds)
425                 settings.setAdditionalTime(timeControl,true,whiteAdditionalTime.hours,whiteAdditionalTime.minutes,whiteAdditionalTime.seconds)
426                 settings.setTurnsPerAddition(timeControl,true,whiteTurnsPerAddition.text)
427
428                 settings.setEqualTimes(timeControl,equalTimesSwitch.checked) //save equal times setting
429
430                 if (equalTimesSwitch.checked)
431                 {
432                     //use same values for white and black
433                     clocksPage.blackInitialTime = 60*60*1000*whiteInitialTime.hours+60*1000*whiteInitialTime.minutes+1000*whiteInitialTime.seconds
434                     clocksPage.blackAdditionalTime = 60*60*1000*whiteAdditionalTime.hours+60*1000*whiteAdditionalTime.minutes+1000*whiteAdditionalTime.seconds
435                     clocksPage.blackTurnsPerAddition = whiteTurnsPerAddition.text
436
437                     //If black values in dialog are not used they are not saved to settings
438                 }
439                 else
440                 {
441                     clocksPage.blackInitialTime = 60*60*1000*blackInitialTime.hours+60*1000*blackInitialTime.minutes+1000*blackInitialTime.seconds
442                     clocksPage.blackAdditionalTime = 60*60*1000*blackAdditionalTime.hours+60*1000*blackAdditionalTime.minutes+1000*blackAdditionalTime.seconds
443                     clocksPage.blackTurnsPerAddition = blackTurnsPerAddition.text
444
445                     //Save settings for black
446                     settings.setInitialTime(timeControl,false,blackInitialTime.hours,blackInitialTime.minutes,blackInitialTime.seconds)
447                     settings.setAdditionalTime(timeControl,false,blackAdditionalTime.hours,blackAdditionalTime.minutes,blackAdditionalTime.seconds)
448                     settings.setTurnsPerAddition(timeControl,false,blackTurnsPerAddition.text)
449                 }
450
451
452
453
454                 pageStack.push(clocksPage)
455
456             }
457
458             }
459
460
461
462
463 TimePickerDialog
464 {
465     id: timePicker
466
467     property string timeType
468     property string player
469
470     titleText: "Choose " + timeType + " time for " + player
471     rejectButtonText: "Cancel"
472     acceptButtonText: "Ok"
473     hourMode: DateTime.TwentyFourHours
474     onAccepted:
475     {
476         if (timeType == "initial")
477         {
478             if (player == "white")
479             {
480                 whiteInitialTime.hours = hour
481                 whiteInitialTime.minutes = minute
482                 whiteInitialTime.seconds = second
483             }
484             else
485             {
486                 blackInitialTime.hours = hour
487                 blackInitialTime.minutes = minute
488                 blackInitialTime.seconds = second
489             }
490         }
491         else if (player == "white")
492             {
493                 whiteAdditionalTime.hours = hour
494                 whiteAdditionalTime.minutes = minute
495                 whiteAdditionalTime.seconds = second
496             }
497             else
498             {
499                 blackAdditionalTime.hours = hour
500                 blackAdditionalTime.minutes = minute
501                 blackAdditionalTime.seconds = second
502             }
503
504     }
505 }
506
507
508
509 TumblerColumn
510 {
511     id: turnsColumn
512     selectedIndex: 1
513     items: turnsList
514
515
516 }
517
518 ListModel
519 {
520     id: turnsList
521
522     Component.onCompleted:
523     {
524         for (var turn = 1; turn <= 99; turn++)
525               {
526                  turnsList.append({"value" : turn});
527               }
528
529     }
530
531 }
532
533
534 TumblerDialog
535 {
536     id: turnsDialog
537
538     property string player
539
540     titleText: "Choose turns per addition for " + player
541     acceptButtonText: "Ok"
542     rejectButtonText: "Cancel"
543
544     columns: [turnsColumn]
545
546     onAccepted:
547     {
548         if (player == "white")
549             whiteTurnsPerAddition.text = turnsColumn.selectedIndex+1
550         else if (player == "black")
551             blackTurnsPerAddition.text = turnsColumn.selectedIndex+1
552
553
554     }
555 }
556
557 }