| 1 | // AircraftModel.hxx - part of GUI launcher using Qt5 |
| 2 | // |
| 3 | // Written by James Turner, started March 2015. |
| 4 | // |
| 5 | // Copyright (C) 2015 James Turner <[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_AIRCRAFT_MODEL |
| 22 | #define FG_GUI_AIRCRAFT_MODEL |
| 23 | |
| 24 | #include <QAbstractListModel> |
| 25 | #include <QDir> |
| 26 | #include <QPixmap> |
| 27 | #include <QStringList> |
| 28 | #include <QUrl> |
| 29 | |
| 30 | #include "LocalAircraftCache.hxx" |
| 31 | |
| 32 | #include <simgear/package/Delegate.hxx> |
| 33 | #include <simgear/package/Root.hxx> |
| 34 | #include <simgear/package/Catalog.hxx> |
| 35 | #include <simgear/package/Package.hxx> |
| 36 | |
| 37 | const int AircraftPathRole = Qt::UserRole + 1; |
| 38 | const int AircraftAuthorsRole = Qt::UserRole + 2; |
| 39 | const int AircraftVariantRole = Qt::UserRole + 3; |
| 40 | const int AircraftVariantCountRole = Qt::UserRole + 4; |
| 41 | const int AircraftPackageIdRole = Qt::UserRole + 6; |
| 42 | const int AircraftPackageStatusRole = Qt::UserRole + 7; |
| 43 | const int AircraftPackageProgressRole = Qt::UserRole + 8; |
| 44 | const int AircraftLongDescriptionRole = Qt::UserRole + 9; |
| 45 | const int AircraftHasRatingsRole = Qt::UserRole + 10; |
| 46 | const int AircraftInstallPercentRole = Qt::UserRole + 11; |
| 47 | const int AircraftPackageSizeRole = Qt::UserRole + 12; |
| 48 | const int AircraftInstallDownloadedSizeRole = Qt::UserRole + 13; |
| 49 | const int AircraftURIRole = Qt::UserRole + 14; |
| 50 | const int AircraftIsHelicopterRole = Qt::UserRole + 16; |
| 51 | const int AircraftIsSeaplaneRole = Qt::UserRole + 17; |
| 52 | const int AircraftPackageRefRole = Qt::UserRole + 19; |
| 53 | const int AircraftIsFavouriteRole = Qt::UserRole + 20; |
| 54 | const int AircraftPrimaryURIRole = Qt::UserRole + 21; |
| 55 | |
| 56 | const int AircraftStatusRole = Qt::UserRole + 22; |
| 57 | const int AircraftMinVersionRole = Qt::UserRole + 23; |
| 58 | |
| 59 | const int AircraftRatingRole = Qt::UserRole + 100; |
| 60 | const int AircraftVariantDescriptionRole = Qt::UserRole + 200; |
| 61 | |
| 62 | class PackageDelegate; |
| 63 | |
| 64 | Q_DECLARE_METATYPE(simgear::pkg::PackageRef) |
| 65 | |
| 66 | class AircraftItemModel : public QAbstractListModel |
| 67 | { |
| 68 | Q_OBJECT |
| 69 | |
| 70 | Q_PROPERTY(int installedAircraftCount READ installedAircraftCount NOTIFY installedAircraftCountChanged) |
| 71 | |
| 72 | public: |
| 73 | |
| 74 | AircraftItemModel(QObject* pr); |
| 75 | |
| 76 | ~AircraftItemModel() override; |
| 77 | |
| 78 | void setPackageRoot(const simgear::pkg::RootRef& root); |
| 79 | |
| 80 | int rowCount(const QModelIndex& parent) const override; |
| 81 | |
| 82 | QVariant data(const QModelIndex& index, int role) const override; |
| 83 | |
| 84 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
| 85 | |
| 86 | QHash<int, QByteArray> roleNames() const override; |
| 87 | |
| 88 | /** |
| 89 | * given a -set.xml path, return the corresponding model index, if one |
| 90 | * exists. |
| 91 | */ |
| 92 | |
| 93 | QModelIndex indexOfAircraftURI(QUrl uri) const; |
| 94 | |
| 95 | /** |
| 96 | * ensure the appropriate variant index is active in the model, for the |
| 97 | * corresponding aircraft URI |
| 98 | */ |
| 99 | void selectVariantForAircraftURI(QUrl uri); |
| 100 | |
| 101 | /** |
| 102 | * return if a given aircraft is ready to be run, or not. Aircraft which |
| 103 | * are not installed, or are downloading, are not runnable. |
| 104 | */ |
| 105 | bool isIndexRunnable(const QModelIndex& index) const; |
| 106 | |
| 107 | /** |
| 108 | * Retrieve the display name for an aircraft specified by URI, without |
| 109 | * changing the current variant state |
| 110 | */ |
| 111 | QString nameForAircraftURI(QUrl uri) const; |
| 112 | |
| 113 | int installedAircraftCount() const; |
| 114 | signals: |
| 115 | void aircraftInstallFailed(QModelIndex index, QString errorMessage); |
| 116 | |
| 117 | void aircraftInstallCompleted(QModelIndex index); |
| 118 | |
| 119 | void contentsChanged(); |
| 120 | |
| 121 | void installedAircraftCountChanged(); |
| 122 | private slots: |
| 123 | void onScanStarted(); |
| 124 | void onScanAddedItems(int count); |
| 125 | void onLocalCacheCleared(); |
| 126 | |
| 127 | private: |
| 128 | friend class PackageDelegate; |
| 129 | |
| 130 | /** |
| 131 | * struct to record persistent state from the item-delegate, about the |
| 132 | * currently visible variant, thumbnail and similar. |
| 133 | */ |
| 134 | struct DelegateState |
| 135 | { |
| 136 | quint32 variant = 0; |
| 137 | }; |
| 138 | |
| 139 | QVariant dataFromItem(AircraftItemPtr item, const DelegateState& state, int role) const; |
| 140 | |
| 141 | QVariant dataFromPackage(const simgear::pkg::PackageRef& item, |
| 142 | const DelegateState& state, int role) const; |
| 143 | |
| 144 | QVariant packageRating(const simgear::pkg::PackageRef& p, int ratingIndex) const; |
| 145 | QVariant packageThumbnail(simgear::pkg::PackageRef p, |
| 146 | const DelegateState& state, bool download = true) const; |
| 147 | |
| 148 | QVariant packagePreviews(simgear::pkg::PackageRef p, const DelegateState &ds) const; |
| 149 | |
| 150 | void refreshPackages(); |
| 151 | |
| 152 | void installSucceeded(QModelIndex index); |
| 153 | void installFailed(QModelIndex index, simgear::pkg::Delegate::StatusCode reason); |
| 154 | |
| 155 | private: |
| 156 | PackageDelegate* m_delegate = nullptr; |
| 157 | |
| 158 | QVector<DelegateState> m_delegateStates; |
| 159 | |
| 160 | simgear::pkg::RootRef m_packageRoot; |
| 161 | simgear::pkg::PackageList m_packages; |
| 162 | int m_cachedLocalAircraftCount = 0; |
| 163 | |
| 164 | }; |
| 165 | |
| 166 | #endif // of FG_GUI_AIRCRAFT_MODEL |