| 1 | #ifndef QMLAIRCRAFTINFO_HXX |
| 2 | #define QMLAIRCRAFTINFO_HXX |
| 3 | |
| 4 | #include <memory> |
| 5 | |
| 6 | #include <QObject> |
| 7 | #include <QUrl> |
| 8 | #include <QSharedPointer> |
| 9 | #include <QAbstractListModel> |
| 10 | |
| 11 | #include <simgear/package/Catalog.hxx> |
| 12 | #include <simgear/package/Package.hxx> |
| 13 | |
| 14 | #include "UnitsModel.hxx" |
| 15 | |
| 16 | struct AircraftItem; |
| 17 | typedef QSharedPointer<AircraftItem> AircraftItemPtr; |
| 18 | |
| 19 | struct AircraftStateInfo { |
| 20 | string_list tags; |
| 21 | QString name; // human-readable name, or blank if we auto-generate this |
| 22 | QString description; // human-readable description |
| 23 | |
| 24 | std::string primaryTag() const |
| 25 | { |
| 26 | return tags.front(); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | using AircraftStateVec = std::vector<AircraftStateInfo>; |
| 31 | |
| 32 | class StatesModel : public QAbstractListModel |
| 33 | { |
| 34 | Q_OBJECT |
| 35 | |
| 36 | public: |
| 37 | StatesModel(QObject* pr); |
| 38 | |
| 39 | void clear(); |
| 40 | |
| 41 | void initWithStates(const AircraftStateVec& states); |
| 42 | |
| 43 | Q_INVOKABLE int indexForTag(QString s) const; |
| 44 | int indexForTag(const std::string& tag) const; |
| 45 | |
| 46 | int rowCount(const QModelIndex&) const override; |
| 47 | |
| 48 | QVariant data(const QModelIndex& index, int role) const override; |
| 49 | QHash<int, QByteArray> roleNames() const override; |
| 50 | |
| 51 | Q_INVOKABLE QString descriptionForState(int row) const; |
| 52 | Q_INVOKABLE QString tagForState(int row) const; |
| 53 | |
| 54 | bool hasExplicitAuto() const; |
| 55 | bool isEmpty() const; |
| 56 | bool hasState(QString st) const; |
| 57 | |
| 58 | private: |
| 59 | bool makeSafeIndex(int row, size_t& t) const; |
| 60 | |
| 61 | AircraftStateVec _data; |
| 62 | bool _explicitAutoState = false; |
| 63 | }; |
| 64 | class QmlAircraftInfo : public QObject |
| 65 | { |
| 66 | Q_OBJECT |
| 67 | |
| 68 | Q_PROPERTY(QUrl uri READ uri WRITE setUri NOTIFY uriChanged) |
| 69 | Q_PROPERTY(quint32 variant READ variant WRITE setVariant NOTIFY variantChanged) |
| 70 | |
| 71 | Q_PROPERTY(QVariantList previews READ previews NOTIFY infoChanged) |
| 72 | Q_PROPERTY(quint32 numVariants READ numVariants NOTIFY infoChanged) |
| 73 | Q_PROPERTY(QStringList variantNames READ variantNames NOTIFY infoChanged) |
| 74 | |
| 75 | Q_PROPERTY(QString name READ name NOTIFY infoChanged) |
| 76 | Q_PROPERTY(QString description READ description NOTIFY infoChanged) |
| 77 | Q_PROPERTY(QString authors READ authors NOTIFY infoChanged) |
| 78 | Q_PROPERTY(QUrl thumbnail READ thumbnail NOTIFY infoChanged) |
| 79 | Q_PROPERTY(QVariantList ratings READ ratings NOTIFY infoChanged) |
| 80 | |
| 81 | Q_PROPERTY(QString pathOnDisk READ pathOnDisk NOTIFY infoChanged) |
| 82 | Q_PROPERTY(QString packageId READ packageId NOTIFY infoChanged) |
| 83 | Q_PROPERTY(quint64 packageSize READ packageSize NOTIFY infoChanged) |
| 84 | Q_PROPERTY(quint64 downloadedBytes READ downloadedBytes NOTIFY downloadChanged) |
| 85 | Q_PROPERTY(bool isPackaged READ isPackaged NOTIFY infoChanged) |
| 86 | |
| 87 | Q_PROPERTY(QVariant status READ status NOTIFY infoChanged) |
| 88 | Q_PROPERTY(QVariant installStatus READ installStatus NOTIFY downloadChanged) |
| 89 | |
| 90 | Q_PROPERTY(QString minimumFGVersion READ minimumFGVersion NOTIFY infoChanged) |
| 91 | |
| 92 | Q_PROPERTY(QUrl homePage READ homePage NOTIFY infoChanged) |
| 93 | Q_PROPERTY(QUrl supportUrl READ supportUrl NOTIFY infoChanged) |
| 94 | Q_PROPERTY(QUrl wikipediaUrl READ wikipediaUrl NOTIFY infoChanged) |
| 95 | |
| 96 | Q_PROPERTY(QuantityValue cruiseSpeed READ cruiseSpeed NOTIFY infoChanged) |
| 97 | Q_PROPERTY(QuantityValue cruiseAltitude READ cruiseAltitude NOTIFY infoChanged) |
| 98 | Q_PROPERTY(QuantityValue approachSpeed READ approachSpeed NOTIFY infoChanged) |
| 99 | |
| 100 | Q_PROPERTY(QString icaoType READ icaoType NOTIFY infoChanged) |
| 101 | |
| 102 | Q_PROPERTY(bool hasStates READ hasStates NOTIFY infoChanged) |
| 103 | Q_PROPERTY(StatesModel* statesModel READ statesModel CONSTANT) |
| 104 | |
| 105 | Q_PROPERTY(bool favourite READ favourite WRITE setFavourite NOTIFY favouriteChanged) |
| 106 | public: |
| 107 | explicit QmlAircraftInfo(QObject *parent = nullptr); |
| 108 | virtual ~QmlAircraftInfo(); |
| 109 | |
| 110 | QUrl uri() const; |
| 111 | |
| 112 | quint32 numVariants() const; |
| 113 | |
| 114 | QString name() const; |
| 115 | QString description() const; |
| 116 | QString authors() const; |
| 117 | QVariantList ratings() const; |
| 118 | |
| 119 | QVariantList previews() const; |
| 120 | |
| 121 | QUrl thumbnail() const; |
| 122 | QString pathOnDisk() const; |
| 123 | |
| 124 | QUrl homePage() const; |
| 125 | QUrl supportUrl() const; |
| 126 | QUrl wikipediaUrl() const; |
| 127 | |
| 128 | QString packageId() const; |
| 129 | quint64 packageSize() const; |
| 130 | quint64 downloadedBytes() const; |
| 131 | |
| 132 | QVariant status() const; |
| 133 | QString minimumFGVersion() const; |
| 134 | |
| 135 | static QVariant packageAircraftStatus(simgear::pkg::PackageRef p); |
| 136 | |
| 137 | quint32 variant() const |
| 138 | { |
| 139 | return _variant; |
| 140 | } |
| 141 | |
| 142 | QVariant installStatus() const; |
| 143 | |
| 144 | simgear::pkg::PackageRef packageRef() const; |
| 145 | |
| 146 | void setDownloadBytes(quint64 bytes); |
| 147 | |
| 148 | QStringList variantNames() const; |
| 149 | |
| 150 | bool isPackaged() const; |
| 151 | |
| 152 | bool hasStates() const; |
| 153 | |
| 154 | bool hasState(QString name) const; |
| 155 | |
| 156 | bool haveExplicitAutoState() const; |
| 157 | |
| 158 | static const int StateTagRole; |
| 159 | static const int StateDescriptionRole; |
| 160 | static const int StateExplicitRole; |
| 161 | |
| 162 | StatesModel* statesModel(); |
| 163 | |
| 164 | QuantityValue cruiseSpeed() const; |
| 165 | QuantityValue approachSpeed() const; |
| 166 | QuantityValue cruiseAltitude() const; |
| 167 | |
| 168 | QString icaoType() const; |
| 169 | |
| 170 | Q_INVOKABLE bool isSpeedBelowLimits(QuantityValue speed) const; |
| 171 | Q_INVOKABLE bool isAltitudeBelowLimits(QuantityValue speed) const; |
| 172 | |
| 173 | Q_INVOKABLE bool hasTag(QString tag) const; |
| 174 | bool favourite() const; |
| 175 | |
| 176 | signals: |
| 177 | void uriChanged(); |
| 178 | void infoChanged(); |
| 179 | void downloadChanged(); |
| 180 | void variantChanged(quint32 variant); |
| 181 | void favouriteChanged(); |
| 182 | |
| 183 | public slots: |
| 184 | |
| 185 | void setUri(QUrl uri); |
| 186 | |
| 187 | void setVariant(quint32 variant); |
| 188 | |
| 189 | void setFavourite(bool favourite); |
| 190 | |
| 191 | private slots: |
| 192 | void onFavouriteChanged(QUrl u); |
| 193 | void retryValidateLocalProps(); |
| 194 | |
| 195 | private: |
| 196 | AircraftItemPtr resolveItem() const; |
| 197 | |
| 198 | void validateStates() const; |
| 199 | void validateLocalProps() const; |
| 200 | |
| 201 | class Delegate; |
| 202 | std::unique_ptr<Delegate> _delegate; |
| 203 | |
| 204 | simgear::pkg::PackageRef _package; |
| 205 | AircraftItemPtr _item; |
| 206 | quint32 _variant = 0; |
| 207 | quint64 _downloadBytes = 0; |
| 208 | |
| 209 | /// either null or owned by us |
| 210 | mutable StatesModel* _statesModel = nullptr; |
| 211 | |
| 212 | mutable bool _statesChecked = false; |
| 213 | |
| 214 | /// if the aircraft is locally installed, this is the cached |
| 215 | /// parsed contents of the -set.xml. |
| 216 | mutable SGPropertyNode_ptr _cachedProps; |
| 217 | }; |
| 218 | |
| 219 | #endif // QMLAIRCRAFTINFO_HXX |