summaryrefslogtreecommitdiffstats
path: root/src/corelib/itemmodels
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/itemmodels')
-rw-r--r--src/corelib/itemmodels/qabstractitemmodel.cpp96
-rw-r--r--src/corelib/itemmodels/qabstractproxymodel.cpp2
-rw-r--r--src/corelib/itemmodels/qconcatenatetablesproxymodel.cpp2
-rw-r--r--src/corelib/itemmodels/qidentityproxymodel.cpp24
-rw-r--r--src/corelib/itemmodels/qidentityproxymodel.h2
-rw-r--r--src/corelib/itemmodels/qitemselectionmodel.cpp19
-rw-r--r--src/corelib/itemmodels/qitemselectionmodel.h10
-rw-r--r--src/corelib/itemmodels/qsortfilterproxymodel.cpp57
-rw-r--r--src/corelib/itemmodels/qsortfilterproxymodel.h22
-rw-r--r--src/corelib/itemmodels/qstringlistmodel.cpp28
10 files changed, 163 insertions, 99 deletions
diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp
index fc69e362c7..f132cba473 100644
--- a/src/corelib/itemmodels/qabstractitemmodel.cpp
+++ b/src/corelib/itemmodels/qabstractitemmodel.cpp
@@ -61,9 +61,9 @@ 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
- QPersistentModelIndexData *d = 0;
+ QPersistentModelIndexData *d = nullptr;
QAbstractItemModel *model = const_cast<QAbstractItemModel *>(index.model());
- QHash<QModelIndex, QPersistentModelIndexData *> &indexes = model->d_func()->persistent.indexes;
+ QMultiHash<QModelIndex, QPersistentModelIndexData *> &indexes = model->d_func()->persistent.indexes;
const auto it = indexes.constFind(index);
if (it != indexes.cend()) {
d = (*it);
@@ -136,7 +136,7 @@ void QPersistentModelIndexData::destroy(QPersistentModelIndexData *data)
*/
QPersistentModelIndex::QPersistentModelIndex()
- : d(0)
+ : d(nullptr)
{
}
@@ -158,7 +158,7 @@ QPersistentModelIndex::QPersistentModelIndex(const QPersistentModelIndex &other)
*/
QPersistentModelIndex::QPersistentModelIndex(const QModelIndex &index)
- : d(0)
+ : d(nullptr)
{
if (index.isValid()) {
d = QPersistentModelIndexData::create(index);
@@ -176,7 +176,7 @@ QPersistentModelIndex::~QPersistentModelIndex()
{
if (d && !d->ref.deref()) {
QPersistentModelIndexData::destroy(d);
- d = 0;
+ d = nullptr;
}
}
@@ -257,7 +257,7 @@ QPersistentModelIndex &QPersistentModelIndex::operator=(const QModelIndex &other
d = QPersistentModelIndexData::create(other);
if (d) d->ref.ref();
} else {
- d = 0;
+ d = nullptr;
}
return *this;
}
@@ -344,7 +344,7 @@ void *QPersistentModelIndex::internalPointer() const
{
if (d)
return d->index.internalPointer();
- return 0;
+ return nullptr;
}
/*!
@@ -432,7 +432,7 @@ Qt::ItemFlags QPersistentModelIndex::flags() const
{
if (d)
return d->index.flags();
- return 0;
+ return { };
}
/*!
@@ -442,7 +442,7 @@ const QAbstractItemModel *QPersistentModelIndex::model() const
{
if (d)
return d->index.model();
- return 0;
+ return nullptr;
}
/*!
@@ -484,7 +484,7 @@ QDebug operator<<(QDebug dbg, const QPersistentModelIndex &idx)
class QEmptyItemModel : public QAbstractItemModel
{
public:
- explicit QEmptyItemModel(QObject *parent = 0) : QAbstractItemModel(parent) {}
+ explicit QEmptyItemModel(QObject *parent = nullptr) : QAbstractItemModel(parent) {}
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; }
@@ -552,32 +552,32 @@ const QHash<int,QByteArray> &QAbstractItemModelPrivate::defaultRoleNames()
bool QAbstractItemModelPrivate::isVariantLessThan(const QVariant &left, const QVariant &right,
Qt::CaseSensitivity cs, bool isLocaleAware)
{
- if (left.userType() == QVariant::Invalid)
+ if (left.userType() == QMetaType::UnknownType)
return false;
- if (right.userType() == QVariant::Invalid)
+ if (right.userType() == QMetaType::UnknownType)
return true;
switch (left.userType()) {
- case QVariant::Int:
+ case QMetaType::Int:
return left.toInt() < right.toInt();
- case QVariant::UInt:
+ case QMetaType::UInt:
return left.toUInt() < right.toUInt();
- case QVariant::LongLong:
+ case QMetaType::LongLong:
return left.toLongLong() < right.toLongLong();
- case QVariant::ULongLong:
+ case QMetaType::ULongLong:
return left.toULongLong() < right.toULongLong();
case QMetaType::Float:
return left.toFloat() < right.toFloat();
- case QVariant::Double:
+ case QMetaType::Double:
return left.toDouble() < right.toDouble();
- case QVariant::Char:
+ case QMetaType::QChar:
return left.toChar() < right.toChar();
- case QVariant::Date:
+ case QMetaType::QDate:
return left.toDate() < right.toDate();
- case QVariant::Time:
+ case QMetaType::QTime:
return left.toTime() < right.toTime();
- case QVariant::DateTime:
+ case QMetaType::QDateTime:
return left.toDateTime() < right.toDateTime();
- case QVariant::String:
+ case QMetaType::QString:
default:
if (isLocaleAware)
return left.toString().localeAwareCompare(right.toString()) < 0;
@@ -591,19 +591,19 @@ static uint typeOfVariant(const QVariant &value)
{
//return 0 for integer, 1 for floating point and 2 for other
switch (value.userType()) {
- case QVariant::Bool:
- case QVariant::Int:
- case QVariant::UInt:
- case QVariant::LongLong:
- case QVariant::ULongLong:
- case QVariant::Char:
+ case QMetaType::Bool:
+ case QMetaType::Int:
+ case QMetaType::UInt:
+ case QMetaType::LongLong:
+ case QMetaType::ULongLong:
+ case QMetaType::QChar:
case QMetaType::Short:
case QMetaType::UShort:
case QMetaType::UChar:
case QMetaType::ULong:
case QMetaType::Long:
return 0;
- case QVariant::Double:
+ case QMetaType::Double:
case QMetaType::Float:
return 1;
default:
@@ -662,8 +662,7 @@ void QAbstractItemModelPrivate::rowsAboutToBeInserted(const QModelIndex &parent,
Q_UNUSED(last);
QVector<QPersistentModelIndexData *> persistent_moved;
if (first < q->rowCount(parent)) {
- for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
- it != persistent.indexes.constEnd(); ++it) {
+ for (auto it = persistent.indexes.constBegin(); it != persistent.indexes.constEnd(); ++it) {
QPersistentModelIndexData *data = *it;
const QModelIndex &index = data->index;
if (index.row() >= first && index.isValid() && index.parent() == parent) {
@@ -699,14 +698,13 @@ void QAbstractItemModelPrivate::itemsAboutToBeMoved(const QModelIndex &srcParent
QVector<QPersistentModelIndexData *> persistent_moved_in_source;
QVector<QPersistentModelIndexData *> persistent_moved_in_destination;
- QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it;
- const QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator begin = persistent.indexes.constBegin();
- const QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator end = persistent.indexes.constEnd();
+ const auto begin = persistent.indexes.constBegin();
+ const auto end = persistent.indexes.constEnd();
const bool sameParent = (srcParent == destinationParent);
const bool movingUp = (srcFirst > destinationChild);
- for ( it = begin; it != end; ++it) {
+ for (auto it = begin; it != end; ++it) {
QPersistentModelIndexData *data = *it;
const QModelIndex &index = data->index;
const QModelIndex &parent = index.parent();
@@ -811,8 +809,7 @@ void QAbstractItemModelPrivate::rowsAboutToBeRemoved(const QModelIndex &parent,
QVector<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 (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
- it != persistent.indexes.constEnd(); ++it) {
+ for (auto it = persistent.indexes.constBegin(); it != persistent.indexes.constEnd(); ++it) {
QPersistentModelIndexData *data = *it;
bool level_changed = false;
QModelIndex current = data->index;
@@ -855,7 +852,9 @@ void QAbstractItemModelPrivate::rowsRemoved(const QModelIndex &parent,
for (QVector<QPersistentModelIndexData *>::const_iterator it = persistent_invalidated.constBegin();
it != persistent_invalidated.constEnd(); ++it) {
QPersistentModelIndexData *data = *it;
- persistent.indexes.erase(persistent.indexes.constFind(data->index));
+ auto pit = persistent.indexes.constFind(data->index);
+ if (pit != persistent.indexes.cend())
+ persistent.indexes.erase(pit);
data->index = QModelIndex();
}
}
@@ -867,8 +866,7 @@ void QAbstractItemModelPrivate::columnsAboutToBeInserted(const QModelIndex &pare
Q_UNUSED(last);
QVector<QPersistentModelIndexData *> persistent_moved;
if (first < q->columnCount(parent)) {
- for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
- it != persistent.indexes.constEnd(); ++it) {
+ for (auto it = persistent.indexes.constBegin(); it != persistent.indexes.constEnd(); ++it) {
QPersistentModelIndexData *data = *it;
const QModelIndex &index = data->index;
if (index.column() >= first && index.isValid() && index.parent() == parent)
@@ -904,8 +902,7 @@ void QAbstractItemModelPrivate::columnsAboutToBeRemoved(const QModelIndex &paren
QVector<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 (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = persistent.indexes.constBegin();
- it != persistent.indexes.constEnd(); ++it) {
+ for (auto it = persistent.indexes.constBegin(); it != persistent.indexes.constEnd(); ++it) {
QPersistentModelIndexData *data = *it;
bool level_changed = false;
QModelIndex current = data->index;
@@ -1950,10 +1947,10 @@ QStringList QAbstractItemModel::mimeTypes() const
QMimeData *QAbstractItemModel::mimeData(const QModelIndexList &indexes) const
{
if (indexes.count() <= 0)
- return 0;
+ return nullptr;
QStringList types = mimeTypes();
if (types.isEmpty())
- return 0;
+ return nullptr;
QMimeData *data = new QMimeData();
QString format = types.at(0);
QByteArray encoded;
@@ -2296,7 +2293,7 @@ Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const
{
Q_D(const QAbstractItemModel);
if (!d->indexValid(index))
- return 0;
+ return { };
return Qt::ItemIsSelectable|Qt::ItemIsEnabled;
}
@@ -2379,7 +2376,7 @@ QModelIndexList QAbstractItemModel::match(const QModelIndex &start, int role,
} else { // QString or regular expression based matching
if (matchType == Qt::MatchRegularExpression) {
if (rx.pattern().isEmpty()) {
- if (value.type() == QVariant::RegularExpression) {
+ if (value.userType() == QMetaType::QRegularExpression) {
rx = value.toRegularExpression();
} else {
rx.setPattern(value.toString());
@@ -3376,8 +3373,7 @@ QModelIndexList QAbstractItemModel::persistentIndexList() const
Q_D(const QAbstractItemModel);
QModelIndexList result;
result.reserve(d->persistent.indexes.count());
- for (QHash<QModelIndex, QPersistentModelIndexData *>::const_iterator it = d->persistent.indexes.constBegin();
- it != d->persistent.indexes.constEnd(); ++it) {
+ for (auto it = d->persistent.indexes.constBegin(); it != d->persistent.indexes.constEnd(); ++it) {
QPersistentModelIndexData *data = *it;
result.append(data->index);
}
@@ -3995,8 +3991,8 @@ bool QAbstractListModel::dropMimeData(const QMimeData *data, Qt::DropAction acti
*/
void QAbstractItemModelPrivate::Persistent::insertMultiAtEnd(const QModelIndex& key, QPersistentModelIndexData *data)
{
- QHash<QModelIndex,QPersistentModelIndexData *>::iterator newIt = indexes.insert(key, data);
- QHash<QModelIndex,QPersistentModelIndexData *>::iterator it = newIt;
+ auto newIt = indexes.insert(key, data);
+ auto it = newIt;
++it;
while (it != indexes.end() && it.key() == key) {
qSwap(*newIt,*it);
diff --git a/src/corelib/itemmodels/qabstractproxymodel.cpp b/src/corelib/itemmodels/qabstractproxymodel.cpp
index c863406afd..87559cd6b2 100644
--- a/src/corelib/itemmodels/qabstractproxymodel.cpp
+++ b/src/corelib/itemmodels/qabstractproxymodel.cpp
@@ -159,7 +159,7 @@ QAbstractItemModel *QAbstractProxyModel::sourceModel() const
{
Q_D(const QAbstractProxyModel);
if (d->model == QAbstractItemModelPrivate::staticEmptyModel())
- return 0;
+ return nullptr;
return d->model;
}
diff --git a/src/corelib/itemmodels/qconcatenatetablesproxymodel.cpp b/src/corelib/itemmodels/qconcatenatetablesproxymodel.cpp
index 0319d215a1..3afa132483 100644
--- a/src/corelib/itemmodels/qconcatenatetablesproxymodel.cpp
+++ b/src/corelib/itemmodels/qconcatenatetablesproxymodel.cpp
@@ -497,7 +497,7 @@ void QConcatenateTablesProxyModel::removeSourceModel(QAbstractItemModel *sourceM
{
Q_D(QConcatenateTablesProxyModel);
Q_ASSERT(d->m_models.contains(sourceModel));
- disconnect(sourceModel, 0, this, 0);
+ disconnect(sourceModel, nullptr, this, nullptr);
const int rowsRemoved = sourceModel->rowCount();
const int rowsPrior = d->computeRowsPrior(sourceModel); // location of removed section
diff --git a/src/corelib/itemmodels/qidentityproxymodel.cpp b/src/corelib/itemmodels/qidentityproxymodel.cpp
index 39992eccd3..f5684c6eda 100644
--- a/src/corelib/itemmodels/qidentityproxymodel.cpp
+++ b/src/corelib/itemmodels/qidentityproxymodel.cpp
@@ -313,6 +313,30 @@ bool QIdentityProxyModel::removeRows(int row, int count, const QModelIndex& pare
/*!
\reimp
+ \since 5.15
+ */
+bool QIdentityProxyModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
+{
+ Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == this : true);
+ Q_ASSERT(destinationParent.isValid() ? destinationParent.model() == this : true);
+ Q_D(QIdentityProxyModel);
+ return d->model->moveRows(mapToSource(sourceParent), sourceRow, count, mapToSource(destinationParent), destinationChild);
+}
+
+/*!
+ \reimp
+ \since 5.15
+ */
+bool QIdentityProxyModel::moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild)
+{
+ Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == this : true);
+ Q_ASSERT(destinationParent.isValid() ? destinationParent.model() == this : true);
+ Q_D(QIdentityProxyModel);
+ return d->model->moveColumns(mapToSource(sourceParent), sourceColumn, count, mapToSource(destinationParent), destinationChild);
+}
+
+/*!
+ \reimp
*/
int QIdentityProxyModel::rowCount(const QModelIndex& parent) const
{
diff --git a/src/corelib/itemmodels/qidentityproxymodel.h b/src/corelib/itemmodels/qidentityproxymodel.h
index 89ac89cdba..4c14e6176a 100644
--- a/src/corelib/itemmodels/qidentityproxymodel.h
+++ b/src/corelib/itemmodels/qidentityproxymodel.h
@@ -77,6 +77,8 @@ public:
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;
+ bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
+ bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destinationParent, int destinationChild) override;
protected:
QIdentityProxyModel(QIdentityProxyModelPrivate &dd, QObject* parent);
diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp
index c93a4d15b9..e4ac5da299 100644
--- a/src/corelib/itemmodels/qitemselectionmodel.cpp
+++ b/src/corelib/itemmodels/qitemselectionmodel.cpp
@@ -656,7 +656,7 @@ void QItemSelectionModelPrivate::initModel(QAbstractItemModel *m)
SLOT(_q_layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)) },
{ SIGNAL(modelReset()),
SLOT(reset()) },
- { 0, 0 }
+ { nullptr, nullptr }
};
if (model == m)
@@ -1627,10 +1627,9 @@ bool QItemSelectionModel::rowIntersectsSelection(int row, const QModelIndex &par
QItemSelection sel = d->ranges;
sel.merge(d->currentSelection, d->currentCommand);
- for (int i = 0; i < sel.count(); ++i) {
- QItemSelectionRange range = sel.at(i);
+ for (const QItemSelectionRange &range : qAsConst(sel)) {
if (range.parent() != parent)
- return false;
+ return false;
int top = range.top();
int bottom = range.bottom();
int left = range.left();
@@ -1661,11 +1660,13 @@ bool QItemSelectionModel::columnIntersectsSelection(int column, const QModelInde
QItemSelection sel = d->ranges;
sel.merge(d->currentSelection, d->currentCommand);
- for (int i = 0; i < sel.count(); ++i) {
- int left = sel.at(i).left();
- int right = sel.at(i).right();
- int top = sel.at(i).top();
- int bottom = sel.at(i).bottom();
+ for (const QItemSelectionRange &range : qAsConst(sel)) {
+ if (range.parent() != parent)
+ return false;
+ int top = range.top();
+ int bottom = range.bottom();
+ int left = range.left();
+ 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();
diff --git a/src/corelib/itemmodels/qitemselectionmodel.h b/src/corelib/itemmodels/qitemselectionmodel.h
index 3c3f9fb1ac..5421eb2afa 100644
--- a/src/corelib/itemmodels/qitemselectionmodel.h
+++ b/src/corelib/itemmodels/qitemselectionmodel.h
@@ -171,11 +171,11 @@ public:
QModelIndex currentIndex() const;
Q_INVOKABLE bool isSelected(const QModelIndex &index) const;
- Q_INVOKABLE bool isRowSelected(int row, const QModelIndex &parent) const;
- Q_INVOKABLE bool isColumnSelected(int column, const QModelIndex &parent) const;
+ Q_INVOKABLE bool isRowSelected(int row, const QModelIndex &parent = QModelIndex()) const;
+ Q_INVOKABLE bool isColumnSelected(int column, const QModelIndex &parent = QModelIndex()) const;
- Q_INVOKABLE bool rowIntersectsSelection(int row, const QModelIndex &parent) const;
- Q_INVOKABLE bool columnIntersectsSelection(int column, const QModelIndex &parent) const;
+ Q_INVOKABLE bool rowIntersectsSelection(int row, const QModelIndex &parent = QModelIndex()) const;
+ Q_INVOKABLE bool columnIntersectsSelection(int column, const QModelIndex &parent = QModelIndex()) const;
bool hasSelection() const;
@@ -241,7 +241,7 @@ inline uint qHash(const QItemSelectionRange &) { return 0; }
# define Q_TEMPLATE_EXTERN extern
# endif
# endif
-Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QList<QItemSelectionRange>;
+Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QItemSelectionRange>;
#endif // Q_CC_MSVC
class Q_CORE_EXPORT QItemSelection : public QList<QItemSelectionRange>
diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.cpp b/src/corelib/itemmodels/qsortfilterproxymodel.cpp
index 675bf4b8c3..033e4e2f97 100644
--- a/src/corelib/itemmodels/qsortfilterproxymodel.cpp
+++ b/src/corelib/itemmodels/qsortfilterproxymodel.cpp
@@ -280,7 +280,7 @@ public:
QVector<int> proxy_rows;
QVector<int> proxy_columns;
QVector<QModelIndex> mapped_children;
- QHash<QModelIndex, Mapping *>::const_iterator map_iter;
+ QModelIndex source_parent;
};
mutable QHash<QModelIndex, Mapping*> source_index_mapping;
@@ -321,7 +321,7 @@ public:
const void *p = proxy_index.internalPointer();
Q_ASSERT(p);
QHash<QModelIndex, Mapping *>::const_iterator it =
- static_cast<const Mapping*>(p)->map_iter;
+ source_index_mapping.constFind(static_cast<const Mapping*>(p)->source_parent);
Q_ASSERT(it != source_index_mapping.constEnd());
Q_ASSERT(it.value());
return it;
@@ -377,6 +377,7 @@ public:
void sort();
bool update_source_sort_column();
+ int find_source_sort_column() const;
void sort_source_rows(QVector<int> &source_rows,
const QModelIndex &source_parent) const;
QVector<QPair<int, QVector<int > > > proxy_intervals_for_source_items_to_add(
@@ -479,11 +480,8 @@ void QSortFilterProxyModelPrivate::_q_clearMapping()
qDeleteAll(source_index_mapping);
source_index_mapping.clear();
- if (dynamic_sortfilter && update_source_sort_column()) {
- //update_source_sort_column might have created wrong mapping so we have to clear it again
- qDeleteAll(source_index_mapping);
- source_index_mapping.clear();
- }
+ if (dynamic_sortfilter)
+ source_sort_column = find_source_sort_column();
// update the persistent indexes
update_persistent_indexes(source_indexes);
@@ -519,8 +517,7 @@ IndexMap::const_iterator QSortFilterProxyModelPrivate::create_mapping(
m->proxy_columns.resize(source_cols);
build_source_to_proxy_mapping(m->source_columns, m->proxy_columns);
- it = IndexMap::const_iterator(source_index_mapping.insert(source_parent, m));
- m->map_iter = it;
+ m->source_parent = source_parent;
if (source_parent.isValid()) {
QModelIndex source_grand_parent = source_parent.parent();
@@ -529,6 +526,7 @@ IndexMap::const_iterator QSortFilterProxyModelPrivate::create_mapping(
it2.value()->mapped_children.append(source_parent);
}
+ it = IndexMap::const_iterator(source_index_mapping.insert(source_parent, m));
Q_ASSERT(it != source_index_mapping.constEnd());
Q_ASSERT(it.value());
@@ -640,6 +638,31 @@ bool QSortFilterProxyModelPrivate::update_source_sort_column()
return old_source_sort_column != source_sort_column;
}
+/*!
+ \internal
+
+ Find the source_sort_column without creating a full mapping and
+ without updating anything.
+*/
+int QSortFilterProxyModelPrivate::find_source_sort_column() const
+{
+ if (proxy_sort_column == -1)
+ return -1;
+
+ const QModelIndex rootIndex;
+ const int source_cols = model->columnCount();
+ int accepted_columns = -1;
+
+ Q_Q(const QSortFilterProxyModel);
+ for (int i = 0; i < source_cols; ++i) {
+ if (q->filterAcceptsColumn(i, rootIndex)) {
+ if (++accepted_columns == proxy_sort_column)
+ return i;
+ }
+ }
+
+ return -1;
+}
/*!
\internal
@@ -1146,7 +1169,8 @@ void QSortFilterProxyModelPrivate::updateChildrenMapping(const QModelIndex &sour
// reinsert moved, mapped indexes
QVector<QPair<QModelIndex, Mapping*> >::iterator it = moved_source_index_mappings.begin();
for (; it != moved_source_index_mappings.end(); ++it) {
- (*it).second->map_iter = QHash<QModelIndex, Mapping *>::const_iterator(source_index_mapping.insert((*it).first, (*it).second));
+ it->second->source_parent = it->first;
+ source_index_mapping.insert(it->first, it->second);
}
}
@@ -1591,11 +1615,8 @@ void QSortFilterProxyModelPrivate::_q_sourceLayoutChanged(const QList<QPersisten
update_persistent_indexes(saved_persistent_indexes);
saved_persistent_indexes.clear();
- if (dynamic_sortfilter && update_source_sort_column()) {
- //update_source_sort_column might have created wrong mapping so we have to clear it again
- qDeleteAll(source_index_mapping);
- source_index_mapping.clear();
- }
+ if (dynamic_sortfilter)
+ source_sort_column = find_source_sort_column();
emit q->layoutChanged(saved_layoutChange_parents);
saved_layoutChange_parents.clear();
@@ -2662,6 +2683,7 @@ void QSortFilterProxyModel::setFilterCaseSensitivity(Qt::CaseSensitivity cs)
d->filter_about_to_be_changed();
d->filter_data.setCaseSensitivity(cs);
d->filter_changed();
+ emit filterCaseSensitivityChanged(cs);
}
/*!
@@ -2687,6 +2709,7 @@ void QSortFilterProxyModel::setSortCaseSensitivity(Qt::CaseSensitivity cs)
d->sort_casesensitivity = cs;
d->sort();
+ emit sortCaseSensitivityChanged(cs);
}
/*!
@@ -2712,6 +2735,7 @@ void QSortFilterProxyModel::setSortLocaleAware(bool on)
d->sort_localeaware = on;
d->sort();
+ emit sortLocaleAwareChanged(on);
}
/*!
@@ -2836,6 +2860,7 @@ void QSortFilterProxyModel::setSortRole(int role)
return;
d->sort_role = role;
d->sort();
+ emit sortRoleChanged(role);
}
/*!
@@ -2861,6 +2886,7 @@ void QSortFilterProxyModel::setFilterRole(int role)
d->filter_about_to_be_changed();
d->filter_role = role;
d->filter_changed();
+ emit filterRoleChanged(role);
}
/*!
@@ -2887,6 +2913,7 @@ void QSortFilterProxyModel::setRecursiveFilteringEnabled(bool recursive)
d->filter_about_to_be_changed();
d->filter_recursive = recursive;
d->filter_changed();
+ emit recursiveFilteringEnabledChanged(recursive);
}
#if QT_DEPRECATED_SINCE(5, 11)
diff --git a/src/corelib/itemmodels/qsortfilterproxymodel.h b/src/corelib/itemmodels/qsortfilterproxymodel.h
index 303226668f..91253dd601 100644
--- a/src/corelib/itemmodels/qsortfilterproxymodel.h
+++ b/src/corelib/itemmodels/qsortfilterproxymodel.h
@@ -68,12 +68,12 @@ class Q_CORE_EXPORT QSortFilterProxyModel : public QAbstractProxyModel
#endif
Q_PROPERTY(int filterKeyColumn READ filterKeyColumn WRITE setFilterKeyColumn)
Q_PROPERTY(bool dynamicSortFilter READ dynamicSortFilter WRITE setDynamicSortFilter)
- Q_PROPERTY(Qt::CaseSensitivity filterCaseSensitivity READ filterCaseSensitivity WRITE setFilterCaseSensitivity)
- Q_PROPERTY(Qt::CaseSensitivity sortCaseSensitivity READ sortCaseSensitivity WRITE setSortCaseSensitivity)
- Q_PROPERTY(bool isSortLocaleAware READ isSortLocaleAware WRITE setSortLocaleAware)
- Q_PROPERTY(int sortRole READ sortRole WRITE setSortRole)
- Q_PROPERTY(int filterRole READ filterRole WRITE setFilterRole)
- Q_PROPERTY(bool recursiveFilteringEnabled READ isRecursiveFilteringEnabled WRITE setRecursiveFilteringEnabled)
+ Q_PROPERTY(Qt::CaseSensitivity filterCaseSensitivity READ filterCaseSensitivity WRITE setFilterCaseSensitivity NOTIFY filterCaseSensitivityChanged)
+ Q_PROPERTY(Qt::CaseSensitivity sortCaseSensitivity READ sortCaseSensitivity WRITE setSortCaseSensitivity NOTIFY sortCaseSensitivityChanged)
+ Q_PROPERTY(bool isSortLocaleAware READ isSortLocaleAware WRITE setSortLocaleAware NOTIFY sortLocaleAwareChanged)
+ Q_PROPERTY(int sortRole READ sortRole WRITE setSortRole NOTIFY sortRoleChanged)
+ Q_PROPERTY(int filterRole READ filterRole WRITE setFilterRole NOTIFY filterRoleChanged)
+ Q_PROPERTY(bool recursiveFilteringEnabled READ isRecursiveFilteringEnabled WRITE setRecursiveFilteringEnabled NOTIFY recursiveFilteringEnabledChanged)
public:
explicit QSortFilterProxyModel(QObject *parent = nullptr);
@@ -185,6 +185,16 @@ public:
QStringList mimeTypes() const override;
Qt::DropActions supportedDropActions() const override;
+
+Q_SIGNALS:
+ void dynamicSortFilterChanged(bool dynamicSortFilter);
+ void filterCaseSensitivityChanged(Qt::CaseSensitivity filterCaseSensitivity);
+ void sortCaseSensitivityChanged(Qt::CaseSensitivity sortCaseSensitivity);
+ void sortLocaleAwareChanged(bool sortLocaleAware);
+ void sortRoleChanged(int sortRole);
+ void filterRoleChanged(int filterRole);
+ void recursiveFilteringEnabledChanged(bool recursiveFilteringEnabled);
+
private:
Q_DECLARE_PRIVATE(QSortFilterProxyModel)
Q_DISABLE_COPY(QSortFilterProxyModel)
diff --git a/src/corelib/itemmodels/qstringlistmodel.cpp b/src/corelib/itemmodels/qstringlistmodel.cpp
index a248cdcd38..a950783ed8 100644
--- a/src/corelib/itemmodels/qstringlistmodel.cpp
+++ b/src/corelib/itemmodels/qstringlistmodel.cpp
@@ -212,6 +212,7 @@ Qt::ItemFlags QStringListModel::flags(const QModelIndex &index) const
\a index in the model, to the provided \a value.
The dataChanged() signal is emitted if the item is changed.
+ Returns \c true after emitting the dataChanged() signal.
\sa Qt::ItemDataRole, data()
*/
@@ -249,6 +250,8 @@ bool QStringListModel::clearItemData(const QModelIndex &index)
specified, indicating that the rows are inserted in the top level of
the model.
+ Returns \c true if the insertion was successful.
+
\sa QAbstractItemModel::insertRows()
*/
@@ -275,6 +278,8 @@ bool QStringListModel::insertRows(int row, int count, const QModelIndex &parent)
specified, indicating that the rows are removed in the top level of
the model.
+ Returns \c true if the row removal was successful.
+
\sa QAbstractItemModel::removeRows()
*/
@@ -301,24 +306,23 @@ bool QStringListModel::moveRows(const QModelIndex &sourceParent, int sourceRow,
{
if (sourceRow < 0
|| sourceRow + count - 1 >= rowCount(sourceParent)
- || destinationChild <= 0
+ || destinationChild < 0
|| destinationChild > rowCount(destinationParent)
+ || sourceRow == destinationChild
|| sourceRow == destinationChild - 1
- || count <= 0) {
+ || count <= 0
+ || sourceParent.isValid()
+ || destinationParent.isValid()) {
return false;
}
if (!beginMoveRows(QModelIndex(), sourceRow, sourceRow + count - 1, QModelIndex(), destinationChild))
return false;
- /*
- QList::move assumes that the second argument is the index where the item will end up to
- i.e. the valid range for that argument is from 0 to QList::size()-1
- QAbstractItemModel::moveRows when source and destinations have the same parent assumes that
- the item will end up being in the row BEFORE the one indicated by destinationChild
- i.e. the valid range for that argument is from 1 to QList::size()
- For this reason we remove 1 from destinationChild when using it inside QList
- */
- destinationChild--;
- const int fromRow = destinationChild < sourceRow ? (sourceRow + count - 1) : sourceRow;
+
+ int fromRow = sourceRow;
+ if (destinationChild < sourceRow)
+ fromRow += count - 1;
+ else
+ destinationChild--;
while (count--)
lst.move(fromRow, destinationChild);
endMoveRows();