]> git.mxchange.org Git - flightgear.git/blob - src/GUI/AircraftModel.hxx
Code cleanups, code updates and fix at least on (possible) devide-by-zero
[flightgear.git] / src / GUI / AircraftModel.hxx
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 <zakalawe@mac.com>
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 <QDateTime>
26 #include <QDir>
27 #include <QPixmap>
28 #include <QStringList>
29 #include <QSharedPointer>
30 #include <QUrl>
31
32 #include <simgear/package/Root.hxx>
33
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;
51
52 const int AircraftRatingRole = Qt::UserRole + 100;
53 const int AircraftVariantDescriptionRole = Qt::UserRole + 200;
54 const int AircraftThumbnailRole = Qt::UserRole + 300;
55
56 class AircraftScanThread;
57 class QDataStream;
58 class PackageDelegate;
59 struct AircraftItem;
60 typedef QSharedPointer<AircraftItem> AircraftItemPtr;
61
62 struct AircraftItem
63 {
64     AircraftItem();
65
66     AircraftItem(QDir dir, QString filePath);
67     
68     // the file-name without -set.xml suffix
69     QString baseName() const;
70     
71     void fromDataStream(QDataStream& ds);
72
73     void toDataStream(QDataStream& ds) const;
74
75     QPixmap thumbnail() const;
76
77     bool excluded;
78     QString path;
79     QString description;
80     QString longDescription;
81     QString authors;
82     int ratings[4];
83     QString variantOf;
84     QDateTime pathModTime;
85     QList<AircraftItemPtr> variants;
86     bool usesHeliports;
87     bool usesSeaports;
88 private:
89     mutable QPixmap m_thumbnail;
90 };
91
92
93 enum AircraftItemStatus {
94     PackageNotInstalled = 0,
95     PackageInstalled,
96     PackageUpdateAvailable,
97     PackageQueued,
98     PackageDownloading,
99     NoOfficialCatalogMessage
100 };
101
102 class AircraftItemModel : public QAbstractListModel
103 {
104     Q_OBJECT
105 public:
106     AircraftItemModel(QObject* pr);
107
108     ~AircraftItemModel();
109
110     void setPackageRoot(const simgear::pkg::RootRef& root);
111
112     void setPaths(QStringList paths);
113
114     void scanDirs();
115
116     virtual int rowCount(const QModelIndex& parent) const;
117     
118     virtual QVariant data(const QModelIndex& index, int role) const;
119     
120     virtual bool setData(const QModelIndex &index, const QVariant &value, int role);
121
122     /**
123      * given a -set.xml path, return the corresponding model index, if one
124      * exists.
125      */
126
127     QModelIndex indexOfAircraftURI(QUrl uri) const;
128     
129     /**
130      * return if a given aircraft is ready to be run, or not. Aircraft which
131      * are not installed, or are downloading, are not runnable.
132      */
133     bool isIndexRunnable(const QModelIndex& index) const;
134
135     /**
136      * should we show the prompt about the official hangar not being installed
137      * or not?
138      */
139     void setOfficialHangarMessageVisible(bool vis);
140
141     QModelIndex officialHangarMessageIndex() const;
142
143     /**
144      * @helper to determine if a particular path is likely to contain
145      * aircraft or not. Checks for -set.xml files one level down in the tree.
146      *
147      */
148     static bool isCandidateAircraftPath(QString path);
149 signals:
150     void aircraftInstallFailed(QModelIndex index, QString errorMessage);
151     
152     void aircraftInstallCompleted(QModelIndex index);
153     
154     void scanCompleted();
155
156 private slots:
157     void onScanResults();
158     
159     void onScanFinished();
160
161 private:
162     friend class PackageDelegate;
163
164     QVariant dataFromItem(AircraftItemPtr item, quint32 variantIndex, int role) const;
165
166     QVariant dataFromPackage(const simgear::pkg::PackageRef& item,
167                              quint32 variantIndex, int role) const;
168
169     QVariant packageThumbnail(simgear::pkg::PackageRef p, int index, bool download = true) const;
170
171     void abandonCurrentScan();
172     void refreshPackages();
173     
174     void installSucceeded(QModelIndex index);
175     void installFailed(QModelIndex index, simgear::pkg::Delegate::StatusCode reason);
176     
177     QStringList m_paths;
178     AircraftScanThread* m_scanThread;
179     QVector<AircraftItemPtr> m_items;
180     PackageDelegate* m_delegate;
181     bool m_showOfficialHangarMessage;
182
183     QVector<quint32> m_activeVariant;
184     QVector<quint32> m_packageVariant;
185     
186     simgear::pkg::RootRef m_packageRoot;
187     simgear::pkg::PackageList m_packages;
188         
189     mutable QHash<QString, QPixmap> m_thumbnailPixmapCache;
190 };
191
192 #endif // of FG_GUI_AIRCRAFT_MODEL