summaryrefslogtreecommitdiffstats
path: root/src/corelib/itemmodels/qabstractitemmodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/itemmodels/qabstractitemmodel.cpp')
-rw-r--r--src/corelib/itemmodels/qabstractitemmodel.cpp173
1 files changed, 95 insertions, 78 deletions
diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp
index fcf6e69534..216db3b42a 100644
--- a/src/corelib/itemmodels/qabstractitemmodel.cpp
+++ b/src/corelib/itemmodels/qabstractitemmodel.cpp
@@ -19,6 +19,8 @@
#include <qdatetime.h>
#include <qloggingcategory.h>
+#include <functional>
+
#include <limits.h>
QT_BEGIN_NAMESPACE
@@ -210,7 +212,7 @@ void QPersistentModelIndexData::destroy(QPersistentModelIndexData *data)
*/
/*!
- \fn template <typename Container> QModelRoleDataSpan::QModelRoleDataSpan(Container &c) noexcept
+ \fn template <typename Container, QModelRoleDataSpan::if_compatible_container<Container> = true> QModelRoleDataSpan::QModelRoleDataSpan(Container &c) noexcept
Constructs an QModelRoleDataSpan spanning over the container \a c,
which can be any contiguous container of QModelRoleData objects.
@@ -286,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.
@@ -373,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
+/*!
+ \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
+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 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
@@ -468,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
-{
- 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
+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();
}
/*!
@@ -684,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(); }
@@ -713,7 +721,7 @@ QAbstractItemModel *QAbstractItemModelPrivate::staticEmptyModel()
void QAbstractItemModelPrivate::invalidatePersistentIndexes()
{
- for (QPersistentModelIndexData *data : qAsConst(persistent.indexes))
+ for (QPersistentModelIndexData *data : std::as_const(persistent.indexes))
data->index = QModelIndex();
persistent.indexes.clear();
}
@@ -840,13 +848,13 @@ void QAbstractItemModelPrivate::removePersistentIndexData(QPersistentModelIndexD
Q_UNUSED(removed);
}
// make sure our optimization still works
- for (int i = persistent.moved.count() - 1; i >= 0; --i) {
+ for (int i = persistent.moved.size() - 1; i >= 0; --i) {
int idx = persistent.moved.at(i).indexOf(data);
if (idx >= 0)
persistent.moved[i].remove(idx);
}
// update the references to invalidated persistent indexes
- for (int i = persistent.invalidated.count() - 1; i >= 0; --i) {
+ for (int i = persistent.invalidated.size() - 1; i >= 0; --i) {
int idx = persistent.invalidated.at(i).indexOf(data);
if (idx >= 0)
persistent.invalidated[i].remove(idx);
@@ -861,7 +869,7 @@ void QAbstractItemModelPrivate::rowsAboutToBeInserted(const QModelIndex &parent,
Q_UNUSED(last);
QList<QPersistentModelIndexData *> persistent_moved;
if (first < q->rowCount(parent)) {
- for (auto *data : qAsConst(persistent.indexes)) {
+ for (auto *data : std::as_const(persistent.indexes)) {
const QModelIndex &index = data->index;
if (index.row() >= first && index.isValid() && index.parent() == parent) {
persistent_moved.append(data);
@@ -897,7 +905,7 @@ void QAbstractItemModelPrivate::itemsAboutToBeMoved(const QModelIndex &srcParent
const bool sameParent = (srcParent == destinationParent);
const bool movingUp = (srcFirst > destinationChild);
- for (auto *data : qAsConst(persistent.indexes)) {
+ for (auto *data : std::as_const(persistent.indexes)) {
const QModelIndex &index = data->index;
const QModelIndex &parent = index.parent();
const bool isSourceIndex = (parent == srcParent);
@@ -995,7 +1003,7 @@ void QAbstractItemModelPrivate::rowsAboutToBeRemoved(const QModelIndex &parent,
QList<QPersistentModelIndexData *> persistent_invalidated;
// find the persistent indexes that are affected by the change, either by being in the removed subtree
// or by being on the same level and below the removed rows
- for (auto *data : qAsConst(persistent.indexes)) {
+ for (auto *data : std::as_const(persistent.indexes)) {
bool level_changed = false;
QModelIndex current = data->index;
while (current.isValid()) {
@@ -1047,7 +1055,7 @@ void QAbstractItemModelPrivate::columnsAboutToBeInserted(const QModelIndex &pare
Q_UNUSED(last);
QList<QPersistentModelIndexData *> persistent_moved;
if (first < q->columnCount(parent)) {
- for (auto *data : qAsConst(persistent.indexes)) {
+ for (auto *data : std::as_const(persistent.indexes)) {
const QModelIndex &index = data->index;
if (index.column() >= first && index.isValid() && index.parent() == parent)
persistent_moved.append(data);
@@ -1080,7 +1088,7 @@ void QAbstractItemModelPrivate::columnsAboutToBeRemoved(const QModelIndex &paren
QList<QPersistentModelIndexData *> persistent_invalidated;
// find the persistent indexes that are affected by the change, either by being in the removed subtree
// or by being on the same level and to the right of the removed columns
- for (auto *data : qAsConst(persistent.indexes)) {
+ for (auto *data : std::as_const(persistent.indexes)) {
bool level_changed = false;
QModelIndex current = data->index;
while (current.isValid()) {
@@ -1155,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
@@ -1325,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
@@ -1497,10 +1504,12 @@ void QAbstractItemModel::resetInternalData()
rows to the model, \l{QAbstractItemModel::}{beginInsertRows()} and
\l{QAbstractItemModel::}{endInsertRows()} must be called.
+ \include models.qdocinc {thread-safety-section1}{QAbstractItemModel}
+
\sa {Model Classes}, {Model Subclassing Reference}, QModelIndex,
QAbstractItemView, {Using drag and drop with item views},
- {Simple DOM Model Example}, {Simple Tree Model Example},
- {Editable Tree Model Example}, {Fetch More Example}
+ {Simple Tree Model Example}, {Editable Tree Model Example},
+ {Fetch More Example}
*/
/*!
@@ -1739,7 +1748,13 @@ QAbstractItemModel::~QAbstractItemModel()
For example:
- \snippet ../widgets/itemviews/simpledommodel/dommodel.cpp 2
+ \code
+ int MyModel::columnCount(const QModelIndex &parent) const
+ {
+ Q_UNUSED(parent);
+ return 3;
+ }
+ \endcode
\note When implementing a table based model, columnCount() should return 0
when the parent is valid.
@@ -2120,7 +2135,7 @@ QStringList QAbstractItemModel::mimeTypes() const
*/
QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const
{
- if (indexes.count() <= 0)
+ if (indexes.size() <= 0)
return nullptr;
QStringList types = mimeTypes();
if (types.isEmpty())
@@ -2159,7 +2174,7 @@ bool QAbstractItemModel::canDropMimeData(const QMimeData *data, Qt::DropAction a
return false;
const QStringList modelTypes = mimeTypes();
- for (int i = 0; i < modelTypes.count(); ++i) {
+ for (int i = 0; i < modelTypes.size(); ++i) {
if (data->hasFormat(modelTypes.at(i)))
return true;
}
@@ -2516,7 +2531,7 @@ QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role,
// iterates twice if wrapping
for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) {
- for (int r = from; (r < to) && (allHits || result.count() < hits); ++r) {
+ for (int r = from; (r < to) && (allHits || result.size() < hits); ++r) {
QModelIndex idx = index(r, column, p);
if (!idx.isValid())
continue;
@@ -2538,8 +2553,10 @@ QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role,
}
}
} else if (matchType == Qt::MatchWildcard) {
- if (rx.pattern().isEmpty())
- rx.setPattern(QRegularExpression::wildcardToRegularExpression(value.toString()));
+ if (rx.pattern().isEmpty()) {
+ const QString pattern = QRegularExpression::wildcardToRegularExpression(value.toString(), QRegularExpression::NonPathWildcardConversion);
+ rx.setPattern(pattern);
+ }
if (cs == Qt::CaseInsensitive)
rx.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
} else
@@ -2582,7 +2599,7 @@ QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role,
if (hasChildren(parent)) { // search the hierarchy
result += match(index(0, column, parent), role,
(text.isEmpty() ? value : text),
- (allHits ? -1 : hits - result.count()), flags);
+ (allHits ? -1 : hits - result.size()), flags);
}
}
}
@@ -2775,15 +2792,15 @@ bool QAbstractItemModel::decodeData(int row, int column, const QModelIndex &pare
// Compute the number of continuous rows upon insertion and modify the rows to match
QList<int> rowsToInsert(bottom + 1);
- for (int i = 0; i < rows.count(); ++i)
+ for (int i = 0; i < rows.size(); ++i)
rowsToInsert[rows.at(i)] = 1;
- for (int i = 0; i < rowsToInsert.count(); ++i) {
+ for (int i = 0; i < rowsToInsert.size(); ++i) {
if (rowsToInsert.at(i) == 1){
rowsToInsert[i] = dragRowCount;
++dragRowCount;
}
}
- for (int i = 0; i < rows.count(); ++i)
+ for (int i = 0; i < rows.size(); ++i)
rows[i] = top + rowsToInsert.at(rows.at(i));
QBitArray isWrittenTo(dragRowCount * dragColumnCount);
@@ -3446,8 +3463,8 @@ void QAbstractItemModel::changePersistentIndexList(const QModelIndexList &from,
if (d->persistent.indexes.isEmpty())
return;
QList<QPersistentModelIndexData *> toBeReinserted;
- toBeReinserted.reserve(to.count());
- for (int i = 0; i < from.count(); ++i) {
+ toBeReinserted.reserve(to.size());
+ for (int i = 0; i < from.size(); ++i) {
if (from.at(i) == to.at(i))
continue;
const auto it = d->persistent.indexes.constFind(from.at(i));
@@ -3460,7 +3477,7 @@ void QAbstractItemModel::changePersistentIndexList(const QModelIndexList &from,
}
}
- for (auto *data : qAsConst(toBeReinserted))
+ for (auto *data : std::as_const(toBeReinserted))
d->persistent.insertMultiAtEnd(data->index, data);
}
@@ -3473,8 +3490,8 @@ QModelIndexList QAbstractItemModel::persistentIndexList() const
{
Q_D(const QAbstractItemModel);
QModelIndexList result;
- result.reserve(d->persistent.indexes.count());
- for (auto *data : qAsConst(d->persistent.indexes))
+ result.reserve(d->persistent.indexes.size());
+ for (auto *data : std::as_const(d->persistent.indexes))
result.append(data->index);
return result;
}
@@ -3726,10 +3743,9 @@ void QAbstractItemModel::multiData(const QModelIndex &index, QModelRoleDataSpan
\note Some general guidelines for subclassing models are available in the
\l{Model Subclassing Reference}.
- \note
+ \include models.qdocinc {thread-safety-section1}{QAbstractTableModel}
- \sa {Model Classes}, QAbstractItemModel, QAbstractListModel,
- {Pixelator Example}
+ \sa {Model Classes}, QAbstractItemModel, QAbstractListModel
*/
/*!
@@ -3880,7 +3896,7 @@ Qt::ItemFlags QAbstractTableModel::flags(const QModelIndex &index) const
\l{Model Subclassing Reference}.
\sa {Model Classes}, {Model Subclassing Reference}, QAbstractItemView,
- QAbstractTableModel, {Item Views Puzzle Example}
+ QAbstractTableModel
*/
/*!
@@ -4112,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
@@ -4158,3 +4174,4 @@ void QAbstractItemModelPrivate::Persistent::insertMultiAtEnd(const QModelIndex&
QT_END_NAMESPACE
#include "moc_qabstractitemmodel.cpp"
+#include "qabstractitemmodel.moc"