-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
262 lines (205 loc) · 10.2 KB
/
mainwindow.cpp
File metadata and controls
262 lines (205 loc) · 10.2 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Backend.h"
#include <QMessageBox>
#include <QStringListModel>
#include <QCompleter>
#include <QDebug>
#include <QElapsedTimer>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, backend(new Backend())
, airportListModel(new QStringListModel(this))
{
ui->setupUi(this);
ui->totalDistanceLabel->setTextFormat(Qt::RichText);
ui->totalTimeLabel->setTextFormat(Qt::RichText);
ui->totalDistanceLabel->setStyleSheet("color: white; font-weight: bold;");
ui->totalTimeLabel->setStyleSheet("color: white; font-weight: bold;");
setWindowTitle("Optimised Flight Path Finder for Domestic Routes");
setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);
QWidget *central = new QWidget(this);
QVBoxLayout *outerLayout = new QVBoxLayout(central);
outerLayout->setContentsMargins(30, 50, 100, 50); // Slight left padding to avoid image
// 10px left margin
// --- Top Section (Title + Description) ---
QLabel *titleLabel = new QLabel("Optimized Flight Path Finder");
titleLabel->setStyleSheet("font-size: 20px; font-weight: bold; color: black;");
//titleLabel->setAlignment(Qt::AlignCenter);
titleLabel->setAlignment(Qt::AlignLeft);
QLabel *descLabel = new QLabel(
"<b>An optimized flight path finder for domestic routes across India,<br>"
"designed to calculate the most efficient route between airports<br>"
"using Dijkstra and A* algorithms.</b>");
descLabel->setStyleSheet("color: black;");
descLabel->setAlignment(Qt::AlignLeft);
descLabel->setWordWrap(true);
// --- Center Form Layout ---
QVBoxLayout *formLayout = new QVBoxLayout();
//formLayout->setAlignment(Qt::AlignCenter);
formLayout->setAlignment(Qt::AlignLeft);
formLayout->setSpacing(12); // spacing between widgets
formLayout->setContentsMargins(20, 0, 400, 0);
formLayout->addWidget(ui->algorithmComboBox);
formLayout->addWidget(ui->resultTextEdit);
formLayout->addWidget(ui->startAirportLineEdit);
formLayout->addWidget(ui->endAirportLineEdit);
formLayout->addWidget(ui->findPathButton);
// --- Add everything to the outer layout ---
outerLayout->addWidget(titleLabel);
outerLayout->addWidget(descLabel);
outerLayout->addLayout(formLayout);
outerLayout->addStretch(); // pushes form to vertical center
// --- Info Box for Total Distance and Time ---
QFrame *infoFrame = new QFrame();
infoFrame->setStyleSheet(
"background-color: rgba(0, 0, 0, 160);" // Semi-transparent black
"border: 2px solid white;"
"border-radius: 10px;"
);
infoFrame->setFrameShape(QFrame::StyledPanel);
infoFrame->setFrameShadow(QFrame::Raised);
// Use rich text for both labels
ui->totalDistanceLabel->setTextFormat(Qt::RichText);
ui->totalTimeLabel->setTextFormat(Qt::RichText);
// Set initial placeholder text (white + bold)
ui->totalDistanceLabel->setText("<span style='color:white; '>Total Distance:</span>");
ui->totalTimeLabel->setText("<span style='color:white;'>Total Time:</span>");
QVBoxLayout *infoLayout = new QVBoxLayout(infoFrame);
infoLayout->setContentsMargins(10, 10, 10, 10);
infoLayout->addWidget(ui->totalDistanceLabel);
infoLayout->addWidget(ui->totalTimeLabel);
// Add to form layout
formLayout->addWidget(infoFrame);
this->setCentralWidget(central);
// Load the background image once and scale it
QPixmap bkgnd("C:/Users/anshu/OneDrive/Desktop/AirportsProject/background.jpg");
if (bkgnd.isNull()) {
qDebug() << "Failed to load background image!";
} else {
// Scale with KeepAspectRatioByExpanding to fill window nicely
bkgnd = bkgnd.scaled(this->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
QPalette palette;
palette.setBrush(QPalette::Window, bkgnd);
this->setAutoFillBackground(true);
this->setPalette(palette);
}
// Set stylesheets for controls
ui->algorithmComboBox->setStyleSheet(
"QComboBox { background-color: #2c2c2c; color: white; border: 1px solid #555; padding: 5px; }"
"QComboBox::drop-down { border-left: 1px solid #555; }"
"QComboBox QAbstractItemView { background-color: #2c2c2c; selection-background-color: #555; }"
);
ui->resultTextEdit->setStyleSheet(
"QTextEdit { background-color: #1e1e1e; color: #dcdcdc; border: 1px solid #444; padding: 5px; }"
);
ui->startAirportLineEdit->setStyleSheet(
"QLineEdit { background-color: #1e1e1e; color: white; border: 1px solid #555; border-radius: 4px; padding: 5px; }"
);
ui->endAirportLineEdit->setStyleSheet(
"QLineEdit { background-color: #1e1e1e; color: white; border: 1px solid #555; border-radius: 4px; padding: 5px; }"
);
ui->findPathButton->setStyleSheet(
"QPushButton { background-color: #007acc; color: white; border-radius: 6px; padding: 6px 12px; }"
"QPushButton:hover { background-color: #005f99; }"
);
ui->totalDistanceLabel->setStyleSheet("QLabel { font-weight: bold; font-size: 14px; margin-top: 10px; color: black; }");
ui->totalTimeLabel->setStyleSheet("QLabel { font-weight: bold; font-size: 14px; margin-top: 10px; color: black; }");
// Check if backend loaded data successfully
if (!backend->isDataLoaded()) {
QMessageBox::critical(this, "Error", "Failed to load airport connection data.");
return;
}
// Prepare airport names for the completers
const auto& airportNamesMap = backend->getAirportNames();
QStringList airportNames;
for (const auto& pair : airportNamesMap) {
airportNames << QString::fromStdString(pair.second + " (" + pair.first + ")");
}
airportNames.sort();
airportListModel->setStringList(airportNames);
QCompleter* completerStart = new QCompleter(airportListModel, this);
completerStart->setCaseSensitivity(Qt::CaseInsensitive);
ui->startAirportLineEdit->setCompleter(completerStart);
ui->startAirportLineEdit->setPlaceholderText("Start Airport (e.g., Indira Gandhi Intl (DEL))");
QCompleter* completerEnd = new QCompleter(airportListModel, this);
completerEnd->setCaseSensitivity(Qt::CaseInsensitive);
ui->endAirportLineEdit->setCompleter(completerEnd);
ui->endAirportLineEdit->setPlaceholderText("End Airport (e.g., Chhatrapati Shivaji Intl (BOM))");
ui->algorithmComboBox->addItem("Dijkstra");
ui->algorithmComboBox->addItem("A*");
// Tooltips for better UX
ui->startAirportLineEdit->setToolTip("Type the starting airport (IATA code or full name)");
ui->endAirportLineEdit->setToolTip("Type the destination airport");
ui->algorithmComboBox->setToolTip("Choose the pathfinding algorithm");
ui->findPathButton->setToolTip("Find the shortest path between airports");
ui->resultTextEdit->setReadOnly(true);
connect(ui->findPathButton, &QPushButton::clicked, this, &MainWindow::findPathButtonClicked);
}
MainWindow::~MainWindow()
{
delete ui;
delete backend;
delete airportListModel;
}
QString MainWindow::extractIataCode(const QString& airportText) const
{
QString trimmed = airportText.trimmed();
int startIndex = trimmed.lastIndexOf('(');
int endIndex = trimmed.lastIndexOf(')');
if (startIndex != -1 && endIndex != -1 && startIndex < endIndex) {
return trimmed.mid(startIndex + 1, endIndex - startIndex - 1).trimmed();
} else if (trimmed.length() == 3 && trimmed.toUpper() == trimmed) {
return trimmed;
}
return "";
}
void MainWindow::findPathButtonClicked()
{
QString startAirportText = ui->startAirportLineEdit->text();
QString endAirportText = ui->endAirportLineEdit->text();
QString selectedAlgorithm = ui->algorithmComboBox->currentText();
std::string startCode = extractIataCode(startAirportText).toStdString();
std::string endCode = extractIataCode(endAirportText).toStdString();
qDebug() << "Start Code:" << QString::fromStdString(startCode);
qDebug() << "End Code:" << QString::fromStdString(endCode);
if (startCode.empty() || endCode.empty()) {
QMessageBox::warning(this, "Input Error", "Please enter valid airport names or IATA codes.");
return;
}
std::pair<std::vector<std::string>, int> result;
QElapsedTimer timer;
timer.start();
if (selectedAlgorithm == "Dijkstra") {
result = backend->dijkstra(startCode, endCode);
} else if (selectedAlgorithm == "A*") {
result = backend->aStar(startCode, endCode);
} else {
QMessageBox::warning(this, "Algorithm Error", "Please select a valid algorithm.");
return;
}
qint64 elapsedNano = timer.nsecsElapsed(); // nanoseconds
double elapsedMs = elapsedNano / 1e6; // milliseconds
const auto& path = result.first;
int distance = result.second;
if (path.empty() || distance == -1) {
ui->resultTextEdit->setText("No path found.");
ui->totalDistanceLabel->setText("<span style='color:white; font-weight:bold;'>Total Distance: No path found</span>");
ui->totalTimeLabel->setText("<span style='color:white; font-weight:bold;'>Total Time: "
+ QString::number(elapsedMs, 'f', 3) + " ms</span>");
return;
}
QString pathText;
for (size_t i = 0; i < path.size(); ++i) {
pathText += QString::fromStdString(backend->getAirportNames().at(path[i])) + " (" + QString::fromStdString(path[i]) + ")";
if (i < path.size() - 1) {
pathText += " -> ";
}
}
ui->resultTextEdit->setText("Path: " + pathText);
ui->totalDistanceLabel->setText("<span style='color:white;'>Total Distance: "
+ QString::number(distance) + " km</span>");
ui->totalTimeLabel->setText("<span style='color:white;'>Total Time: "
+ QString::number(elapsedMs, 'f', 3) + " ms</span>");
}