-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs_bfs_widget.cpp
More file actions
executable file
·198 lines (179 loc) · 7.56 KB
/
Copy pathdfs_bfs_widget.cpp
File metadata and controls
executable file
·198 lines (179 loc) · 7.56 KB
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
#include "dfs_bfs_widget.h"
#include "ui_dfs_bfs_widget.h"
DFS_BFS_Widget::DFS_BFS_Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::DFS_BFS_Widget),
inputDialog(new InputDialog())
{
// Initialize
ui->setupUi(this);
algorithm = "None";
setWindowIcon(QIcon(":/images/dfsbfs.ico"));
//setFont(QFont("Tahoma", 12));// for windows version
setFont(QFont("Ubuntu", 10));// for linux version
setWindowTitle("DFS - BFS Module Widget");
// Input Data signal-slot
QObject::connect(ui->algorithmsBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(setAlgorithm()));
QObject::connect(ui->dataStructures, SIGNAL(buttonClicked(int)), this, SLOT(setDataStructure()));
QObject::connect(ui->graphTypes, SIGNAL(buttonClicked(int)), this, SLOT(setGraphType()));
QObject::connect(ui->loadFileButton, SIGNAL(clicked(bool)), this, SLOT(openFileDialog()));
QObject::connect(this, SIGNAL(gotFilePath(QString)), ui->filePathLineEdit, SLOT(setText(QString)));
QObject::connect(ui->filePathLineEdit, SIGNAL(textChanged(QString)), this, SLOT(setFilePath(QString)));
QObject::connect(this, SIGNAL(callInputDialog(int)), inputDialog.data(), SLOT(setValue(int)));
QObject::connect(inputDialog.data(), SIGNAL(gotAllValues(int,int)), this, SLOT(setStartEnd(int,int)));
// Process signal-slot
QObject::connect(ui->analysisButton, SIGNAL(clicked(bool)), this, SLOT(getAnalysisAlgorithm()));
QObject::connect(ui->sourceCodeButton, SIGNAL(clicked(bool)), this, SLOT(getAppsAlgorithm()));
QObject::connect(ui->travelButton, SIGNAL(clicked(bool)), this, SLOT(travelGraph()));
QObject::connect(ui->connectedComButton, SIGNAL(clicked(bool)), this, SLOT(findConnectedComponents()));
QObject::connect(ui->findWayButton, SIGNAL(clicked(bool)), this, SLOT(getStartEnd()));
QObject::connect(ui->simplifyButton, SIGNAL(clicked(bool)), this, SLOT(simplifyGraph()));
}
DFS_BFS_Widget::~DFS_BFS_Widget()
{
// the compiler will do everyhting for you
}
void DFS_BFS_Widget::setAlgorithm()
{
algorithm = ui->algorithmsBox->currentText();
}
void DFS_BFS_Widget::setDataStructure()
{
dataStructure = ui->dataStructures->checkedButton()->text();
}
void DFS_BFS_Widget::setGraphType()
{
graphType = ui->graphTypes->checkedButton()->text();
}
void DFS_BFS_Widget::setStartEnd(int startValue, int endValue)
{
startVertex = startValue;
endVertex = endValue;
ui->solutionView->clear();
ui->solutionView->setHtml(IOModule::findWayBetween2Vertices(startVertex, endVertex, pass, matrix, totalNumVertices));
}
void DFS_BFS_Widget::setFilePath(QString path)
{
filePath = path;
}
void DFS_BFS_Widget::openFileDialog()
{
QString fileName = QFileDialog::getOpenFileName(nullptr, "Chọn tệp chứa thông tin về đồ thị", QDir::currentPath(), "text file(*.txt)");
if (!fileName.isEmpty())
{
emit gotFilePath(fileName);
}
}
bool DFS_BFS_Widget::checkInputData() const
{
ui->solutionView->clear();
if (algorithm == "None")
{
ui->solutionView->setHtml("<i>Bạn chưa chọn thuật toán.</i>");
return false;
}
else if (filePath.isEmpty())
{
ui->solutionView->setHtml("<i>Bạn chưa chọn tệp chứa thông tin về đồ thị.</i>");
return false;
}
else if (dataStructure.isEmpty())
{
ui->solutionView->setHtml("<i>Bạn chưa chọn kiểu biểu diễn cho đồ thị.</i>");
return false;
}
else if (graphType.isEmpty())
{
ui->solutionView->setHtml("<i>Bạn chưa chọn loại đồ thị.</i>");
return false;
}
else
{
ui->solutionView->setHtml(algorithm);
ui->solutionView->append("Dạng đồ thị: " + dataStructure);
ui->solutionView->append("Loại đồ thị: " + graphType);
return true;
}
}
bool DFS_BFS_Widget::loadGraphData()
{
static QString preFilePath{};
static QString preStructure{};
static QString preGraphType{};
static bool isNotSafe{false};
try
{
if (filePath != preFilePath || dataStructure != preStructure || graphType != preGraphType)
// Anything was changed will cause the program to reload the adjacent matrix
{
preFilePath = filePath;
preStructure = dataStructure;
preGraphType = graphType;
if (dataStructure == "Ma trận kề") IOModule::readAdjaMatrix(filePath, matrix, totalNumVertices, mode::dfs_bfs);
else if (dataStructure == "Danh sách cạnh/cung")
{
if (graphType == "Vô hướng") IOModule::readUGEdgeList(filePath, matrix, totalNumVertices, mode::dfs_bfs);
else IOModule::readDGEdgeList(filePath, matrix, totalNumVertices, mode::dfs_bfs);
}
isNotSafe = false; // without exceptions, it will be safe to start doing something
return true;
}
if (isNotSafe) throw(QString("Hãy cập nhật lại dữ liệu đầu vào!"));
return true;
}catch(const QString error)
{
QMessageBox::critical(this, "Error", error);
isNotSafe = true;
return false; // exceptions
}
}
void DFS_BFS_Widget::getAnalysisAlgorithm() const
{
ui->solutionView->clear();
if (algorithm == "None") ui->solutionView->setHtml("<i>Bạn chưa chọn thuật toán.</i>");
else if (algorithm == "Thuật toán DFS") ui->solutionView->setSource(QUrl::fromLocalFile(":/url/dfs.html"));
else if (algorithm == "Thuật toán BFS") ui->solutionView->setHtml(IOModule::BFS_Analysis());
}
void DFS_BFS_Widget::getAppsAlgorithm() const
{
ui->solutionView->clear();
if (algorithm == "None") ui->solutionView->setHtml("<i>Bạn chưa chọn thuật toán.</i>");
else if (algorithm == "Thuật toán DFS") ui->solutionView->setSource(QUrl::fromLocalFile(":/url/dfs_code.html"));
else if (algorithm == "Thuật toán BFS") ui->solutionView->setHtml(IOModule::BFS_Applications());
}
void DFS_BFS_Widget::getInfoAlgorithm() const
{
// nothing to do here
}
void DFS_BFS_Widget::travelGraph()
{
if (checkInputData() == true && loadGraphData() == true)
{
ui->solutionView->clear();
if (algorithm == "Thuật toán DFS") ui->solutionView->setHtml(IOModule::DFS(pass, matrix, totalNumVertices));
else ui->solutionView->setHtml(IOModule::BFS(pass, matrix, totalNumVertices));
}
}
void DFS_BFS_Widget::findConnectedComponents()
{
if (checkInputData() == true && loadGraphData() == true)
{
ui->solutionView->clear();
ui->solutionView->setHtml(IOModule::checkConectivityUGraph(pass, matrix, totalNumVertices));
//ui->solutionView->setHtml(IOModule::outputMatrix(matrix, totalNumVertices));
}
}
void DFS_BFS_Widget::simplifyGraph()
{
if (checkInputData() == true && loadGraphData() == true)
{
ui->solutionView->clear();
if (algorithm == "Thuật toán DFS") ui->solutionView->setHtml(IOModule::simplifyGraph(pass, matrix, totalNumVertices, IOModule::DFS_1_Simplify));
else ui->solutionView->setHtml(IOModule::simplifyGraph(pass, matrix, totalNumVertices, IOModule::BFS_1_Simplify));
}
}
void DFS_BFS_Widget::getStartEnd()
{
if (checkInputData() == true && loadGraphData() == true)
emit callInputDialog(totalNumVertices);
}