diff --git a/src/GUI/AircraftProxyModel.cxx b/src/GUI/AircraftProxyModel.cxx index 0cd44add6..86f199352 100644 --- a/src/GUI/AircraftProxyModel.cxx +++ b/src/GUI/AircraftProxyModel.cxx @@ -16,8 +16,6 @@ AircraftProxyModel::AircraftProxyModel(QObject *pr, QAbstractItemModel * source) setSortCaseSensitivity(Qt::CaseInsensitive); setFilterCaseSensitivity(Qt::CaseInsensitive); - - // important we sort on the primary name role and not Qt::DisplayRole // otherwise the aircraft jump when switching variant setSortRole(AircraftVariantDescriptionRole); @@ -240,3 +238,27 @@ void AircraftProxyModel::saveRatingsSettings() settings.setValue("ratings-filter", vRatings); } +/// +/// Custom sorting based on aircraft variants and URI +/// +/// \param left first item to sort +/// \param right second item to sort +/// \return 0 when the items are equal, < 0 or > 0 when they differs +bool AircraftProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const +{ + const QString variantLeft = left.data(AircraftVariantDescriptionRole).toString(); + const QString variantRight = right.data(AircraftVariantDescriptionRole).toString(); + + // we're comparing by default by variantDescriptionRole but when the variantDescriptionRole + // is equal (e.g. two the same aircrafts installed from different sources - fgaddon + git) + // we sort them by the AircraftURIRole. This ensures that the order of the same + // items in the view is constant + const int c = QString::compare(variantLeft, variantRight, Qt::CaseInsensitive); + if (c == 0) { + const QString uriLeft = left.data(AircraftURIRole).toString(); + const QString uriRight = right.data(AircraftURIRole).toString(); + return QString::localeAwareCompare(uriLeft, uriRight) < 0; + } else { + return c < 0; + } +} \ No newline at end of file diff --git a/src/GUI/AircraftProxyModel.hxx b/src/GUI/AircraftProxyModel.hxx index 10d0bb32e..2a362a0fd 100644 --- a/src/GUI/AircraftProxyModel.hxx +++ b/src/GUI/AircraftProxyModel.hxx @@ -65,6 +65,7 @@ public slots: void setShowFavourites(bool e); protected: bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; + bool lessThan(const QModelIndex& left, const QModelIndex& right) const override; private: bool filterAircraft(const QModelIndex& sourceIndex) const;