/
widget.cpp
410 lines (337 loc) · 11.3 KB
/
widget.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{
ui->setupUi(this);
setObjectName("main gui");
init();
//guiHostt = this;
}
Widget::~Widget()
{
delete g;
delete ui;
}
void Widget::init()
{
setCentralWidgetPropreties();
setSoDukoBoardsPropreties();
setsolvePropreties();
setDBPropreties();
setlogPropreties();
}
void Widget::setCentralWidgetPropreties()
{
this->setWindowTitle("Vadim Khakham & Gilad Eini Soduku Solver using sat4j");
this->resize(QDesktopWidget().availableGeometry(this).size() * 0.7);
mainLayout = new QVBoxLayout;
this->setLayout(mainLayout);
}
void Widget::setSoDukoBoardsPropreties()
{
QVBoxLayout* originBlockLayout = new QVBoxLayout;
titleOrigin = new QLabel("origin");
titleOrigin->setStyleSheet("font-family: \"Times New Roman\", Times, serif; font-size: 20px; background-color: yellow");
titleOrigin->setAlignment(Qt::AlignCenter);
originBoardLayout = new QGridLayout();
originBoardLayout->setObjectName("origin board");
build81x81board(originBoardLayout,1);
originBlockLayout->addWidget(titleOrigin);
originBlockLayout->addLayout(originBoardLayout);
originBlockLayout->setSpacing(2);
QVBoxLayout* resultBlockLayout = new QVBoxLayout;
titleResult = new QLabel("result");
titleResult->setStyleSheet("font-family: \"Times New Roman\", Times, serif; font-size: 20px;");
titleResult->setAlignment(Qt::AlignCenter);
setTitleResultColor(1);
resultBoardLayout = new QGridLayout();
resultBoardLayout->setObjectName("result board");
build81x81board(resultBoardLayout,0);
resultBlockLayout->addWidget(titleResult);
resultBlockLayout->addLayout(resultBoardLayout);
resultBlockLayout->setSpacing(2);
QHBoxLayout* originAndResultBlocksLayouts = new QHBoxLayout;
originAndResultBlocksLayouts->addLayout(originBlockLayout);
originAndResultBlocksLayouts->addLayout(resultBlockLayout);
//hl1->addWidget(changeBoard);
originAndResultBlocksLayouts->setSpacing(50);
QWidget* nonResizeBlocks = new QWidget(this);
nonResizeBlocks->setFixedSize(QSize(500,300));
nonResizeBlocks->setLayout(originAndResultBlocksLayouts);
mainLayout->addWidget(nonResizeBlocks);
mainLayout->setAlignment(nonResizeBlocks, Qt::AlignCenter);
}
void Widget::setlogPropreties()
{
log = new QTextEdit();
log->setStyleSheet("font-family: \"Times New Roman\", Times, serif; font-size: 20px;");
log->setReadOnly(true);
mainLayout->addWidget(log);
}
void Widget::setDBPropreties()
{
changeBoard = new QGroupBox;
changeBoard->setTitle("DB Options");
//changeBoard->setStyleSheet("border: 1px solid gray; border-radius: 9px; margin-top: 0.5em; border-color: red");
QHBoxLayout* nextAndPrevButtons = new QHBoxLayout;
QPushButton* nextBoard = new QPushButton("next");
nextBoard->setObjectName("next");
connect(nextBoard,SIGNAL(clicked(bool)),this,SLOT(changeBoards()));
QPushButton* prevBoard = new QPushButton("prev");
prevBoard->setObjectName("prev");
connect(prevBoard,SIGNAL(clicked(bool)),this,SLOT(changeBoards()));
nextAndPrevButtons->addWidget(prevBoard);
nextAndPrevButtons->addWidget(nextBoard);
QHBoxLayout* customPathLayout = new QHBoxLayout;
QLabel* customPath = new QLabel("custom");
customPath->setToolTip("enter path to board file. ex: <name>.txt");
newBoardPath = new QLineEdit("");
newBoardPath->setToolTip("enter path to board file. ex: <name>.txt");
QPushButton* newBoardPathLoad = new QPushButton("load");
newBoardPathLoad->setObjectName("load");
connect(newBoardPathLoad,SIGNAL(clicked(bool)),this,SLOT(changeBoards()));
compareBoardPath = new QLineEdit("");
compareBoardPath->setReadOnly(true);
compareBoardPath->setToolTip("path to solved board.after solving the board, comparing result with validated solution");
customPathLayout->addWidget(customPath);
customPathLayout->addWidget(newBoardPath);
customPathLayout->addWidget(newBoardPathLoad);
customPathLayout->addWidget(compareBoardPath);
QVBoxLayout* changeBoardLayout = new QVBoxLayout;
changeBoardLayout->addLayout(nextAndPrevButtons);
changeBoardLayout->addLayout(customPathLayout);
changeBoard->setLayout(changeBoardLayout);
QHBoxLayout* spacersLayout = new QHBoxLayout();
spacersLayout->addSpacerItem(new QSpacerItem(0,0,QSizePolicy::MinimumExpanding,QSizePolicy::Maximum));
spacersLayout->addWidget(changeBoard);
spacersLayout->addSpacerItem(new QSpacerItem(0,0,QSizePolicy::MinimumExpanding,QSizePolicy::Maximum));
mainLayout->addLayout(spacersLayout);
}
void Widget::setsolvePropreties()
{
runSolverButton = new QPushButton("solve");
runSolverButton->setToolTip("run sat solver on chosen board");
connect(runSolverButton,SIGNAL(clicked(bool)),this,SLOT(solveWasPressed()));
printVtButton = new QPushButton("print VT");
printVtButton->setToolTip("print 729 cells of the Variable Table");
connect(printVtButton,SIGNAL(clicked(bool)),this,SLOT(printVt()));
animationButton = new QPushButton("turn animation on");
animationButton->setToolTip("printing with animation");
connect(animationButton,SIGNAL(clicked(bool)),this,SLOT(animationButtonPressed()));
QHBoxLayout* buttonLayout = new QHBoxLayout();
buttonLayout->addSpacerItem(new QSpacerItem(0,0,QSizePolicy::MinimumExpanding,QSizePolicy::Maximum));
buttonLayout->addWidget(runSolverButton);
buttonLayout->addWidget(printVtButton);
buttonLayout->addWidget(animationButton);
buttonLayout->addSpacerItem(new QSpacerItem(0,0,QSizePolicy::MinimumExpanding,QSizePolicy::Maximum));
mainLayout->addLayout(buttonLayout);
}
void Widget::build81x81board(QGridLayout *mainLayout, int QMap)
{
int rowStart = 0;
int colStart = 0;
mainLayout->setSpacing(0);
mainLayout->setMargin(0);
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
QFrame* box3x3frame = new QFrame;
box3x3frame->setObjectName(QString::number(i*3+j+1));
build3x3frame(box3x3frame,rowStart,colStart,QMap);
mainLayout->addWidget(box3x3frame,i,j,1,1);
colStart+=3;
}
rowStart+=3;
}
}
void Widget::build3x3frame(QFrame *box3x3frame, int rowStart, int colStart, int QMap)
{
box3x3frame->setFrameStyle(QFrame::Plain);
box3x3frame->setFrameShape(QFrame::Box);
QGridLayout* box3x3 = new QGridLayout(box3x3frame);
box3x3->setSpacing(0);
box3x3->setMargin(0);
int x = rowStart%9;
for (int i = 0; i < 3; ++i)
{
int y = colStart%9;
for (int j = 0; j < 3; ++j)
{
QLabel* cell = new QLabel("");
cell->setAlignment(Qt::AlignCenter);
//cell->setAlignment(Qt::AlignVCenter);
if(QMap) originPointToCellMap.insert(QPair<int,int>(x,y++),cell);
else resultPointToCellMap.insert(QPair<int,int>(x,y++),cell);
cell->setFrameStyle(QFrame::Plain);
cell->setFrameShape(QFrame::Box);
cell->setMargin(5);
box3x3->addWidget(cell,i,j,1,1);
}
x++;
}
}
int Widget::mainEngine()
{
animation = false;
string board_filename = "board0.txt";
currnetBoardNumber = 0;
g = new Globals(DEBUG,this);
solved = false;
g->createBoardZeroIfNotExist("board0.txt");
g->init_board(board_filename);
g->print_board(board_filename + " -default board", true);
appendRowToLog("Welcome to sudoku solver");
printOptionsToLog();
return 0;
}
void Widget::setTitleResultColor(bool start)
{
QString color = start ? "font-family: \"Times New Roman\", Times, serif; font-size: 20px; background-color: red;" : "font-family: \"Times New Roman\", Times, serif; font-size: 20px; background-color: green;";
titleResult->setStyleSheet(color);
}
void Widget::setTitleOrigin(QString newTitle)
{
titleOrigin->setText(newTitle);
}
void Widget::setTitleResult(QString newTitle)
{
titleResult->setText(newTitle);
}
void Widget::appendRowToLog(QString newRow)
{
log->append(newRow);
}
QGridLayout *Widget::getOriginBoard()
{
return originBoardLayout;
}
QGridLayout *Widget::getResultBoard()
{
return resultBoardLayout;
}
QMap<QPair<int, int>, QLabel *> Widget::getOriginPointToCellMap()
{
return originPointToCellMap;
}
QMap<QPair<int, int>, QLabel *> Widget::getResultPointToCellMap()
{
return resultPointToCellMap;
}
void Widget::changeBoards()
{
setTitleResultColor(1);
delete g;
g = new Globals(DEBUG,this);
QString senderName = QObject::sender()->objectName();
log->clear();
if(senderName == "load")
{
QString path = newBoardPath->text();
currnetBoardNumber = 0;
int result = g->chooseBoard(path,compareBoardPath);
if(result)
{
titleOrigin->setText(path);
appendRowToLog("success loading " + path);
}
else
{
newBoardPath->setText("");
//QMessageBox msgBox;msgBox.setText("can't find board file with the name: " + path);msgBox.exec();
newBoardPath->setText("board0.txt");
g->chooseBoard("board0.txt",compareBoardPath);
titleOrigin->setText(QString(g->m_board_filename.c_str()) +" -default board");
appendRowToLog("failue loading " + path + "- loaded default board instead: board0.txt");
}
}
else
{
cleanHighlights();
g->switchBoard(senderName,currnetBoardNumber);
}
solved = false;
}
void Widget::printVt()
{
log->clear();
if (!solved) {
appendRowToLog("you have to solve the board first.only than you can print the var table");
}
else
{
g->print_var_table();
}
}
void Widget::animationButtonPressed()
{
animation = !animation;
animation ? animationButton->setText("turn animation off") : animationButton->setText("turn animation on") ;
}
void Widget::printOptionsToLog()
{
g->countDB();
}
void Widget::cleanHighlights()
{
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
QLabel* cellOrigin = getOriginPointToCellMap()[QPair<int,int>(i,j)];
QLabel* cellResult = getResultPointToCellMap()[QPair<int,int>(i,j)];;
cellResult->setText(" ");
cellResult->setStyleSheet(" ");
cellOrigin->setStyleSheet(" ");
}
}
}
void Widget::wait()
{
msgBox = new QMessageBox();
msgBox->setGeometry(5000,5000,5000,5000);
QTimer::singleShot(0, this, SLOT(closeMessageBox()));
msgBox->exec();
}
void Widget::solveWasPressed()
{
log->clear();
if (!solved) {
g->runProgram(animation);
solved = true;
}
else
{
string currentBoard = g->m_board_filename + ".txt";
delete g;
g = new Globals(DEBUG,this);
g->init_board(currentBoard);
g->runProgram(animation);
}
}
void Widget::closeMessageBox()
{
msgBox->close();
}
int Widget::Animation() const
{
return animation;
}
void Widget::setAnimation(bool value)
{
animation = value;
}
QLabel *Widget::getLastCell() const
{
return lastCell;
}
void Widget::setLastCell(QLabel *value)
{
lastCell = value;
}
QPair<int, int> *Widget::getLastPair() const
{
return lastPair;
}
void Widget::setLastPair(QPair<int,int> *value)
{
lastPair = value;
}