-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
310 lines (254 loc) · 7.68 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "astar.h"
#include "bestfirstsearch.h"
#include "bfs.h"
#include "dfs.h"
#include "dijkstra.h"
#include "mapdialog.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new GraphicsScene(this);
ui->graphicsView->setScene(scene);
ui->graphicsView->setRenderHint(QPainter::Antialiasing);
ui->graphicsView->setMouseTracking(true);
ui->algoComboBox->addItem("A*");
ui->algoComboBox->addItem("Dijkstra");
ui->algoComboBox->addItem("Best First Search");
ui->algoComboBox->addItem("BFS");
ui->algoComboBox->addItem("DFS");
height = 35;
width = 55;
node.resize(height);
// add nodes in the window
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
node[i].push_back(new GraphNode(i, j, 22, 22));
scene->addItem(node[i][j]);
}
}
// generate neighbours
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
if(i > 0)
node[i][j]->addNeighbour(node[i-1][j]); // top
if(j < width - 1)
node[i][j]->addNeighbour(node[i][j+1]); // right
if(i < height - 1)
node[i][j]->addNeighbour(node[i+1][j]); // bottom
if(j > 0)
node[i][j]->addNeighbour(node[i][j-1]); // left
if(i > 0 && j < width - 1)
node[i][j]->addNeighbour(node[i-1][j+1]); // top-right
if(i < height - 1 && j < width - 1)
node[i][j]->addNeighbour(node[i+1][j+1]); // bottom-right
if(j > 0 && i < height - 1)
node[i][j]->addNeighbour(node[i+1][j-1]); // bottom-left
if(i > 0 && j > 0)
node[i][j]->addNeighbour(node[i-1][j-1]); // top-left
}
}
// default position for start and end nodes
scene->setStartNode(node[2][2]);
scene->setEndNode(node[height-3][width-3]);
startup();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::resetScreen()
{
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
if(node[i][j] == scene->getStartNode() || node[i][j] == scene->getEndNode())
continue;
node[i][j]->resetNode();
}
// update every 'x' milliseconds
QEventLoop loop;
QTimer::singleShot(1, &loop, SLOT(quit()));
loop.exec();
}
scene->setStartNode(scene->getStartNode());
scene->setEndNode(scene->getEndNode());
}
void MainWindow::read(const QJsonObject &json)
{
QString mName = ui->mapComboBox->currentText();
if(json.contains(mName) && json[mName].isArray())
{
QJsonArray jArray = json[mName].toArray();
for(auto arr : jArray)
{
if(arr.isArray())
{
QJsonArray jArr = arr.toArray();
node[jArr[0].toInt()][jArr[1].toInt()]->setObstacle();
}
}
}
}
void MainWindow::write(QJsonObject &json, QString mName) const
{
QJsonArray jArray;
for(int i = 0; i < node.size(); i++)
{
for(int j = 0; j < node[0].size(); j++)
{
QJsonArray jArr;
if(node[i][j]->isObstacle())
{
jArr.append(i);
jArr.append(j);
jArray.append(jArr);
}
}
}
json.insert(mName, jArray);
}
void MainWindow::startup()
{
QFile loadFile(QStringLiteral("save.json"));
if (!loadFile.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open save file.");
return;
}
QByteArray saveData = loadFile.readAll();
QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));
mObj = loadDoc.object();
ui->mapComboBox->addItems(mObj.keys());
}
void MainWindow::on_startAlgorithm_clicked()
{
ui->startAlgorithm->setEnabled(false);
if(!scene->getStartNode() || !scene->getEndNode())
{
qDebug() << "nullptr start/end nodes";
return;
}
// select algorithm to run from comboBox
QString selection = ui->algoComboBox->currentText();
if(selection == QString("A*"))
{
AStar algo;
algo.runAStar(scene->getStartNode(), scene->getEndNode(), node, speed);
ui->nodesBox->setText(QString::number(algo.getTotalNodes()));
}
else if(selection == QString("Dijkstra"))
{
Dijkstra algo;
algo.runDijkstra(scene->getStartNode(), scene->getEndNode(), node, speed);
ui->nodesBox->setText(QString::number(algo.getTotalNodes()));
}
else if(selection == "Best First Search")
{
BestFirstSearch algo;
algo.runBestFirstSearch(scene->getStartNode(), scene->getEndNode(), node, speed);
ui->nodesBox->setText(QString::number(algo.getTotalNodes()));
}
else if(selection == QString("BFS"))
{
BFS algo;
algo.runBFS(scene->getStartNode(), scene->getEndNode(), node, speed);
ui->nodesBox->setText(QString::number(algo.getTotalNodes()));
}
else if(selection == QString("DFS"))
{
DFS algo;
algo.runDFS(scene->getStartNode(), scene->getEndNode(), node, speed);
ui->nodesBox->setText(QString::number(algo.getTotalNodes()));
}
// display cost on ui
if(scene->getEndNode()->getGCost() == INT32_MAX)
ui->costBox->setText(QString("NA"));
else
ui->costBox->setText(QString::number(scene->getEndNode()->getGCost()));
// reconstruct path from end to start
auto ptr = scene->getEndNode();
while(ptr)
{
ptr->setPath();
ptr = ptr->getParent();
// update every 'x' milliseconds
QEventLoop loop;
QTimer::singleShot(speed, &loop, SLOT(quit()));
loop.exec();
}
ui->startAlgorithm->setEnabled(true);
}
void MainWindow::on_clearButton_clicked()
{
resetScreen();
ui->costBox->clear();
ui->nodesBox->clear();
}
void MainWindow::on_spinBox_valueChanged(int arg1)
{
ui->speedSlider->setValue(arg1);
}
void MainWindow::on_speedSlider_valueChanged(int value)
{
speed = value;
ui->spinBox->setValue(value);
}
void MainWindow::on_saveButton_clicked()
{
MapDialog map;
map.setModal(true);
map.exec();
mName = map.getName();
if(mName == "")
return;
if(!mObj.contains(mName))
{
ui->mapComboBox->addItem(mName);
}
else
{
QMessageLogger msg;
msg.warning("duplicate overwritten!");
}
QFile saveFile(QStringLiteral("save.json"));
if (!saveFile.open(QIODevice::WriteOnly))
{
qWarning("Couldn't open save file.");
return;
}
write(mObj, mName);
saveFile.write(QJsonDocument(mObj).toJson(QJsonDocument::Compact));
qDebug() << "saving...";
}
void MainWindow::on_generateButton_clicked()
{
resetScreen();
read(mObj);
}
void MainWindow::on_deleteButton_clicked()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Delete?", "Are you sure you want to delete " + ui->mapComboBox->currentText() + "?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes && ui->mapComboBox->currentText() != QString("Default"))
{
mObj.remove(ui->mapComboBox->currentText());
ui->mapComboBox->removeItem(ui->mapComboBox->currentIndex());
QFile saveFile(QStringLiteral("save.json"));
if (!saveFile.open(QIODevice::WriteOnly))
{
qWarning("Couldn't open save file.");
return;
}
saveFile.write(QJsonDocument(mObj).toJson(QJsonDocument::Compact));
}
}