diff options
Diffstat (limited to 'src/corelib/itemmodels')
-rw-r--r-- | src/corelib/itemmodels/qabstractitemmodel.cpp | 153 | ||||
-rw-r--r-- | src/corelib/itemmodels/qabstractitemmodel.h | 55 | ||||
-rw-r--r-- | src/corelib/itemmodels/qabstractproxymodel.h | 46 | ||||
-rw-r--r-- | src/corelib/itemmodels/qidentityproxymodel.h | 40 | ||||
-rw-r--r-- | src/corelib/itemmodels/qitemselectionmodel.cpp | 10 | ||||
-rw-r--r-- | src/corelib/itemmodels/qitemselectionmodel.h | 2 | ||||
-rw-r--r-- | src/corelib/itemmodels/qsortfilterproxymodel.cpp | 7 | ||||
-rw-r--r-- | src/corelib/itemmodels/qsortfilterproxymodel.h | 62 | ||||
-rw-r--r-- | src/corelib/itemmodels/qstringlistmodel.h | 22 |
9 files changed, 278 insertions, 119 deletions
diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index f893cf06e3..af87f56255 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -48,11 +48,14 @@ #include <qstack.h> #include <qbitarray.h> #include <qdatetime.h> +#include <qloggingcategory.h> #include <limits.h> QT_BEGIN_NAMESPACE +Q_LOGGING_CATEGORY(lcCheckIndex, "qt.core.qabstractitemmodel.checkindex") + QPersistentModelIndexData *QPersistentModelIndexData::create(const QModelIndex &index) { Q_ASSERT(index.isValid()); // we will _never_ insert an invalid index in the list @@ -482,12 +485,12 @@ class QEmptyItemModel : public QAbstractItemModel { public: explicit QEmptyItemModel(QObject *parent = 0) : QAbstractItemModel(parent) {} - QModelIndex index(int, int, const QModelIndex &) const Q_DECL_OVERRIDE { return QModelIndex(); } - QModelIndex parent(const QModelIndex &) const Q_DECL_OVERRIDE { return QModelIndex(); } - int rowCount(const QModelIndex &) const Q_DECL_OVERRIDE { return 0; } - int columnCount(const QModelIndex &) const Q_DECL_OVERRIDE { return 0; } - bool hasChildren(const QModelIndex &) const Q_DECL_OVERRIDE { return false; } - QVariant data(const QModelIndex &, int) const Q_DECL_OVERRIDE { return QVariant(); } + QModelIndex index(int, int, const QModelIndex &) const override { return QModelIndex(); } + QModelIndex parent(const QModelIndex &) const override { return QModelIndex(); } + int rowCount(const QModelIndex &) const override { return 0; } + int columnCount(const QModelIndex &) const override { return 0; } + bool hasChildren(const QModelIndex &) const override { return false; } + QVariant data(const QModelIndex &, int) const override { return QVariant(); } }; Q_GLOBAL_STATIC(QEmptyItemModel, qEmptyModel) @@ -2686,6 +2689,7 @@ bool QAbstractItemModel::decodeData(int row, int column, const QModelIndex &pare void QAbstractItemModel::beginInsertRows(const QModelIndex &parent, int first, int last) { Q_ASSERT(first >= 0); + Q_ASSERT(first <= rowCount(parent)); // == is allowed, to insert at the end Q_ASSERT(last >= first); Q_D(QAbstractItemModel); d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last)); @@ -2741,6 +2745,7 @@ void QAbstractItemModel::beginRemoveRows(const QModelIndex &parent, int first, i { Q_ASSERT(first >= 0); Q_ASSERT(last >= first); + Q_ASSERT(last < rowCount(parent)); Q_D(QAbstractItemModel); d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last)); emit rowsAboutToBeRemoved(parent, first, last, QPrivateSignal()); @@ -2987,6 +2992,7 @@ void QAbstractItemModel::endMoveRows() void QAbstractItemModel::beginInsertColumns(const QModelIndex &parent, int first, int last) { Q_ASSERT(first >= 0); + Q_ASSERT(first <= columnCount(parent)); // == is allowed, to insert at the end Q_ASSERT(last >= first); Q_D(QAbstractItemModel); d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last)); @@ -3043,6 +3049,7 @@ void QAbstractItemModel::beginRemoveColumns(const QModelIndex &parent, int first { Q_ASSERT(first >= 0); Q_ASSERT(last >= first); + Q_ASSERT(last < columnCount(parent)); Q_D(QAbstractItemModel); d->changes.push(QAbstractItemModelPrivate::Change(parent, first, last)); emit columnsAboutToBeRemoved(parent, first, last, QPrivateSignal()); @@ -3316,6 +3323,140 @@ QModelIndexList QAbstractItemModel::persistentIndexList() const return result; } +/*! + \enum QAbstractItemModel::CheckIndexOption + \since 5.11 + + This enum can be used to control the checks performed by + QAbstractItemModel::checkIndex(). + + \value CheckIndexOption::NoOption No check options are specified. + + \value CheckIndexOption::IndexIsValid The model index passed to + QAbstractItemModel::checkIndex() is checked to be a valid model index. + + \value CheckIndexOption::DoNotUseParent Does not perform any check + involving the usage of the parent of the index passed to + QAbstractItemModel::checkIndex(). + + \value CheckIndexOption::ParentIsInvalid The parent of the model index + passed to QAbstractItemModel::checkIndex() is checked to be an invalid + model index. If both this option and CheckIndexOption::DoNotUseParent + are specified, then this option is ignored. +*/ + +/*! + \since 5.11 + + This function checks whether \a index is a legal model index for + this model. A legal model index is either an invalid model index, or a + valid model index for which all the following holds: + + \list + + \li the index' model is \c{this}; + \li the index' row is greater or equal than zero; + \li the index' row is less than the row count for the index' parent; + \li the index' column is greater or equal than zero; + \li the index' column is less than the column count for the index' parent. + + \endlist + + The \a options argument may change some of these checks. If \a options + contains \c{CheckIndexOption::IndexIsValid}, then \a index must be a valid + index; this is useful when reimplementing functions such as \l{data()} or + \l{setData()}, which expect valid indexes. + + If \a options contains \c{CheckIndexOption::DoNotUseParent}, then the + checks that would call \l{parent()} are omitted; this allows calling this + function from a \l{parent()} reimplementation (otherwise, this would result + in endless recursion and a crash). + + If \a options does not contain \c{CheckIndexOption::DoNotUseParent}, and it + contains \c{CheckIndexOption::ParentIsInvalid}, then an additional check is + performed: the parent index is checked for not being valid. This is useful + when implementing flat models such as lists or tables, where no model index + should have a valid parent index. + + This function returns true if all the checks succeeded, and false otherwise. + This allows to use the function in \l{Q_ASSERT} and similar other debugging + mechanisms. If some check failed, a warning message will be printed in the + \c{qt.core.qabstractitemmodel.checkindex} logging category, containing + some information that may be useful for debugging the failure. + + \note This function is a debugging helper for implementing your own item + models. When developing complex models, as well as when building + complicated model hierarchies (e.g. using proxy models), it is useful to + call this function in order to catch bugs relative to illegal model indices + (as defined above) accidentally passed to some QAbstractItemModel API. + + \warning Note that it's undefined behavior to pass illegal indices to item + models, so applications must refrain from doing so, and not rely on any + "defensive" programming that item models could employ to handle illegal + indexes gracefully. + + \sa QModelIndex +*/ +bool QAbstractItemModel::checkIndex(const QModelIndex &index, CheckIndexOptions options) const +{ + if (!index.isValid()) { + if (options & CheckIndexOption::IndexIsValid) { + qCWarning(lcCheckIndex) << "Index" << index << "is not valid (expected valid)"; + return false; + } + return true; + } + + if (index.model() != this) { + qCWarning(lcCheckIndex) << "Index" << index + << "is for model" << index.model() + << "which is different from this model" << this; + return false; + } + + if (index.row() < 0) { + qCWarning(lcCheckIndex) << "Index" << index + << "has negative row" << index.row(); + return false; + } + + if (index.column() < 0) { + qCWarning(lcCheckIndex) << "Index" << index + << "has negative column" << index.column(); + return false; + } + + if (!(options & CheckIndexOption::DoNotUseParent)) { + const QModelIndex parentIndex = index.parent(); + if (options & CheckIndexOption::ParentIsInvalid) { + if (parentIndex.isValid()) { + qCWarning(lcCheckIndex) << "Index" << index + << "has valid parent" << parentIndex + << "(expected an invalid parent)"; + return false; + } + } + + const int rc = rowCount(parentIndex); + if (index.row() >= rc) { + qCWarning(lcCheckIndex) << "Index" << index + << "has out of range row" << index.row() + << "rowCount() is" << rc; + return false; + } + + const int cc = columnCount(parentIndex); + if (index.column() >= cc) { + qCWarning(lcCheckIndex) << "Index" << index + << "has out of range column" << index.column() + << "columnCount() is" << cc; + return false; + + } + } + + return true; +} /*! \class QAbstractTableModel diff --git a/src/corelib/itemmodels/qabstractitemmodel.h b/src/corelib/itemmodels/qabstractitemmodel.h index a211d8e8ca..621f284c34 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.h +++ b/src/corelib/itemmodels/qabstractitemmodel.h @@ -55,7 +55,7 @@ class Q_CORE_EXPORT QModelIndex { friend class QAbstractItemModel; public: - Q_DECL_CONSTEXPR inline QModelIndex() Q_DECL_NOTHROW : r(-1), c(-1), i(0), m(Q_NULLPTR) {} + Q_DECL_CONSTEXPR inline QModelIndex() Q_DECL_NOTHROW : r(-1), c(-1), i(0), m(nullptr) {} // compiler-generated copy/move ctors/assignment operators are fine! Q_DECL_CONSTEXPR inline int row() const Q_DECL_NOTHROW { return r; } Q_DECL_CONSTEXPR inline int column() const Q_DECL_NOTHROW { return c; } @@ -69,7 +69,7 @@ public: inline QVariant data(int role = Qt::DisplayRole) const; inline Qt::ItemFlags flags() const; Q_DECL_CONSTEXPR inline const QAbstractItemModel *model() const Q_DECL_NOTHROW { return m; } - Q_DECL_CONSTEXPR inline bool isValid() const Q_DECL_NOTHROW { return (r >= 0) && (c >= 0) && (m != Q_NULLPTR); } + Q_DECL_CONSTEXPR inline bool isValid() const Q_DECL_NOTHROW { return (r >= 0) && (c >= 0) && (m != nullptr); } Q_DECL_CONSTEXPR inline bool operator==(const QModelIndex &other) const Q_DECL_NOTHROW { return (other.r == r) && (other.i == i) && (other.c == c) && (other.m == m); } Q_DECL_CONSTEXPR inline bool operator!=(const QModelIndex &other) const Q_DECL_NOTHROW @@ -115,7 +115,7 @@ public: QPersistentModelIndex &operator=(const QPersistentModelIndex &other); #ifdef Q_COMPILER_RVALUE_REFS inline QPersistentModelIndex(QPersistentModelIndex &&other) Q_DECL_NOTHROW - : d(other.d) { other.d = Q_NULLPTR; } + : d(other.d) { other.d = nullptr; } inline QPersistentModelIndex &operator=(QPersistentModelIndex &&other) Q_DECL_NOTHROW { qSwap(d, other.d); return *this; } #endif @@ -171,7 +171,7 @@ class Q_CORE_EXPORT QAbstractItemModel : public QObject friend class QIdentityProxyModel; public: - explicit QAbstractItemModel(QObject *parent = Q_NULLPTR); + explicit QAbstractItemModel(QObject *parent = nullptr); virtual ~QAbstractItemModel(); Q_INVOKABLE bool hasIndex(int row, int column, const QModelIndex &parent = QModelIndex()) const; @@ -250,6 +250,17 @@ public: }; Q_ENUM(LayoutChangeHint) + enum class CheckIndexOption { + NoOption = 0x0000, + IndexIsValid = 0x0001, + DoNotUseParent = 0x0002, + ParentIsInvalid = 0x0004, + }; + Q_ENUM(CheckIndexOption) + Q_DECLARE_FLAGS(CheckIndexOptions, CheckIndexOption) + + Q_REQUIRED_RESULT bool checkIndex(const QModelIndex &index, CheckIndexOptions options = CheckIndexOption::NoOption) const; + Q_SIGNALS: void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>()); void headerDataChanged(Qt::Orientation orientation, int first, int last); @@ -286,9 +297,9 @@ protected Q_SLOTS: void resetInternalData(); protected: - QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent = Q_NULLPTR); + QAbstractItemModel(QAbstractItemModelPrivate &dd, QObject *parent = nullptr); - inline QModelIndex createIndex(int row, int column, void *data = Q_NULLPTR) const; + inline QModelIndex createIndex(int row, int column, void *data = nullptr) const; inline QModelIndex createIndex(int row, int column, quintptr id) const; void encodeData(const QModelIndexList &indexes, QDataStream &stream) const; @@ -343,6 +354,8 @@ private: Q_DISABLE_COPY(QAbstractItemModel) }; +Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractItemModel::CheckIndexOptions) + inline bool QAbstractItemModel::insertRow(int arow, const QModelIndex &aparent) { return insertRows(arow, 1, aparent); } inline bool QAbstractItemModel::insertColumn(int acolumn, const QModelIndex &aparent) @@ -367,15 +380,15 @@ class Q_CORE_EXPORT QAbstractTableModel : public QAbstractItemModel Q_OBJECT public: - explicit QAbstractTableModel(QObject *parent = Q_NULLPTR); + explicit QAbstractTableModel(QObject *parent = nullptr); ~QAbstractTableModel(); - QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; - QModelIndex sibling(int row, int column, const QModelIndex &idx) const Q_DECL_OVERRIDE; + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; + QModelIndex sibling(int row, int column, const QModelIndex &idx) const override; bool dropMimeData(const QMimeData *data, Qt::DropAction action, - int row, int column, const QModelIndex &parent) Q_DECL_OVERRIDE; + int row, int column, const QModelIndex &parent) override; - Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE; + Qt::ItemFlags flags(const QModelIndex &index) const override; using QObject::parent; @@ -384,8 +397,8 @@ protected: private: Q_DISABLE_COPY(QAbstractTableModel) - QModelIndex parent(const QModelIndex &child) const Q_DECL_OVERRIDE; - bool hasChildren(const QModelIndex &parent) const Q_DECL_OVERRIDE; + QModelIndex parent(const QModelIndex &child) const override; + bool hasChildren(const QModelIndex &parent) const override; }; class Q_CORE_EXPORT QAbstractListModel : public QAbstractItemModel @@ -393,15 +406,15 @@ class Q_CORE_EXPORT QAbstractListModel : public QAbstractItemModel Q_OBJECT public: - explicit QAbstractListModel(QObject *parent = Q_NULLPTR); + explicit QAbstractListModel(QObject *parent = nullptr); ~QAbstractListModel(); - QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; - QModelIndex sibling(int row, int column, const QModelIndex &idx) const Q_DECL_OVERRIDE; + QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override; + QModelIndex sibling(int row, int column, const QModelIndex &idx) const override; bool dropMimeData(const QMimeData *data, Qt::DropAction action, - int row, int column, const QModelIndex &parent) Q_DECL_OVERRIDE; + int row, int column, const QModelIndex &parent) override; - Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE; + Qt::ItemFlags flags(const QModelIndex &index) const override; using QObject::parent; @@ -410,9 +423,9 @@ protected: private: Q_DISABLE_COPY(QAbstractListModel) - QModelIndex parent(const QModelIndex &child) const Q_DECL_OVERRIDE; - int columnCount(const QModelIndex &parent) const Q_DECL_OVERRIDE; - bool hasChildren(const QModelIndex &parent) const Q_DECL_OVERRIDE; + QModelIndex parent(const QModelIndex &child) const override; + int columnCount(const QModelIndex &parent) const override; + bool hasChildren(const QModelIndex &parent) const override; }; // inline implementations diff --git a/src/corelib/itemmodels/qabstractproxymodel.h b/src/corelib/itemmodels/qabstractproxymodel.h index c9eafa09ee..6aa82b21ee 100644 --- a/src/corelib/itemmodels/qabstractproxymodel.h +++ b/src/corelib/itemmodels/qabstractproxymodel.h @@ -56,7 +56,7 @@ class Q_CORE_EXPORT QAbstractProxyModel : public QAbstractItemModel Q_PROPERTY(QAbstractItemModel* sourceModel READ sourceModel WRITE setSourceModel NOTIFY sourceModelChanged) public: - explicit QAbstractProxyModel(QObject *parent = Q_NULLPTR); + explicit QAbstractProxyModel(QObject *parent = nullptr); ~QAbstractProxyModel(); virtual void setSourceModel(QAbstractItemModel *sourceModel); @@ -68,34 +68,34 @@ public: Q_INVOKABLE virtual QItemSelection mapSelectionToSource(const QItemSelection &selection) const; Q_INVOKABLE virtual QItemSelection mapSelectionFromSource(const QItemSelection &selection) const; - bool submit() Q_DECL_OVERRIDE; - void revert() Q_DECL_OVERRIDE; + bool submit() override; + void revert() override; - QVariant data(const QModelIndex &proxyIndex, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; - QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; - QMap<int, QVariant> itemData(const QModelIndex &index) const Q_DECL_OVERRIDE; - Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE; + QVariant data(const QModelIndex &proxyIndex, int role = Qt::DisplayRole) const override; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + QMap<int, QVariant> itemData(const QModelIndex &index) const override; + Qt::ItemFlags flags(const QModelIndex &index) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE; - bool setItemData(const QModelIndex& index, const QMap<int, QVariant> &roles) Q_DECL_OVERRIDE; - bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; + bool setItemData(const QModelIndex& index, const QMap<int, QVariant> &roles) override; + bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override; - QModelIndex buddy(const QModelIndex &index) const Q_DECL_OVERRIDE; - bool canFetchMore(const QModelIndex &parent) const Q_DECL_OVERRIDE; - void fetchMore(const QModelIndex &parent) Q_DECL_OVERRIDE; - void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) Q_DECL_OVERRIDE; - QSize span(const QModelIndex &index) const Q_DECL_OVERRIDE; - bool hasChildren(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; - QModelIndex sibling(int row, int column, const QModelIndex &idx) const Q_DECL_OVERRIDE; + QModelIndex buddy(const QModelIndex &index) const override; + bool canFetchMore(const QModelIndex &parent) const override; + void fetchMore(const QModelIndex &parent) override; + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; + QSize span(const QModelIndex &index) const override; + bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; + QModelIndex sibling(int row, int column, const QModelIndex &idx) const override; - QMimeData* mimeData(const QModelIndexList &indexes) const Q_DECL_OVERRIDE; + QMimeData* mimeData(const QModelIndexList &indexes) const override; bool canDropMimeData(const QMimeData *data, Qt::DropAction action, - int row, int column, const QModelIndex &parent) const Q_DECL_OVERRIDE; + int row, int column, const QModelIndex &parent) const override; bool dropMimeData(const QMimeData *data, Qt::DropAction action, - int row, int column, const QModelIndex &parent) Q_DECL_OVERRIDE; - QStringList mimeTypes() const Q_DECL_OVERRIDE; - Qt::DropActions supportedDragActions() const Q_DECL_OVERRIDE; - Qt::DropActions supportedDropActions() const Q_DECL_OVERRIDE; + int row, int column, const QModelIndex &parent) override; + QStringList mimeTypes() const override; + Qt::DropActions supportedDragActions() const override; + Qt::DropActions supportedDropActions() const override; Q_SIGNALS: void sourceModelChanged(QPrivateSignal); diff --git a/src/corelib/itemmodels/qidentityproxymodel.h b/src/corelib/itemmodels/qidentityproxymodel.h index e93740c1a2..d2b1ed9498 100644 --- a/src/corelib/itemmodels/qidentityproxymodel.h +++ b/src/corelib/itemmodels/qidentityproxymodel.h @@ -54,29 +54,29 @@ class Q_CORE_EXPORT QIdentityProxyModel : public QAbstractProxyModel { Q_OBJECT public: - explicit QIdentityProxyModel(QObject* parent = Q_NULLPTR); + explicit QIdentityProxyModel(QObject* parent = nullptr); ~QIdentityProxyModel(); - int columnCount(const QModelIndex& parent = QModelIndex()) const Q_DECL_OVERRIDE; - QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const Q_DECL_OVERRIDE; - QModelIndex mapFromSource(const QModelIndex& sourceIndex) const Q_DECL_OVERRIDE; - QModelIndex mapToSource(const QModelIndex& proxyIndex) const Q_DECL_OVERRIDE; - QModelIndex parent(const QModelIndex& child) const Q_DECL_OVERRIDE; + int columnCount(const QModelIndex& parent = QModelIndex()) const override; + QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override; + QModelIndex mapFromSource(const QModelIndex& sourceIndex) const override; + QModelIndex mapToSource(const QModelIndex& proxyIndex) const override; + QModelIndex parent(const QModelIndex& child) const override; using QObject::parent; - int rowCount(const QModelIndex& parent = QModelIndex()) const Q_DECL_OVERRIDE; - QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; - bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) Q_DECL_OVERRIDE; - QModelIndex sibling(int row, int column, const QModelIndex &idx) const Q_DECL_OVERRIDE; - - QItemSelection mapSelectionFromSource(const QItemSelection& selection) const Q_DECL_OVERRIDE; - QItemSelection mapSelectionToSource(const QItemSelection& selection) const Q_DECL_OVERRIDE; - QModelIndexList match(const QModelIndex& start, int role, const QVariant& value, int hits = 1, Qt::MatchFlags flags = Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const Q_DECL_OVERRIDE; - void setSourceModel(QAbstractItemModel* sourceModel) Q_DECL_OVERRIDE; - - bool insertColumns(int column, int count, const QModelIndex& parent = QModelIndex()) Q_DECL_OVERRIDE; - bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) Q_DECL_OVERRIDE; - bool removeColumns(int column, int count, const QModelIndex& parent = QModelIndex()) Q_DECL_OVERRIDE; - bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) Q_DECL_OVERRIDE; + int rowCount(const QModelIndex& parent = QModelIndex()) const override; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) override; + QModelIndex sibling(int row, int column, const QModelIndex &idx) const override; + + QItemSelection mapSelectionFromSource(const QItemSelection& selection) const override; + QItemSelection mapSelectionToSource(const QItemSelection& selection) const override; + QModelIndexList match(const QModelIndex& start, int role, const QVariant& value, int hits = 1, Qt::MatchFlags flags = Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const override; + void setSourceModel(QAbstractItemModel* sourceModel) override; + + bool insertColumns(int column, int count, const QModelIndex& parent = QModelIndex()) override; + bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) override; + bool removeColumns(int column, int count, const QModelIndex& parent = QModelIndex()) override; + bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override; protected: QIdentityProxyModel(QIdentityProxyModelPrivate &dd, QObject* parent); diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp index 59a10e9057..9af4fd9133 100644 --- a/src/corelib/itemmodels/qitemselectionmodel.cpp +++ b/src/corelib/itemmodels/qitemselectionmodel.cpp @@ -218,13 +218,15 @@ QT_BEGIN_NAMESPACE */ bool QItemSelectionRange::intersects(const QItemSelectionRange &other) const { - return (isValid() && other.isValid() - && parent() == other.parent() - && model() == other.model() + // isValid() and parent() last since they are more expensive + return (model() == other.model() && ((top() <= other.top() && bottom() >= other.top()) || (top() >= other.top() && top() <= other.bottom())) && ((left() <= other.left() && right() >= other.left()) - || (left() >= other.left() && left() <= other.right()))); + || (left() >= other.left() && left() <= other.right())) + && parent() == other.parent() + && isValid() && other.isValid() + ); } /*! diff --git a/src/corelib/itemmodels/qitemselectionmodel.h b/src/corelib/itemmodels/qitemselectionmodel.h index 9d33303ddc..091c5a21a5 100644 --- a/src/corelib/itemmodels/qitemselectionmodel.h +++ b/src/corelib/itemmodels/qitemselectionmodel.h @@ -164,7 +164,7 @@ public: Q_DECLARE_FLAGS(SelectionFlags, SelectionFlag) Q_FLAG(SelectionFlags) - explicit QItemSelectionModel(QAbstractItemModel *model = Q_NULLPTR); + explicit QItemSelectionModel(QAbstractItemModel *model = nullptr); explicit QItemSelectionModel(QAbstractItemModel *model, QObject *parent); virtual ~QItemSelectionModel(); diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp index 7dad892606..c70fbaf63b 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp +++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp @@ -300,7 +300,7 @@ public: void updateChildrenMapping(const QModelIndex &source_parent, Mapping *parent_mapping, Qt::Orientation orient, int start, int end, int delta_item_count, bool remove); - virtual void _q_sourceModelDestroyed() Q_DECL_OVERRIDE; + virtual void _q_sourceModelDestroyed() override; bool needsReorder(const QVector<int> &source_rows, const QModelIndex &source_parent) const; @@ -1857,6 +1857,9 @@ void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel) { Q_D(QSortFilterProxyModel); + if (sourceModel == d->model) + return; + beginResetModel(); disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)), @@ -1910,6 +1913,7 @@ void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel) disconnect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_sourceAboutToBeReset())); disconnect(d->model, SIGNAL(modelReset()), this, SLOT(_q_sourceReset())); + d->_q_sourceModelDestroyed(); QAbstractProxyModel::setSourceModel(sourceModel); connect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)), @@ -1963,7 +1967,6 @@ void QSortFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel) connect(d->model, SIGNAL(modelAboutToBeReset()), this, SLOT(_q_sourceAboutToBeReset())); connect(d->model, SIGNAL(modelReset()), this, SLOT(_q_sourceReset())); - d->_q_clearMapping(); endResetModel(); if (d->update_source_sort_column() && d->dynamic_sortfilter) d->sort(); diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.h b/src/corelib/itemmodels/qsortfilterproxymodel.h index 9f9b59733d..6c620f4812 100644 --- a/src/corelib/itemmodels/qsortfilterproxymodel.h +++ b/src/corelib/itemmodels/qsortfilterproxymodel.h @@ -70,16 +70,16 @@ class Q_CORE_EXPORT QSortFilterProxyModel : public QAbstractProxyModel Q_PROPERTY(bool recursiveFilteringEnabled READ isRecursiveFilteringEnabled WRITE setRecursiveFilteringEnabled) public: - explicit QSortFilterProxyModel(QObject *parent = Q_NULLPTR); + explicit QSortFilterProxyModel(QObject *parent = nullptr); ~QSortFilterProxyModel(); - void setSourceModel(QAbstractItemModel *sourceModel) Q_DECL_OVERRIDE; + void setSourceModel(QAbstractItemModel *sourceModel) override; - QModelIndex mapToSource(const QModelIndex &proxyIndex) const Q_DECL_OVERRIDE; - QModelIndex mapFromSource(const QModelIndex &sourceIndex) const Q_DECL_OVERRIDE; + QModelIndex mapToSource(const QModelIndex &proxyIndex) const override; + QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override; - QItemSelection mapSelectionToSource(const QItemSelection &proxySelection) const Q_DECL_OVERRIDE; - QItemSelection mapSelectionFromSource(const QItemSelection &sourceSelection) const Q_DECL_OVERRIDE; + QItemSelection mapSelectionToSource(const QItemSelection &proxySelection) const override; + QItemSelection mapSelectionFromSource(const QItemSelection &sourceSelection) const override; QRegExp filterRegExp() const; void setFilterRegExp(const QRegExp ®Exp); @@ -129,44 +129,44 @@ protected: public: using QObject::parent; - QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; - QModelIndex parent(const QModelIndex &child) const Q_DECL_OVERRIDE; - QModelIndex sibling(int row, int column, const QModelIndex &idx) const Q_DECL_OVERRIDE; + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; + QModelIndex parent(const QModelIndex &child) const override; + QModelIndex sibling(int row, int column, const QModelIndex &idx) const override; - int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; - int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; - bool hasChildren(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; bool setHeaderData(int section, Qt::Orientation orientation, - const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE; + const QVariant &value, int role = Qt::EditRole) override; - QMimeData *mimeData(const QModelIndexList &indexes) const Q_DECL_OVERRIDE; + QMimeData *mimeData(const QModelIndexList &indexes) const override; bool dropMimeData(const QMimeData *data, Qt::DropAction action, - int row, int column, const QModelIndex &parent) Q_DECL_OVERRIDE; + int row, int column, const QModelIndex &parent) override; - bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE; - bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE; - bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE; - bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE; + bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; + bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override; + bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; + bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override; - void fetchMore(const QModelIndex &parent) Q_DECL_OVERRIDE; - bool canFetchMore(const QModelIndex &parent) const Q_DECL_OVERRIDE; - Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE; + void fetchMore(const QModelIndex &parent) override; + bool canFetchMore(const QModelIndex &parent) const override; + Qt::ItemFlags flags(const QModelIndex &index) const override; - QModelIndex buddy(const QModelIndex &index) const Q_DECL_OVERRIDE; + QModelIndex buddy(const QModelIndex &index) const override; QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits = 1, Qt::MatchFlags flags = - Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const Q_DECL_OVERRIDE; - QSize span(const QModelIndex &index) const Q_DECL_OVERRIDE; - void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) Q_DECL_OVERRIDE; + Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const override; + QSize span(const QModelIndex &index) const override; + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; - QStringList mimeTypes() const Q_DECL_OVERRIDE; - Qt::DropActions supportedDropActions() const Q_DECL_OVERRIDE; + QStringList mimeTypes() const override; + Qt::DropActions supportedDropActions() const override; private: Q_DECLARE_PRIVATE(QSortFilterProxyModel) Q_DISABLE_COPY(QSortFilterProxyModel) diff --git a/src/corelib/itemmodels/qstringlistmodel.h b/src/corelib/itemmodels/qstringlistmodel.h index 3bda848f48..38da1022ea 100644 --- a/src/corelib/itemmodels/qstringlistmodel.h +++ b/src/corelib/itemmodels/qstringlistmodel.h @@ -52,26 +52,26 @@ class Q_CORE_EXPORT QStringListModel : public QAbstractListModel { Q_OBJECT public: - explicit QStringListModel(QObject *parent = Q_NULLPTR); - explicit QStringListModel(const QStringList &strings, QObject *parent = Q_NULLPTR); + explicit QStringListModel(QObject *parent = nullptr); + explicit QStringListModel(const QStringList &strings, QObject *parent = nullptr); - int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; - QModelIndex sibling(int row, int column, const QModelIndex &idx) const Q_DECL_OVERRIDE; + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + QModelIndex sibling(int row, int column, const QModelIndex &idx) const override; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE; + Qt::ItemFlags flags(const QModelIndex &index) const override; - bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE; - bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE; + bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; + bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; - void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) Q_DECL_OVERRIDE; + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; QStringList stringList() const; void setStringList(const QStringList &strings); - Qt::DropActions supportedDropActions() const Q_DECL_OVERRIDE; + Qt::DropActions supportedDropActions() const override; private: Q_DISABLE_COPY(QStringListModel) |