Initial time chabged to buttons
[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             Button
198             {
199                 id: whiteInitialTime
200
201                 anchors.top: whiteSmall.bottom
202                 anchors.left: whiteTurnsPerAddition.left
203                 anchors.right: whiteTurnsPerAddition.right
204
205                 property int hours: 0
206                 property int minutes: 30
207                 property int seconds: 0
208
209                 text: {hours + " h " + minutes + " min " + seconds + " s"}
210
211
212
213                     onClicked:
214                     {
215                         timePicker.timeType = "initial"
216                         timePicker.player = "white"
217                         timePicker.hour = hours
218                         timePicker.minute = minutes
219                         timePicker.second = seconds
220                         timePicker.open()
221                     }
222
223             }
224
225
226
227             Button
228             {
229                 id: blackInitialTime
230                 visible: !equalTimesSwitch.checked
231
232                 anchors.top: whiteSmall.bottom
233                 anchors.left:  blackTurnsPerAddition.left
234                 anchors.right: blackTurnsPerAddition.right
235
236                 property int hours: 0
237                 property int minutes: 30
238                 property int seconds: 0
239
240                 text: hours + " h " + minutes + " min " + seconds + " s"
241
242
243
244                     onClicked:
245                     {
246                         timePicker.timeType = "initial"
247                         timePicker.player = "black"
248                         timePicker.hour = hours
249                         timePicker.minute = minutes
250                         timePicker.second = seconds
251                         timePicker.open()
252                     }
253
254             }
255
256
257
258             Text
259             {
260                 id: additionalTimeText
261
262                 anchors.verticalCenter: whiteAdditionalTime.verticalCenter
263
264                 text: "Additional time"
265                 color: "white"
266                 font.pointSize: 26
267                 visible: newGameDialogPage.askAddition
268 //                    anchors.top: initialTimeText.bottom
269 //                    anchors.horizontalCenter: parent.horizontalCenter
270             }
271
272
273
274             TextField
275             {
276                 id: whiteAdditionalTime
277                 visible:  newGameDialogPage.askAddition
278                 readOnly: true
279
280                 anchors.top: whiteInitialTime.bottom
281                 anchors.topMargin: 15
282   //              anchors.left: additionalTimeText.right
283                 anchors.left: whiteTurnsPerAddition.left
284                 property int hours: 0
285                 property int minutes: 0
286                 property int seconds: 30
287
288                 text: hours + " h " + minutes + " min " + seconds + " s"
289
290
291                 MouseArea
292                 {
293                     anchors.fill: parent
294                     onClicked:
295                     {
296                         timePicker.timeType = "additional"
297                         timePicker.player = "white"
298                         timePicker.hour = parent.hours
299                         timePicker.minute = parent.minutes
300                         timePicker.second = parent.seconds
301                         timePicker.open()
302                     }
303                 }
304             }
305
306             TextField
307             {
308                 id: blackAdditionalTime
309                 visible: newGameDialogPage.askAddition && !equalTimesSwitch.checked
310                 readOnly: true
311
312                 anchors.top: whiteAdditionalTime.top
313                 anchors.left: blackTurnsPerAddition.left
314
315                 property int hours: 0
316                 property int minutes: 0
317                 property int seconds: 30
318
319                 text: hours + " h " + minutes + " min " + seconds + " s"
320
321                 MouseArea
322                 {
323                     anchors.fill: parent
324                     onClicked:
325                     {
326                         timePicker.timeType = "additional"
327                         timePicker.player = "black"
328                         timePicker.hour = parent.hours
329                         timePicker.minute = parent.minutes
330                         timePicker.second = parent.seconds
331                         timePicker.open()
332                     }
333                 }
334
335
336             }
337
338             Text
339             {
340                 id: turnsPerAdditionText
341                 text:  "Turns per addition"
342                 color: "white"
343                 font.pointSize: 26
344                 visible: newGameDialogPage.askTurnsPerAddition
345                 anchors.verticalCenter: whiteTurnsPerAddition.verticalCenter
346             }
347
348             TextField
349             {
350                 id: whiteTurnsPerAddition
351                 visible: newGameDialogPage.askTurnsPerAddition
352                 readOnly: true;
353
354                 anchors.top: whiteAdditionalTime.bottom
355                 anchors.topMargin: 15
356                 anchors.left: turnsPerAdditionText.right
357                 anchors.leftMargin: 25
358
359                 MouseArea
360                 {
361                     anchors.fill: parent
362                     onClicked:
363                     {
364                         turnsDialog.player = "white"
365                         turnsDialog.open()
366                         turnsColumn.selectedIndex = parent.text-1 //Needs to be after open(),  or gets overridden by the previous chosen value
367
368
369                     }
370                 }
371
372             }
373
374             TextField
375             {
376                 id: blackTurnsPerAddition
377                 visible: newGameDialogPage.askTurnsPerAddition && !equalTimesSwitch.checked
378                 readOnly: true;
379
380                 anchors.top: whiteTurnsPerAddition.top
381                 anchors.left: whiteTurnsPerAddition.right
382                 anchors.leftMargin: 25
383
384                 MouseArea
385                 {
386                     anchors.fill: parent
387                     onClicked:
388                     {
389                         turnsDialog.player = "black"
390                         turnsDialog.open()
391                         turnsColumn.selectedIndex = parent.text-1 //Needs to be after open(),  or gets overridden by the previous chosen value
392                     }
393                 }
394
395             }
396
397
398             Button
399             {
400                 id: okButton
401                 text:  "Start game"
402
403                 anchors.top: whiteTurnsPerAddition.bottom
404                 anchors.topMargin: 15
405                 anchors.right: parent.right
406
407                 onClicked:
408                 {
409
410
411                 clocksPage.timeControl = timeControl
412
413
414                 clocksPage.whiteInitialTime = 60*60*1000*whiteInitialTime.hours+60*1000*whiteInitialTime.minutes+1000*whiteInitialTime.seconds
415                 clocksPage.whiteAdditionalTime = 60*60*1000*whiteAdditionalTime.hours+60*1000*whiteAdditionalTime.minutes+1000*whiteAdditionalTime.seconds
416                 clocksPage.whiteTurnsPerAddition = whiteTurnsPerAddition.text
417
418
419
420                 //Save settings for white
421                 settings.setInitialTime(timeControl,true,whiteInitialTime.hours,whiteInitialTime.minutes,whiteInitialTime.seconds)
422                 settings.setAdditionalTime(timeControl,true,whiteAdditionalTime.hours,whiteAdditionalTime.minutes,whiteAdditionalTime.seconds)
423                 settings.setTurnsPerAddition(timeControl,true,whiteTurnsPerAddition.text)
424
425                 settings.setEqualTimes(timeControl,equalTimesSwitch.checked) //save equal times setting
426
427                 if (equalTimesSwitch.checked)
428                 {
429                     //use same values for white and black
430                     clocksPage.blackInitialTime = 60*60*1000*whiteInitialTime.hours+60*1000*whiteInitialTime.minutes+1000*whiteInitialTime.seconds
431                     clocksPage.blackAdditionalTime = 60*60*1000*whiteAdditionalTime.hours+60*1000*whiteAdditionalTime.minutes+1000*whiteAdditionalTime.seconds
432                     clocksPage.blackTurnsPerAddition = whiteTurnsPerAddition.text
433
434                     //If black values in dialog are not used they are not saved to settings
435                 }
436                 else
437                 {
438                     clocksPage.blackInitialTime = 60*60*1000*blackInitialTime.hours+60*1000*blackInitialTime.minutes+1000*blackInitialTime.seconds
439                     clocksPage.blackAdditionalTime = 60*60*1000*blackAdditionalTime.hours+60*1000*blackAdditionalTime.minutes+1000*blackAdditionalTime.seconds
440                     clocksPage.blackTurnsPerAddition = blackTurnsPerAddition.text
441
442                     //Save settings for black
443                     settings.setInitialTime(timeControl,false,blackInitialTime.hours,blackInitialTime.minutes,blackInitialTime.seconds)
444                     settings.setAdditionalTime(timeControl,false,blackAdditionalTime.hours,blackAdditionalTime.minutes,blackAdditionalTime.seconds)
445                     settings.setTurnsPerAddition(timeControl,false,blackTurnsPerAddition.text)
446                 }
447
448
449
450
451                 pageStack.push(clocksPage)
452
453             }
454
455             }
456
457
458
459
460 TimePickerDialog
461 {
462     id: timePicker
463
464     property string timeType
465     property string player
466
467     titleText: "Choose " + timeType + " time for " + player
468     rejectButtonText: "Cancel"
469     acceptButtonText: "Ok"
470     hourMode: DateTime.TwentyFourHours
471     onAccepted:
472     {
473         if (timeType == "initial")
474         {
475             if (player == "white")
476             {
477                 whiteInitialTime.hours = hour
478                 whiteInitialTime.minutes = minute
479                 whiteInitialTime.seconds = second
480             }
481             else
482             {
483                 blackInitialTime.hours = hour
484                 blackInitialTime.minutes = minute
485                 blackInitialTime.seconds = second
486             }
487         }
488         else if (player == "white")
489             {
490                 whiteAdditionalTime.hours = hour
491                 whiteAdditionalTime.minutes = minute
492                 whiteAdditionalTime.seconds = second
493             }
494             else
495             {
496                 blackAdditionalTime.hours = hour
497                 blackAdditionalTime.minutes = minute
498                 blackAdditionalTime.seconds = second
499             }
500
501     }
502 }
503
504
505
506 TumblerColumn
507 {
508     id: turnsColumn
509     items: turnsList
510
511
512 }
513
514 ListModel
515 {
516     id: turnsList
517
518     Component.onCompleted:
519     {
520         for (var turn = 1; turn <= 99; turn++)
521               {
522                  turnsList.append({"value" : turn});
523               }
524
525     }
526
527 }
528
529
530 TumblerDialog
531 {
532     id: turnsDialog
533
534     property string player
535
536     titleText: "Choose turns per addition for " + player
537     acceptButtonText: "Ok"
538     rejectButtonText: "Cancel"
539
540     columns: [turnsColumn]
541
542     onAccepted:
543     {
544         if (player == "white")
545             whiteTurnsPerAddition.text = turnsColumn.selectedIndex+1
546         else if (player == "black")
547             blackTurnsPerAddition.text = turnsColumn.selectedIndex+1
548
549
550     }
551 }
552
553 }