| 1 | #include "AircraftProxyModel.hxx" |
| 2 | |
| 3 | #include <QSettings> |
| 4 | #include <QDebug> |
| 5 | |
| 6 | #include "AircraftItemModel.hxx" |
| 7 | #include "FavouriteAircraftData.hxx" |
| 8 | #include "SettingsWrapper.hxx" |
| 9 | |
| 10 | #include <simgear/package/Package.hxx> |
| 11 | |
| 12 | AircraftProxyModel::AircraftProxyModel(QObject *pr, QAbstractItemModel * source) : |
| 13 | QSortFilterProxyModel(pr) |
| 14 | { |
| 15 | m_ratings = {4, 4, 4, 4}; |
| 16 | setSourceModel(source); |
| 17 | setSortCaseSensitivity(Qt::CaseInsensitive); |
| 18 | setFilterCaseSensitivity(Qt::CaseInsensitive); |
| 19 | |
| 20 | // important we sort on the primary name role and not Qt::DisplayRole |
| 21 | // otherwise the aircraft jump when switching variant |
| 22 | setSortRole(AircraftVariantDescriptionRole); |
| 23 | |
| 24 | setDynamicSortFilter(true); |
| 25 | |
| 26 | // kick off initial sort |
| 27 | sort(0); |
| 28 | |
| 29 | connect(this, &QAbstractItemModel::rowsInserted, this, &AircraftProxyModel::countChanged); |
| 30 | connect(this, &QAbstractItemModel::rowsRemoved, this, &AircraftProxyModel::countChanged); |
| 31 | connect(this, &QAbstractItemModel::modelReset, this, &AircraftProxyModel::countChanged); |
| 32 | } |
| 33 | |
| 34 | void AircraftProxyModel::setRatings(QList<int> ratings) |
| 35 | { |
| 36 | if (ratings == m_ratings) |
| 37 | return; |
| 38 | m_ratings = ratings; |
| 39 | invalidate(); |
| 40 | emit ratingsChanged(); |
| 41 | emit summaryTextChanged(); |
| 42 | } |
| 43 | |
| 44 | void AircraftProxyModel::setAircraftFilterString(QString s) |
| 45 | { |
| 46 | m_filterString = s; |
| 47 | |
| 48 | m_filterProps = new SGPropertyNode; |
| 49 | int index = 0; |
| 50 | #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) |
| 51 | Q_FOREACH (QString term, s.split(QRegularExpression("\\W+"), Qt::SkipEmptyParts)) { |
| 52 | #else |
| 53 | Q_FOREACH (QString term, s.split(QRegularExpression("\\W+"), QString::SkipEmptyParts)) { |
| 54 | #endif |
| 55 | m_filterProps->getNode("all-of/text", index++, true)->setStringValue(term.toStdString()); |
| 56 | } |
| 57 | |
| 58 | invalidate(); |
| 59 | emit countChanged(); |
| 60 | } |
| 61 | |
| 62 | int AircraftProxyModel::indexForURI(QUrl uri) const |
| 63 | { |
| 64 | auto sourceIndex = qobject_cast<AircraftItemModel*>(sourceModel())->indexOfAircraftURI(uri); |
| 65 | auto ourIndex = mapFromSource(sourceIndex); |
| 66 | if (!sourceIndex.isValid() || !ourIndex.isValid()) { |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | return ourIndex.row(); |
| 71 | } |
| 72 | |
| 73 | void AircraftProxyModel::selectVariantForAircraftURI(QUrl uri) |
| 74 | { |
| 75 | qobject_cast<AircraftItemModel*>(sourceModel())->selectVariantForAircraftURI(uri); |
| 76 | } |
| 77 | |
| 78 | void AircraftProxyModel::setRatingFilterEnabled(bool e) |
| 79 | { |
| 80 | if (e == m_ratingsFilter) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | m_ratingsFilter = e; |
| 85 | invalidate(); |
| 86 | emit ratingsFilterEnabledChanged(); |
| 87 | emit summaryTextChanged(); |
| 88 | emit countChanged(); |
| 89 | } |
| 90 | |
| 91 | QString AircraftProxyModel::summaryText() const |
| 92 | { |
| 93 | const int unfilteredCount = sourceModel()->rowCount(); |
| 94 | if (m_ratingsFilter) { |
| 95 | return tr("(%1 of %2 aircraft)").arg(rowCount()).arg(unfilteredCount); |
| 96 | } |
| 97 | |
| 98 | return tr("(%1 aircraft)").arg(unfilteredCount); |
| 99 | } |
| 100 | |
| 101 | int AircraftProxyModel::count() const |
| 102 | { |
| 103 | return rowCount(); |
| 104 | } |
| 105 | |
| 106 | void AircraftProxyModel::setInstalledFilterEnabled(bool e) |
| 107 | { |
| 108 | if (e == m_onlyShowInstalled) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | m_onlyShowInstalled = e; |
| 113 | invalidate(); |
| 114 | } |
| 115 | |
| 116 | void AircraftProxyModel::setHaveUpdateFilterEnabled(bool e) |
| 117 | { |
| 118 | if (e == m_onlyShowWithUpdate) |
| 119 | return; |
| 120 | |
| 121 | m_onlyShowWithUpdate = e; |
| 122 | invalidate(); |
| 123 | } |
| 124 | |
| 125 | void AircraftProxyModel::setShowFavourites(bool e) |
| 126 | { |
| 127 | if (e == m_onlyShowFavourites) |
| 128 | return; |
| 129 | |
| 130 | m_onlyShowFavourites = e; |
| 131 | if (e) { |
| 132 | setDynamicSortFilter(false); |
| 133 | connect(FavouriteAircraftData::instance(), &FavouriteAircraftData::changed, |
| 134 | [this]() { |
| 135 | this->invalidate(); |
| 136 | }); |
| 137 | } |
| 138 | invalidate(); |
| 139 | } |
| 140 | |
| 141 | bool AircraftProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const |
| 142 | { |
| 143 | QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); |
| 144 | QVariant v = index.data(AircraftPackageStatusRole); |
| 145 | |
| 146 | if (!filterAircraft(index)) { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | if (m_onlyShowInstalled) { |
| 151 | QVariant v = index.data(AircraftPackageStatusRole); |
| 152 | const auto status = static_cast<LocalAircraftCache::PackageStatus>(v.toInt()); |
| 153 | if (status == LocalAircraftCache::PackageNotInstalled) { |
| 154 | return false; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (m_onlyShowWithUpdate) { |
| 159 | QVariant v = index.data(AircraftPackageStatusRole); |
| 160 | const auto status = static_cast<LocalAircraftCache::PackageStatus>(v.toInt()); |
| 161 | switch (status) { |
| 162 | case LocalAircraftCache::PackageNotInstalled: |
| 163 | case LocalAircraftCache::PackageInstalled: |
| 164 | case LocalAircraftCache::NotPackaged: |
| 165 | return false; // no updated need / possible |
| 166 | |
| 167 | // otherwise, show in the update list |
| 168 | default: |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (m_onlyShowFavourites) { |
| 174 | if (!index.data(AircraftIsFavouriteRole).toBool()) |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | // if there is no search active, i.e we are browsing, we might apply the |
| 179 | // ratings filter. |
| 180 | if (m_filterString.isEmpty() && !m_onlyShowInstalled && m_ratingsFilter) { |
| 181 | for (int i=0; i<m_ratings.size(); ++i) { |
| 182 | if (m_ratings.at(i) > index.data(AircraftRatingRole + i).toInt()) { |
| 183 | return false; |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | bool AircraftProxyModel::filterAircraft(const QModelIndex &sourceIndex) const |
| 192 | { |
| 193 | if (m_filterString.isEmpty()) { |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | simgear::pkg::PackageRef pkg = sourceIndex.data(AircraftPackageRefRole).value<simgear::pkg::PackageRef>(); |
| 198 | if (pkg) { |
| 199 | return pkg->matches(m_filterProps.ptr()); |
| 200 | } |
| 201 | |
| 202 | QString baseName = sourceIndex.data(Qt::DisplayRole).toString(); |
| 203 | if (baseName.contains(m_filterString, Qt::CaseInsensitive)) { |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | QString longDesc = sourceIndex.data(AircraftLongDescriptionRole).toString(); |
| 208 | if (longDesc.contains(m_filterString, Qt::CaseInsensitive)) { |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | const int variantCount = sourceIndex.data(AircraftVariantCountRole).toInt(); |
| 213 | for (int variant = 0; variant < variantCount; ++variant) { |
| 214 | QString desc = sourceIndex.data(AircraftVariantDescriptionRole + variant).toString(); |
| 215 | if (desc.contains(m_filterString, Qt::CaseInsensitive)) { |
| 216 | return true; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | void AircraftProxyModel::loadRatingsSettings() |
| 224 | { |
| 225 | auto settings = flightgear::getQSettings(); |
| 226 | m_ratingsFilter = settings.value("enable-ratings-filter", true).toBool(); |
| 227 | QVariantList vRatings = settings.value("ratings-filter").toList(); |
| 228 | if (vRatings.size() == 4) { |
| 229 | for (int i=0; i < 4; ++i) { |
| 230 | m_ratings[i] = vRatings.at(i).toInt(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | invalidate(); |
| 235 | } |
| 236 | |
| 237 | void AircraftProxyModel::saveRatingsSettings() |
| 238 | { |
| 239 | auto settings = flightgear::getQSettings(); |
| 240 | settings.setValue("enable-ratings-filter", m_ratingsFilter); |
| 241 | QVariantList vRatings; |
| 242 | for (int i=0; i < 4; ++i) { |
| 243 | vRatings.append(m_ratings.at(i)); |
| 244 | } |
| 245 | settings.setValue("ratings-filter", vRatings); |
| 246 | } |
| 247 | |
| 248 | /// |
| 249 | /// Custom sorting based on aircraft variants and URI |
| 250 | /// |
| 251 | /// \param left first item to sort |
| 252 | /// \param right second item to sort |
| 253 | /// \return 0 when the items are equal, < 0 or > 0 when they differs |
| 254 | bool AircraftProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const |
| 255 | { |
| 256 | const QString variantLeft = left.data(AircraftVariantDescriptionRole).toString(); |
| 257 | const QString variantRight = right.data(AircraftVariantDescriptionRole).toString(); |
| 258 | |
| 259 | // we're comparing by default by variantDescriptionRole but when the variantDescriptionRole |
| 260 | // is equal (e.g. two the same aircrafts installed from different sources - fgaddon + git) |
| 261 | // we sort them by the AircraftURIRole. This ensures that the order of the same |
| 262 | // items in the view is constant |
| 263 | const int c = QString::compare(variantLeft, variantRight, Qt::CaseInsensitive); |
| 264 | if (c == 0) { |
| 265 | const QString uriLeft = left.data(AircraftURIRole).toString(); |
| 266 | const QString uriRight = right.data(AircraftURIRole).toString(); |
| 267 | return QString::localeAwareCompare(uriLeft, uriRight) < 0; |
| 268 | } else { |
| 269 | return c < 0; |
| 270 | } |
| 271 | } |