Skip to content

Commit 1870336

Browse files
committed
Add resetting new game to defaults
1 parent d3959b5 commit 1870336

File tree

2 files changed

+72
-89
lines changed

2 files changed

+72
-89
lines changed

src/new_game_dialog.cpp

Lines changed: 58 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -22,70 +22,6 @@
2222

2323
//-----------------------------------------------------------------------------
2424

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-
8925
NewGameDialog::NewGameDialog(QWidget* parent)
9026
: QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
9127
, m_minimum(3)
@@ -161,50 +97,56 @@ NewGameDialog::NewGameDialog(QWidget* parent)
16197
layout->addSpacing(6);
16298

16399
// 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);
166102

167-
QWidget* timers_widget = new QWidget(area);
103+
QWidget* timers_widget = new QWidget(m_timers_area);
168104
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);
172108

173109
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);
181114
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);
184125

185-
if (timer.id() == previous_timer) {
126+
if (timer == previous_timer) {
186127
button->setDefault(true);
187128
button->setFocus();
188129
active_timer = button;
189130
}
190131
}
191132

192133
// 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);
195137
layout->addSpacing(6);
196-
layout->addWidget(buttons);
138+
layout->addWidget(m_buttons);
197139

198140
// 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));
202144
QSize size = sizeHint();
203-
size.setHeight(size.height() - (area->sizeHint().height() / 3));
145+
size.setHeight(size.height() - (m_timers_area->sizeHint().height() / 3));
204146
resize(QSettings().value("NewGameDialog/Size", size).toSize());
205147
show();
206148
if (active_timer) {
207-
area->ensureWidgetVisible(active_timer);
149+
m_timers_area->ensureWidgetVisible(active_timer);
208150
}
209151
}
210152

@@ -256,3 +198,31 @@ void NewGameDialog::timerChosen(int timer)
256198
}
257199

258200
//-----------------------------------------------------------------------------
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+
//-----------------------------------------------------------------------------

src/new_game_dialog.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
SPDX-FileCopyrightText: 2010-2011 Graeme Gott <[email protected]>
2+
SPDX-FileCopyrightText: 2010-2021 Graeme Gott <[email protected]>
33
44
SPDX-License-Identifier: GPL-3.0-or-later
55
*/
@@ -8,7 +8,11 @@
88
#define TANGLET_NEW_GAME_DIALOG_H
99

1010
#include <QDialog>
11+
class QAbstractButton;
12+
class QCommandLinkButton;
1113
class QComboBox;
14+
class QDialogButtonBox;
15+
class QScrollArea;
1216
class QToolButton;
1317

1418
/**
@@ -50,12 +54,21 @@ private slots:
5054
*/
5155
void timerChosen(int timer);
5256

57+
/**
58+
* Checks if the player has activated the restore button and resets if they have.
59+
* @param button which dialog button the player activated
60+
*/
61+
void restoreDefaults(QAbstractButton* button);
62+
5363
private:
5464
QToolButton* m_normal_size; /**< option for a normal board */
5565
QToolButton* m_large_size; /**< option for a large board */
5666
QComboBox* m_density; /**< option to select the amount of words on the board */
5767
QComboBox* m_length; /**< option to select minimum word length */
5868
int m_minimum; /**< length of the shortest word allowed */
69+
QScrollArea* m_timers_area; /**< scrollarea containing timer buttons */
70+
QList<QCommandLinkButton*> m_timers; /**< actions to choose timer mode and start game */
71+
QDialogButtonBox* m_buttons; /**< buttons to control dialog */
5972
};
6073

6174
#endif // TANGLET_NEW_GAME_DIALOG_H

0 commit comments

Comments
 (0)