File indexing completed on 2024-11-24 04:15:37

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KOSMINDOORMAP_PLATFORMMODEL_H
0008 #define KOSMINDOORMAP_PLATFORMMODEL_H
0009 
0010 #include "kosmindoormap_export.h"
0011 
0012 #include <KOSMIndoorMap/MapData>
0013 #include <KOSMIndoorMap/Platform>
0014 
0015 #include <QAbstractItemModel>
0016 #include <QTimer>
0017 
0018 namespace KOSMIndoorMap {
0019 
0020 /** Lists all platforms/tracks and platform sections found in the current map.
0021  *  There's also the concept of (optional) arrival/departure platforms in here to highlight
0022  *  arriving/departing locations when used in context of a planned journey.
0023  */
0024 class KOSMINDOORMAP_EXPORT PlatformModel : public QAbstractItemModel
0025 {
0026     Q_OBJECT
0027     Q_PROPERTY(KOSMIndoorMap::MapData mapData READ mapData WRITE setMapData NOTIFY mapDataChanged)
0028     Q_PROPERTY(bool isEmpty READ isEmpty NOTIFY mapDataChanged)
0029 
0030     /** Row indexes of the matched arrival/departure platforms, if found and/or set, otherwise @c -1. */
0031     Q_PROPERTY(int arrivalPlatformRow READ arrivalPlatformRow NOTIFY platformIndexChanged)
0032     Q_PROPERTY(int departurePlatformRow READ departurePlatformRow NOTIFY platformIndexChanged)
0033 
0034     /** Platform search parameters (name/mode/ifopt) for matching arrival/departure platform against what we found in the map data. */
0035     Q_PROPERTY(KOSMIndoorMap::Platform arrivalPlatform READ arrivalPlatform WRITE setArrivalPlatform NOTIFY arrivalPlatformChanged)
0036     Q_PROPERTY(KOSMIndoorMap::Platform departurePlatform READ departurePlatform WRITE setDeparturePlatform NOTIFY departurePlatformChanged)
0037 
0038 public:
0039     explicit PlatformModel(QObject *parent = nullptr);
0040     ~PlatformModel();
0041 
0042     MapData mapData() const;
0043     void setMapData(const MapData &data);
0044 
0045     bool isEmpty() const;
0046 
0047     enum Role {
0048         CoordinateRole = Qt::UserRole,
0049         ElementRole,
0050         LevelRole,
0051         TransportModeRole,
0052         LinesRole,
0053         ArrivalPlatformRole,
0054         DeparturePlatformRole,
0055     };
0056     Q_ENUM(Role)
0057 
0058     int columnCount(const QModelIndex &parent = {}) const override;
0059     int rowCount(const QModelIndex &parent = {}) const override;
0060     QVariant data(const QModelIndex &index, int role) const override;
0061     QModelIndex index(int row, int column, const QModelIndex &parent = {}) const override;
0062     QModelIndex parent(const QModelIndex &child) const override;
0063     QHash<int, QByteArray> roleNames() const override;
0064 
0065     /** Match arrival/departure platform against what we found in the map data. */
0066     Platform arrivalPlatform() const;
0067     void setArrivalPlatform(const Platform &platform);
0068     Q_INVOKABLE [[deprecated("use arrivalPlatform property")]] void setArrivalPlatform(const QString &name, KOSMIndoorMap::Platform::Mode mode);
0069     Platform departurePlatform() const;
0070     void setDeparturePlatform(const Platform &platform);
0071     Q_INVOKABLE [[deprecated("use departurePlatform property")]] void setDeparturePlatform(const QString &name, KOSMIndoorMap::Platform::Mode mode);
0072 
0073     int arrivalPlatformRow() const;
0074     int departurePlatformRow() const;
0075 
0076 Q_SIGNALS:
0077     void mapDataChanged();
0078     void platformIndexChanged();
0079     void arrivalPlatformChanged();
0080     void departurePlatformChanged();
0081 
0082 private:
0083     void matchPlatforms();
0084     int matchPlatform(const Platform &platform) const;
0085     void createLabels();
0086     void setPlatformTag(int idx, OSM::TagKey key, bool enabled);
0087 
0088     QStringView effectiveArrivalSections() const;
0089     QStringView effectiveDepartureSections() const;
0090     void applySectionSelection(int platformIdx, OSM::TagKey key, QStringView sections);
0091 
0092     std::vector<Platform> m_platforms;
0093     MapData m_data;
0094     struct {
0095         OSM::TagKey arrival;
0096         OSM::TagKey departure;
0097     } m_tagKeys;
0098 
0099     std::vector<OSM::UniqueElement> m_platformLabels;
0100     std::vector<std::vector<OSM::UniqueElement>> m_sectionsLabels;
0101 
0102     Platform m_arrivalPlatform;
0103     Platform m_departurePlatform;
0104     int m_arrivalPlatformRow = -1;
0105     int m_departurePlatformRow = -1;
0106 
0107     QTimer m_matchTimer;
0108 };
0109 
0110 }
0111 
0112 #endif // KOSMINDOORMAP_PLATFORMMODEL_H