-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLine.cpp
More file actions
104 lines (85 loc) · 2.45 KB
/
Line.cpp
File metadata and controls
104 lines (85 loc) · 2.45 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
#include "Line.h"
Line::Line(int number)
{
this->number = number;
this->length = 0;
this->mapFileName = QString(":/maps/resources/maps/Line%1.png").arg(number);
this->model_Stations = new QStringListModel();
}
void Line::AddStation(Station *station)
{
stations.append(station);
list_Stations << station->name;
model_Stations->setStringList(list_Stations);
length ++;
}
QList<Line*> lineList;
void ReadStationInfo(const QString fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << file.errorString();
return;
}
QTextStream in(&file);
while (!in.atEnd()) {
QString textLine = in.readLine();
QStringList fields = textLine.split(',');
// 解析并添加站点信息
QString idTemp = fields[0] + fields[1];
int lineNoTemp = fields[0].toInt();
QString nameTemp = fields[2];
Station* newStation = new Station(idTemp, nameTemp);
// 查找或创建对应线路,添加站点
Line* currentLine = nullptr;
for (Line* line : lineList) {
if (line->number == (uint)lineNoTemp) {
currentLine = line;
break;
}
}
if (currentLine == nullptr) {
currentLine = new Line(lineNoTemp);
lineList.append(currentLine);
// qDebug() << "New line: " << lineNoTemp;
}
currentLine->AddStation(newStation);
//qDebug() << "Add station:" << nameTemp << " to line" << lineNoTemp;
}
qDebug() << "Finish reading" << fileName;
file.close();
}
Line* FindLineByNumber(int number)
{
for (Line* line : lineList) {
if (line->number == (uint)number) {
return line;
}
}
qDebug() << "Error: Line " << number << " not found.";
return nullptr;
}
QList<Station*> FindStationsByName(QString stationName)
{
QList<Station*> matchingStations;
for (Line* line : lineList) {
for (Station* station : line->stations) {
if (station->name == stationName) {
matchingStations.append(station);
break;
}
}
}
return matchingStations;
}
Station* FindStationById(QString stationId)
{
for (Line* line : lineList) {
for (Station* station : line->stations) {
if (station->id == stationId) {
return station;
}
}
}
return nullptr; // 未找到对应站点
}