Enroute Flight Navigation
A navigation app for VFR pilots
WaypointLibrary.h
1/***************************************************************************
2 * Copyright (C) 2022-2026 by Stefan Kebekus *
3 * stefan.kebekus@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#pragma once
22
23#include <QAbstractListModel>
24#include <QQmlEngine>
25#include <QStandardPaths>
26
27#include "GlobalObject.h"
28#include "geomaps/Waypoint.h"
29
30namespace GeoMaps
31{
32
45
46 class WaypointLibrary : public QAbstractListModel
47 {
48 Q_OBJECT
49 QML_ELEMENT
50 QML_SINGLETON
51
52 public:
61 explicit WaypointLibrary(QObject *parent = nullptr);
62
63 // No default constructor, important for QML singleton
64 explicit WaypointLibrary() = delete;
65
66 // factory function for QML singleton
67 static GeoMaps::WaypointLibrary* create(QQmlEngine* /*unused*/, QJSEngine* /*unused*/)
68 {
70 }
71
72 ~WaypointLibrary() override = default;
73
75 enum Role : int {
77 WaypointRole = Qt::UserRole + 1,
78
81 };
82
83
84 //
85 // Properties
86 //
87
92 Q_PROPERTY(QList<GeoMaps::Waypoint> waypoints READ waypoints NOTIFY waypointsChanged)
93
94
98 Q_PROPERTY(QByteArray GeoJSON READ GeoJSON NOTIFY waypointsChanged)
99
100
101 //
102 // Getter Methods
103 //
104
109 [[nodiscard]] QList<GeoMaps::Waypoint> waypoints() const
110 {
111 return m_waypoints;
112 }
113
118 [[nodiscard]] QByteArray GeoJSON() const;
119
120
121 //
122 // Model API
123 //
124
131 [[nodiscard]] int rowCount(const QModelIndex& parent = QModelIndex()) const override;
132
141 [[nodiscard]] QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
142
147 [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
148
149
150 //
151 // Methods
152 //
153
159 Q_INVOKABLE void add(const GeoMaps::Waypoint &waypoint);
160
162 Q_INVOKABLE void clear();
163
171 [[nodiscard]] Q_INVOKABLE bool contains(const GeoMaps::Waypoint &waypoint) const
172 {
173 return m_waypoints.contains(waypoint);
174 }
175
184 [[nodiscard]] Q_INVOKABLE bool hasNearbyEntry(const GeoMaps::Waypoint& waypoint) const;
185
196 [[nodiscard]] Q_INVOKABLE QString import(const QString& fileName, bool skip);
197
211 [[nodiscard]] Q_INVOKABLE QString loadFromGeoJSON(QString fileName = {});
212
222 Q_INVOKABLE bool remove(const GeoMaps::Waypoint &waypoint);
223
237 Q_INVOKABLE bool replace(const GeoMaps::Waypoint &oldWaypoint, const GeoMaps::Waypoint &newWaypoint);
238
250 [[nodiscard]] Q_INVOKABLE QString save(QString fileName = {}) const;
251
260 [[nodiscard]] Q_INVOKABLE QByteArray toGpx() const;
261
262 signals:
269
270 private:
271 Q_DISABLE_COPY_MOVE(WaypointLibrary)
272
273 // Row at which a waypoint with the given name is to be inserted, so
274 // that m_waypoints stays sorted by name
275 [[nodiscard]] int insertionRow(const QString& name) const;
276
277 // Sorts a list of waypoints by name
278 static void sortByName(QList<GeoMaps::Waypoint>& waypoints);
279
280 // Standard file name for save() and loadFromGeoJGON() methods
281 QString stdFileName{QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/waypoint library.geojson"};
282
283 // Acutual list of waypoints, sorted by name
284 QList<GeoMaps::Waypoint> m_waypoints;
285 };
286
287} // namespace GeoMaps
Library of user-defined waypoints.
Q_INVOKABLE bool contains(const GeoMaps::Waypoint &waypoint) const
Checks if library contains an given waypoint.
QByteArray GeoJSON
List of waypoints.
Q_INVOKABLE QByteArray toGpx() const
Serialize into GPX document.
Q_INVOKABLE bool hasNearbyEntry(const GeoMaps::Waypoint &waypoint) const
Check if the library contains a waypoint near to a given one.
WaypointLibrary(QObject *parent=nullptr)
Creates a new waypoint library.
Q_INVOKABLE void clear()
Clears the waypoint library.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Re-implemented from QAbstractListModel.
void waypointsChanged()
Notification signal for the property with the same name.
Q_INVOKABLE void add(const GeoMaps::Waypoint &waypoint)
Adds a waypoint to the library.
QHash< int, QByteArray > roleNames() const override
Re-implemented from QAbstractListModel.
QList< GeoMaps::Waypoint > waypoints
List of waypoints.
Q_INVOKABLE QString loadFromGeoJSON(QString fileName={})
Read from file.
Q_INVOKABLE bool remove(const GeoMaps::Waypoint &waypoint)
Remove waypoint.
Q_INVOKABLE bool replace(const GeoMaps::Waypoint &oldWaypoint, const GeoMaps::Waypoint &newWaypoint)
Replace waypoint.
@ NameRole
Name of the waypoint, a QString.
@ WaypointRole
The waypoint, of type GeoMaps::Waypoint.
Q_INVOKABLE QString save(QString fileName={}) const
Save to file.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Re-implemented from QAbstractListModel.
QByteArray GeoJSON() const
Getter function for property with the same name.
Waypoint, such as an airfield, a navaid station or a reporting point.
Definition Waypoint.h:43
static Q_INVOKABLE GeoMaps::WaypointLibrary * waypointLibrary()
Pointer to appplication-wide static WaypointLibrary instance.