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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/*
* Quackle -- Crossword game artificial intelligence and analysis tool
* Copyright (C) 2005-2019 Jason Katz-Brown, John O'Laughlin, and John Fultz.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include <QtWidgets>
#include <boardparameters.h>
#include <board.h>
#include <datamanager.h>
#include <quackleio/util.h>
#include "boardsetupdialog.h"
#include "boardsetup.h"
#include "brb.h"
#include "customqsettings.h"
#include "geometry.h"
#include "widgetfactory.h"
#include "settings.h"
BoardSetupDialog::BoardSetupDialog(QWidget *parent) : QDialog(parent)
{
resize(700,550);
setSizeGripEnabled(true);
// construct the board
BoardSetupFactory factory;
BRB * brb = new BRB(&factory);
m_boardFrame = static_cast<BoardSetup *>(brb->getBoardView())->boardFrame();
initializeBoardName();
// construct the UI elements
m_horizontalSymmetry = new QCheckBox(tr("Horizontal"));
m_verticalSymmetry = new QCheckBox(tr("Vertical"));
m_diagonalSymmetry = new QCheckBox(tr("Diagonal"));
m_horizontalSymmetry->setCheckState(Qt::Checked);
m_verticalSymmetry->setCheckState(Qt::Checked);
m_diagonalSymmetry->setCheckState(Qt::Checked);
m_horizontalDimension = constructDimensionComboBox(QUACKLE_BOARD_PARAMETERS->width());
m_verticalDimension = constructDimensionComboBox(QUACKLE_BOARD_PARAMETERS->height());
m_boardName = new QLineEdit(QuackleIO::Util::uvStringToQString(QUACKLE_BOARD_PARAMETERS->name()));
m_saveChanges = new QPushButton(tr("&Save Changes"));
m_cancel = new QPushButton(tr("&Cancel"));
m_undoAll = new QPushButton(tr("&Undo All Changes"));
m_deleteBoard = new QPushButton(tr("&Delete Board"));
QVBoxLayout * superLayout = new QVBoxLayout;
Geometry::setupFramedLayout(superLayout);
QHBoxLayout * mainLayout = new QHBoxLayout;
Geometry::setupInnerLayout(mainLayout);
QVBoxLayout * leftSideLayout = new QVBoxLayout;
Geometry::setupInnerLayout(leftSideLayout);
QLabel * boardNameLabel = new QLabel(tr("&Board name:"));
boardNameLabel->setBuddy(m_boardName);
QGroupBox * dimensionGroup = new QGroupBox(tr("Board dimensions"));
QHBoxLayout * dimensionRow = new QHBoxLayout(dimensionGroup);
Geometry::setupFramedLayout(dimensionRow);
QLabel * dimensionLabel = new QLabel(tr(" by "));
QGroupBox * symmetryGroup = new QGroupBox(tr("Board symmetry"));
QVBoxLayout * symmetryCol = new QVBoxLayout(symmetryGroup);
Geometry::setupFramedLayout(symmetryCol);
QHBoxLayout * buttonRow = new QHBoxLayout;
Geometry::setupInnerLayout(buttonRow);
// build the layout
dimensionRow->addWidget(m_horizontalDimension);
dimensionRow->addWidget(dimensionLabel);
dimensionRow->addWidget(m_verticalDimension);
dimensionRow->addStretch();
symmetryCol->addWidget(m_horizontalSymmetry);
symmetryCol->addWidget(m_verticalSymmetry);
symmetryCol->addWidget(m_diagonalSymmetry);
buttonRow->addStretch(1);
buttonRow->addWidget(m_cancel);
buttonRow->addWidget(m_saveChanges);
leftSideLayout->addWidget(boardNameLabel);
leftSideLayout->addWidget(m_boardName);
leftSideLayout->addWidget(dimensionGroup);
leftSideLayout->addWidget(symmetryGroup);
leftSideLayout->addWidget(m_undoAll);
if (!m_originalName.isEmpty())
leftSideLayout->addWidget(m_deleteBoard);
leftSideLayout->addStretch();
mainLayout->addLayout(leftSideLayout);
mainLayout->addWidget(brb);
mainLayout->setStretchFactor(leftSideLayout, 0);
mainLayout->setStretchFactor(brb, 10);
superLayout->addLayout(mainLayout);
superLayout->addLayout(buttonRow);
setLayout(superLayout);
m_saveChanges->setDefault(true);
// hook up signals and slots
connect(m_horizontalDimension, SIGNAL(activated(int)), this, SLOT(parametersChanged(int)));
connect(m_horizontalDimension, SIGNAL(activated(int)), this, SLOT(symmetryChanged()));
connect(m_verticalDimension, SIGNAL(activated(int)), this, SLOT(parametersChanged(int)));
connect(m_verticalDimension, SIGNAL(activated(int)), this, SLOT(symmetryChanged()));
connect(m_boardName, SIGNAL(textEdited(const QString &)), this, SLOT(parametersChanged(0)));
connect(m_saveChanges, SIGNAL(clicked()), this, SLOT(accept()));
connect(m_cancel, SIGNAL(clicked()), this, SLOT(reject()));
connect(m_undoAll, SIGNAL(clicked()), this, SLOT(undoAllChanges()));
connect(m_deleteBoard, SIGNAL(clicked()), this, SLOT(deleteBoard()));
connect(m_horizontalSymmetry, SIGNAL(stateChanged(int)), this, SLOT(symmetryChanged()));
connect(m_verticalSymmetry, SIGNAL(stateChanged(int)), this, SLOT(symmetryChanged()));
connect(m_diagonalSymmetry, SIGNAL(stateChanged(int)), this, SLOT(symmetryChanged()));
setWindowTitle(tr("Configure Board - Quackle"));
// sync game board with control states and draw board
ostringstream boardStream;
QUACKLE_BOARD_PARAMETERS->Serialize(boardStream);
m_serializedOriginalBoard = boardStream.str();
parametersChanged(0);
symmetryChanged();
}
BoardSetupDialog::~BoardSetupDialog()
{
}
QComboBox * BoardSetupDialog::constructDimensionComboBox(int defaultDimension)
{
QComboBox * returnValue = new QComboBox;
for (int i = QUACKLE_MINIMUM_BOARD_SIZE; i <= QUACKLE_MAXIMUM_BOARD_SIZE; i++)
{
returnValue->addItem(QString::number(i));
if (i == defaultDimension)
returnValue->setCurrentIndex(returnValue->count() - 1);
}
return returnValue;
}
void BoardSetupDialog::initializeBoardName()
{
m_originalName = QuackleIO::Util::uvStringToQString(QUACKLE_BOARD_PARAMETERS->name());
if (m_originalName.isEmpty())
{
CustomQSettings settings;
QString generatedName = "New Board";
int i = 1;
settings.beginGroup("quackle/boardparameters");
while (settings.contains(generatedName))
{
generatedName = "New Board ";
generatedName += QString::number(i++);
}
QUACKLE_BOARD_PARAMETERS->setName(QuackleIO::Util::qstringToString(generatedName));
}
}
void BoardSetupDialog::parametersChanged(int unused)
{
QUACKLE_BOARD_PARAMETERS->setWidth(m_horizontalDimension->currentIndex() + QUACKLE_MINIMUM_BOARD_SIZE);
QUACKLE_BOARD_PARAMETERS->setHeight(m_verticalDimension->currentIndex() + QUACKLE_MINIMUM_BOARD_SIZE);
if (QUACKLE_BOARD_PARAMETERS->startColumn() >= QUACKLE_BOARD_PARAMETERS->width())
QUACKLE_BOARD_PARAMETERS->setStartColumn(QUACKLE_BOARD_PARAMETERS->width() - 1);
if (QUACKLE_BOARD_PARAMETERS->startRow() >= QUACKLE_BOARD_PARAMETERS->height())
QUACKLE_BOARD_PARAMETERS->setStartRow(QUACKLE_BOARD_PARAMETERS->height() - 1);
UVString boardName = QuackleIO::Util::qstringToString(m_boardName->text());
QUACKLE_BOARD_PARAMETERS->setName(boardName);
m_game.reset();
m_game.addPosition();
m_boardFrame->setBoard(m_game.currentPosition().board());
m_boardFrame->parametersChanged();
}
void BoardSetupDialog::symmetryChanged()
{
bool allowDiagonalSymmetry =
m_horizontalSymmetry->isChecked() && m_verticalSymmetry->isChecked() &&
(m_horizontalDimension->currentIndex() == m_verticalDimension->currentIndex());
m_diagonalSymmetry->setEnabled(allowDiagonalSymmetry);
m_boardFrame->setSymmetry(
m_horizontalSymmetry->isChecked(),
m_verticalSymmetry->isChecked(),
m_diagonalSymmetry->isChecked() && m_diagonalSymmetry->isEnabled());
}
void BoardSetupDialog::accept()
{
if (m_boardName->text().isEmpty())
{
QMessageBox::critical(this, tr("Missing board name"),
"You must type in a board name before saving this change.",
QMessageBox::Ok, QMessageBox::NoButton);
return;
}
else if (m_boardName->text() != m_originalName)
{
CustomQSettings settings;
settings.beginGroup("quackle/boardparameters");
if (settings.contains(m_boardName->text()))
{
if (QMessageBox::warning(this, tr("Overwrite existing board?"),
tr("You've specified a board name which already exists. Do you want to overwrite the existing board?"),
QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
return;
}
}
PixmapCacher::self()->invalidate();
QDialog::accept();
}
void BoardSetupDialog::reject()
{
undoAllChanges();
QDialog::reject();
}
void BoardSetupDialog::undoAllChanges()
{
istringstream boardStream(m_serializedOriginalBoard);
QUACKLE_DATAMANAGER->setBoardParameters(Quackle::BoardParameters::Deserialize(boardStream));
parametersChanged(0);
}
void BoardSetupDialog::deleteBoard()
{
QString message = "Do you really want to delete the game board \"";
message += m_originalName;
message += "\"?";
if (QMessageBox::warning(NULL, QString("Confirm Deletion"), message,
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::No | QMessageBox::Escape) == QMessageBox::Yes)
{
CustomQSettings settings;
settings.beginGroup("quackle/boardparameters");
settings.remove(m_originalName);
QDialog::reject();
}
}
|