-
Notifications
You must be signed in to change notification settings - Fork 2
/
timecontrol.cpp
123 lines (110 loc) · 4.3 KB
/
timecontrol.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <array>
#include <QCheckBox>
#include "timecontrol.hpp"
#include "globals.hpp"
using namespace std;
TimeControl::TimeControl(const QString& title,QWidget* const parent) :
QGroupBox(title,parent),
formLayout(this),
absoluteMoveTime(1,tr("unlimited")),
maxReserve(1,tr("unlimited")),
globalButtons{make_unique<QRadioButton>(tr("unlimited")),
make_unique<QRadioButton>(tr("time")),
make_unique<QRadioButton>(tr("moves"))},
totalGameTime(1)
{
formLayout.addRow(tr("Time given per move:"),&moveTime);
formLayout.addRow(tr("Starting reserve:"),&startingReserve);
formLayout.addRow(tr("Absolute move time:"),&absoluteMoveTime);
formLayout.addRow(tr("Maximum reserve:"),&maxReserve);
carryover.setRange(0,100);
carryover.setValue(100);
carryover.setSuffix(" %");
formLayout.addRow(tr("Unused time added to reserve:"),&carryover);
globalButtonGroup.setExclusive(true);
for (const auto& globalButton:globalButtons) {
globalButtonGroup.addButton(globalButton.get(),globalButtonGroup.buttons().size());
totalGameDuration.addWidget(globalButton.get());
}
stackedLayout.addWidget(&emptyWidget);
totalGameTimeWidget.setLayout(&totalGameTime);
stackedLayout.addWidget(&totalGameTimeWidget);
totalGameMoves.setRange(1,std::numeric_limits<int>::max());
totalGameMoves.setValue(80);
stackedLayout.addWidget(&totalGameMoves);
totalGameDuration.addLayout(&stackedLayout);
globalButtons[0]->setChecked(true);
connect(&globalButtonGroup,static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::idClicked),&stackedLayout,[=](const int id){
stackedLayout.setCurrentIndex(id);
QSpinBox* const spinBox=std::array<QSpinBox*,3>{nullptr,&totalGameTime.seconds,&totalGameMoves}[id];
if (spinBox!=nullptr) {
spinBox->setFocus();
spinBox->selectAll();
}
});
formLayout.addRow(tr("Maximum game duration:"),&totalGameDuration);
}
QString TimeControl::toString(const bool timed) const
{
if (!timed)
return "0/0/0/0/0";
bool truncatable=true;
QString result;
if (!absoluteMoveTime.checkBox->isChecked()) {
result="/"+absoluteMoveTime.toString();
truncatable=false;
}
if (globalButtonGroup.checkedId()>0) {
if (globalButtonGroup.checkedId()==1)
result.prepend("/"+totalGameTime.toString());
else
result.prepend("/"+totalGameMoves.text()+"t");
truncatable=false;
}
else if (!truncatable)
result.prepend("/0");
if (!maxReserve.checkBox->isChecked()) {
result.prepend("/"+maxReserve.toString());
truncatable=false;
}
else if (!truncatable)
result.prepend("/0");
if (true) //(carryover.value()!=100) // SERVER BUG: Default carryover percentage is 0 instead of 100.
result.prepend("/"+carryover.cleanText());
else if (!truncatable)
result.prepend("/100");
return moveTime.toString()+"/"+startingReserve.toString()+result;
}
void TimeControl::readSettings(QSettings& settings)
{
settings.beginGroup("TimeControl");
moveTime.readSetting(settings,"move_time");
startingReserve.readSetting(settings,"starting_reserve");
absoluteMoveTime.readSetting(settings,"absolute_move_time");
maxReserve.readSetting(settings,"max_reserve");
carryover.setValue(settings.value("carry_over",carryover.value()).toUInt());
const auto globalIndex=settings.value("total_game_duration_type").toInt();
if (globalIndex>=0 && globalIndex<=3) {
globalButtons[globalIndex]->click();
if (globalIndex==1)
totalGameTime.readSetting(settings,"total_game_duration");
else if (globalIndex==2)
totalGameMoves.setValue(settings.value("total_game_duration",totalGameMoves.value()).toUInt());
}
settings.endGroup();
}
void TimeControl::writeSettings(QSettings& settings) const
{
settings.beginGroup("TimeControl");
moveTime.writeSetting(settings,"move_time");
startingReserve.writeSetting(settings,"starting_reserve");
absoluteMoveTime.writeSetting(settings,"absolute_move_time");
maxReserve.writeSetting(settings,"max_reserve");
settings.setValue("carry_over",carryover.value());
settings.setValue("total_game_duration_type",globalButtonGroup.checkedId());
switch (globalButtonGroup.checkedId()) {
case 1: totalGameTime.writeSetting(settings,"total_game_duration"); break;
case 2: settings.setValue("total_game_duration",totalGameMoves.value()); break;
}
settings.endGroup();
}