-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
254 lines (231 loc) · 8.85 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
// 初始化主窗口
ui->setupUi(this);
this->setWindowTitle("重庆地铁线路查询系统");
// 初始化路线显示窗口
ui->routeTextBrowser->setTextBackgroundColor(Qt::white);
ui->routeTextBrowser->setTextColor(Qt::black);
ui->routeTextBrowser->setStyleSheet("background-color:white;");
// 初始化地图显示窗口
myView = new Graphics_view_zoom(ui->graphicsView);
myView->set_modifiers(Qt::NoModifier);
ui->graphicsView->setRenderHint(QPainter::Antialiasing);
scene = new QGraphicsScene;
scene->setSceneRect(-LINE_INFO_WIDTH, 0, SCENE_WIDTH, SCENE_HEIGHT);
ui->graphicsView->setScene(scene);
ui->graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);
ui->graphicsView->setBackgroundBrush(Qt::white);
// 初始化后台地铁线路图
subwayGraph = new Graph;
bool flag = subwayGraph->readFileData(":/data/subway.txt");
if (!flag) {
QMessageBox box;
box.setWindowTitle("Error reading subway line data");
box.setText("读取地铁线路数据错误!");
box.addButton("确定", QMessageBox::AcceptRole);
if (box.exec() == QMessageBox::Accepted) {
box.close();
}
}
// 连接信号和槽函数
connect(ui->startLineComboBox,
&QComboBox::currentTextChanged,
this,
&MainWindow::transferStartLineChanged);
connect(ui->dstLineComboBox,
&QComboBox::currentTextChanged,
this,
&MainWindow::transferDstLineChanged);
connect(ui->allLineButton, &QPushButton::clicked, this, &MainWindow::showMap);
connect(ui->transferButton, &QPushButton::clicked, this, &MainWindow::transferQuery);
updateTranserQueryInfo(); // 更新换乘选择信息
showMap(); // 显示总地铁线路图
}
MainWindow::~MainWindow()
{
delete ui;
delete myView;
delete scene;
delete subwayGraph;
}
// 将站点的经纬度地理坐标转为视图坐标
QPointF MainWindow::transferCoord(QPointF coord)
{
QPointF minCoord = subwayGraph->getMinCoord();
QPointF maxCoord = subwayGraph->getMaxCoord();
double x = (coord.x() - minCoord.x()) / (maxCoord.x() - minCoord.x()) * NET_WIDTH + MARGIN;
double y = (maxCoord.y() - coord.y()) / (maxCoord.y() - minCoord.y()) * NET_HEIGHT + MARGIN;
return QPointF(x, y);
}
// 计算线路混合颜色
QColor MainWindow::getLinesColor(const QList<int> &linesList)
{
QColor color1 = QColor(255, 255, 255), color2;
for (int i = 0; i < linesList.size(); ++i) {
color2 = subwayGraph->getLineColor(linesList[i]);
color1.setRed(color1.red() * color2.red() / 255);
color1.setGreen(color1.green() * color2.green() / 255);
color1.setBlue(color1.blue() * color2.blue() / 255);
}
return color1;
}
// 获得线路表的名字集
QString MainWindow::getLinesName(const QList<int> &linesList)
{
QString str = "\t";
for (int i = 0; i < linesList.size(); ++i) {
str += " " + subwayGraph->getLineName(linesList[i]);
}
return str;
}
// 绘制网络图的边
void MainWindow::drawEdges(const QList<Edge> &edgesList)
{
for (int i = 0; i < edgesList.size(); ++i) {
int s1 = edgesList[i].first;
int s2 = edgesList[i].second;
QList<int> linesList = subwayGraph->getCommonLines(s1, s2);
QColor color = getLinesColor(linesList);
QString tip = "途经:" + subwayGraph->getStationName(s1) + "--"
+ subwayGraph->getStationName(s2) + "\n线路:" + getLinesName(linesList);
QPointF s1Pos = transferCoord(subwayGraph->getStationCoord(s1));
QPointF s2Pos = transferCoord(subwayGraph->getStationCoord(s2));
QGraphicsLineItem *edgeItem = new QGraphicsLineItem;
edgeItem->setPen(QPen(color, EDGE_PEN_WIDTH));
edgeItem->setCursor(Qt::PointingHandCursor);
edgeItem->setToolTip(tip);
edgeItem->setPos(s1Pos);
edgeItem->setLine(0, 0, s2Pos.x() - s1Pos.x(), s2Pos.y() - s1Pos.y());
scene->addItem(edgeItem);
}
}
// 绘制网络图的站点节点
void MainWindow::drawStations(const QList<int> &stationsList)
{
for (auto s = stationsList.begin(); s != stationsList.end(); s++) {
QString name = subwayGraph->getStationName(*s);
QList<int> linesList = subwayGraph->getStationLinesInfo(*s);
QColor color = getLinesColor(linesList);
QPointF coord = transferCoord(subwayGraph->getStationCoord(*s));
QString tip = "站名:" + name + "\n途径线路:" + getLinesName(linesList);
QGraphicsEllipseItem *stationItem = new QGraphicsEllipseItem;
stationItem->setRect(-NODE_HALF_WIDTH,
-NODE_HALF_WIDTH,
NODE_HALF_WIDTH << 1,
NODE_HALF_WIDTH << 1);
stationItem->setPos(coord);
stationItem->setPen(color);
stationItem->setCursor(Qt::PointingHandCursor);
stationItem->setToolTip(tip);
if (linesList.size() <= 1) {
stationItem->setBrush(QColor(QRgb(0xffffff)));
}
scene->addItem(stationItem);
QGraphicsTextItem *textItem = new QGraphicsTextItem;
textItem->setDefaultTextColor("0xffffff");
textItem->setPlainText(name);
textItem->setFont(QFont("", 4, 1));
textItem->setPos(coord.x(), coord.y() - NODE_HALF_WIDTH * 2);
scene->addItem(textItem);
}
}
// -------------------槽函数-------------------
// 换乘出发线路改变槽函数
void MainWindow::transferStartLineChanged(QString lineName)
{
ui->startStationComboBox->clear();
int lineHash = subwayGraph->getLineHash(lineName);
if (lineHash != -1) {
QList<QString> stationsList = subwayGraph->getLineStationsList(lineHash);
for (auto &a : stationsList) {
ui->startStationComboBox->addItem(a);
}
}
}
// 换乘目的线路改变槽函数
void MainWindow::transferDstLineChanged(QString lineName)
{
ui->dstStationComboBox->clear();
int lineHash = subwayGraph->getLineHash(lineName);
if (lineHash != -1) {
QList<QString> stationsList = subwayGraph->getLineStationsList(lineHash);
for (auto &a : stationsList) {
ui->dstStationComboBox->addItem(a);
}
}
}
// 更新换乘选择信息
void MainWindow::updateTranserQueryInfo()
{
ui->startLineComboBox->clear();
ui->dstLineComboBox->clear();
QList<QString> linesList = subwayGraph->getLinesNameList();
for (auto &a : linesList) {
ui->startLineComboBox->addItem(a);
ui->dstLineComboBox->addItem(a);
}
transferStartLineChanged(ui->startLineComboBox->itemText(0));
transferDstLineChanged(ui->dstLineComboBox->itemText(0));
}
// 动作查看所有线路图槽函数
void MainWindow::showMap()
{
scene->clear();
ui->routeTextBrowser->clear();
QList<int> stationsList;
QList<Edge> edgesList;
subwayGraph->getGraph(stationsList, edgesList);
drawEdges(edgesList);
drawStations(stationsList);
}
// 换乘查询槽函数
void MainWindow::transferQuery()
{
int s1 = subwayGraph->getStationHash(ui->startStationComboBox->currentText());
int s2 = subwayGraph->getStationHash(ui->dstStationComboBox->currentText());
bool way = ui->minTransferButton->isChecked();
if (s1 == -1 || s2 == -1) {
QMessageBox box;
box.setWindowTitle("换乘查询");
box.setText("请选择有站点的线路");
box.addButton("确定", QMessageBox::AcceptRole);
if (box.exec() == QMessageBox::Accepted) {
box.close();
}
} else {
QList<int> stationsList;
QList<Edge> edgesList;
bool flag{true};
flag = way ? subwayGraph->queryTransferMinTransfer(s1, s2, stationsList, edgesList)
: subwayGraph->queryTransferMinTime(s1, s2, stationsList, edgesList);
if (flag) {
scene->clear();
drawEdges(edgesList);
drawStations(stationsList);
QString text;
for (int i = 0; i < stationsList.size(); ++i) {
if (i) {
text += "\n ↓\n";
}
text += subwayGraph->getStationName(stationsList[i]);
QString linesStr = getLinesName(subwayGraph->getStationLinesInfo(stationsList[i]));
text += linesStr;
}
ui->routeTextBrowser->clear();
ui->routeTextBrowser->setText(text);
} else {
QMessageBox box;
box.setWindowTitle("查询失败");
box.setText("该起始和终止站点暂时无法到达");
box.addButton("确定", QMessageBox::AcceptRole);
if (box.exec() == QMessageBox::Accepted) {
box.close();
}
}
}
}