forked from haophancs/simple-graph-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
722 lines (656 loc) · 29.6 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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "graphics/headers/GraphGraphicsView.h"
#include "basis/headers/GraphUtils.h"
#include "widgets/headers/MultiLineInputDialog.h"
#include "widgets/headers/MultiComboboxDialog.h"
#include <QtGui>
#include <QMessageBox>
#include <QTimer>
#include <widgets/headers/GraphOptionDialog.h>
#include "utils/qdebugstream.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
_ui(new Ui::MainWindow) {
setWindowState(Qt::WindowMaximized);
_ui->setupUi(this);
_ui->statusBar->setStyleSheet("color: darkgrey");
_ui->consoleText->setReadOnly(true);
_ui->consoleText->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
_ui->verticalSplitter->setStretchFactor(0, 1);
this->_dataNeedSaving = false;
this->setWindowTitle("Simple Graph Tool");
auto *m = new QSignalMapper(this);
auto *s1 = new QShortcut(QKeySequence("Alt+1"), this);
auto *s2 = new QShortcut(QKeySequence("Alt+2"), this);
auto *s3 = new QShortcut(QKeySequence("Alt+3"), this);
connect(s1, SIGNAL(activated()), m, SLOT(map()));
connect(s2, SIGNAL(activated()), m, SLOT(map()));
connect(s3, SIGNAL(activated()), m, SLOT(map()));
m->setMapping(s1, 0);
m->setMapping(s2, 1);
m->setMapping(s3, 2);
connect(m, SIGNAL(mapped(int)), _ui->tabWidget, SLOT(setCurrentIndex(int)));
QFont btnFont;
btnFont.setPixelSize(32);
_ui->createGraphButton->setFont(btnFont);
_ui->openGraphButton->setFont(btnFont);
this->_graph = new Graph(false, false);
this->_scene = new GraphGraphicsScene(_graph);
this->_view = new GraphGraphicsView();
this->_adjMatrix = new AdjacencyMatrixTable(_graph);
this->_incidenceMatrix = new IncidenceMatrixTable(_graph);
this->_elementPropertiesTable = new ElementPropertiesTable(_graph);
this->_graphPropertiesTable = new GraphPropertiesTable(_graph);
connect(_adjMatrix, SIGNAL(graphChanged()), _scene, SLOT(reload()));
connect(_adjMatrix, SIGNAL(graphChanged()), _incidenceMatrix, SLOT(reload()));
connect(_scene, SIGNAL(graphChanged()), _adjMatrix, SLOT(reload()));
connect(_scene, SIGNAL(graphChanged()), _incidenceMatrix, SLOT(reload()));
connect(this, SIGNAL(graphChanged()), _scene, SLOT(reload()));
connect(this, SIGNAL(graphChanged()), _adjMatrix, SLOT(reload()));
connect(this, SIGNAL(graphChanged()), _incidenceMatrix, SLOT(reload()));
connect(this, SIGNAL(graphChanged()), _view, SLOT(redraw()));
connect(this, SIGNAL(graphChanged()), this, SLOT(onGraphChanged()));
connect(_adjMatrix, SIGNAL(graphChanged()), this, SLOT(onGraphChanged()));
connect(_scene, SIGNAL(graphChanged()), this, SLOT(onGraphChanged()));
_scene->setInterval(_ui->horizontalSlider->value());
connect(_ui->horizontalSlider, &QSlider::valueChanged, _scene, &GraphGraphicsScene::setInterval);
connect(_view, SIGNAL(unSelected()), _elementPropertiesTable, SLOT(onUnSelected()));
connect(this, SIGNAL(graphChanged()), _elementPropertiesTable, SLOT(onGraphChanged()));
connect(this, SIGNAL(graphChanged()), _graphPropertiesTable, SLOT(onGraphChanged()));
connect(_adjMatrix, SIGNAL(graphChanged()), _graphPropertiesTable, SLOT(onGraphChanged()));
connect(_scene, SIGNAL(graphChanged()), _graphPropertiesTable, SLOT(onGraphChanged()));
connect(_view, &GraphGraphicsView::nodeSelected, _elementPropertiesTable, &ElementPropertiesTable::onNodeSelected);
connect(_view, &GraphGraphicsView::edgeSelected, _elementPropertiesTable, &ElementPropertiesTable::onEdgeSelected);
connect(_adjMatrix, &AdjacencyMatrixTable::edgeSelected, _elementPropertiesTable,
&ElementPropertiesTable::onEdgeSelected);
connect(_incidenceMatrix, &IncidenceMatrixTable::edgeSelected, _elementPropertiesTable,
&ElementPropertiesTable::onEdgeSelected);
connect(this, SIGNAL(startDemoAlgorithm(std::list<std::list<std::string> >, GraphDemoFlag)), _scene,
SLOT(demoAlgorithm(std::list<std::list<std::string> >, GraphDemoFlag)));
connect(this, SIGNAL(startDemoAlgorithm(std::list<std::string>, GraphDemoFlag)), _scene,
SLOT(demoAlgorithm(std::list<std::string>, GraphDemoFlag)));
connect(this, SIGNAL(startDemoAlgorithm(std::list<std::pair<std::string, std::string> >, GraphDemoFlag)), _scene,
SLOT(demoAlgorithm(std::list<std::pair<std::string, std::string> >, GraphDemoFlag)));
connect(_view, &GraphGraphicsView::nodeAdded, this, [this](QPointF pos, bool auto_naming) {
if (!auto_naming) {
showNewNodeDialog(pos);
return;
}
this->_graph->addNode(Node(_graph->nextNodeName(), pos));
emit graphChanged();
});
connect(_view, &GraphGraphicsView::nodeRemoved, this, [this](const std::string &node_name) {
if (this->_graph->removeNode(node_name))
emit graphChanged();
});
connect(_view, &GraphGraphicsView::nodeIsolated, this, [this](const std::string &node_name) {
if (this->_graph->isolateNode(node_name))
emit graphChanged();
});
connect(_view, &GraphGraphicsView::edgeRemoved, this, [this](const std::string &uname, const std::string &vname) {
if (_graph->removeEdge(uname, vname))
emit graphChanged();
});
connect(_view, &GraphGraphicsView::edgeSet, this, [this](const std::string &uname, const std::string &vname) {
bool ok{};
int defaultValue = _graph->hasEdge(uname, vname) ? _graph->weight(uname, vname) : 1;
int w = _graph->isWeighted() ?
(QInputDialog::getInt(this, tr("Set weight for edge(")
+ QString::fromStdString(_graph->node(uname)->name()) + ", "
+ QString::fromStdString(_graph->node(vname)->name()) + tr(")"),
"0 <= weight < " + QString::number(INT_MAX),
defaultValue, 1, INT_MAX, 1, &ok))
: 1;
qDebug() << w;
if ((ok || _graph->isUnweighted()) && this->_graph->setEdge(uname, vname, w))
emit graphChanged();
});
connect(_view, &GraphGraphicsView::startAlgorithm, this,
[this](const StartAlgoFlag &algo, const std::string &source_name) {
QDebugStream qout(std::cout, _ui->consoleText);
if (algo == StartAlgoFlag::BFS) {
this->_ui->consoleText->clear();
auto result = GraphUtils::BFSToDemo(this->_graph, source_name);
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
} else if (algo == StartAlgoFlag::DFS) {
this->_ui->consoleText->clear();
auto result = GraphUtils::DFSToDemo(this->_graph, source_name);
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
} else if (algo == StartAlgoFlag::Prim) {
this->_ui->consoleText->clear();
auto result = GraphUtils::Prim(this->_graph, source_name);
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
} else if (algo == StartAlgoFlag::Dijkstra) {
bool ok;
QStringList items;
for (auto node: _graph->nodeList())
items.append(QString::fromStdString(node->name()));
auto goal = QInputDialog::getItem(this, "Target node:", "Name", items, 0, false, &ok);
if (ok) {
if (goal.isNull())
return;
auto target = this->_graph->node(goal.toStdString());
if (!this->_graph->hasNode(target)) {
QMessageBox::critical(this, "Error", tr("No node named ") + goal);
return;
}
this->_ui->consoleText->clear();
auto result = GraphUtils::Dijkstra(this->_graph, source_name, target->name());
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
}
} else if (algo == StartAlgoFlag::AStar) {
bool ok;
QStringList items;
for (auto node: _graph->nodeList())
items.append(QString::fromStdString(node->name()));
auto goal = QInputDialog::getItem(this, "Target node:", "Name", items, 0, false, &ok);
if (ok) {
if (goal.isNull())
return;
auto target = this->_graph->node(goal.toStdString());
if (!this->_graph->hasNode(target)) {
QMessageBox::critical(this, "Error", tr("No node named ") + goal);
return;
}
this->_ui->consoleText->clear();
auto result = GraphUtils::AStar(this->_graph, source_name, target->name());
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
}
} else if (algo == StartAlgoFlag::ST_BFS) {
this->_ui->consoleText->clear();
auto result = GraphUtils::spanningTreeBFS(this->_graph, source_name);
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
} else if (algo == StartAlgoFlag::ST_DFS) {
this->_ui->consoleText->clear();
auto result = GraphUtils::spanningTreeDFS(this->_graph, source_name);
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
}
});
connect(_view, &GraphGraphicsView::nodeEdited, this, [this](const std::string &node_name) {
bool ok;
QRegExp re("[a-zA-Z0-9]{1,30}");
auto new_name = QInputDialog::getText(this, "Rename node", "Name: ", QLineEdit::Normal,
QString::fromStdString(_graph->nextNodeName()), &ok);
if (ok) {
if (!re.exactMatch(new_name)) {
QMessageBox::critical(this, "Error",
tr("Node's name contains only alphabetical or numeric characters\n")
+ tr("Length of the name mustn't be greater than 30 or smaller than 1"));
return;
}
if (this->_graph->hasNode(new_name.toStdString()))
QMessageBox::critical(this, "Error", "This name has been used by another node");
else {
this->_graph->setNodeName(node_name, new_name.toStdString());
emit graphChanged();
}
}
});
_ui->adjMatLayout->addWidget(this->_adjMatrix, 0, Qt::AlignCenter);
_ui->incMatLayout->addWidget(this->_incidenceMatrix, 0, Qt::AlignCenter);
auto gLabel = new QLabel(this);
gLabel->setText("Graph properties");
_ui->propertiesLayout->addWidget(gLabel);
_ui->propertiesLayout->addWidget(this->_graphPropertiesTable, 0, Qt::AlignTop);
auto eLabel = new QLabel(this);
eLabel->setText("Selected element properties");
eLabel->setContentsMargins(0, 12, 0, 0);
_ui->propertiesLayout->addWidget(eLabel);
_ui->propertiesLayout->addWidget(this->_elementPropertiesTable, 0, Qt::AlignTop);
this->_view->setScene(this->_scene);
_ui->visualLayout->addWidget(this->_view);
this->_view->show();
setWorkspaceEnabled(false);
}
void MainWindow::resetGraph(Graph *graph) {
delete this->_graph;
this->_graph = graph;
this->_scene->setGraph(_graph);
this->_adjMatrix->setGraph(_graph);
this->_incidenceMatrix->setGraph(_graph);
this->_elementPropertiesTable->setGraph(_graph);
this->_graphPropertiesTable->setGraph(_graph);
_ui->coloringBtn->setVisible(_graph->isUndirected());
_ui->actionColoring->setVisible(_graph->isUndirected());
_ui->topoSortBtn->setVisible(_graph->isDirected());
_ui->cyclesBtn->setVisible(_graph->isUndirected());
_ui->actionTopo_Sorting->setVisible(_graph->isDirected());
_ui->weaklyConnectedBtn->setVisible(_graph->isDirected());
_ui->actionFind_weakly_connected_components->setVisible(_graph->isDirected());
_ui->connectedComponentsBtn->setText(_graph->isDirected() ?
"Find strongly connected components"
: "Find connected components");
_ui->actionFind_connected_components->setText(_graph->isDirected() ?
"Find strongly connected components"
: "Find connected components");
}
void MainWindow::initWorkspace(const QString &filename, bool new_file) {
try {
if (!new_file) {
if (this->_dataNeedSaving) {
QMessageBox::StandardButton reply = QMessageBox::question(this, "Save Graph?",
"Your changes will be lost if you don't save them!",
QMessageBox::No | QMessageBox::Yes |
QMessageBox::Cancel);
if (reply == QMessageBox::Cancel)
return;
}
resetGraph(new Graph(Graph::readFromFile(filename.toStdString())));
this->_dataNeedSaving = false;
} else {
bool weighted, directed, ok;
int node_num;
GraphOptionDialog::initGraph(this, weighted, directed, node_num, ok);
if (!ok) return;
this->_dataNeedSaving = true;
resetGraph(new Graph(node_num, directed, weighted));
}
emit graphChanged();
}
catch (...) {
setWorkspaceEnabled(false);
QMessageBox::critical(this, "Error",
"Something is wrong with this file",
QMessageBox::Cancel);
if (_workingFilename != "") setWorkspaceEnabled(true);
return;
}
this->_workingFilename = filename;
int index = filename.toStdString().find_last_of("/\\");
std::string input_trace_filename = filename.toStdString().substr(index + 1);
setWindowTitle(QString::fromStdString(input_trace_filename) + " - Simple Graph Tool");
setWorkspaceEnabled(true);
}
MainWindow::~MainWindow() {
delete _ui;
delete _graph;
delete _scene;
delete _view;
delete _elementPropertiesTable;
}
void MainWindow::closeEvent(QCloseEvent *event) {
if (_dataNeedSaving) {
QMessageBox::StandardButton reply = QMessageBox::question(this, "Save Graph?",
"Your changes will be lost if you don't save them!",
QMessageBox::No | QMessageBox::Yes |
QMessageBox::Cancel);
if (reply == QMessageBox::Yes)
Graph::writeToFile(_workingFilename.toStdString(), *_graph);
else if (reply == QMessageBox::Cancel)
event->ignore();
}
}
QString MainWindow::showOpenFileDialog() {
return QFileDialog::getOpenFileName(
this,
tr("Open Document"),
QDir::currentPath(),
tr("Graph files (*.gph)"),
nullptr,
QFileDialog::DontUseNativeDialog);
}
QString MainWindow::showSaveFileDialog() {
QString newFilename;
for (int i = 1; true; ++i) {
std::ifstream is(QDir::currentPath().toStdString()
+ "/graph" + std::to_string(i) + ".gph");
if (!is.good()) {
newFilename = QString::fromStdString(QDir::currentPath().toStdString()
+ "/graph" + std::to_string(i) + ".gph");
break;
}
}
return QFileDialog::getSaveFileName(this, tr("New Graph"),
newFilename,
tr("Graph files (*.gph)"),
nullptr,
QFileDialog::DontUseNativeDialog);
}
void MainWindow::showNewNodeDialog(QPointF pos) {
bool ok;
QRegExp re("[a-zA-Z0-9]{1,3}");
QString newNodeName = QInputDialog::getText(this, "Add new node", "Name: ", QLineEdit::Normal,
QString::fromStdString(_graph->nextNodeName()), &ok);
if (ok) {
if (!re.exactMatch(newNodeName)) {
QMessageBox::critical(this, "Error", tr("Node's name contains only alphabetical or numeric characters\n")
+
tr("Length of the name mustn't be greater than 3 or smaller than 1"));
return;
}
Node newNode(newNodeName.toStdString(), pos);
bool succeeded = _graph->addNode(newNode);
if (!succeeded)
QMessageBox::critical(this, "Error", "This name has been used by another node");
else
emit graphChanged();
}
}
void MainWindow::setWorkspaceEnabled(bool ready) {
if (ready) _ui->entryWidget->setVisible(false);
_ui->workingWidget->setVisible(ready);
_ui->menuGraph->setEnabled(ready);
_ui->menuAlgorithms->setEnabled(ready);
for (auto action: _ui->menuFile->actions())
if (!action->menu() && !action->isSeparator()
&& action->text().contains("Save"))
action->setEnabled(ready);
if (ready)
_view->scale(1, 1);
}
void MainWindow::onGraphChanged() {
this->_dataNeedSaving = true;
_ui->statusBar->clearMessage();
_ui->consoleText->clear();
}
void MainWindow::on_createGraphButton_clicked() {
QString filename = showSaveFileDialog();
if (!filename.isNull())
initWorkspace(filename, true);
}
void MainWindow::on_openGraphButton_clicked() {
QString filename = showOpenFileDialog();
if (!filename.isNull())
initWorkspace(filename);
}
void MainWindow::on_actionSave_triggered() {
if (_dataNeedSaving) {
this->_dataNeedSaving = false;
Graph::writeToFile(_workingFilename.toStdString(), *_graph);
_ui->statusBar->showMessage("Saved successfully");
QTimer::singleShot(2000, this, [this]() {
this->_ui->statusBar->clearMessage();
});
}
}
void MainWindow::on_actionSave_As_triggered() {
QString filename = showSaveFileDialog();
if (!filename.isNull())
Graph::writeToFile(_workingFilename.toStdString(), *_graph);
}
void MainWindow::on_actionNew_Graph_triggered() {
QString filename = showSaveFileDialog();
if (!filename.isNull())
initWorkspace(filename, true);
}
void MainWindow::on_actionOpen_Graph_triggered() {
QString filename = showOpenFileDialog();
if (!filename.isNull() && filename != _workingFilename)
initWorkspace(filename);
}
void MainWindow::on_actionCredits_triggered() {
QMessageBox::about(this, "Credit", "Author: Hao Phan Phu - KHTN2018 - UIT");
}
void MainWindow::on_actionExit_triggered() {
QApplication::exit(0);
}
void MainWindow::on_actionAddNode_triggered() {
showNewNodeDialog();
}
void MainWindow::on_actionAddEdge_triggered() {
bool ok{};
QList<QString> labelText;
labelText.push_back("From node: ");
labelText.push_back("To node: ");
labelText.push_back("Weight: ");
QList<QString> list = MultiLineInputDialog::getStrings(this, "Add new edge", labelText, &ok);
QRegExp re("\\d*");
if (ok && !list.empty() && re.exactMatch(list[2])) {
bool succeeded = _graph->setEdge(list[0].toStdString(), list[1].toStdString(), list[2].toInt());
if (succeeded)
emit graphChanged();
else
QMessageBox::critical(this, "Error", "Cannot set this edge!");
}
}
void MainWindow::on_actionEditEdge_triggered() {
bool ok{};
QList<QString> labelText;
labelText.push_back("From node: ");
labelText.push_back("To node: ");
labelText.push_back("Weight: ");
QList<QString> list = MultiLineInputDialog::getStrings(this, "Edit edge", labelText, &ok);
QRegExp re("\\d*");
if (ok && !list.empty() && re.exactMatch(list[2])) {
if (_graph->hasEdge(list[0].toStdString(), list[1].toStdString())) {
bool succeeded = _graph->setEdge(list[0].toStdString(), list[1].toStdString(), list[2].toInt());
if (succeeded) {
emit graphChanged();
return;
}
}
QMessageBox::critical(this, "Error", "There's no edge like this!");
}
}
void MainWindow::on_actionDelNode_triggered() {
bool ok;
QString nameToDel = QInputDialog::getText(this, "Delete node", "Name: ", QLineEdit::Normal, QString(), &ok);
if (ok) {
bool succeeded = _graph->removeNode(nameToDel.toStdString());
if (!succeeded)
QMessageBox::critical(this, "Error", "There's no node like this!");
else
emit graphChanged();
}
}
void MainWindow::on_actionDelEdge_triggered() {
bool ok{};
QList<QString> labelText;
labelText.push_back("From node: ");
labelText.push_back("To node: ");
QList<QString> list = MultiLineInputDialog::getStrings(this, "Delete edge", labelText, &ok);
if (ok && !list.empty()) {
bool succeeded = _graph->removeEdge(list[0].toStdString(), list[1].toStdString());
if (succeeded)
emit graphChanged();
else
QMessageBox::critical(this, "Error", "There's no edge like this!");
}
}
void MainWindow::on_articulationNodeBtn_clicked() {
_ui->consoleText->clear();
QDebugStream qout(std::cout, _ui->consoleText);
auto result = GraphUtils::displayArticulationNodes(_graph);
emit startDemoAlgorithm(result, GraphDemoFlag::OnlyNode);
}
void MainWindow::on_bridgesBtn_clicked() {
_ui->consoleText->clear();
QDebugStream qout(std::cout, _ui->consoleText);
auto result = GraphUtils::displayBridges(_graph);
emit startDemoAlgorithm(result, GraphDemoFlag::OnlyEdge);
}
void MainWindow::on_coloringBtn_clicked() {
_ui->consoleText->clear();
QDebugStream qout(std::cout, _ui->consoleText);
auto result = GraphUtils::displayColoring(_graph);
emit startDemoAlgorithm(result, GraphDemoFlag::Coloring);
}
void MainWindow::on_weaklyConnectedBtn_clicked() {
_ui->consoleText->clear();
QDebugStream qout(std::cout, _ui->consoleText);
auto result = GraphUtils::displayConnectedComponents(_graph, false);
emit startDemoAlgorithm(result, GraphDemoFlag::Component);
}
void MainWindow::on_connectedComponentsBtn_clicked() {
_ui->consoleText->clear();
QDebugStream qout(std::cout, _ui->consoleText);
auto result = GraphUtils::displayConnectedComponents(_graph);
emit startDemoAlgorithm(result, GraphDemoFlag::Component);
}
void MainWindow::on_cyclesBtn_clicked() {
_ui->consoleText->clear();
QDebugStream qout(std::cout, _ui->consoleText);
auto result = GraphUtils::displayAllCycles(_graph);
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
}
void MainWindow::on_dijkstraBtn_clicked() {
_ui->consoleText->clear();
QDebugStream qout(std::cout, _ui->consoleText);
bool ok{};
QStringList labels;
labels << "From node: " << "To node: ";
QList<QStringList> itemLists;
QStringList items;
for (auto node: _graph->nodeList())
items << QString::fromStdString(node->name());
itemLists << items << items;
QList<QString> replies = MultiComboboxDialog::getItems(this, "Dijkstra", labels, itemLists, &ok);
if (ok) {
replies[0] = replies[0].trimmed();
replies[1] = replies[1].trimmed();
if (replies[0].isNull() || replies[1].isNull() || replies[0] == replies[1])
return;
auto startNode = _graph->node(replies[0].toStdString());
auto endNode = _graph->node(replies[1].toStdString());
if (!_graph->hasNode(startNode)) {
QMessageBox::critical(this, "Error", tr("No node named ") + replies[0]);
return;
}
if (!_graph->hasNode(endNode)) {
QMessageBox::critical(this, "Error", tr("No node named ") + replies[1]);
return;
}
auto result = GraphUtils::Dijkstra(_graph, startNode->name(), endNode->name());
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
}
}
void MainWindow::on_aStarBtn_clicked() {
_ui->consoleText->clear();
QDebugStream qout(std::cout, _ui->consoleText);
bool ok{};
QStringList labels;
labels << "From node: " << "To node: ";
QList<QStringList> itemLists;
QStringList items;
for (auto node: _graph->nodeList())
items << QString::fromStdString(node->name());
itemLists << items << items;
QList<QString> replies = MultiComboboxDialog::getItems(this, "Dijkstra", labels, itemLists, &ok);
if (ok) {
replies[0] = replies[0].trimmed();
replies[1] = replies[1].trimmed();
if (replies[0].isNull() || replies[1].isNull() || replies[0] == replies[1])
return;
auto startNode = _graph->node(replies[0].toStdString());
auto endNode = _graph->node(replies[1].toStdString());
if (!_graph->hasNode(startNode)) {
QMessageBox::critical(this, "Error", tr("No node named ") + replies[0]);
return;
}
if (!_graph->hasNode(endNode)) {
QMessageBox::critical(this, "Error", tr("No node named ") + replies[1]);
return;
}
auto result = GraphUtils::AStar(_graph, startNode->name(), endNode->name());
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
}
}
void MainWindow::on_topoSortBtn_clicked() {
_ui->consoleText->clear();
QDebugStream qout(std::cout, _ui->consoleText);
auto result = GraphUtils::displayTopoSort(_graph);
emit startDemoAlgorithm(result, GraphDemoFlag::OnlyNode);
}
void MainWindow::on_BFSbtn_clicked() {
_ui->consoleText->clear();
bool ok{};
QStringList items;
for (auto node: _graph->nodeList())
items.append(QString::fromStdString(node->name()));
auto source_str = QInputDialog::getItem(this, "Source node:", "Name", items, 0, false, &ok);
if (ok) {
if (source_str.isNull())
return;
auto source = _graph->node(source_str.toStdString());
if (_graph->hasNode(source)) {
QDebugStream qout(std::cout, _ui->consoleText);
auto result = GraphUtils::BFSToDemo(_graph, source->name());
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
} else {
QMessageBox::critical(this, "Error", tr("No node named ") + source_str);
}
}
}
void MainWindow::on_DFSbtn_clicked() {
_ui->consoleText->clear();
bool ok{};
QStringList items;
for (auto node: _graph->nodeList())
items.append(QString::fromStdString(node->name()));
auto source_str = QInputDialog::getItem(this, "Source node:", "Name", items, 0, false, &ok);
if (ok) {
if (source_str.isNull())
return;
auto source = _graph->node(source_str.toStdString());
if (_graph->hasNode(source)) {
QDebugStream qout(std::cout, _ui->consoleText);
auto result = GraphUtils::DFSToDemo(_graph, source->name());
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
} else
QMessageBox::critical(this, "Error", tr("No node named ") + source_str);
}
}
void MainWindow::on_EulerBtn_clicked() {
_ui->consoleText->clear();
QDebugStream qout(std::cout, _ui->consoleText);
auto result = GraphUtils::displayAllEulerianCircuits(_graph);
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
}
void MainWindow::on_HamiltonBtn_clicked() {
_ui->consoleText->clear();
QDebugStream qout(std::cout, _ui->consoleText);
auto result = GraphUtils::displayAllHamiltonianCircuits(_graph);
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
}
void MainWindow::on_spanningTreeBtn_clicked() {
_ui->consoleText->clear();
QDebugStream qout(std::cout, _ui->consoleText);
auto result = GraphUtils::Prim(_graph);
emit startDemoAlgorithm(result, GraphDemoFlag::EdgeAndNode);
}
void MainWindow::on_actionBFS_triggered() {
on_BFSbtn_clicked();
}
void MainWindow::on_actionDFS_triggered() {
on_DFSbtn_clicked();
}
void MainWindow::on_actionColoring_triggered() {
on_coloringBtn_clicked();
}
void MainWindow::on_actionTopo_Sorting_triggered() {
on_topoSortBtn_clicked();
}
void MainWindow::on_actionEuler_Cycle_triggered() {
on_EulerBtn_clicked();
}
void MainWindow::on_actionHamiltonian_Cycle_triggered() {
on_HamiltonBtn_clicked();
}
void MainWindow::on_actionDijkstra_triggered() {
on_dijkstraBtn_clicked();
}
void MainWindow::on_actionA_star_triggered() {
on_aStarBtn_clicked();
}
void MainWindow::on_actionFind_all_cycles_triggered() {
on_cyclesBtn_clicked();
}
void MainWindow::on_actionFind_all_bridges_triggered() {
on_bridgesBtn_clicked();
}
void MainWindow::on_actionFind_all_Articulation_nodes_triggered() {
on_articulationNodeBtn_clicked();
}
void MainWindow::on_actionFind_connected_components_triggered() {
on_connectedComponentsBtn_clicked();
}
void MainWindow::on_actionFInd_minimum_spanning_tree_triggered() {
on_spanningTreeBtn_clicked();
}
void MainWindow::on_actionFind_weakly_connected_components_triggered() {
on_weaklyConnectedBtn_clicked();
}
void MainWindow::on_tabWidget_currentChanged(int index) {
_elementPropertiesTable->onUnSelected();
}