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