diff options
Diffstat (limited to 'src/corelib/itemmodels')
-rw-r--r-- | src/corelib/itemmodels/qabstractitemmodel.cpp | 102 | ||||
-rw-r--r-- | src/corelib/itemmodels/qabstractitemmodel.h | 42 | ||||
-rw-r--r-- | src/corelib/itemmodels/qitemselectionmodel.cpp | 10 | ||||
-rw-r--r-- | src/corelib/itemmodels/qitemselectionmodel.h | 14 | ||||
-rw-r--r-- | src/corelib/itemmodels/qsortfilterproxymodel.cpp | 11 |
5 files changed, 107 insertions, 72 deletions
diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index c1749526bc..216db3b42a 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -288,6 +288,9 @@ void QPersistentModelIndexData::destroy(QPersistentModelIndexData *data) \brief The QPersistentModelIndex class is used to locate data in a data model. \ingroup model-view + \compares strong + \compareswith strong QModelIndex + \endcompareswith A QPersistentModelIndex is a model index that can be stored by an application, and later used to access information in a model. @@ -375,45 +378,53 @@ QPersistentModelIndex::~QPersistentModelIndex() } /*! - Returns \c{true} if this persistent model index is equal to the \a other - persistent model index; otherwise returns \c{false}. + \fn bool QPersistentModelIndex::operator==(const QPersistentModelIndex &lhs, const QPersistentModelIndex &rhs) + Returns \c{true} if \a lhs persistent model index is equal to the \a rhs + persistent model index; otherwise returns \c{false}. - The internal data pointer, row, column, and model values in the persistent - model index are used when comparing with another persistent model index. + The internal data pointer, row, column, and model values in the persistent + model index are used when comparing with another persistent model index. */ -bool QPersistentModelIndex::operator==(const QPersistentModelIndex &other) const noexcept +/*! + \fn bool QPersistentModelIndex::operator!=(const QPersistentModelIndex &lhs, const QPersistentModelIndex &rhs) + \since 4.2 + + Returns \c{true} if \a lhs persistent model index is not equal to the \a rhs + persistent model index; otherwise returns \c{false}. +*/ +bool comparesEqual(const QPersistentModelIndex &lhs, const QPersistentModelIndex &rhs) noexcept { - if (d && other.d) - return d->index == other.d->index; - return d == other.d; + if (lhs.d && rhs.d) + return lhs.d->index == rhs.d->index; + return lhs.d == rhs.d; } /*! + \fn bool QPersistentModelIndex::operator<(const QPersistentModelIndex &lhs, const QPersistentModelIndex &rhs) \since 4.1 - Returns \c{true} if this persistent model index is smaller than the \a other + Returns \c{true} if \a lhs persistent model index is smaller than the \a rhs persistent model index; otherwise returns \c{false}. The internal data pointer, row, column, and model values in the persistent model index are used when comparing with another persistent model index. */ - -bool QPersistentModelIndex::operator<(const QPersistentModelIndex &other) const noexcept +Qt::strong_ordering compareThreeWay(const QPersistentModelIndex &lhs, + const QPersistentModelIndex &rhs) noexcept { - if (d && other.d) - return d->index < other.d->index; + if (lhs.d && rhs.d) + return compareThreeWay(lhs.d->index, rhs.d->index); - return std::less<>{}(d, other.d); + using Qt::totally_ordered_wrapper; + return compareThreeWay(totally_ordered_wrapper{lhs.d}, totally_ordered_wrapper{rhs.d}); } -/*! - \fn bool QPersistentModelIndex::operator!=(const QPersistentModelIndex &other) const - \since 4.2 - - Returns \c{true} if this persistent model index is not equal to the \a - other persistent model index; otherwise returns \c{false}. -*/ +Qt::strong_ordering compareThreeWay(const QPersistentModelIndex &lhs, + const QModelIndex &rhs) noexcept +{ + return compareThreeWay(lhs.d ? lhs.d->index : QModelIndex{}, rhs); +} /*! Sets the persistent model index to refer to the same item in a model @@ -470,32 +481,26 @@ QPersistentModelIndex::operator QModelIndex() const } /*! - Returns \c{true} if this persistent model index refers to the same location as - the \a other model index; otherwise returns \c{false}. + \fn bool QPersistentModelIndex::operator==(const QPersistentModelIndex &lhs, const QModelIndex &rhs) + Returns \c{true} if \a lhs persistent model index refers to the same location as + the \a rhs model index; otherwise returns \c{false}. The internal data pointer, row, column, and model values in the persistent model index are used when comparing with another model index. -*/ - -bool QPersistentModelIndex::operator==(const QModelIndex &other) const noexcept -{ - if (d) - return d->index == other; - return !other.isValid(); -} + */ /*! - \fn bool QPersistentModelIndex::operator!=(const QModelIndex &other) const + \fn bool QPersistentModelIndex::operator!=(const QPersistentModelIndex &lhs, const QModelIndex &rhs) - Returns \c{true} if this persistent model index does not refer to the same - location as the \a other model index; otherwise returns \c{false}. + Returns \c{true} if \a lhs persistent model index does not refer to the same + location as the \a rhs model index; otherwise returns \c{false}. */ -bool QPersistentModelIndex::operator!=(const QModelIndex &other) const noexcept +bool comparesEqual(const QPersistentModelIndex &lhs, const QModelIndex &rhs) noexcept { - if (d) - return d->index != other; - return other.isValid(); + if (lhs.d) + return lhs.d->index == rhs; + return !rhs.isValid(); } /*! @@ -686,6 +691,7 @@ QDebug operator<<(QDebug dbg, const QPersistentModelIndex &idx) class QEmptyItemModel : public QAbstractItemModel { + Q_OBJECT public: explicit QEmptyItemModel(QObject *parent = nullptr) : QAbstractItemModel(parent) {} QModelIndex index(int, int, const QModelIndex &) const override { return QModelIndex(); } @@ -1157,6 +1163,7 @@ void QAbstractItemModel::resetInternalData() \ingroup model-view + \compares strong This class is used as an index into item models derived from QAbstractItemModel. The index is used by item views, delegates, and @@ -1327,24 +1334,22 @@ void QAbstractItemModel::resetInternalData() */ /*! - \fn bool QModelIndex::operator==(const QModelIndex &other) const + \fn bool QModelIndex::operator==(const QModelIndex &lhs, const QModelIndex &rhs) - Returns \c{true} if this model index refers to the same location as the - \a other model index; otherwise returns \c{false}. + Returns \c{true} if \a lhs model index refers to the same location as the + \a rhs model index; otherwise returns \c{false}. The internal data pointer, row, column, and model values are used when comparing with another model index. */ - /*! - \fn bool QModelIndex::operator!=(const QModelIndex &other) const + \fn bool QModelIndex::operator!=(const QModelIndex &lhs, const QModelIndex &rhs) - Returns \c{true} if this model index does not refer to the same location as - the \a other model index; otherwise returns \c{false}. + Returns \c{true} if \a lhs model index does not refer to the same location as + the \a rhs model index; otherwise returns \c{false}. */ - /*! \fn QModelIndex QModelIndex::parent() const @@ -4123,10 +4128,10 @@ bool QAbstractListModel::dropMimeData(const QMimeData *data, Qt::DropAction acti */ /*! - \fn bool QModelIndex::operator<(const QModelIndex &other) const + \fn bool QModelIndex::operator<(const QModelIndex &lhs, const QModelIndex &rhs) \since 4.1 - Returns \c{true} if this model index is smaller than the \a other + Returns \c{true} if \a lhs model index is smaller than the \a rhs model index; otherwise returns \c{false}. The less than calculation is not directly useful to developers - the way that indexes @@ -4169,3 +4174,4 @@ void QAbstractItemModelPrivate::Persistent::insertMultiAtEnd(const QModelIndex& QT_END_NAMESPACE #include "moc_qabstractitemmodel.cpp" +#include "qabstractitemmodel.moc" diff --git a/src/corelib/itemmodels/qabstractitemmodel.h b/src/corelib/itemmodels/qabstractitemmodel.h index 8f22f14989..418fc6b864 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.h +++ b/src/corelib/itemmodels/qabstractitemmodel.h @@ -5,11 +5,14 @@ #ifndef QABSTRACTITEMMODEL_H #define QABSTRACTITEMMODEL_H +#include <QtCore/qcompare.h> #include <QtCore/qhash.h> #include <QtCore/qlist.h> #include <QtCore/qobject.h> #include <QtCore/qvariant.h> +#include <tuple> + QT_REQUIRE_CONFIG(itemmodel); QT_BEGIN_NAMESPACE @@ -138,19 +141,16 @@ public: inline QVariant data(int role = Qt::DisplayRole) const; inline void multiData(QModelRoleDataSpan roleDataSpan) const; inline Qt::ItemFlags flags() const; - constexpr inline const QAbstractItemModel *model() const noexcept { return m; } + constexpr inline const QAbstractItemModel *model() const noexcept { return m.get(); } constexpr inline bool isValid() const noexcept { return (r >= 0) && (c >= 0) && (m != nullptr); } - constexpr inline bool operator==(const QModelIndex &other) const noexcept - { return (other.r == r) && (other.i == i) && (other.c == c) && (other.m == m); } - constexpr inline bool operator!=(const QModelIndex &other) const noexcept - { return !(*this == other); } - constexpr inline bool operator<(const QModelIndex &other) const noexcept - { - return r < other.r - || (r == other.r && (c < other.c - || (c == other.c && (i < other.i - || (i == other.i && std::less<const QAbstractItemModel *>()(m, other.m)))))); - } + +private: + constexpr auto asTuple() const noexcept { return std::tie(r, c, i, m); } + friend constexpr bool comparesEqual(const QModelIndex &lhs, const QModelIndex &rhs) noexcept + { return lhs.asTuple() == rhs.asTuple(); } + friend constexpr Qt::strong_ordering compareThreeWay(const QModelIndex &lhs, const QModelIndex &rhs) noexcept + { return QtOrderingPrivate::compareThreeWayMulti(lhs.asTuple(), rhs.asTuple()); } + Q_DECLARE_STRONGLY_ORDERED_LITERAL_TYPE(QModelIndex) private: inline QModelIndex(int arow, int acolumn, const void *ptr, const QAbstractItemModel *amodel) noexcept : r(arow), c(acolumn), i(reinterpret_cast<quintptr>(ptr)), m(amodel) {} @@ -158,7 +158,7 @@ private: : r(arow), c(acolumn), i(id), m(amodel) {} int r, c; quintptr i; - const QAbstractItemModel *m; + Qt::totally_ordered_wrapper<const QAbstractItemModel *> m; }; Q_DECLARE_TYPEINFO(QModelIndex, Q_RELOCATABLE_TYPE); @@ -178,17 +178,21 @@ public: QPersistentModelIndex(const QModelIndex &index); QPersistentModelIndex(const QPersistentModelIndex &other); ~QPersistentModelIndex(); +#if QT_CORE_REMOVED_SINCE(6, 8) bool operator<(const QPersistentModelIndex &other) const noexcept; bool operator==(const QPersistentModelIndex &other) const noexcept; inline bool operator!=(const QPersistentModelIndex &other) const noexcept { return !operator==(other); } +#endif QPersistentModelIndex &operator=(const QPersistentModelIndex &other); inline QPersistentModelIndex(QPersistentModelIndex &&other) noexcept : d(std::exchange(other.d, nullptr)) {} QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPersistentModelIndex) void swap(QPersistentModelIndex &other) noexcept { qt_ptr_swap(d, other.d); } +#if QT_CORE_REMOVED_SINCE(6, 8) bool operator==(const QModelIndex &other) const noexcept; bool operator!=(const QModelIndex &other) const noexcept; +#endif QPersistentModelIndex &operator=(const QModelIndex &other); operator QModelIndex() const; int row() const; @@ -208,6 +212,18 @@ private: friend size_t qHash(const QPersistentModelIndex &, size_t seed) noexcept; friend bool qHashEquals(const QPersistentModelIndex &a, const QPersistentModelIndex &b) noexcept { return a.d == b.d; } + friend Q_CORE_EXPORT bool + comparesEqual(const QPersistentModelIndex &lhs, const QPersistentModelIndex &rhs) noexcept; + friend Q_CORE_EXPORT bool + comparesEqual(const QPersistentModelIndex &lhs, const QModelIndex &rhs) noexcept; + friend Q_CORE_EXPORT Qt::strong_ordering // ### Qt 7: partial_ordering? + compareThreeWay(const QPersistentModelIndex &lhs, const QPersistentModelIndex &rhs) noexcept; + friend Q_CORE_EXPORT Qt::strong_ordering // ### Qt 7: partial_ordering? + compareThreeWay(const QPersistentModelIndex &lhs, const QModelIndex &rhs) noexcept; +#if !QT_CORE_REMOVED_SINCE(6, 8) + Q_DECLARE_STRONGLY_ORDERED(QPersistentModelIndex) + Q_DECLARE_STRONGLY_ORDERED(QPersistentModelIndex, QModelIndex) +#endif #ifndef QT_NO_DEBUG_STREAM friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &); #endif diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp index 70063a10bf..6df60aaf61 100644 --- a/src/corelib/itemmodels/qitemselectionmodel.cpp +++ b/src/corelib/itemmodels/qitemselectionmodel.cpp @@ -27,6 +27,8 @@ QT_IMPL_METATYPE_EXTERN(QItemSelection) \ingroup model-view + \compares equality + A QItemSelectionRange contains information about a range of selected items in a model. A range of items is a contiguous array of model items, extending to cover a number of adjacent rows and @@ -216,17 +218,17 @@ QItemSelectionRange QItemSelectionRange::intersected(const QItemSelectionRange & } /*! - \fn bool QItemSelectionRange::operator==(const QItemSelectionRange &other) const + \fn bool QItemSelectionRange::operator==(const QItemSelectionRange &lhs, const QItemSelectionRange &rhs) - Returns \c true if the selection range is exactly the same as the \a other + Returns \c true if \a lhs selection range is exactly the same as the \a rhs range given; otherwise returns \c false. */ /*! - \fn bool QItemSelectionRange::operator!=(const QItemSelectionRange &other) const + \fn bool QItemSelectionRange::operator!=(const QItemSelectionRange &lhs, const QItemSelectionRange &rhs) - Returns \c true if the selection range differs from the \a other range given; + Returns \c true if \a lhs selection range differs from the \a rhs range given; otherwise returns \c false. */ diff --git a/src/corelib/itemmodels/qitemselectionmodel.h b/src/corelib/itemmodels/qitemselectionmodel.h index a19923a90b..aaf32e68ef 100644 --- a/src/corelib/itemmodels/qitemselectionmodel.h +++ b/src/corelib/itemmodels/qitemselectionmodel.h @@ -57,12 +57,12 @@ public: bool intersects(const QItemSelectionRange &other) const; QItemSelectionRange intersected(const QItemSelectionRange &other) const; - +#if QT_CORE_REMOVED_SINCE(6, 8) inline bool operator==(const QItemSelectionRange &other) const - { return (tl == other.tl && br == other.br); } + { return comparesEqual(*this, other); } inline bool operator!=(const QItemSelectionRange &other) const - { return !operator==(other); } - + { return !operator==(other); } +#endif inline bool isValid() const { return (tl.isValid() && br.isValid() && tl.parent() == br.parent() @@ -74,6 +74,12 @@ public: QModelIndexList indexes() const; private: + friend bool comparesEqual(const QItemSelectionRange &lhs, + const QItemSelectionRange &rhs) noexcept + { + return comparesEqual(lhs.tl, rhs.tl) && comparesEqual(lhs.br, rhs.br); + } + Q_DECLARE_EQUALITY_COMPARABLE(QItemSelectionRange) QPersistentModelIndex tl, br; }; Q_DECLARE_TYPEINFO(QItemSelectionRange, Q_RELOCATABLE_TYPE); diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp index a9ead2e1eb..a5284dbad4 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp +++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp @@ -688,8 +688,10 @@ void QSortFilterProxyModelPrivate::sort_source_rows( QSortFilterProxyModelGreaterThan gt(source_sort_column, source_parent, model, q); std::stable_sort(source_rows.begin(), source_rows.end(), gt); } - } else { // restore the source model order - std::stable_sort(source_rows.begin(), source_rows.end()); + } else if (sort_order == Qt::AscendingOrder) { + std::stable_sort(source_rows.begin(), source_rows.end(), std::less{}); + } else { + std::stable_sort(source_rows.begin(), source_rows.end(), std::greater{}); } } @@ -2490,7 +2492,10 @@ QSize QSortFilterProxyModel::span(const QModelIndex &index) const } /*! - \reimp + \reimp + Sorts the model by \a column in the given \a order. + If the sort \a column is less than zero, the model will be sorted by source model row + in the given \a order. */ void QSortFilterProxyModel::sort(int column, Qt::SortOrder order) { |