summaryrefslogtreecommitdiffstats
path: root/src/corelib/itemmodels/qitemselectionmodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/itemmodels/qitemselectionmodel.cpp')
-rw-r--r--src/corelib/itemmodels/qitemselectionmodel.cpp328
1 files changed, 161 insertions, 167 deletions
diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp
index 4653bd26ba..6df60aaf61 100644
--- a/src/corelib/itemmodels/qitemselectionmodel.cpp
+++ b/src/corelib/itemmodels/qitemselectionmodel.cpp
@@ -1,47 +1,13 @@
-/****************************************************************************
-**
-** Copyright (C) 2020 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qitemselectionmodel.h"
#include "qitemselectionmodel_p.h"
#include <private/qitemselectionmodel_p.h>
+#include <private/qabstractitemmodel_p.h>
#include <private/qduplicatetracker_p.h>
+#include <private/qoffsetstringarray_p.h>
#include <qdebug.h>
#include <algorithm>
@@ -49,6 +15,9 @@
QT_BEGIN_NAMESPACE
+QT_IMPL_METATYPE_EXTERN(QItemSelectionRange)
+QT_IMPL_METATYPE_EXTERN(QItemSelection)
+
/*!
\class QItemSelectionRange
\inmodule QtCore
@@ -58,6 +27,8 @@ QT_BEGIN_NAMESPACE
\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
@@ -247,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.
*/
@@ -269,7 +240,7 @@ QItemSelectionRange QItemSelectionRange::intersected(const QItemSelectionRange &
*/
-static void rowLengthsFromRange(const QItemSelectionRange &range, QList<QPair<QPersistentModelIndex, uint>> &result)
+static void rowLengthsFromRange(const QItemSelectionRange &range, QList<std::pair<QPersistentModelIndex, uint>> &result)
{
if (range.isValid() && range.model()) {
const QModelIndex topLeft = range.topLeft();
@@ -280,11 +251,16 @@ static void rowLengthsFromRange(const QItemSelectionRange &range, QList<QPair<QP
// We don't need to keep track of ItemIsSelectable and ItemIsEnabled here. That is
// required in indexesFromRange() because that method is called from public API
// which requires the limitation.
- result.push_back(qMakePair(QPersistentModelIndex(topLeft.sibling(row, column)), width));
+ result.emplace_back(topLeft.sibling(row, column), width);
}
}
}
+static bool isSelectableAndEnabled(Qt::ItemFlags flags)
+{
+ return flags.testFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
+}
+
template<typename ModelIndexContainer>
static void indexesFromRange(const QItemSelectionRange &range, ModelIndexContainer &result)
{
@@ -296,8 +272,7 @@ static void indexesFromRange(const QItemSelectionRange &range, ModelIndexContain
const QModelIndex columnLeader = topLeft.sibling(row, topLeft.column());
for (int column = topLeft.column(); column <= right; ++column) {
QModelIndex index = columnLeader.sibling(row, column);
- Qt::ItemFlags flags = range.model()->flags(index);
- if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled))
+ if (isSelectableAndEnabled(range.model()->flags(index)))
result.push_back(index);
}
}
@@ -314,7 +289,9 @@ static ModelIndexContainer qSelectionIndexes(const QItemSelection &selection)
}
/*!
- Returns \c true if the selection range contains no selectable item
+ Returns \c true if the selection range contains either no items
+ or only items which are either disabled or marked as not selectable.
+
\since 4.7
*/
@@ -326,8 +303,7 @@ bool QItemSelectionRange::isEmpty() const
for (int column = left(); column <= right(); ++column) {
for (int row = top(); row <= bottom(); ++row) {
QModelIndex index = model()->index(row, column, parent());
- Qt::ItemFlags flags = model()->flags(index);
- if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled))
+ if (isSelectableAndEnabled(model()->flags(index)))
return false;
}
}
@@ -440,7 +416,7 @@ void QItemSelection::select(const QModelIndex &topLeft, const QModelIndex &botto
bool QItemSelection::contains(const QModelIndex &index) const
{
- if (index.flags() & Qt::ItemIsSelectable) {
+ if (isSelectableAndEnabled(index.flags())) {
QList<QItemSelectionRange>::const_iterator it = begin();
for (; it != end(); ++it)
if ((*it).contains(index))
@@ -458,9 +434,9 @@ QModelIndexList QItemSelection::indexes() const
return qSelectionIndexes<QModelIndexList>(*this);
}
-static QList<QPair<QPersistentModelIndex, uint>> qSelectionPersistentRowLengths(const QItemSelection &sel)
+static QList<std::pair<QPersistentModelIndex, uint>> qSelectionPersistentRowLengths(const QItemSelection &sel)
{
- QList<QPair<QPersistentModelIndex, uint>> result;
+ QList<std::pair<QPersistentModelIndex, uint>> result;
for (const QItemSelectionRange &range : sel)
rowLengthsFromRange(range, result);
return result;
@@ -484,25 +460,23 @@ void QItemSelection::merge(const QItemSelection &other, QItemSelectionModel::Sel
command & QItemSelectionModel::Toggle))
return;
- QItemSelection newSelection = other;
+ QItemSelection newSelection;
+ newSelection.reserve(other.size());
// Collect intersections
QItemSelection intersections;
- QItemSelection::iterator it = newSelection.begin();
- while (it != newSelection.end()) {
- if (!(*it).isValid()) {
- it = newSelection.erase(it);
+ for (const auto &range : other) {
+ if (!range.isValid())
continue;
+ newSelection.push_back(range);
+ for (int t = 0; t < size(); ++t) {
+ if (range.intersects(at(t)))
+ intersections.append(at(t).intersected(range));
}
- for (int t = 0; t < count(); ++t) {
- if ((*it).intersects(at(t)))
- intersections.append(at(t).intersected(*it));
- }
- ++it;
}
// Split the old (and new) ranges using the intersections
- for (int i = 0; i < intersections.count(); ++i) { // for each intersection
- for (int t = 0; t < count();) { // splitt each old range
+ for (int i = 0; i < intersections.size(); ++i) { // for each intersection
+ for (int t = 0; t < size();) { // splitt each old range
if (at(t).intersects(intersections.at(i))) {
split(at(t), intersections.at(i), this);
removeAt(t);
@@ -511,7 +485,7 @@ void QItemSelection::merge(const QItemSelection &other, QItemSelectionModel::Sel
}
}
// only split newSelection if Toggle is specified
- for (int n = 0; (command & QItemSelectionModel::Toggle) && n < newSelection.count();) {
+ for (int n = 0; (command & QItemSelectionModel::Toggle) && n < newSelection.size();) {
if (newSelection.at(n).intersects(intersections.at(i))) {
split(newSelection.at(n), intersections.at(i), &newSelection);
newSelection.removeAt(n);
@@ -578,57 +552,55 @@ void QItemSelection::split(const QItemSelectionRange &range,
void QItemSelectionModelPrivate::initModel(QAbstractItemModel *m)
{
- struct Cx {
- const char *signal;
- const char *slot;
- };
- static const Cx connections[] = {
- { SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
- SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)) },
- { SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
- SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)) },
- { SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
- SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)) },
- { SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
- SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)) },
- { SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
- SLOT(_q_layoutAboutToBeChanged()) },
- { SIGNAL(columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
- SLOT(_q_layoutAboutToBeChanged()) },
- { SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
- SLOT(_q_layoutChanged()) },
- { SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)),
- SLOT(_q_layoutChanged()) },
- { SIGNAL(layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
- SLOT(_q_layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)) },
- { SIGNAL(layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
- SLOT(_q_layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)) },
- { SIGNAL(modelReset()),
- SLOT(reset()) },
- { SIGNAL(destroyed(QObject*)),
- SLOT(_q_modelDestroyed()) },
- { nullptr, nullptr }
- };
-
- if (model == m)
+ Q_Q(QItemSelectionModel);
+ const QAbstractItemModel *oldModel = model.valueBypassingBindings();
+ if (oldModel == m)
return;
- Q_Q(QItemSelectionModel);
- if (model.value()) {
- for (const Cx *cx = &connections[0]; cx->signal; cx++)
- QObject::disconnect(model.value(), cx->signal, q, cx->slot);
+ if (oldModel) {
q->reset();
+ disconnectModel();
}
// Caller has to call notify(), unless calling during construction (the common case).
model.setValueBypassingBindings(m);
- if (model.value()) {
- for (const Cx *cx = &connections[0]; cx->signal; cx++)
- QObject::connect(model.value(), cx->signal, q, cx->slot);
+ if (m) {
+ connections = std::array<QMetaObject::Connection, 12> {
+ QObjectPrivate::connect(m, &QAbstractItemModel::rowsAboutToBeRemoved,
+ this, &QItemSelectionModelPrivate::rowsAboutToBeRemoved),
+ QObjectPrivate::connect(m, &QAbstractItemModel::columnsAboutToBeRemoved,
+ this, &QItemSelectionModelPrivate::columnsAboutToBeRemoved),
+ QObjectPrivate::connect(m, &QAbstractItemModel::rowsAboutToBeInserted,
+ this, &QItemSelectionModelPrivate::rowsAboutToBeInserted),
+ QObjectPrivate::connect(m, &QAbstractItemModel::columnsAboutToBeInserted,
+ this, &QItemSelectionModelPrivate::columnsAboutToBeInserted),
+ QObjectPrivate::connect(m, &QAbstractItemModel::rowsAboutToBeMoved,
+ this, &QItemSelectionModelPrivate::triggerLayoutToBeChanged),
+ QObjectPrivate::connect(m, &QAbstractItemModel::columnsAboutToBeMoved,
+ this, &QItemSelectionModelPrivate::triggerLayoutToBeChanged),
+ QObjectPrivate::connect(m, &QAbstractItemModel::rowsMoved,
+ this, &QItemSelectionModelPrivate::triggerLayoutChanged),
+ QObjectPrivate::connect(m, &QAbstractItemModel::columnsMoved,
+ this, &QItemSelectionModelPrivate::triggerLayoutChanged),
+ QObjectPrivate::connect(m, &QAbstractItemModel::layoutAboutToBeChanged,
+ this, &QItemSelectionModelPrivate::layoutAboutToBeChanged),
+ QObjectPrivate::connect(m, &QAbstractItemModel::layoutChanged,
+ this, &QItemSelectionModelPrivate::layoutChanged),
+ QObject::connect(m, &QAbstractItemModel::modelReset,
+ q, &QItemSelectionModel::reset),
+ QObjectPrivate::connect(m, &QAbstractItemModel::destroyed,
+ this, &QItemSelectionModelPrivate::modelDestroyed)
+ };
}
}
+void QItemSelectionModelPrivate::disconnectModel()
+{
+ for (auto &connection : connections)
+ QObject::disconnect(connection);
+}
+
/*!
\internal
@@ -646,7 +618,7 @@ QItemSelection QItemSelectionModelPrivate::expandSelection(const QItemSelection
QItemSelection expanded;
if (command & QItemSelectionModel::Rows) {
- for (int i = 0; i < selection.count(); ++i) {
+ for (int i = 0; i < selection.size(); ++i) {
QModelIndex parent = selection.at(i).parent();
int colCount = model->columnCount(parent);
QModelIndex tl = model->index(selection.at(i).top(), 0, parent);
@@ -656,7 +628,7 @@ QItemSelection QItemSelectionModelPrivate::expandSelection(const QItemSelection
}
}
if (command & QItemSelectionModel::Columns) {
- for (int i = 0; i < selection.count(); ++i) {
+ for (int i = 0; i < selection.size(); ++i) {
QModelIndex parent = selection.at(i).parent();
int rowCount = model->rowCount(parent);
QModelIndex tl = model->index(0, selection.at(i).left(), parent);
@@ -671,7 +643,7 @@ QItemSelection QItemSelectionModelPrivate::expandSelection(const QItemSelection
/*!
\internal
*/
-void QItemSelectionModelPrivate::_q_rowsAboutToBeRemoved(const QModelIndex &parent,
+void QItemSelectionModelPrivate::rowsAboutToBeRemoved(const QModelIndex &parent,
int start, int end)
{
Q_Q(QItemSelectionModel);
@@ -754,7 +726,7 @@ void QItemSelectionModelPrivate::_q_rowsAboutToBeRemoved(const QModelIndex &pare
/*!
\internal
*/
-void QItemSelectionModelPrivate::_q_columnsAboutToBeRemoved(const QModelIndex &parent,
+void QItemSelectionModelPrivate::columnsAboutToBeRemoved(const QModelIndex &parent,
int start, int end)
{
Q_Q(QItemSelectionModel);
@@ -791,7 +763,7 @@ void QItemSelectionModelPrivate::_q_columnsAboutToBeRemoved(const QModelIndex &p
Split selection ranges if columns are about to be inserted in the middle.
*/
-void QItemSelectionModelPrivate::_q_columnsAboutToBeInserted(const QModelIndex &parent,
+void QItemSelectionModelPrivate::columnsAboutToBeInserted(const QModelIndex &parent,
int start, int end)
{
Q_UNUSED(end);
@@ -821,7 +793,7 @@ void QItemSelectionModelPrivate::_q_columnsAboutToBeInserted(const QModelIndex &
Split selection ranges if rows are about to be inserted in the middle.
*/
-void QItemSelectionModelPrivate::_q_rowsAboutToBeInserted(const QModelIndex &parent,
+void QItemSelectionModelPrivate::rowsAboutToBeInserted(const QModelIndex &parent,
int start, int end)
{
Q_Q(QItemSelectionModel);
@@ -862,7 +834,8 @@ void QItemSelectionModelPrivate::_q_rowsAboutToBeInserted(const QModelIndex &par
preparation for the layoutChanged() signal, where the indexes can be
merged again.
*/
-void QItemSelectionModelPrivate::_q_layoutAboutToBeChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint hint)
+void QItemSelectionModelPrivate::layoutAboutToBeChanged(const QList<QPersistentModelIndex> &,
+ QAbstractItemModel::LayoutChangeHint hint)
{
savedPersistentIndexes.clear();
savedPersistentCurrentIndexes.clear();
@@ -871,7 +844,7 @@ void QItemSelectionModelPrivate::_q_layoutAboutToBeChanged(const QList<QPersiste
// optimization for when all indexes are selected
// (only if there is lots of items (1000) because this is not entirely correct)
- if (ranges.isEmpty() && currentSelection.count() == 1) {
+ if (ranges.isEmpty() && currentSelection.size() == 1) {
QItemSelectionRange range = currentSelection.constFirst();
QModelIndex parent = range.parent();
tableRowCount = model->rowCount(parent);
@@ -904,14 +877,14 @@ void QItemSelectionModelPrivate::_q_layoutAboutToBeChanged(const QList<QPersiste
/*!
\internal
*/
-static QItemSelection mergeRowLengths(const QList<QPair<QPersistentModelIndex, uint>> &rowLengths)
+static QItemSelection mergeRowLengths(const QList<std::pair<QPersistentModelIndex, uint>> &rowLengths)
{
if (rowLengths.isEmpty())
return QItemSelection();
QItemSelection result;
int i = 0;
- while (i < rowLengths.count()) {
+ while (i < rowLengths.size()) {
const QPersistentModelIndex &tl = rowLengths.at(i).first;
if (!tl.isValid()) {
++i;
@@ -919,7 +892,7 @@ static QItemSelection mergeRowLengths(const QList<QPair<QPersistentModelIndex, u
}
QPersistentModelIndex br = tl;
const uint length = rowLengths.at(i).second;
- while (++i < rowLengths.count()) {
+ while (++i < rowLengths.size()) {
const QPersistentModelIndex &next = rowLengths.at(i).first;
if (!next.isValid())
continue;
@@ -949,7 +922,7 @@ static QItemSelection mergeIndexes(const QList<QPersistentModelIndex> &indexes)
QItemSelection colSpans;
// merge columns
int i = 0;
- while (i < indexes.count()) {
+ while (i < indexes.size()) {
const QPersistentModelIndex &tl = indexes.at(i);
if (!tl.isValid()) {
++i;
@@ -959,7 +932,7 @@ static QItemSelection mergeIndexes(const QList<QPersistentModelIndex> &indexes)
QModelIndex brParent = br.parent();
int brRow = br.row();
int brColumn = br.column();
- while (++i < indexes.count()) {
+ while (++i < indexes.size()) {
const QPersistentModelIndex &next = indexes.at(i);
if (!next.isValid())
continue;
@@ -982,11 +955,11 @@ static QItemSelection mergeIndexes(const QList<QPersistentModelIndex> &indexes)
// merge rows
QItemSelection rowSpans;
i = 0;
- while (i < colSpans.count()) {
+ while (i < colSpans.size()) {
QModelIndex tl = colSpans.at(i).topLeft();
QModelIndex br = colSpans.at(i).bottomRight();
QModelIndex prevTl = tl;
- while (++i < colSpans.count()) {
+ while (++i < colSpans.size()) {
QModelIndex nextTl = colSpans.at(i).topLeft();
QModelIndex nextBr = colSpans.at(i).bottomRight();
@@ -1009,7 +982,7 @@ static QItemSelection mergeIndexes(const QList<QPersistentModelIndex> &indexes)
/*!
\internal
- Sort predicate function for QItemSelectionModelPrivate::_q_layoutChanged(),
+ Sort predicate function for QItemSelectionModelPrivate::layoutChanged(),
sorting by parent first in addition to operator<(). This is to prevent
fragmentation of the selection by grouping indexes with the same row, column
of different parents next to each other, which may happen when a selection
@@ -1027,7 +1000,7 @@ static bool qt_PersistentModelIndexLessThan(const QPersistentModelIndex &i1, con
Merge the selected indexes into selection ranges again.
*/
-void QItemSelectionModelPrivate::_q_layoutChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint hint)
+void QItemSelectionModelPrivate::layoutChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint hint)
{
// special case for when all indexes are selected
if (tableSelected && tableColCount == model->columnCount(tableParent)
@@ -1111,9 +1084,10 @@ void QItemSelectionModelPrivate::_q_layoutChanged(const QList<QPersistentModelIn
We decide to break the new rule, imposed by bindable properties, and not break the old
rule, because that may break existing code.
*/
-void QItemSelectionModelPrivate::_q_modelDestroyed()
+void QItemSelectionModelPrivate::modelDestroyed()
{
model.setValueBypassingBindings(nullptr);
+ disconnectModel();
model.notify();
}
@@ -1153,7 +1127,7 @@ void QItemSelectionModelPrivate::_q_modelDestroyed()
\l{QItemSelectionModel::hasSelection()}{hasSelection}, and
\l{QItemSelectionModel::currentIndex()}{currentIndex} are meta-object properties.
- \sa {Model/View Programming}, QAbstractItemModel, {Chart Example}
+ \sa {Model/View Programming}, QAbstractItemModel
*/
/*!
@@ -1322,7 +1296,7 @@ void QItemSelectionModel::select(const QItemSelection &selection, QItemSelection
// If d->ranges is non-empty when the source model is reset the persistent indexes
// it contains will be invalid. We can't clear them in a modelReset slot because that might already
// be too late if another model observer is connected to the same modelReset slot and is invoked first
- // it might call select() on this selection model before any such QItemSelectionModelPrivate::_q_modelReset() slot
+ // it might call select() on this selection model before any such QItemSelectionModelPrivate::modelReset() slot
// is invoked, so it would not be cleared yet. We clear it invalid ranges in it here.
d->ranges.removeIf(QtFunctionObjects::IsNotValid());
@@ -1395,7 +1369,7 @@ void QItemSelectionModel::reset()
void QItemSelectionModel::clearSelection()
{
Q_D(QItemSelectionModel);
- if (d->ranges.count() == 0 && d->currentSelection.count() == 0)
+ if (d->ranges.size() == 0 && d->currentSelection.size() == 0)
return;
select(QItemSelection(), Clear);
@@ -1466,7 +1440,7 @@ bool QItemSelectionModel::isSelected(const QModelIndex &index) const
}
// check currentSelection
- if (d->currentSelection.count()) {
+ if (d->currentSelection.size()) {
if ((d->currentCommand & Deselect) && selected)
selected = !d->currentSelection.contains(index);
else if (d->currentCommand & Toggle)
@@ -1475,10 +1449,8 @@ bool QItemSelectionModel::isSelected(const QModelIndex &index) const
selected = d->currentSelection.contains(index);
}
- if (selected) {
- Qt::ItemFlags flags = d->model->flags(index);
- return flags.testAnyFlag(Qt::ItemIsSelectable);
- }
+ if (selected)
+ return isSelectableAndEnabled(d->model->flags(index));
return false;
}
@@ -1503,8 +1475,8 @@ bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent) cons
return false;
// return false if row exist in currentSelection (Deselect)
- if (d->currentCommand & Deselect && d->currentSelection.count()) {
- for (int i=0; i<d->currentSelection.count(); ++i) {
+ if (d->currentCommand & Deselect && d->currentSelection.size()) {
+ for (int i=0; i<d->currentSelection.size(); ++i) {
if (d->currentSelection.at(i).parent() == parent &&
row >= d->currentSelection.at(i).top() &&
row <= d->currentSelection.at(i).bottom())
@@ -1513,19 +1485,18 @@ bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent) cons
}
// return false if ranges in both currentSelection and ranges
// intersect and have the same row contained
- if (d->currentCommand & Toggle && d->currentSelection.count()) {
- for (int i=0; i<d->currentSelection.count(); ++i)
+ if (d->currentCommand & Toggle && d->currentSelection.size()) {
+ for (int i=0; i<d->currentSelection.size(); ++i)
if (d->currentSelection.at(i).top() <= row &&
d->currentSelection.at(i).bottom() >= row)
- for (int j=0; j<d->ranges.count(); ++j)
+ for (int j=0; j<d->ranges.size(); ++j)
if (d->ranges.at(j).top() <= row && d->ranges.at(j).bottom() >= row
&& d->currentSelection.at(i).intersected(d->ranges.at(j)).isValid())
return false;
}
auto isSelectable = [&](int row, int column) {
- Qt::ItemFlags flags = d->model->index(row, column, parent).flags();
- return (flags & Qt::ItemIsSelectable);
+ return isSelectableAndEnabled(d->model->index(row, column, parent).flags());
};
const int colCount = d->model->columnCount(parent);
@@ -1533,7 +1504,7 @@ bool QItemSelectionModel::isRowSelected(int row, const QModelIndex &parent) cons
// add ranges and currentSelection and check through them all
QList<QItemSelectionRange>::const_iterator it;
QList<QItemSelectionRange> joined = d->ranges;
- if (d->currentSelection.count())
+ if (d->currentSelection.size())
joined += d->currentSelection;
for (int column = 0; column < colCount; ++column) {
if (!isSelectable(row, column)) {
@@ -1578,8 +1549,8 @@ bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent
return false;
// return false if column exist in currentSelection (Deselect)
- if (d->currentCommand & Deselect && d->currentSelection.count()) {
- for (int i = 0; i < d->currentSelection.count(); ++i) {
+ if (d->currentCommand & Deselect && d->currentSelection.size()) {
+ for (int i = 0; i < d->currentSelection.size(); ++i) {
if (d->currentSelection.at(i).parent() == parent &&
column >= d->currentSelection.at(i).left() &&
column <= d->currentSelection.at(i).right())
@@ -1588,11 +1559,11 @@ bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent
}
// return false if ranges in both currentSelection and the selection model
// intersect and have the same column contained
- if (d->currentCommand & Toggle && d->currentSelection.count()) {
- for (int i = 0; i < d->currentSelection.count(); ++i) {
+ if (d->currentCommand & Toggle && d->currentSelection.size()) {
+ for (int i = 0; i < d->currentSelection.size(); ++i) {
if (d->currentSelection.at(i).left() <= column &&
d->currentSelection.at(i).right() >= column) {
- for (int j = 0; j < d->ranges.count(); ++j) {
+ for (int j = 0; j < d->ranges.size(); ++j) {
if (d->ranges.at(j).left() <= column && d->ranges.at(j).right() >= column
&& d->currentSelection.at(i).intersected(d->ranges.at(j)).isValid()) {
return false;
@@ -1603,8 +1574,7 @@ bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent
}
auto isSelectable = [&](int row, int column) {
- Qt::ItemFlags flags = d->model->index(row, column, parent).flags();
- return (flags & Qt::ItemIsSelectable);
+ return isSelectableAndEnabled(d->model->index(row, column, parent).flags());
};
const int rowCount = d->model->rowCount(parent);
int unselectable = 0;
@@ -1612,7 +1582,7 @@ bool QItemSelectionModel::isColumnSelected(int column, const QModelIndex &parent
// add ranges and currentSelection and check through them all
QList<QItemSelectionRange>::const_iterator it;
QList<QItemSelectionRange> joined = d->ranges;
- if (d->currentSelection.count())
+ if (d->currentSelection.size())
joined += d->currentSelection;
for (int row = 0; row < rowCount; ++row) {
if (!isSelectable(row, column)) {
@@ -1653,7 +1623,7 @@ bool QItemSelectionModel::rowIntersectsSelection(int row, const QModelIndex &par
QItemSelection sel = d->ranges;
sel.merge(d->currentSelection, d->currentCommand);
- for (const QItemSelectionRange &range : qAsConst(sel)) {
+ for (const QItemSelectionRange &range : std::as_const(sel)) {
if (range.parent() != parent)
return false;
int top = range.top();
@@ -1662,8 +1632,7 @@ bool QItemSelectionModel::rowIntersectsSelection(int row, const QModelIndex &par
int right = range.right();
if (top <= row && bottom >= row) {
for (int j = left; j <= right; j++) {
- const Qt::ItemFlags flags = d->model->index(row, j, parent).flags();
- if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled))
+ if (isSelectableAndEnabled(d->model->index(row, j, parent).flags()))
return true;
}
}
@@ -1689,7 +1658,7 @@ bool QItemSelectionModel::columnIntersectsSelection(int column, const QModelInde
QItemSelection sel = d->ranges;
sel.merge(d->currentSelection, d->currentCommand);
- for (const QItemSelectionRange &range : qAsConst(sel)) {
+ for (const QItemSelectionRange &range : std::as_const(sel)) {
if (range.parent() != parent)
return false;
int top = range.top();
@@ -1698,8 +1667,7 @@ bool QItemSelectionModel::columnIntersectsSelection(int column, const QModelInde
int right = range.right();
if (left <= column && right >= column) {
for (int j = top; j <= bottom; j++) {
- const Qt::ItemFlags flags = d->model->index(j, column, parent).flags();
- if ((flags & Qt::ItemIsSelectable) && (flags & Qt::ItemIsEnabled))
+ if (isSelectableAndEnabled(d->model->index(j, column, parent).flags()))
return true;
}
}
@@ -1709,20 +1677,47 @@ bool QItemSelectionModel::columnIntersectsSelection(int column, const QModelInde
}
/*!
+ \internal
+
+ Check whether the selection is empty.
+ In contrast to selection.isEmpty(), this takes into account
+ whether items are enabled and whether they are selectable.
+*/
+static bool selectionIsEmpty(const QItemSelection &selection)
+{
+ return std::all_of(selection.begin(), selection.end(),
+ [](const QItemSelectionRange &r) { return r.isEmpty(); });
+}
+
+/*!
\since 4.2
- Returns \c true if the selection model contains any selection ranges;
+ Returns \c true if the selection model contains any selected item,
otherwise returns \c false.
*/
bool QItemSelectionModel::hasSelection() const
{
Q_D(const QItemSelectionModel);
+
+ // QTreeModel unfortunately sorts itself lazily.
+ // When it sorts itself, it emits are layoutChanged signal.
+ // This layoutChanged signal invalidates d->ranges here.
+ // So QTreeModel must not sort itself while we are iterating over
+ // d->ranges here. It sorts itself in executePendingOperations,
+ // thus preventing the sort to happen inside of selectionIsEmpty below.
+ // Sad story, read more in QTBUG-94546
+ const QAbstractItemModel *model = QItemSelectionModel::model();
+ if (model != nullptr) {
+ auto model_p = static_cast<const QAbstractItemModelPrivate *>(QObjectPrivate::get(model));
+ model_p->executePendingOperations();
+ }
+
if (d->currentCommand & (Toggle | Deselect)) {
QItemSelection sel = d->ranges;
sel.merge(d->currentSelection, d->currentCommand);
- return !sel.isEmpty();
+ return !selectionIsEmpty(sel);
} else {
- return !(d->ranges.isEmpty() && d->currentSelection.isEmpty());
+ return !(selectionIsEmpty(d->ranges) && selectionIsEmpty(d->currentSelection));
}
}
@@ -1771,7 +1766,7 @@ QModelIndexList QItemSelectionModel::selectedRows(int column) const
QDuplicateTracker<RowOrColumnDefinition> rowsSeen;
const QItemSelection ranges = selection();
- for (int i = 0; i < ranges.count(); ++i) {
+ for (int i = 0; i < ranges.size(); ++i) {
const QItemSelectionRange &range = ranges.at(i);
QModelIndex parent = range.parent();
for (int row = range.top(); row <= range.bottom(); row++) {
@@ -1800,7 +1795,7 @@ QModelIndexList QItemSelectionModel::selectedColumns(int row) const
QDuplicateTracker<RowOrColumnDefinition> columnsSeen;
const QItemSelection ranges = selection();
- for (int i = 0; i < ranges.count(); ++i) {
+ for (int i = 0; i < ranges.size(); ++i) {
const QItemSelectionRange &range = ranges.at(i);
QModelIndex parent = range.parent();
for (int column = range.left(); column <= range.right(); column++) {
@@ -1893,9 +1888,8 @@ void QItemSelectionModel::setModel(QAbstractItemModel *model)
{
Q_D(QItemSelectionModel);
d->model.removeBindingUnlessInWrapper();
- if (d->model == model)
+ if (d->model.valueBypassingBindings() == model)
return;
-
d->initModel(model);
d->model.notify();
}
@@ -1923,9 +1917,9 @@ void QItemSelectionModel::emitSelectionChanged(const QItemSelection &newSelectio
// remove equal ranges
bool advance;
- for (int o = 0; o < deselected.count(); ++o) {
+ for (int o = 0; o < deselected.size(); ++o) {
advance = true;
- for (int s = 0; s < selected.count() && o < deselected.count();) {
+ for (int s = 0; s < selected.size() && o < deselected.size();) {
if (deselected.at(o) == selected.at(s)) {
deselected.removeAt(o);
selected.removeAt(s);
@@ -1940,17 +1934,17 @@ void QItemSelectionModel::emitSelectionChanged(const QItemSelection &newSelectio
// find intersections
QItemSelection intersections;
- for (int o = 0; o < deselected.count(); ++o) {
- for (int s = 0; s < selected.count(); ++s) {
+ for (int o = 0; o < deselected.size(); ++o) {
+ for (int s = 0; s < selected.size(); ++s) {
if (deselected.at(o).intersects(selected.at(s)))
intersections.append(deselected.at(o).intersected(selected.at(s)));
}
}
// compare remaining ranges with intersections and split them to find deselected and selected
- for (int i = 0; i < intersections.count(); ++i) {
+ for (int i = 0; i < intersections.size(); ++i) {
// split deselected
- for (int o = 0; o < deselected.count();) {
+ for (int o = 0; o < deselected.size();) {
if (deselected.at(o).intersects(intersections.at(i))) {
QItemSelection::split(deselected.at(o), intersections.at(i), &deselected);
deselected.removeAt(o);
@@ -1959,7 +1953,7 @@ void QItemSelectionModel::emitSelectionChanged(const QItemSelection &newSelectio
}
}
// split selected
- for (int s = 0; s < selected.count();) {
+ for (int s = 0; s < selected.size();) {
if (selected.at(s).intersects(intersections.at(i))) {
QItemSelection::split(selected.at(s), intersections.at(i), &selected);
selected.removeAt(s);