File indexing completed on 2025-02-02 05:02:28

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef FAVORITELOCATIONMODEL_H
0008 #define FAVORITELOCATIONMODEL_H
0009 
0010 #include <QAbstractListModel>
0011 #include <QExplicitlySharedDataPointer>
0012 
0013 #include <vector>
0014 
0015 class QJsonArray;
0016 class QJsonObject;
0017 
0018 class FavoriteLocationPrivate;
0019 
0020 /** Favorite location. */
0021 class FavoriteLocation
0022 {
0023     Q_GADGET
0024     Q_PROPERTY(QString name READ name WRITE setName)
0025     Q_PROPERTY(float latitude READ latitude WRITE setLatitude)
0026     Q_PROPERTY(float longitude READ longitude WRITE setLongitude)
0027 
0028 public:
0029     FavoriteLocation();
0030     FavoriteLocation(const FavoriteLocation&);
0031     FavoriteLocation(FavoriteLocation&&);
0032     ~FavoriteLocation();
0033     FavoriteLocation& operator=(const FavoriteLocation&);
0034 
0035     /** Name set and coordinate valid. */
0036     bool isValid() const;
0037 
0038     QString name() const;
0039     void setName(const QString &name);
0040     float latitude() const;
0041     void setLatitude(float lat);
0042     float longitude() const;
0043     void setLongitude(float lon);
0044 
0045     static FavoriteLocation fromJson(const QJsonObject &obj);
0046     static std::vector<FavoriteLocation> fromJson(const QJsonArray &array);
0047     static QJsonArray toJson(const std::vector<FavoriteLocation> &locs);
0048     static QJsonObject toJson(const FavoriteLocation &loc);
0049 private:
0050     QExplicitlySharedDataPointer<FavoriteLocationPrivate> d;
0051 };
0052 
0053 /** Favorite location management for transfer elements. */
0054 class FavoriteLocationModel : public QAbstractListModel
0055 {
0056     Q_OBJECT
0057 public:
0058     enum Role {
0059         LatitudeRole = Qt::UserRole,
0060         LongitudeRole,
0061         FavoriteLocationRole,
0062     };
0063     Q_ENUM(Role)
0064 
0065     explicit FavoriteLocationModel(QObject *parent = nullptr);
0066     ~FavoriteLocationModel() override;
0067 
0068     /** Appends a new dummy location. */
0069     Q_INVOKABLE void appendNewLocation();
0070     /** Appends the given location if there is none yet that matches its position. */
0071     void appendLocationIfMissing(FavoriteLocation &&loc);
0072     /** Removes location at index @row. */
0073     Q_INVOKABLE void removeLocation(int row);
0074 
0075     /** All favorite locations. */
0076     const std::vector<FavoriteLocation>& favoriteLocations() const;
0077     /** Set favorite locations to @p locs.
0078      *  Used for importing.
0079      */
0080     void setFavoriteLocations(std::vector<FavoriteLocation> &&locs);
0081 
0082     /** Export to GPX. */
0083     Q_INVOKABLE void exportToGpx(const QString &filePath) const;
0084     /** Import from GPX. */
0085     Q_INVOKABLE void importFromGpx(const QString &filePath);
0086 
0087     int rowCount(const QModelIndex &parent = {}) const override;
0088     QVariant data(const QModelIndex &index, int role) const override;
0089     bool setData(const QModelIndex & index, const QVariant & value, int role) override;
0090     QHash<int, QByteArray> roleNames() const override;
0091 
0092 private:
0093     void saveLocations() const;
0094 
0095     std::vector<FavoriteLocation> m_locations;
0096 };
0097 
0098 Q_DECLARE_METATYPE(FavoriteLocation)
0099 
0100 #endif // FAVORITELOCATIONMODEL_H