This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
107 lines (91 loc) · 3.28 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
/* TODO: Create a new class, ButtonHandler to handle button clicks */
/* TODO: Create a new class, ActionHandler to handle action */
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
buttonHandler = new ButtonHandler;
QWidget* widget = new QWidget(this);
setCentralWidget(widget);
qDebug() << this->size().width() << ' ' << this->size().height() << '\n';
QHBoxLayout *layout = new QHBoxLayout;
QWidget* widget2 = new QWidget(widget);
QVBoxLayout *subLayout = new QVBoxLayout;
graphWidget = new GraphWidget(this);
/* graphWidget->setStyleSheet("border: 1px solid red"); */
textEdit = new QTextEdit(widget2);
drawButton = new QPushButton("Draw", widget2);
requestEdit = new QTextEdit(widget2);
calculateButton = new QPushButton("Calculate", widget2);
subLayout->addWidget(textEdit);
subLayout->addWidget(drawButton);
subLayout->addWidget(requestEdit);
subLayout->addWidget(calculateButton);
layout->addWidget(widget2, 2);
layout->addWidget(graphWidget, 8);
/* layout->addWidget(widget2, 8); */
widget->setLayout(layout);
widget2->setLayout(subLayout);
setupMenuBar(parent);
connect(drawButton, &QPushButton::released, this, &MainWindow::handleButton);
connect(calculateButton, &QPushButton::released, this, &MainWindow::handleCalculateButton);
graphWidget->draw();
}
void MainWindow::setupMenuBar(QWidget *parent) {
QMenuBar *menuBar = new QMenuBar(parent);
this->setMenuBar(menuBar);
QMenu * fileMenu = this->menuBar()->addMenu(tr("&File"));
QAction* importAction = new QAction("Import graph from street.txt and transport_type.txt");
QAction* requestAction = new QAction("Import query from query.txt");
fileMenu->addAction(importAction);
fileMenu->addAction(requestAction);
connect(importAction, &QAction::triggered, this, &MainWindow::importActionTrigger);
connect(requestAction, &QAction::triggered, this, &MainWindow::requestActionTrigger);
}
MainWindow::~MainWindow()
{
/* delete ui; */
}
void MainWindow::handleButton() {
buttonHandler->draw(textEdit, graphWidget);
}
void MainWindow::handleCalculateButton() {
buttonHandler->calculatePath(requestEdit, graphWidget);
}
void MainWindow::importActionTrigger() {
textEdit->clear();
graphWidget->clear();
ifstream ifs1("street.txt");
ifstream ifs2("transport_type.txt");
if (! ifs1 || ! ifs2) return;
int n;
int w, allow;
ifs1 >> n;
ifs2 >> n;
QString text;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
ifs1 >> w;
ifs2 >> allow;
if (w == 0) continue;
graphWidget->addEdge(i, j, w, allow);
text.append(QString::number(i) + " " + QString::number(j) + " " + QString::number(w) + " " + QString::number(allow) + "\n");
}
}
textEdit->setText(text);
ifs1.close();
ifs2.close();
return;
}
void MainWindow::requestActionTrigger() {
requestEdit->clear();
QString text;
ifstream ifs("query.txt");
if (! ifs) return;
int allow, u, v;
ifs >> allow >> u >> v;
text.append(QString::number(allow) + " " + QString::number(u) + " " + QString::number(v));
ifs.close();
requestEdit->setText(text);
}