From 057ec49a3e62c68e94c9fb15da54c660d7d05002 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Thu, 24 Dec 2020 12:33:05 +0100 Subject: [PATCH] Remove hack in MergedProxyModel and use QAbstractProxyModel::modelAboutToBeReset() --- src/core/mergedproxymodel.cpp | 20 ++++++++++++-------- src/core/mergedproxymodel.h | 1 + 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/core/mergedproxymodel.cpp b/src/core/mergedproxymodel.cpp index 633fc930f..985a792aa 100644 --- a/src/core/mergedproxymodel.cpp +++ b/src/core/mergedproxymodel.cpp @@ -100,6 +100,7 @@ void MergedProxyModel::DeleteAllMappings() { void MergedProxyModel::AddSubModel(const QModelIndex &source_parent, QAbstractItemModel *submodel) { + connect(submodel, SIGNAL(modelAboutToBeReset()), this, SLOT(SubModelAboutToBeReset())); connect(submodel, SIGNAL(modelReset()), this, SLOT(SubModelReset())); connect(submodel, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)), this, SLOT(RowsAboutToBeInserted(QModelIndex, int, int))); connect(submodel, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)), this, SLOT(RowsAboutToBeRemoved(QModelIndex, int, int))); @@ -191,20 +192,15 @@ void MergedProxyModel::SourceModelReset() { } -void MergedProxyModel::SubModelReset() { +void MergedProxyModel::SubModelAboutToBeReset() { QAbstractItemModel *submodel = qobject_cast(sender()); - // TODO: When we require Qt 4.6, use beginResetModel() and endResetModel() in CollectionModel and catch those here - // that will let us do away with this std::numeric_limits::max() hack. - - // Remove all the children of the item that got deleted QModelIndex source_parent = merge_points_.value(submodel); QModelIndex proxy_parent = mapFromSource(source_parent); - // We can't know how many children it had, since it's already disappeared... resetting_model_ = submodel; - beginRemoveRows(proxy_parent, 0, std::numeric_limits::max() - 1); + beginRemoveRows(proxy_parent, 0, submodel->rowCount()); endRemoveRows(); resetting_model_ = nullptr; @@ -221,6 +217,15 @@ void MergedProxyModel::SubModelReset() { } } +} + +void MergedProxyModel::SubModelReset() { + + QAbstractItemModel *submodel = static_cast(sender()); + + QModelIndex source_parent = merge_points_.value(submodel); + QModelIndex proxy_parent = mapFromSource(source_parent); + // "Insert" items from the newly reset submodel int count = submodel->rowCount(); if (count) { @@ -553,4 +558,3 @@ QModelIndexList MergedProxyModel::mapToSource(const QModelIndexList &proxy_index return ret; } - diff --git a/src/core/mergedproxymodel.h b/src/core/mergedproxymodel.h index 1be8a93a8..03c6b2387 100644 --- a/src/core/mergedproxymodel.h +++ b/src/core/mergedproxymodel.h @@ -87,6 +87,7 @@ class MergedProxyModel : public QAbstractProxyModel { private slots: void SourceModelReset(); + void SubModelAboutToBeReset(); void SubModelReset(); void RowsAboutToBeInserted(const QModelIndex &source_parent, int start, int end);