File indexing completed on 2024-05-12 04:42:51

0001 /*
0002     SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "platformlayout.h"
0007 
0008 #include <KPublicTransport/Stopover>
0009 
0010 using namespace KPublicTransport;
0011 
0012 static void expandPlatformRange(std::vector<QString> &sections, const QString &range)
0013 {
0014     if (range.isEmpty()) {
0015         return;
0016     }
0017 
0018     // TODO
0019     sections.push_back(range);
0020 }
0021 
0022 static void addPlatformSectionsForVehicleSections(std::vector<QString> &sections, const Stopover &stopover, const VehicleSection &coach)
0023 {
0024     if (coach.hasPlatformPosition()) {
0025         for (const auto &s : stopover.platformLayout().sections()) {
0026             if (s.name().isEmpty() || s.begin() > coach.platformPositionEnd() || s.end() < coach.platformPositionBegin()) {
0027                 continue;
0028             }
0029             sections.push_back(s.name());
0030         }
0031         return;
0032     }
0033 
0034     expandPlatformRange(sections, coach.platformSectionName());
0035 }
0036 
0037 static QString mergeSections(std::vector<QString> &&sections)
0038 {
0039     std::sort(sections.begin(), sections.end());
0040     sections.erase(std::unique(sections.begin(), sections.end()), sections.end());
0041     if (sections.empty()) {
0042         return {};
0043     }
0044 
0045     QStringList l;
0046     for (std::size_t i = 0; i < sections.size(); ++i) {
0047         if (sections[i].size() > 1) {
0048             l.push_back(sections[i]);
0049             continue;
0050         }
0051 
0052         std::size_t j = i + 1;
0053         for (; j < sections.size(); ++j) {
0054             if (sections[j].size() != 1 || sections[j][0].cell() - sections[j-1][0].cell() != 1) {
0055                 break;
0056             }
0057         }
0058         if (j - i == 1) {
0059             l.push_back(sections[i]);
0060         } else {
0061             l.push_back(sections[i] + QLatin1Char('-') + sections[j - 1]);
0062             i = j - 1;
0063         }
0064     }
0065 
0066     return l.join(QLatin1String(","));
0067 }
0068 
0069 QString PlatformLayout::sectionsForVehicle(const Stopover &stopover)
0070 {
0071     std::vector<QString> secs;
0072     for (const auto &coach : stopover.vehicleLayout().sections()) {
0073         if (coach.type() == VehicleSection::Engine || coach.type() == VehicleSection::PowerCar) {
0074             continue;
0075         }
0076         addPlatformSectionsForVehicleSections(secs, stopover, coach);
0077     }
0078     return mergeSections(std::move(secs));
0079 }
0080 
0081 QString PlatformLayout::sectionsForClass(const Stopover &stopover, VehicleSection::Class cls)
0082 {
0083     std::vector<QString> secs;
0084     for (const auto &coach : stopover.vehicleLayout().sections()) {
0085         if (coach.classes() & cls) {
0086             addPlatformSectionsForVehicleSections(secs, stopover, coach);
0087         }
0088     }
0089     return mergeSections(std::move(secs));
0090 }
0091 
0092 QString PlatformLayout::sectionsForVehicleSection(const Stopover &stopover, const QString &coachName)
0093 {
0094     std::vector<QString> secs;
0095     for (const auto &coach : stopover.vehicleLayout().sections()) {
0096         if (coach.name() == coachName) {
0097             addPlatformSectionsForVehicleSections(secs, stopover, coach);
0098         }
0099     }
0100     return mergeSections(std::move(secs));
0101 }