1 // AircraftModel.hxx - part of GUI launcher using Qt5
3 // Written by James Turner, started March 2015.
5 // Copyright (C) 2015 James Turner <zakalawe@mac.com>
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.
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.
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.
21 #ifndef FG_GUI_AIRCRAFT_MODEL
22 #define FG_GUI_AIRCRAFT_MODEL
24 #include <QAbstractListModel>
28 #include <QStringList>
29 #include <QSharedPointer>
32 #include <simgear/package/Root.hxx>
34 const int AircraftPathRole = Qt::UserRole + 1;
35 const int AircraftAuthorsRole = Qt::UserRole + 2;
36 const int AircraftVariantRole = Qt::UserRole + 3;
37 const int AircraftVariantCountRole = Qt::UserRole + 4;
38 const int AircraftThumbnailCountRole = Qt::UserRole + 5;
39 const int AircraftPackageIdRole = Qt::UserRole + 6;
40 const int AircraftPackageStatusRole = Qt::UserRole + 7;
41 const int AircraftPackageProgressRole = Qt::UserRole + 8;
42 const int AircraftLongDescriptionRole = Qt::UserRole + 9;
43 const int AircraftHasRatingsRole = Qt::UserRole + 10;
44 const int AircraftInstallPercentRole = Qt::UserRole + 11;
45 const int AircraftPackageSizeRole = Qt::UserRole + 12;
46 const int AircraftInstallDownloadedSizeRole = Qt::UserRole + 13;
47 const int AircraftURIRole = Qt::UserRole + 14;
48 const int AircraftThumbnailSizeRole = Qt::UserRole + 15;
49 const int AircraftIsHelicopterRole = Qt::UserRole + 16;
50 const int AircraftIsSeaplaneRole = Qt::UserRole + 17;
52 const int AircraftRatingRole = Qt::UserRole + 100;
53 const int AircraftVariantDescriptionRole = Qt::UserRole + 200;
54 const int AircraftThumbnailRole = Qt::UserRole + 300;
56 class AircraftScanThread;
58 class PackageDelegate;
60 typedef QSharedPointer<AircraftItem> AircraftItemPtr;
66 AircraftItem(QDir dir, QString filePath);
68 // the file-name without -set.xml suffix
69 QString baseName() const;
71 void fromDataStream(QDataStream& ds);
73 void toDataStream(QDataStream& ds) const;
75 QPixmap thumbnail() const;
83 QDateTime pathModTime;
84 QList<AircraftItemPtr> variants;
88 mutable QPixmap m_thumbnail;
92 enum AircraftItemStatus {
93 PackageNotInstalled = 0,
95 PackageUpdateAvailable,
100 class AircraftItemModel : public QAbstractListModel
104 AircraftItemModel(QObject* pr, const simgear::pkg::RootRef& root);
106 ~AircraftItemModel();
108 void setPaths(QStringList paths);
112 virtual int rowCount(const QModelIndex& parent) const;
114 virtual QVariant data(const QModelIndex& index, int role) const;
116 virtual bool setData(const QModelIndex &index, const QVariant &value, int role);
119 * given a -set.xml path, return the corresponding model index, if one
123 QModelIndex indexOfAircraftURI(QUrl uri) const;
126 * return if a given aircraft is ready to be run, or not. Aircraft which
127 * are not installed, or are downloading, are not runnable.
129 bool isIndexRunnable(const QModelIndex& index) const;
132 void aircraftInstallFailed(QModelIndex index, QString errorMessage);
134 void aircraftInstallCompleted(QModelIndex index);
136 void scanCompleted();
139 void onScanResults();
141 void onScanFinished();
144 friend class PackageDelegate;
146 QVariant dataFromItem(AircraftItemPtr item, quint32 variantIndex, int role) const;
148 QVariant dataFromPackage(const simgear::pkg::PackageRef& item,
149 quint32 variantIndex, int role) const;
151 QVariant packageThumbnail(simgear::pkg::PackageRef p, int index, bool download = true) const;
153 void abandonCurrentScan();
154 void refreshPackages();
156 void installSucceeded(QModelIndex index);
157 void installFailed(QModelIndex index, simgear::pkg::Delegate::StatusCode reason);
160 AircraftScanThread* m_scanThread;
161 QVector<AircraftItemPtr> m_items;
162 PackageDelegate* m_delegate;
164 QVector<quint32> m_activeVariant;
165 QVector<quint32> m_packageVariant;
167 simgear::pkg::RootRef m_packageRoot;
168 simgear::pkg::PackageList m_packages;
170 mutable QHash<QString, QPixmap> m_thumbnailPixmapCache;
173 #endif // of FG_GUI_AIRCRAFT_MODEL