KPublicTransport

platformlayout.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "platformlayout.h"
7
8#include <KPublicTransport/Stopover>
9
10using namespace KPublicTransport;
11
12static void expandPlatformRange(std::vector<QString> &sections, const QString &range)
13{
14 if (range.isEmpty()) {
15 return;
16 }
17
18 // TODO
19 sections.push_back(range);
20}
21
22static void addPlatformSectionsForVehicleSections(std::vector<QString> &sections, const Stopover &stopover, const VehicleSection &coach)
23{
24 if (coach.hasPlatformPosition()) {
25 const auto platformSections = stopover.platformLayout().sections();
26 for (const auto &s : platformSections) {
27 if (s.name().isEmpty() || s.begin() > coach.platformPositionEnd() || s.end() < coach.platformPositionBegin()) {
28 continue;
29 }
30 sections.push_back(s.name());
31 }
32 return;
33 }
34
35 expandPlatformRange(sections, coach.platformSectionName());
36}
37
38static QString mergeSections(std::vector<QString> &&sections)
39{
40 std::sort(sections.begin(), sections.end());
41 sections.erase(std::unique(sections.begin(), sections.end()), sections.end());
42 if (sections.empty()) {
43 return {};
44 }
45
47 for (std::size_t i = 0; i < sections.size(); ++i) {
48 if (sections[i].size() > 1) {
49 l.push_back(sections[i]);
50 continue;
51 }
52
53 std::size_t j = i + 1;
54 for (; j < sections.size(); ++j) {
55 if (sections[j].size() != 1 || sections[j][0].cell() - sections[j-1][0].cell() != 1) {
56 break;
57 }
58 }
59 if (j - i == 1) {
60 l.push_back(sections[i]);
61 } else {
62 l.push_back(sections[i] + QLatin1Char('-') + sections[j - 1]);
63 i = j - 1;
64 }
65 }
66
67 return l.join(QLatin1String(","));
68}
69
71{
72 std::vector<QString> secs;
73 const auto coachs = stopover.vehicleLayout().sections();
74 for (const auto &coach : coachs) {
75 if (coach.type() == VehicleSection::Engine || coach.type() == VehicleSection::PowerCar) {
76 continue;
77 }
78 addPlatformSectionsForVehicleSections(secs, stopover, coach);
79 }
80 return mergeSections(std::move(secs));
81}
82
83QString PlatformLayout::sectionsForClass(const Stopover &stopover, VehicleSection::Class cls)
84{
85 std::vector<QString> secs;
86 const auto coachs = stopover.vehicleLayout().sections();
87 for (const auto &coach : coachs) {
88 if (coach.classes() & cls) {
89 addPlatformSectionsForVehicleSections(secs, stopover, coach);
90 }
91 }
92 return mergeSections(std::move(secs));
93}
94
96{
97 std::vector<QString> secs;
98 const auto coachs = stopover.vehicleLayout().sections();
99 for (const auto &coach : coachs) {
100 if (coach.name() == coachName) {
101 addPlatformSectionsForVehicleSections(secs, stopover, coach);
102 }
103 }
104 return mergeSections(std::move(secs));
105}
static QString sectionsForVehicleSection(const Stopover &stopover, const QString &coachName)
Returns all platform sections the coach coachName stops at.
static QString sectionsForClass(const Stopover &stopover, VehicleSection::Class cls)
Returns all platform sections the given vehicle has coaches of class @cls at.
static QString sectionsForVehicle(const Stopover &stopover)
Returns all platform sections the given vehicle has coaches stopping at.
QVariantList sections
Platform sections for consumption by QML.
Definition platform.h:62
Information about an arrival and/or departure of a vehicle at a stop area.
Definition stopover.h:26
KPublicTransport::Vehicle vehicleLayout
Vehicle coach layout information at this stopover.
Definition stopover.h:77
KPublicTransport::Platform platformLayout
Platform layout information.
Definition stopover.h:79
Information about a part of a vehicle.
Definition vehicle.h:25
QString name
Human readable identifier of this section, typically the coach number.
Definition vehicle.h:31
Type type
Type of this vehicle section.
Definition vehicle.h:56
QString platformSectionName
Name of the platform section(s) this coach is position in.
Definition vehicle.h:115
float platformPositionBegin
Relative position [0-1] of the begin of this vehicle section on the platform.
Definition vehicle.h:36
Classes classes
Classes available in this vehicle section.
Definition vehicle.h:70
float platformPositionEnd
Relative position [0-1] of the end of this vehicle section on the platform.
Definition vehicle.h:40
QVariantList sections
Journey sections for consumption by QML.
Definition vehicle.h:175
Query operations and data types for accessing realtime public transport information from online servi...
void push_back(parameter_type value)
bool isEmpty() const const
QString join(QChar separator) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.