FlightGear next
AircraftItemModel.hxx
Go to the documentation of this file.
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 <QDir>
26#include <QPixmap>
27#include <QStringList>
28#include <QUrl>
29
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
37const int AircraftPathRole = Qt::UserRole + 1;
38const int AircraftAuthorsRole = Qt::UserRole + 2;
39const int AircraftVariantRole = Qt::UserRole + 3;
40const int AircraftVariantCountRole = Qt::UserRole + 4;
41const int AircraftPackageIdRole = Qt::UserRole + 6;
42const int AircraftPackageStatusRole = Qt::UserRole + 7;
43const int AircraftPackageProgressRole = Qt::UserRole + 8;
44const int AircraftLongDescriptionRole = Qt::UserRole + 9;
45const int AircraftHasRatingsRole = Qt::UserRole + 10;
46const int AircraftInstallPercentRole = Qt::UserRole + 11;
47const int AircraftPackageSizeRole = Qt::UserRole + 12;
48const int AircraftInstallDownloadedSizeRole = Qt::UserRole + 13;
49const int AircraftURIRole = Qt::UserRole + 14;
50const int AircraftIsHelicopterRole = Qt::UserRole + 16;
51const int AircraftIsSeaplaneRole = Qt::UserRole + 17;
52const int AircraftPackageRefRole = Qt::UserRole + 19;
53const int AircraftIsFavouriteRole = Qt::UserRole + 20;
54const int AircraftPrimaryURIRole = Qt::UserRole + 21;
55
56const int AircraftStatusRole = Qt::UserRole + 22;
57const int AircraftMinVersionRole = Qt::UserRole + 23;
58
59const int AircraftRatingRole = Qt::UserRole + 100;
60const int AircraftVariantDescriptionRole = Qt::UserRole + 200;
61
62class PackageDelegate;
63
64Q_DECLARE_METATYPE(simgear::pkg::PackageRef)
65
66class AircraftItemModel : public QAbstractListModel
67{
68 Q_OBJECT
69
71
72public:
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
92
93 QModelIndex indexOfAircraftURI(QUrl uri) const;
94
99 void selectVariantForAircraftURI(QUrl uri);
100
105 bool isIndexRunnable(const QModelIndex& index) const;
106
111 QString nameForAircraftURI(QUrl uri) const;
112
113 int installedAircraftCount() const;
114signals:
115 void aircraftInstallFailed(QModelIndex index, QString errorMessage);
116
117 void aircraftInstallCompleted(QModelIndex index);
118
120
122private slots:
123 void onScanStarted();
124 void onScanAddedItems(int count);
125 void onLocalCacheCleared();
126
127private:
128 friend class PackageDelegate;
129
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
155private:
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
const int AircraftVariantCountRole
const int AircraftInstallPercentRole
const int AircraftInstallDownloadedSizeRole
const int AircraftMinVersionRole
const int AircraftRatingRole
const int AircraftIsSeaplaneRole
const int AircraftVariantDescriptionRole
const int AircraftHasRatingsRole
const int AircraftPathRole
const int AircraftPackageIdRole
const int AircraftAuthorsRole
const int AircraftLongDescriptionRole
const int AircraftPrimaryURIRole
const int AircraftPackageProgressRole
const int AircraftPackageRefRole
const int AircraftIsHelicopterRole
const int AircraftStatusRole
const int AircraftURIRole
const int AircraftIsFavouriteRole
const int AircraftPackageSizeRole
const int AircraftVariantRole
const int AircraftPackageStatusRole
#define p(x)
QSharedPointer< AircraftItem > AircraftItemPtr
QModelIndex indexOfAircraftURI(QUrl uri) const
given a -set.xml path, return the corresponding model index, if one exists.
void setPackageRoot(const simgear::pkg::RootRef &root)
bool isIndexRunnable(const QModelIndex &index) const
return if a given aircraft is ready to be run, or not.
int rowCount(const QModelIndex &parent) const override
bool setData(const QModelIndex &index, const QVariant &value, int role) override
void aircraftInstallFailed(QModelIndex index, QString errorMessage)
QHash< int, QByteArray > roleNames() const override
AircraftItemModel(QObject *pr)
AircraftItemModel::AircraftItemModel.
void selectVariantForAircraftURI(QUrl uri)
ensure the appropriate variant index is active in the model, for the corresponding aircraft URI
QVariant data(const QModelIndex &index, int role) const override
void installedAircraftCountChanged()
void aircraftInstallCompleted(QModelIndex index)
QString nameForAircraftURI(QUrl uri) const
Retrieve the display name for an aircraft specified by URI, without changing the current variant stat...