|
22 | 22 |
|
23 | 23 | //----------------------------------------------------------------------------- |
24 | 24 |
|
25 | | -namespace |
26 | | -{ |
27 | | - |
28 | | -/** |
29 | | - * @brief The TimerDescription class describes a timer. |
30 | | - */ |
31 | | -class TimerDescription |
32 | | -{ |
33 | | -public: |
34 | | - /** |
35 | | - * Constructs a timer description. |
36 | | - * @param id the timer mode to load details of |
37 | | - */ |
38 | | - TimerDescription(int id) |
39 | | - : m_name(Clock::timerToString(id)) |
40 | | - , m_description(Clock::timerDescription(id)) |
41 | | - , m_id(id) |
42 | | - { |
43 | | - } |
44 | | - |
45 | | - /** |
46 | | - * @return translated name of timer |
47 | | - */ |
48 | | - QString name() const |
49 | | - { |
50 | | - return m_name; |
51 | | - } |
52 | | - |
53 | | - /** |
54 | | - * @return translated description of timer |
55 | | - */ |
56 | | - QString description() const |
57 | | - { |
58 | | - return m_description; |
59 | | - } |
60 | | - |
61 | | - /** |
62 | | - * @return the timer mode represented by this description |
63 | | - */ |
64 | | - int id() const |
65 | | - { |
66 | | - return m_id; |
67 | | - } |
68 | | - |
69 | | - /** |
70 | | - * Locale-aware sorts the descriptions. |
71 | | - * @param timer the description to compare to |
72 | | - * @return whether this description sorts first |
73 | | - */ |
74 | | - bool operator<(const TimerDescription& timer) const |
75 | | - { |
76 | | - return m_name.localeAwareCompare(timer.m_name) < 0; |
77 | | - } |
78 | | - |
79 | | -private: |
80 | | - QString m_name; /**< translated name of timer */ |
81 | | - QString m_description; /**< translated description of timer */ |
82 | | - int m_id; /**< the timer mode */ |
83 | | -}; |
84 | | - |
85 | | -} |
86 | | - |
87 | | -//----------------------------------------------------------------------------- |
88 | | - |
89 | 25 | NewGameDialog::NewGameDialog(QWidget* parent) |
90 | 26 | : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint) |
91 | 27 | , m_minimum(3) |
@@ -161,50 +97,56 @@ NewGameDialog::NewGameDialog(QWidget* parent) |
161 | 97 | layout->addSpacing(6); |
162 | 98 |
|
163 | 99 | // Create timer buttons |
164 | | - QScrollArea* area = new QScrollArea(this); |
165 | | - layout->addWidget(area); |
| 100 | + m_timers_area = new QScrollArea(this); |
| 101 | + layout->addWidget(m_timers_area); |
166 | 102 |
|
167 | | - QWidget* timers_widget = new QWidget(area); |
| 103 | + QWidget* timers_widget = new QWidget(m_timers_area); |
168 | 104 | QVBoxLayout* timers_layout = new QVBoxLayout(timers_widget); |
169 | | - area->setWidget(timers_widget); |
170 | | - area->setWidgetResizable(true); |
171 | | - area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
| 105 | + m_timers_area->setWidget(timers_widget); |
| 106 | + m_timers_area->setWidgetResizable(true); |
| 107 | + m_timers_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
172 | 108 |
|
173 | 109 | QCommandLinkButton* active_timer = nullptr; |
174 | | - QList<TimerDescription> timers; |
175 | | - for (int i = Clock::Tanglet; i < Clock::TotalTimers; ++i) { |
176 | | - timers.append(i); |
177 | | - } |
178 | | - std::sort(timers.begin(), timers.end()); |
179 | | - for (const TimerDescription& timer : timers) { |
180 | | - QCommandLinkButton* button = new QCommandLinkButton(timer.name(), timer.description(), timers_widget); |
| 110 | + for (int timer = 0; timer < Clock::TotalTimers; ++timer) { |
| 111 | + const QString name = Clock::timerToString(timer); |
| 112 | + |
| 113 | + QCommandLinkButton* button = new QCommandLinkButton(name, Clock::timerDescription(timer), timers_widget); |
181 | 114 | button->setMinimumWidth(500); |
182 | | - connect(button, &QCommandLinkButton::clicked, this, [this, timer] { timerChosen(timer.id()); }); |
183 | | - timers_layout->addWidget(button); |
| 115 | + connect(button, &QCommandLinkButton::clicked, this, [this, timer] { timerChosen(timer); }); |
| 116 | + |
| 117 | + int i; |
| 118 | + for (i = 0; i < m_timers.size(); ++i) { |
| 119 | + if (m_timers[i]->text().localeAwareCompare(name) >= 0) { |
| 120 | + break; |
| 121 | + } |
| 122 | + } |
| 123 | + m_timers.insert(i, button); |
| 124 | + timers_layout->insertWidget(i, button); |
184 | 125 |
|
185 | | - if (timer.id() == previous_timer) { |
| 126 | + if (timer == previous_timer) { |
186 | 127 | button->setDefault(true); |
187 | 128 | button->setFocus(); |
188 | 129 | active_timer = button; |
189 | 130 | } |
190 | 131 | } |
191 | 132 |
|
192 | 133 | // Create cancel button |
193 | | - QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Cancel, Qt::Horizontal, this); |
194 | | - connect(buttons, &QDialogButtonBox::rejected, this, &NewGameDialog::reject); |
| 134 | + m_buttons = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::RestoreDefaults, Qt::Horizontal, this); |
| 135 | + connect(m_buttons, &QDialogButtonBox::rejected, this, &NewGameDialog::reject); |
| 136 | + connect(m_buttons, &QDialogButtonBox::clicked, this, &NewGameDialog::restoreDefaults); |
195 | 137 | layout->addSpacing(6); |
196 | | - layout->addWidget(buttons); |
| 138 | + layout->addWidget(m_buttons); |
197 | 139 |
|
198 | 140 | // Show contents |
199 | | - area->setMinimumWidth(timers_widget->sizeHint().width() |
200 | | - + area->verticalScrollBar()->sizeHint().width() |
201 | | - + (area->frameWidth() * 2)); |
| 141 | + m_timers_area->setMinimumWidth(timers_widget->sizeHint().width() |
| 142 | + + m_timers_area->verticalScrollBar()->sizeHint().width() |
| 143 | + + (m_timers_area->frameWidth() * 2)); |
202 | 144 | QSize size = sizeHint(); |
203 | | - size.setHeight(size.height() - (area->sizeHint().height() / 3)); |
| 145 | + size.setHeight(size.height() - (m_timers_area->sizeHint().height() / 3)); |
204 | 146 | resize(QSettings().value("NewGameDialog/Size", size).toSize()); |
205 | 147 | show(); |
206 | 148 | if (active_timer) { |
207 | | - area->ensureWidgetVisible(active_timer); |
| 149 | + m_timers_area->ensureWidgetVisible(active_timer); |
208 | 150 | } |
209 | 151 | } |
210 | 152 |
|
@@ -256,3 +198,31 @@ void NewGameDialog::timerChosen(int timer) |
256 | 198 | } |
257 | 199 |
|
258 | 200 | //----------------------------------------------------------------------------- |
| 201 | + |
| 202 | +void NewGameDialog::restoreDefaults(QAbstractButton* button) |
| 203 | +{ |
| 204 | + if (m_buttons->buttonRole(button) != QDialogButtonBox::ResetRole) { |
| 205 | + return; |
| 206 | + } |
| 207 | + |
| 208 | + m_normal_size->setChecked(true); |
| 209 | + m_density->setCurrentIndex(1); |
| 210 | + sizeChanged(); |
| 211 | + m_length->setCurrentIndex(0); |
| 212 | + |
| 213 | + const QString name = Clock::timerToString(Clock::Tanglet); |
| 214 | + QCommandLinkButton* timer = nullptr; |
| 215 | + for (QCommandLinkButton* b : qAsConst(m_timers)) { |
| 216 | + if (b->text() == name) { |
| 217 | + timer = b; |
| 218 | + break; |
| 219 | + } |
| 220 | + } |
| 221 | + if (timer) { |
| 222 | + timer->setDefault(true); |
| 223 | + timer->setFocus(); |
| 224 | + m_timers_area->ensureWidgetVisible(timer); |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +//----------------------------------------------------------------------------- |
0 commit comments