From 2c1d1c136102a17eef9ae3c4e9f0cf01338306ae Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Thu, 20 May 2010 13:07:41 +0200 Subject: Deselect the current selection when the QItemSelectionModel::model is reset. Merge-request: 639 Reviewed-by: Olivier Goffart --- src/gui/itemviews/qitemselectionmodel.cpp | 25 +++++++++--- src/gui/itemviews/qitemselectionmodel.h | 1 + src/gui/itemviews/qitemselectionmodel_p.h | 3 ++ .../tst_qitemselectionmodel.cpp | 47 ++++++++++++++++++++-- 4 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/gui/itemviews/qitemselectionmodel.cpp b/src/gui/itemviews/qitemselectionmodel.cpp index f848321..fc99439 100644 --- a/src/gui/itemviews/qitemselectionmodel.cpp +++ b/src/gui/itemviews/qitemselectionmodel.cpp @@ -566,6 +566,8 @@ void QItemSelectionModelPrivate::initModel(QAbstractItemModel *model) q, SLOT(_q_layoutAboutToBeChanged())); QObject::connect(model, SIGNAL(layoutChanged()), q, SLOT(_q_layoutChanged())); + QObject::connect(model, SIGNAL(modelAboutToBeReset()), + q, SLOT(_q_modelAboutToBeReset())); } } @@ -896,6 +898,13 @@ void QItemSelectionModelPrivate::_q_layoutChanged() savedPersistentCurrentIndexes.clear(); } +void QItemSelectionModelPrivate::_q_modelAboutToBeReset() +{ + Q_Q(QItemSelectionModel); + q->clearSelection(); + clearCurrentIndex(); +} + /*! \class QItemSelectionModel @@ -1095,12 +1104,18 @@ void QItemSelectionModel::clear() { Q_D(QItemSelectionModel); clearSelection(); - QModelIndex previous = d->currentIndex; - d->currentIndex = QModelIndex(); + d->clearCurrentIndex(); +} + +void QItemSelectionModelPrivate::clearCurrentIndex() +{ + Q_Q(QItemSelectionModel); + QModelIndex previous = currentIndex; + currentIndex = QModelIndex(); if (previous.isValid()) { - emit currentChanged(d->currentIndex, previous); - emit currentRowChanged(d->currentIndex, previous); - emit currentColumnChanged(d->currentIndex, previous); + emit q->currentChanged(currentIndex, previous); + emit q->currentRowChanged(currentIndex, previous); + emit q->currentColumnChanged(currentIndex, previous); } } diff --git a/src/gui/itemviews/qitemselectionmodel.h b/src/gui/itemviews/qitemselectionmodel.h index 436514f..8682109 100644 --- a/src/gui/itemviews/qitemselectionmodel.h +++ b/src/gui/itemviews/qitemselectionmodel.h @@ -197,6 +197,7 @@ private: Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeInserted(const QModelIndex&, int, int)) Q_PRIVATE_SLOT(d_func(), void _q_layoutAboutToBeChanged()) Q_PRIVATE_SLOT(d_func(), void _q_layoutChanged()) + Q_PRIVATE_SLOT(d_func(), void _q_modelAboutToBeReset()) }; Q_DECLARE_OPERATORS_FOR_FLAGS(QItemSelectionModel::SelectionFlags) diff --git a/src/gui/itemviews/qitemselectionmodel_p.h b/src/gui/itemviews/qitemselectionmodel_p.h index 5afa90d..c2fe976 100644 --- a/src/gui/itemviews/qitemselectionmodel_p.h +++ b/src/gui/itemviews/qitemselectionmodel_p.h @@ -78,6 +78,7 @@ public: void _q_columnsAboutToBeInserted(const QModelIndex &parent, int start, int end); void _q_layoutAboutToBeChanged(); void _q_layoutChanged(); + void _q_modelAboutToBeReset(); inline void remove(QList &r) { @@ -93,6 +94,8 @@ public: currentSelection.clear(); } + void clearCurrentIndex(); + QPointer model; QItemSelection ranges; QItemSelection currentSelection; diff --git a/tests/auto/qitemselectionmodel/tst_qitemselectionmodel.cpp b/tests/auto/qitemselectionmodel/tst_qitemselectionmodel.cpp index 3b2a716..9858829 100644 --- a/tests/auto/qitemselectionmodel/tst_qitemselectionmodel.cpp +++ b/tests/auto/qitemselectionmodel/tst_qitemselectionmodel.cpp @@ -1536,6 +1536,43 @@ public: inline void reset() { QStandardItemModel::reset(); } }; +class ResetObserver : public QObject +{ + QItemSelectionModel * const m_selectionModel; + QItemSelection m_selection; + Q_OBJECT +public: + ResetObserver(QItemSelectionModel *selectionModel) + : m_selectionModel(selectionModel) + { + connect(selectionModel->model(), SIGNAL(modelAboutToBeReset()),SLOT(modelAboutToBeReset())); + connect(selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(selectionChanged(QItemSelection,QItemSelection))); + connect(selectionModel->model(), SIGNAL(modelReset()),SLOT(modelReset())); + } + +private slots: + void modelAboutToBeReset() + { + m_selection = m_selectionModel->selection(); + foreach(const QItemSelectionRange &range, m_selection) + { + QVERIFY(range.isValid()); + } + } + + void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) + { + qDebug() << deselected << selected; + QCOMPARE(m_selection, deselected); + m_selection.clear(); + } + + void modelReset() + { + QVERIFY(m_selectionModel->selection().isEmpty()); + } +}; + void tst_QItemSelectionModel::resetModel() { MyStandardItemModel model(20, 20); @@ -1555,11 +1592,13 @@ void tst_QItemSelectionModel::resetModel() view.selectionModel()->select(QItemSelection(model.index(0, 0), model.index(5, 5)), QItemSelectionModel::Select); - QCOMPARE(spy.count(), 2); - QCOMPARE(spy.at(1).count(), 2); + // We get this signal three times. Twice in this test method and once because the source reset causes the current selection to + // be deselected. + QCOMPARE(spy.count(), 3); + QCOMPARE(spy.at(2).count(), 2); // make sure we don't get an "old selection" - QCOMPARE(spy.at(1).at(1).userType(), qMetaTypeId()); - QVERIFY(qvariant_cast(spy.at(1).at(1)).isEmpty()); + QCOMPARE(spy.at(2).at(1).userType(), qMetaTypeId()); + QVERIFY(qvariant_cast(spy.at(2).at(1)).isEmpty()); } void tst_QItemSelectionModel::removeRows_data() -- cgit v0.12