feat(ProfileShowcase): Fixing MovableModel signal handling after the sync state changes

1. Fixing duplicate connections on source model
2. Update connections and indexes when the internal data gets reset
This commit is contained in:
Alex Jbanca 2024-02-29 09:32:25 +02:00 committed by Alex Jbanca
parent 07484cb15c
commit c916bfa84e
1 changed files with 39 additions and 26 deletions

View File

@ -98,13 +98,13 @@ void MovableModel::desyncOrder()
&MovableModel::sourceLayoutChanged); &MovableModel::sourceLayoutChanged);
connect(m_sourceModel, &QAbstractItemModel::dataChanged, this, connect(m_sourceModel, &QAbstractItemModel::dataChanged, this,
&MovableModel::desyncedSourceDataChanged); &MovableModel::desyncedSourceDataChanged, Qt::UniqueConnection);
connect(m_sourceModel, &QAbstractItemModel::rowsInserted, this, connect(m_sourceModel, &QAbstractItemModel::rowsInserted, this,
&MovableModel::sourceRowsInserted); &MovableModel::sourceRowsInserted, Qt::UniqueConnection);
connect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, connect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeRemoved, this,
&MovableModel::sourceRowsAboutToBeRemoved); &MovableModel::sourceRowsAboutToBeRemoved, Qt::UniqueConnection);
auto count = m_sourceModel->rowCount(); auto count = m_sourceModel->rowCount();
@ -127,18 +127,6 @@ void MovableModel::syncOrder()
auto sourceModel = m_sourceModel; auto sourceModel = m_sourceModel;
disconnect(m_sourceModel, nullptr, this, nullptr);
connectSignalsForSyncedState();
for (int i = 0; i < m_indexes.size(); ++i) {
const QModelIndex idx = m_indexes[i];
if (i == idx.row())
continue;
changePersistentIndex(index(i, 0), index(idx.row(), 0));
}
resetInternalData(); resetInternalData();
emit layoutChanged(); emit layoutChanged();
} }
@ -199,6 +187,18 @@ bool MovableModel::synced() const
void MovableModel::resetInternalData() void MovableModel::resetInternalData()
{ {
disconnect(m_sourceModel, nullptr, this, nullptr);
connectSignalsForSyncedState();
for (int i = 0; i < m_indexes.size(); ++i) {
const QModelIndex idx = m_indexes[i];
if (i == idx.row())
continue;
changePersistentIndex(index(i, 0), index(idx.row(), 0));
}
QAbstractListModel::resetInternalData(); QAbstractListModel::resetInternalData();
m_indexes.clear(); m_indexes.clear();
@ -288,6 +288,7 @@ void MovableModel::sourceRowsAboutToBeRemoved(const QModelIndex& parent,
int first, int last) int first, int last)
{ {
Q_ASSERT(!parent.isValid()); Q_ASSERT(!parent.isValid());
Q_ASSERT(!m_synced);
std::vector<int> indicesToRemove; std::vector<int> indicesToRemove;
indicesToRemove.reserve(last - first + 1); indicesToRemove.reserve(last - first + 1);
@ -299,6 +300,9 @@ void MovableModel::sourceRowsAboutToBeRemoved(const QModelIndex& parent,
indicesToRemove.push_back(i); indicesToRemove.push_back(i);
} }
if (indicesToRemove.empty())
return;
std::vector<std::pair<int, int>> sequences; std::vector<std::pair<int, int>> sequences;
auto sequenceBegin = indicesToRemove.front(); auto sequenceBegin = indicesToRemove.front();
auto sequenceEnd = sequenceBegin; auto sequenceEnd = sequenceBegin;
@ -338,35 +342,44 @@ void MovableModel::connectSignalsForSyncedState()
return; return;
connect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeInserted, this, connect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeInserted, this,
&MovableModel::beginInsertRows); &MovableModel::beginInsertRows, Qt::UniqueConnection);
connect(m_sourceModel, &QAbstractItemModel::rowsInserted, this, connect(m_sourceModel, &QAbstractItemModel::rowsInserted, this,
&MovableModel::endInsertRows); &MovableModel::endInsertRows, Qt::UniqueConnection);
connect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, connect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeRemoved, this,
&MovableModel::beginRemoveRows); &MovableModel::beginRemoveRows, Qt::UniqueConnection);
connect(m_sourceModel, &QAbstractItemModel::rowsRemoved, this, connect(m_sourceModel, &QAbstractItemModel::rowsRemoved, this,
&MovableModel::endRemoveRows); &MovableModel::endRemoveRows, Qt::UniqueConnection);
connect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeMoved, this, connect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeMoved, this,
&MovableModel::beginMoveRows); &MovableModel::beginMoveRows, Qt::UniqueConnection);
connect(m_sourceModel, &QAbstractItemModel::rowsMoved, this, connect(m_sourceModel, &QAbstractItemModel::rowsMoved, this,
&MovableModel::endMoveRows); &MovableModel::endMoveRows, Qt::UniqueConnection);
connect(m_sourceModel, &QAbstractItemModel::dataChanged, this, connect(m_sourceModel, &QAbstractItemModel::dataChanged, this,
&MovableModel::syncedSourceDataChanged); &MovableModel::syncedSourceDataChanged, Qt::UniqueConnection);
connect(m_sourceModel, &QAbstractItemModel::layoutAboutToBeChanged, this, connect(m_sourceModel, &QAbstractItemModel::layoutAboutToBeChanged, this,
&MovableModel::sourceLayoutAboutToBeChanged); &MovableModel::sourceLayoutAboutToBeChanged, Qt::UniqueConnection);
connect(m_sourceModel, &QAbstractItemModel::layoutChanged, this, connect(m_sourceModel, &QAbstractItemModel::layoutChanged, this,
&MovableModel::sourceLayoutChanged); &MovableModel::sourceLayoutChanged, Qt::UniqueConnection);
connect(m_sourceModel, &QAbstractItemModel::modelAboutToBeReset, this, connect(m_sourceModel, &QAbstractItemModel::modelAboutToBeReset, this,
&MovableModel::beginResetModel); &MovableModel::beginResetModel, Qt::UniqueConnection);
connect(m_sourceModel, &QAbstractItemModel::modelReset, this, connect(m_sourceModel, &QAbstractItemModel::modelReset, this,
&MovableModel::endResetModel); &MovableModel::endResetModel, Qt::UniqueConnection);
disconnect(m_sourceModel, &QAbstractItemModel::dataChanged, this,
&MovableModel::desyncedSourceDataChanged);
disconnect(m_sourceModel, &QAbstractItemModel::rowsInserted, this,
&MovableModel::sourceRowsInserted);
disconnect(m_sourceModel, &QAbstractItemModel::rowsAboutToBeRemoved, this,
&MovableModel::sourceRowsAboutToBeRemoved);
} }