Enroute Flight Navigation
A navigation app for VFR pilots
VACLibrary.h
1/***************************************************************************
2 * Copyright (C) 2024-2025 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 <QFile>
25#include <QProperty>
26#include <QStandardPaths>
27#include <QTimer>
28
29#include "geomaps/VAC.h"
30
31
32namespace GeoMaps
33{
34
53
54class VACLibrary : public QAbstractListModel
55{
56 Q_OBJECT
57 QML_ELEMENT
58 QML_SINGLETON
59
60public:
65 VACLibrary(QObject *parent = nullptr);
66
68 ~VACLibrary() override;
69
71 enum Role : int {
73 VacRole = Qt::UserRole + 1,
74
77
80
83 };
84
85 //
86 // Properties
87 //
88
91
92
93 Q_PROPERTY(bool isEmpty READ isEmpty BINDABLE bindableIsEmpty NOTIFY isEmptyChanged)
94
100 Q_PROPERTY(QList<GeoMaps::VAC> vacs READ vacs NOTIFY vacsChanged)
101
102
103 //
104 // Getter Methods
105 //
106
111 [[nodiscard]] bool hasManuallyImported() const { return m_hasManuallyImported.value(); }
112
117 [[nodiscard]] QBindable<bool> bindableHasManuallyImported() const { return &m_hasManuallyImported; }
118
123 [[nodiscard]] bool isEmpty() const { return m_isEmpty.value(); }
124
129 [[nodiscard]] QBindable<bool> bindableIsEmpty() const { return &m_isEmpty; }
130
135 [[nodiscard]] QList<GeoMaps::VAC> vacs() const { return m_rows; }
136
137
138 //
139 // Model API
140 //
141
148 [[nodiscard]] int rowCount(const QModelIndex& parent = QModelIndex()) const override;
149
158 [[nodiscard]] QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
159
164 [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
165
166
167 //
168 // Methods
169 //
170
175 Q_INVOKABLE void clear();
176
184 [[nodiscard]] Q_INVOKABLE GeoMaps::VAC get(const QString& name);
185
192 [[nodiscard]] Q_INVOKABLE QString importTripKit(const QString& fileName);
193
207 [[nodiscard]] Q_INVOKABLE GeoMaps::VAC materialize(const GeoMaps::VAC& vac);
208
218 [[nodiscard]] Q_INVOKABLE QString importVAC(GeoMaps::VAC vac);
219
226 Q_INVOKABLE void remove(const QString& name);
227
236 [[nodiscard]] Q_INVOKABLE QString rename(const QString& oldName, const QString& newName);
237
247 [[nodiscard]] Q_INVOKABLE QVector<GeoMaps::VAC> vacs4Point(const QGeoCoordinate& position);
248
249signals:
252
255
258
266 void importTripKitStatus(double percent);
267
268private:
269 Q_DISABLE_COPY_MOVE(VACLibrary)
270
271 // This method cleans the VAC directory. It deletes all VAC from m_vacs that
272 // have no raster image files. It looks for unmanaged raster image files and
273 // either imports them or moves them to the subdirectory "unrecognised".
274 void janitor();
275
276 // This method re-reads the chart index from all VAC collection files
277 // managed by DataManagement::DataManager, rebuilds m_collectionVacs and
278 // deletes stale entries from the extraction cache.
279 void updateCollections();
280
281 // This method saves m_vacs to m_dataFileName, atomically.
282 void save();
283
284 // This method returns the absolute path of a given VAC. Needed for iOS
285 // after App Update. See GeoMaps::VACLibrary::janitor
286 QString absolutePathForVac(const GeoMaps::VAC&);
287 QString absolutePathForVac(const QString& name);
288
289 // Replaces the manually imported charts and rebuilds the model rows
290 void setManualVacs(const QVector<GeoMaps::VAC>& vacs);
291
292 // Replaces the charts from VAC collections and rebuilds the model rows
293 void setCollectionVacs(const QVector<GeoMaps::VAC>& vacs);
294
295 // Recomputes m_rows from m_vacs and m_collectionVacs, announces the
296 // difference with row-granular model signals, updates the derived
297 // properties and emits vacsChanged() if anything changed.
298 void rebuildRows();
299
300 // Sort order of the model rows: by section, then by name, then by file
301 // name, so that distinct charts never compare equal
302 static bool lessThan(const GeoMaps::VAC& first, const GeoMaps::VAC& second);
303
304 // Source data: manually imported charts and charts from VAC collections.
305 // Mutations go through setManualVacs() and setCollectionVacs(): modify a
306 // local copy, then assign it back in a single call.
307 QVector<GeoMaps::VAC> m_vacs;
308 QVector<GeoMaps::VAC> m_collectionVacs;
309
310 // Model rows: m_vacs and m_collectionVacs together, sorted with lessThan()
311 QVector<GeoMaps::VAC> m_rows;
312
313 // Derived properties, updated by rebuildRows()
314 Q_OBJECT_BINDABLE_PROPERTY(GeoMaps::VACLibrary, bool, m_isEmpty, &GeoMaps::VACLibrary::isEmptyChanged)
315 Q_OBJECT_BINDABLE_PROPERTY(GeoMaps::VACLibrary, bool, m_hasManuallyImported, &GeoMaps::VACLibrary::hasManuallyImportedChanged)
316
317 QString m_vacDirectory {QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + u"/VAC"_s};
318 QString m_cacheDirectory {QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + u"/VAC"_s};
319 QString m_dataFileName {QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + u"/VAC.data"_s};
320
321 // Compresses multiple change notifications from DataManager into a single
322 // call to updateCollections()
323 QTimer m_updateCollectionsTimer;
324
325};
326
327} // namespace GeoMaps
328
Library of visual approach charts.
Definition VACLibrary.h:55
QBindable< bool > bindableIsEmpty() const
Getter function for property of the same name.
Definition VACLibrary.h:129
QHash< int, QByteArray > roleNames() const override
Re-implemented from QAbstractListModel.
Q_INVOKABLE void clear()
Removes all VACs.
Q_INVOKABLE GeoMaps::VAC get(const QString &name)
Obtain VACs from the library.
Q_INVOKABLE void remove(const QString &name)
Remove one VACs.
bool isEmpty() const
Getter function for property of the same name.
Definition VACLibrary.h:123
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Re-implemented from QAbstractListModel.
Q_INVOKABLE QString importVAC(GeoMaps::VAC vac)
Import VAC.
~VACLibrary() override
Destructor.
VACLibrary(QObject *parent=nullptr)
Constructor.
QList< GeoMaps::VAC > vacs
List of all VACs installed.
Definition VACLibrary.h:100
Role
Model roles.
Definition VACLibrary.h:71
@ NameRole
Name of the chart, a QString.
Definition VACLibrary.h:76
@ CenterRole
Center of the chart, a QGeoCoordinate.
Definition VACLibrary.h:82
@ VacRole
The chart, of type GeoMaps::VAC.
Definition VACLibrary.h:73
@ SectionRole
Section of the chart, a QString; see GeoMaps::VAC::section().
Definition VACLibrary.h:79
QBindable< bool > bindableHasManuallyImported() const
Getter function for property of the same name.
Definition VACLibrary.h:117
void hasManuallyImportedChanged()
Notifier signal.
bool isEmpty
True if library is empty.
Definition VACLibrary.h:93
QList< GeoMaps::VAC > vacs() const
Getter function for property of the same name.
Definition VACLibrary.h:135
void isEmptyChanged()
Notifier signal.
Q_INVOKABLE QVector< GeoMaps::VAC > vacs4Point(const QGeoCoordinate &position)
List of all VACs that contain a given point.
Q_INVOKABLE GeoMaps::VAC materialize(const GeoMaps::VAC &vac)
Obtain a VAC whose fileName points to a raster image file.
Q_INVOKABLE QString rename(const QString &oldName, const QString &newName)
Rename a VACs.
Q_INVOKABLE QString importTripKit(const QString &fileName)
Import trip kit.
void importTripKitStatus(double percent)
Progress report when importing a trip kit.
void vacsChanged()
Notifier signal.
bool hasManuallyImported
True if the library contains manually imported charts.
Definition VACLibrary.h:90
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Re-implemented from QAbstractListModel.
Visual approach chart.
Definition VAC.h:45