Merge branch 'settings' into harmattan
[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: 0
206                 property int minutes: 30
207                 property int seconds: 0
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: 0
240                 property int minutes: 30
241                 property int seconds: 0
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: 0
290                 property int minutes: 0
291                 property int seconds: 30
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: 0
321                 property int minutes: 0
322                 property int seconds: 30
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                         turnsDialog.open()
371                         turnsColumn.selectedIndex = parent.text-1 //Needs to be after open(),  or gets overridden by the previous chosen value
372
373
374                     }
375                 }
376
377             }
378
379             TextField
380             {
381                 id: blackTurnsPerAddition
382                 visible: newGameDialogPage.askTurnsPerAddition && !equalTimesSwitch.checked
383                 readOnly: true;
384
385                 anchors.top: whiteTurnsPerAddition.top
386                 anchors.left: whiteTurnsPerAddition.right
387                 anchors.leftMargin: 25
388
389                 MouseArea
390                 {
391                     anchors.fill: parent
392                     onClicked:
393                     {
394                         turnsDialog.player = "black"
395                         turnsDialog.open()
396                         turnsColumn.selectedIndex = parent.text-1 //Needs to be after open(),  or gets overridden by the previous chosen value
397                     }
398                 }
399
400             }
401
402
403             Button
404             {
405                 id: okButton
406                 text:  "Start game"
407
408                 anchors.top: whiteTurnsPerAddition.bottom
409                 anchors.topMargin: 15
410                 anchors.right: parent.right
411
412                 onClicked:
413                 {
414
415
416                 clocksPage.timeControl = timeControl
417
418
419                 clocksPage.whiteInitialTime = 60*60*1000*whiteInitialTime.hours+60*1000*whiteInitialTime.minutes+1000*whiteInitialTime.seconds
420                 clocksPage.whiteAdditionalTime = 60*60*1000*whiteAdditionalTime.hours+60*1000*whiteAdditionalTime.minutes+1000*whiteAdditionalTime.seconds
421                 clocksPage.whiteTurnsPerAddition = whiteTurnsPerAddition.text
422
423
424
425                 //Save settings for white
426                 settings.setInitialTime(timeControl,true,whiteInitialTime.hours,whiteInitialTime.minutes,whiteInitialTime.seconds)
427                 settings.setAdditionalTime(timeControl,true,whiteAdditionalTime.hours,whiteAdditionalTime.minutes,whiteAdditionalTime.seconds)
428                 settings.setTurnsPerAddition(timeControl,true,whiteTurnsPerAddition.text)
429
430                 settings.setEqualTimes(timeControl,equalTimesSwitch.checked) //save equal times setting
431
432                 if (equalTimesSwitch.checked)
433                 {
434                     //use same values for white and black
435                     clocksPage.blackInitialTime = 60*60*1000*whiteInitialTime.hours+60*1000*whiteInitialTime.minutes+1000*whiteInitialTime.seconds
436                     clocksPage.blackAdditionalTime = 60*60*1000*whiteAdditionalTime.hours+60*1000*whiteAdditionalTime.minutes+1000*whiteAdditionalTime.seconds
437                     clocksPage.blackTurnsPerAddition = whiteTurnsPerAddition.text
438
439                     //If black values in dialog are not used they are not saved to settings
440                 }
441                 else
442                 {
443                     clocksPage.blackInitialTime = 60*60*1000*blackInitialTime.hours+60*1000*blackInitialTime.minutes+1000*blackInitialTime.seconds
444                     clocksPage.blackAdditionalTime = 60*60*1000*blackAdditionalTime.hours+60*1000*blackAdditionalTime.minutes+1000*blackAdditionalTime.seconds
445                     clocksPage.blackTurnsPerAddition = blackTurnsPerAddition.text
446
447                     //Save settings for black
448                     settings.setInitialTime(timeControl,false,blackInitialTime.hours,blackInitialTime.minutes,blackInitialTime.seconds)
449                     settings.setAdditionalTime(timeControl,false,blackAdditionalTime.hours,blackAdditionalTime.minutes,blackAdditionalTime.seconds)
450                     settings.setTurnsPerAddition(timeControl,false,blackTurnsPerAddition.text)
451                 }
452
453
454
455
456                 pageStack.push(clocksPage)
457
458             }
459
460             }
461
462
463
464
465 TimePickerDialog
466 {
467     id: timePicker
468
469     property string timeType
470     property string player
471
472     titleText: "Choose " + timeType + " time for " + player
473     rejectButtonText: "Cancel"
474     acceptButtonText: "Ok"
475     hourMode: DateTime.TwentyFourHours
476     onAccepted:
477     {
478         if (timeType == "initial")
479         {
480             if (player == "white")
481             {
482                 whiteInitialTime.hours = hour
483                 whiteInitialTime.minutes = minute
484                 whiteInitialTime.seconds = second
485             }
486             else
487             {
488                 blackInitialTime.hours = hour
489                 blackInitialTime.minutes = minute
490                 blackInitialTime.seconds = second
491             }
492         }
493         else if (player == "white")
494             {
495                 whiteAdditionalTime.hours = hour
496                 whiteAdditionalTime.minutes = minute
497                 whiteAdditionalTime.seconds = second
498             }
499             else
500             {
501                 blackAdditionalTime.hours = hour
502                 blackAdditionalTime.minutes = minute
503                 blackAdditionalTime.seconds = second
504             }
505
506     }
507 }
508
509
510
511 TumblerColumn
512 {
513     id: turnsColumn
514     items: turnsList
515
516
517 }
518
519 ListModel
520 {
521     id: turnsList
522
523     Component.onCompleted:
524     {
525         for (var turn = 1; turn <= 99; turn++)
526               {
527                  turnsList.append({"value" : turn});
528               }
529
530     }
531
532 }
533
534
535 TumblerDialog
536 {
537     id: turnsDialog
538
539     property string player
540
541     titleText: "Choose turns per addition for " + player
542     acceptButtonText: "Ok"
543     rejectButtonText: "Cancel"
544
545     columns: [turnsColumn]
546
547     onAccepted:
548     {
549         if (player == "white")
550             whiteTurnsPerAddition.text = turnsColumn.selectedIndex+1
551         else if (player == "black")
552             blackTurnsPerAddition.text = turnsColumn.selectedIndex+1
553
554
555     }
556 }
557
558 }