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