-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
267 lines (210 loc) · 9.78 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGlobal>
#include <QTime>
#include <QGraphicsScene>
#include <QGraphicsEllipseItem>
#include <QCoreApplication>
#include <cmath>
#include <QDebug>
#include <QCheckBox>
#include "graphfunctions.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QIcon icon("C:\\Users\\LEGEND\\Documents\\Assignment\\qt.png");
this->setWindowIcon(icon);
this->setStyleSheet("background-color: #9ac2ed;");
// Customize button styles
QString buttonStyle = "QPushButton {"
" background-color: #4CAF50; /* Green background color */"
" border: none; /* No border */"
" color: white; /* White text color */"
" text-decoration: none; /* No underline */"
" cursor: pointer; /* Cursor on hover */"
" border-radius: 2px; /* Rounded corners */"
" box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4); /* Subtle shadow */"
"}"
"QPushButton:hover {"
" background-color: #45a049; /* Darker green on hover */"
"}";
// Customize input box styles
QString inputBoxStyle = "QLineEdit, QTextEdit {"
" border-radius: 5px; /* Rounded corners */"
" background-color: #FFFFFF;"
" box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Subtle shadow */"
"}";
ui->lineEdit_vertices->setStyleSheet(inputBoxStyle);
ui->lineEdit_density->setStyleSheet(inputBoxStyle);
ui->node_input->setStyleSheet(inputBoxStyle);
// Customize adjacency matrix box style
QString adjacencyMatrixStyle = "QTextEdit#textEdit_adjacency_matrix {"
" border-radius: 5px; /* Rounded corners */"
" background-color: #FFFFFF;"
" padding: 5px; /* Padding for better appearance */"
" box-shadow: 0 4px 8px rgba(1, 0, 0, 1); /* Subtle shadow */"
"}";
ui->textEdit_adjacency_matrix->setStyleSheet(adjacencyMatrixStyle);
// Customize graph box style
QString graphBoxStyle = "QGraphicsView#graphicsView {"
" border-radius: 5px; /* Rounded corners */"
" background-color: #FFFFFF;"
" padding: 5px; /* Padding for better appearance */"
" box-shadow: 0 2px 4px rgba(0, 0, 0, 0.6); /* Subtle shadow */"
"}";
ui->graphicsView->setStyleSheet(graphBoxStyle);
// Customize neighbors box style
QString neighborsBoxStyle = "QMessageBox#MessageBox {"
" border: 1px solid #999; /* Thin border */"
" border-radius: 5px; /* Rounded corners */"
" padding: 5px; /* Padding for better appearance */"
" box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Subtle shadow */"
"}";
// Set style for QMessageBox
QMessageBox msgBox;
msgBox.setStyleSheet(neighborsBoxStyle);
ui->btn_generate->setStyleSheet(buttonStyle);
ui->btn_find_neighbors->setStyleSheet(buttonStyle);
connect(ui->btn_generate, &QPushButton::clicked, this, &MainWindow::generateGraph);
connect(ui->btn_find_neighbors, &QPushButton::clicked, this, &MainWindow::findNeighbours);
srand(QTime::currentTime().msec());
// Connect the stateChanged signal of the toggleSwitch to a slot
// connect(ui->toggleSwitch, &QCheckBox::stateChanged, this, &MainWindow::onToggleSwitchStateChanged);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::generateGraph()
{
// Retrieve user input
bool okVertices, okDensity;
numVertices = ui->lineEdit_vertices->text().toInt(&okVertices);
double density = ui->lineEdit_density->text().toDouble(&okDensity);
// std::vector<std::vector<int>> adjacencyMatrix = generateAdjacencyMatrix(numVertices, density);
adjacencyMatrix = generateAdjacencyMatrix(numVertices, density);
QVector<QGraphicsEllipseItem*> vertices;
if (okVertices && okDensity) {
// Create a scene for the graph
// numVertices = ui->lineEdit_vertices->text().toInt(&okVertices);
QGraphicsScene* scene = new QGraphicsScene(this);
qreal centerX = 300;
qreal centerY = 200;
qreal radius = 100;
// Calculate angle increment based on the number of vertices
qreal angleIncrement = 2.0 * M_PI / numVertices;
qreal currentAngle = 0;
// Example: Display vertices in a circular shape
for (int i = 0; i < numVertices; ++i) {
qreal x = centerX + radius * qCos(currentAngle);
qreal y = centerY + radius * qSin(currentAngle);
QGraphicsEllipseItem* vertex = new QGraphicsEllipseItem(x, y, 5,5);
scene->addItem(vertex);
vertices.append(vertex);
// Increment the angle for the next vertex
currentAngle += angleIncrement;
}
// Example: Display edges (connect vertices based on adjacency matrix)
for (int i = 0; i < numVertices; ++i) {
for (int j = i + 1; j < numVertices; ++j) {
if (adjacencyMatrix[i][j] == 1) {
// Get the center points of the vertices
QPointF centerI = vertices[i]->sceneBoundingRect().center();
QPointF centerJ = vertices[j]->sceneBoundingRect().center();
// Draw an edge between vertices i and j
QGraphicsLineItem* edge = new QGraphicsLineItem(centerI.x(), centerI.y(),
centerJ.x(), centerJ.y());
scene->addItem(edge);
}
}
}
QString adjacencyMatrixStr;
for (const auto& row : adjacencyMatrix) {
for (int value : row) {
adjacencyMatrixStr += QString::number(value) + " ";
}
adjacencyMatrixStr += "\n";
}
ui->textEdit_adjacency_matrix->clear();
ui->textEdit_adjacency_matrix->insertPlainText(adjacencyMatrixStr);
int maxDegreeNode = findMaxDegreeNode(adjacencyMatrix);
int minDegreeNode = findMinDegreeNode(adjacencyMatrix);
ui->label_max_degree->setText("Node with maximum degree: " + QString::number(maxDegreeNode));
ui->label_min_degree->setText("Node with minimum degree: " + QString::number(minDegreeNode));
// Set the scene to the QGraphicsView
ui->graphicsView->setScene(scene);
} else {
// Handle invalid input
}
}
int MainWindow::findMaxDegreeNode(const std::vector<std::vector<int>>& adjacencyMatrix)
{
int maxDegree = 0;
int maxDegreeNode = -1;
for (int i = 0; i < adjacencyMatrix.size(); ++i) {
int degree = 0;
for (int j = 0; j < adjacencyMatrix[i].size(); ++j) {
if (adjacencyMatrix[i][j] == 1) {
++degree;
}
}
if (degree > maxDegree) {
maxDegree = degree;
maxDegreeNode = i;
}
}
return maxDegreeNode;
}
int MainWindow::findMinDegreeNode(const std::vector<std::vector<int>>& adjacencyMatrix)
{ int minDegree = std::numeric_limits<int>::max();
int minDegreeNode = -1;
for (int i = 0; i < adjacencyMatrix.size(); ++i) {
int degree = 0;
for (int j = 0; j < adjacencyMatrix[i].size(); ++j) {
if (adjacencyMatrix[i][j] == 1) {
++degree;
}
}
if (degree < minDegree) {
minDegree = degree;
minDegreeNode = i;
}
}
return minDegreeNode;
}
void MainWindow::findNeighbours()
{
// Get the user input from the QLineEdit
QString userInput = ui->node_input->text();
bool ok;
int node = userInput.toInt(&ok);
qDebug() << "Entered node: " << node;
if (!ok) {
QMessageBox::warning(this, "Invalid Input", "Please enter a valid node number.");
return;
}
// Check if the entered node is within the valid range
if (node < 0 || node >= numVertices) {
QMessageBox::warning(this, "Invalid Node", "Please enter a node within the valid range."); //warning shows red message box
return;
}
// Find neighbors of the entered node
QVector<int> neighbors;
for (int i = 0; i < adjacencyMatrix[node].size(); ++i) {
if (adjacencyMatrix[node][i] == 1) {
neighbors.append(i);
}
}
qDebug() << "Neighbors: " << neighbors;
// Display the neighbors
QString neighborsStr = "Neighbors of Node " + QString::number(node) + ": ";
for (int neighbor : neighbors) {
neighborsStr += QString::number(neighbor) + " ";
}
qDebug() << "Neighbors string: " << neighborsStr;
// Display the neighbors in a QMessageBox
QMessageBox::information(this, "Node Neighbors", neighborsStr + "\n\✨by Anns Khan✨");
}