Enroute Flight Navigation
A navigation app for VFR pilots
FileLibraryModel.h
1/***************************************************************************
2 * Copyright (C) 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 <QFileSystemWatcher>
25#include <QQmlEngine>
26#include <QTimer>
27
28
45
46class FileLibraryModel : public QAbstractListModel
47{
48 Q_OBJECT
49 QML_ELEMENT
50 QML_UNCREATABLE("Use Librarian.aircraftModel or Librarian.routesModel")
51
52public:
54 enum Role : int {
56 NameRole = Qt::UserRole + 1
57 };
58
69 explicit FileLibraryModel(QString directory, QString suffix, QObject* parent = nullptr);
70
71 // Default destructor
72 ~FileLibraryModel() override = default;
73
74
75 //
76 // Getter Methods
77 //
78
83 [[nodiscard]] QStringList names() const { return m_names; }
84
85
86 //
87 // Model API
88 //
89
96 [[nodiscard]] int rowCount(const QModelIndex& parent = QModelIndex()) const override;
97
106 [[nodiscard]] QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
107
112 [[nodiscard]] QHash<int, QByteArray> roleNames() const override;
113
114public slots:
122 void refresh();
123
124private:
125 Q_DISABLE_COPY_MOVE(FileLibraryModel)
126
127 // Base names of the library files in the directory, sorted with lessThan()
128 [[nodiscard]] QStringList scan() const;
129
130 // Sort order of the model: case-insensitive, with a case-sensitive
131 // tie-break so that distinct names never compare equal
132 static bool lessThan(const QString& first, const QString& second);
133
134 QString m_directory;
135 QString m_suffix;
136
137 // Model content, sorted with lessThan()
138 QStringList m_names;
139
140 QFileSystemWatcher m_watcher;
141
142 // Directory changes arrive in bursts, e.g. when a file is saved
143 // atomically. The timer coalesces them into one refresh().
144 QTimer m_debounce;
145};
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Re-implemented from QAbstractListModel.
QStringList names() const
Base names of the library entries.
void refresh()
Rescans the directory.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Re-implemented from QAbstractListModel.
@ NameRole
Base name of the entry, a QString.
FileLibraryModel(QString directory, QString suffix, QObject *parent=nullptr)
Standard constructor.
QHash< int, QByteArray > roleNames() const override
Re-implemented from QAbstractListModel.