[kdelibs/KDE/4.11] kdeui: Bring back "KLinkItemSelectionModel: Synchronize currentIndex"

Aurélien Gâteau agateau at kde.org
Wed Sep 11 13:54:26 UTC 2013


Git commit e1618e3abece58cd72537cc741fd827df2b52aba by Aurélien Gâteau.
Committed on 11/09/2013 at 09:29.
Pushed by gateau into branch 'KDE/4.11'.

Bring back "KLinkItemSelectionModel: Synchronize currentIndex"

The changes from this commit were initially committed to KDE/4.11
as 14e2ff5b7359d572cd520fd7da3791230ded169a, but I asked for it to
be reverted before 4.11.1 went out because it caused a bad regression
in Gwenview. It was thus reverted by commit 26201fd95cf69f6fda1d443714beebec3b5be40d.

The fix for the regression in Gwenview has just been committed, so
I am bringing back this one.

CCMAIL:release-team at kde.org

M  +17   -0    kdeui/itemviews/klinkitemselectionmodel.cpp
M  +2    -0    kdeui/itemviews/klinkitemselectionmodel.h
M  +53   -14   kdeui/tests/klinkitemselectionmodeltest.cpp
M  +16   -0    kdeui/tests/klinkitemselectionmodeltest.h

http://commits.kde.org/kdelibs/e1618e3abece58cd72537cc741fd827df2b52aba

diff --git a/kdeui/itemviews/klinkitemselectionmodel.cpp b/kdeui/itemviews/klinkitemselectionmodel.cpp
index ee55d4f..be8395f 100644
--- a/kdeui/itemviews/klinkitemselectionmodel.cpp
+++ b/kdeui/itemviews/klinkitemselectionmodel.cpp
@@ -56,6 +56,8 @@ public:
     }
 
     void sourceSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
+    void sourceCurrentChanged(const QModelIndex& current);
+    void slotCurrentChanged(const QModelIndex& current);
 
     QAbstractItemModel * const m_model;
     QItemSelectionModel * const m_linkedItemSelectionModel;
@@ -68,6 +70,8 @@ KLinkItemSelectionModel::KLinkItemSelectionModel(QAbstractItemModel *model, QIte
         d_ptr(new KLinkItemSelectionModelPrivate(this, model, proxySelector))
 {
     connect(proxySelector, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(sourceSelectionChanged(QItemSelection,QItemSelection)));
+    connect(proxySelector, SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(sourceCurrentChanged(QModelIndex)));
+    connect(this, SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(slotCurrentChanged(QModelIndex)));
 }
 
 KLinkItemSelectionModel::~KLinkItemSelectionModel()
@@ -144,6 +148,12 @@ void KLinkItemSelectionModel::select(const QItemSelection &selection, QItemSelec
     d->m_ignoreCurrentChanged = false;
 }
 
+void KLinkItemSelectionModelPrivate::slotCurrentChanged(const QModelIndex& current)
+{
+    const QModelIndex mappedCurrent = m_indexMapper->mapLeftToRight(current);
+    m_linkedItemSelectionModel->setCurrentIndex(mappedCurrent, QItemSelectionModel::NoUpdate);
+}
+
 void KLinkItemSelectionModelPrivate::sourceSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
 {
     Q_Q(KLinkItemSelectionModel);
@@ -163,4 +173,11 @@ void KLinkItemSelectionModelPrivate::sourceSelectionChanged(const QItemSelection
     q->QItemSelectionModel::select(mappedSelection, QItemSelectionModel::Select);
 }
 
+void KLinkItemSelectionModelPrivate::sourceCurrentChanged(const QModelIndex& current)
+{
+    Q_Q(KLinkItemSelectionModel);
+    const QModelIndex mappedCurrent = m_indexMapper->mapRightToLeft(current);
+    q->setCurrentIndex(mappedCurrent, QItemSelectionModel::NoUpdate);
+}
+
 #include "klinkitemselectionmodel.moc"
diff --git a/kdeui/itemviews/klinkitemselectionmodel.h b/kdeui/itemviews/klinkitemselectionmodel.h
index 392da46..13393de 100644
--- a/kdeui/itemviews/klinkitemselectionmodel.h
+++ b/kdeui/itemviews/klinkitemselectionmodel.h
@@ -110,6 +110,8 @@ protected:
 private:
     Q_DECLARE_PRIVATE(KLinkItemSelectionModel)
     Q_PRIVATE_SLOT( d_func(), void sourceSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected))
+    Q_PRIVATE_SLOT( d_func(), void sourceCurrentChanged(const QModelIndex &current))
+    Q_PRIVATE_SLOT( d_func(), void slotCurrentChanged(const QModelIndex &current))
 };
 
 #endif
diff --git a/kdeui/tests/klinkitemselectionmodeltest.cpp b/kdeui/tests/klinkitemselectionmodeltest.cpp
index c3f7132..91540fd 100644
--- a/kdeui/tests/klinkitemselectionmodeltest.cpp
+++ b/kdeui/tests/klinkitemselectionmodeltest.cpp
@@ -29,34 +29,73 @@
 
 QTEST_KDEMAIN(KLinkItemSelectionModelTest, GUI)
 
-void KLinkItemSelectionModelTest::testToggle()
+void KLinkItemSelectionModelTest::init()
 {
-    // Init mainModel
-    QStandardItemModel mainModel;
+    // Init m_mainModel
+    m_mainModel = new QStandardItemModel;
     for (int x=0; x < 10; ++x) {
-        mainModel.appendRow(new QStandardItem(QString::number(x)));
+        m_mainModel->appendRow(new QStandardItem(QString::number(x)));
     }
-    QItemSelectionModel mainSelectionModel(&mainModel);
+    m_mainSelectionModel = new QItemSelectionModel(m_mainModel);
 
     // Init subModel
-    QSortFilterProxyModel subModel;
-    subModel.setFilterRegExp(QRegExp("^[5-9]"));
-    subModel.setSourceModel(&mainModel);
-    KLinkItemSelectionModel subSelectionModel(&subModel, &mainSelectionModel);
+    m_subModel = new QSortFilterProxyModel;
+    m_subModel->setFilterRegExp(QRegExp("^[5-9]"));
+    m_subModel->setSourceModel(m_mainModel);
+    m_subSelectionModel = new KLinkItemSelectionModel(m_subModel, m_mainSelectionModel);
+}
+
+void KLinkItemSelectionModelTest::cleanup()
+{
+    delete m_mainSelectionModel;
+    m_mainSelectionModel = 0;
+    delete m_mainModel;
+    m_mainModel = 0;
+    delete m_subSelectionModel;
+    m_subSelectionModel = 0;
+    delete m_subModel;
+    m_subModel = 0;
+}
 
+void KLinkItemSelectionModelTest::testToggle()
+{
     // Select last index in subModel
-    QModelIndex subIndex = subModel.index(subModel.rowCount() - 1, 0);
-    subSelectionModel.select(subIndex, QItemSelectionModel::Toggle);
+    QModelIndex subIndex = m_subModel->index(m_subModel->rowCount() - 1, 0);
+    m_subSelectionModel->select(subIndex, QItemSelectionModel::Toggle);
 
     // Check selections
-    QModelIndexList subList = subSelectionModel.selectedIndexes();
+    QModelIndexList subList = m_subSelectionModel->selectedIndexes();
     QCOMPARE(subList.count(), 1);
     QCOMPARE(subList.first(), subIndex);
 
-    QModelIndexList mainList = mainSelectionModel.selectedIndexes();
-    QModelIndex mainIndex = mainModel.index(mainModel.rowCount() - 1, 0);
+    QModelIndexList mainList = m_mainSelectionModel->selectedIndexes();
+    QModelIndex mainIndex = m_mainModel->index(m_mainModel->rowCount() - 1, 0);
     QCOMPARE(mainList.count(), 1);
     QCOMPARE(mainList.first(), mainIndex);
 }
 
+void KLinkItemSelectionModelTest::testMainSetCurrent()
+{
+    // Set last index of mainModel as current
+    QModelIndex mainIndex = m_mainModel->index(m_mainModel->rowCount() - 1, 0);
+    m_mainSelectionModel->setCurrentIndex(mainIndex, QItemSelectionModel::Current);
+
+    // Last index of subModel should be current as well
+    QModelIndex subIndex = m_subSelectionModel->currentIndex();
+    QVERIFY(subIndex.isValid());
+    QCOMPARE(subIndex, m_subModel->index(m_subModel->rowCount() - 1, 0));
+}
+
+void KLinkItemSelectionModelTest::testSubSetCurrent()
+{
+    // Set last index of subModel as current
+    QModelIndex subIndex = m_subModel->index(m_subModel->rowCount() - 1, 0);
+    m_subSelectionModel->setCurrentIndex(subIndex, QItemSelectionModel::Current);
+
+    // Last index of mainModel should be current as well
+    QModelIndex mainIndex = m_mainSelectionModel->currentIndex();
+    QVERIFY(mainIndex.isValid());
+    QCOMPARE(mainIndex, m_mainModel->index(m_mainModel->rowCount() - 1, 0));
+}
+
 #include <klinkitemselectionmodeltest.moc>
diff --git a/kdeui/tests/klinkitemselectionmodeltest.h b/kdeui/tests/klinkitemselectionmodeltest.h
index f3e0fd1..6e9c178 100644
--- a/kdeui/tests/klinkitemselectionmodeltest.h
+++ b/kdeui/tests/klinkitemselectionmodeltest.h
@@ -21,12 +21,28 @@
 
 #include <QtCore/QObject>
 
+class QItemSelectionModel;
+class QStandardItemModel;
+class QSortFilterProxyModel;
+
+class KLinkItemSelectionModel;
+
 class KLinkItemSelectionModelTest : public QObject
 {
     Q_OBJECT
 
 private Q_SLOTS:
+    void init();
+    void cleanup();
     void testToggle();
+    void testMainSetCurrent();
+    void testSubSetCurrent();
+
+private:
+    QStandardItemModel *m_mainModel;
+    QItemSelectionModel *m_mainSelectionModel;
+    QSortFilterProxyModel *m_subModel;
+    KLinkItemSelectionModel *m_subSelectionModel;
 };
 
 #endif /* KLINKITEMSELECTIONMODELTEST_H */


More information about the release-team mailing list