| 1 | // AddonsModel.hxx - part of GUI launcher using Qt5 |
| 2 | // |
| 3 | // Written by Dan Wickstrom, started February 2019. |
| 4 | // |
| 5 | // Copyright (C) 2019 Daniel Wickstrom <[email protected]> |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | |
| 21 | #ifndef FG_GUI_METADATA_LIST_MODEL |
| 22 | #define FG_GUI_METADATA_LIST_MODEL |
| 23 | |
| 24 | #include <QAbstractListModel> |
| 25 | #include <QDateTime> |
| 26 | #include <QDir> |
| 27 | #include <QPixmap> |
| 28 | #include <QStringList> |
| 29 | #include <set> |
| 30 | #include <simgear/package/Root.hxx> |
| 31 | #include <simgear/package/Catalog.hxx> |
| 32 | #include "Add-ons/addon_fwd.hxx" |
| 33 | #include "Add-ons/Addon.hxx" |
| 34 | #include "Add-ons/AddonMetadataParser.hxx" |
| 35 | |
| 36 | |
| 37 | |
| 38 | class AddonsModel : public QAbstractListModel |
| 39 | { |
| 40 | Q_OBJECT |
| 41 | |
| 42 | using AddonsMeta = struct { bool enable; flightgear::addons::AddonRef addon; }; |
| 43 | using AddonsMap = QHash<QString, AddonsMeta>; |
| 44 | public: |
| 45 | |
| 46 | AddonsModel(QObject* pr); |
| 47 | |
| 48 | ~AddonsModel(); |
| 49 | |
| 50 | int rowCount(const QModelIndex& parent) const override; |
| 51 | |
| 52 | QVariant data(const QModelIndex& index, int role) const override; |
| 53 | Q_INVOKABLE QVariant get(int index, int role) const; |
| 54 | Q_INVOKABLE QVariant get(int index, QString role) const; |
| 55 | |
| 56 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
| 57 | |
| 58 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 59 | |
| 60 | QHash<int, QByteArray> roleNames() const override; |
| 61 | |
| 62 | bool append(QString path, flightgear::addons::AddonRef& addon, bool enable); |
| 63 | |
| 64 | void resetData(const QStringList& ndata); |
| 65 | |
| 66 | Q_INVOKABLE bool checkVersion(QString path) const; |
| 67 | |
| 68 | bool getPathEnable(const QString& path) { return m_addonsMap[path].enable; } |
| 69 | bool containsPath(const QString& path) { return m_addonsMap.contains(path); } |
| 70 | |
| 71 | Q_INVOKABLE void enable(int index, bool enable); |
| 72 | |
| 73 | // this must stay in sync with m_roles stringlist |
| 74 | enum AddonRoles |
| 75 | { |
| 76 | IdRole = Qt::UserRole + 1, |
| 77 | NameRole, |
| 78 | PathRole, |
| 79 | VersionRole, |
| 80 | AuthorsRole, |
| 81 | MaintainersRole, |
| 82 | ShortDescriptionRole, |
| 83 | LongDescriptionRole, |
| 84 | LicenseDesignationRole, |
| 85 | LicenseUrlRole, |
| 86 | TagsRole, |
| 87 | MinFGVersionRole, |
| 88 | MaxFGVersionRole, |
| 89 | HomePageRole, |
| 90 | DownloadUrlRole, |
| 91 | SupportUrlRole, |
| 92 | CodeRepoUrlRole, |
| 93 | EnableRole |
| 94 | }; |
| 95 | |
| 96 | Q_ENUMS(AddonRoles) |
| 97 | |
| 98 | signals: |
| 99 | void modulesChanged(); |
| 100 | |
| 101 | private: |
| 102 | // this must stay in sync with MetaRoles enum |
| 103 | const QStringList m_roles = { |
| 104 | "id", |
| 105 | "name", |
| 106 | "path", |
| 107 | "version", |
| 108 | "authors", |
| 109 | "maintainers", |
| 110 | "short_description", |
| 111 | "long_description", |
| 112 | "license_designation", |
| 113 | "license_url", |
| 114 | "tags", |
| 115 | "minversion_fg", |
| 116 | "maxversion_fg", |
| 117 | "homepage", |
| 118 | "download_url", |
| 119 | "support_url", |
| 120 | "coderepo_url", |
| 121 | "enable" |
| 122 | }; |
| 123 | |
| 124 | QStringList m_addonsList; |
| 125 | AddonsMap m_addonsMap; |
| 126 | QHash<int, QByteArray> m_roleToName; |
| 127 | QHash<QString, int> m_nameToRole; |
| 128 | }; |
| 129 | |
| 130 | #endif // of Metadata_LIST_MODEL |