From aeb16937ced1bf426c2ca21dcc270f9e4a96645c Mon Sep 17 00:00:00 2001 From: Frank Reininghaus Date: Mon, 3 Oct 2011 16:42:02 +0200 Subject: [PATCH] Add function KItemListSelectionManager::isSelected(int index) This function is used in KItemListView::updateWidgetProperties() to find out if an item is selected in a more efficient way. The new function is tested in KItemListSelectionManagerTest. I've factored out some code from KItemListSelectionManagerTest::testChangeSelection() to a new member to simplify the test. --- src/kitemviews/kitemlistselectionmanager.cpp | 20 ++++++ src/kitemviews/kitemlistselectionmanager.h | 1 + src/kitemviews/kitemlistview.cpp | 9 +-- src/tests/kitemlistselectionmanagertest.cpp | 75 +++++++++----------- 4 files changed, 54 insertions(+), 51 deletions(-) diff --git a/src/kitemviews/kitemlistselectionmanager.cpp b/src/kitemviews/kitemlistselectionmanager.cpp index 131ee46e6..fb279796f 100644 --- a/src/kitemviews/kitemlistselectionmanager.cpp +++ b/src/kitemviews/kitemlistselectionmanager.cpp @@ -95,6 +95,26 @@ QSet KItemListSelectionManager::selectedItems() const return selectedItems; } +bool KItemListSelectionManager::isSelected(int index) const +{ + if (m_selectedItems.contains(index)) { + return true; + } + + if (m_isAnchoredSelectionActive && m_anchorItem != m_currentItem) { + Q_ASSERT(m_anchorItem >= 0); + Q_ASSERT(m_currentItem >= 0); + const int from = qMin(m_anchorItem, m_currentItem); + const int to = qMax(m_anchorItem, m_currentItem); + + if (from <= index && index <= to) { + return true; + } + } + + return false; +} + bool KItemListSelectionManager::hasSelection() const { return !m_selectedItems.isEmpty() || (m_isAnchoredSelectionActive && m_anchorItem != m_currentItem); diff --git a/src/kitemviews/kitemlistselectionmanager.h b/src/kitemviews/kitemlistselectionmanager.h index a8ef5ca2d..82ae13abb 100644 --- a/src/kitemviews/kitemlistselectionmanager.h +++ b/src/kitemviews/kitemlistselectionmanager.h @@ -54,6 +54,7 @@ public: void setSelectedItems(const QSet& items); QSet selectedItems() const; + bool isSelected(int index) const; bool hasSelection() const; void setSelected(int index, int count = 1, SelectionMode mode = Select); diff --git a/src/kitemviews/kitemlistview.cpp b/src/kitemviews/kitemlistview.cpp index d96cf14ce..ad4221394 100644 --- a/src/kitemviews/kitemlistview.cpp +++ b/src/kitemviews/kitemlistview.cpp @@ -1393,16 +1393,9 @@ void KItemListView::updateWidgetProperties(KItemListWidget* widget, int index) const KItemListSelectionManager* selectionManager = m_controller->selectionManager(); widget->setCurrent(index == selectionManager->currentItem()); - - if (selectionManager->hasSelection()) { - const QSet selectedItems = selectionManager->selectedItems(); - widget->setSelected(selectedItems.contains(index)); - } else { - widget->setSelected(false); - } + widget->setSelected(selectionManager->isSelected(index)); widget->setHovered(false); - widget->setIndex(index); widget->setData(m_model->data(index)); } diff --git a/src/tests/kitemlistselectionmanagertest.cpp b/src/tests/kitemlistselectionmanagertest.cpp index 2a3601bb0..2fa6f677b 100644 --- a/src/tests/kitemlistselectionmanagertest.cpp +++ b/src/tests/kitemlistselectionmanagertest.cpp @@ -69,6 +69,8 @@ private slots: void testChangeSelection(); private: + void verifySelectionChange(QSignalSpy& spy, const QSet& currentSelection, const QSet& previousSelection) const; + KItemListSelectionManager* m_selectionManager; }; @@ -425,18 +427,8 @@ void KItemListSelectionManagerTest::testChangeSelection() // Perform the initial selectiion m_selectionManager->setSelectedItems(initialSelection); - QCOMPARE(m_selectionManager->selectedItems(), initialSelection); - if (initialSelection.isEmpty()) { - QVERIFY(!m_selectionManager->hasSelection()); - QCOMPARE(spySelectionChanged.count(), 0); - } - else { - QVERIFY(m_selectionManager->hasSelection()); - QCOMPARE(spySelectionChanged.count(), 1); - QList arguments = spySelectionChanged.takeFirst(); - QCOMPARE(qvariant_cast >(arguments.at(0)), initialSelection); - QCOMPARE(qvariant_cast >(arguments.at(1)), QSet()); - } + + verifySelectionChange(spySelectionChanged, initialSelection, QSet()); // Perform an anchored selection. // Note that current and anchor index are equal first because this is the case in typical uses of the @@ -446,17 +438,8 @@ void KItemListSelectionManagerTest::testChangeSelection() m_selectionManager->setCurrentItem(current); QCOMPARE(m_selectionManager->m_anchorItem, anchor); QCOMPARE(m_selectionManager->currentItem(), current); - QCOMPARE(m_selectionManager->selectedItems(), expectedSelection); - QCOMPARE(m_selectionManager->hasSelection(), !expectedSelection.isEmpty()); - if (expectedSelection == initialSelection) { - QCOMPARE(spySelectionChanged.count(), 0); - } - else { - QCOMPARE(spySelectionChanged.count(), 1); - QList arguments = spySelectionChanged.takeFirst(); - QCOMPARE(qvariant_cast >(arguments.at(0)), expectedSelection); - QCOMPARE(qvariant_cast >(arguments.at(1)), initialSelection); - } + + verifySelectionChange(spySelectionChanged, expectedSelection, initialSelection); // Change the model by inserting or removing items. switch (changeType) { @@ -483,31 +466,37 @@ void KItemListSelectionManagerTest::testChangeSelection() break; } - QCOMPARE(m_selectionManager->selectedItems(), finalSelection); - QCOMPARE(m_selectionManager->hasSelection(), !finalSelection.isEmpty()); - if (finalSelection == expectedSelection) { - QCOMPARE(spySelectionChanged.count(), 0); - } - else { - QCOMPARE(spySelectionChanged.count(), 1); - QList arguments = spySelectionChanged.takeFirst(); - QCOMPARE(qvariant_cast >(arguments.at(0)), finalSelection); - QCOMPARE(qvariant_cast >(arguments.at(1)), expectedSelection); - } + verifySelectionChange(spySelectionChanged, finalSelection, expectedSelection); // Finally, clear the selection m_selectionManager->clearSelection(); - QCOMPARE(m_selectionManager->selectedItems(), QSet()); - QVERIFY(!m_selectionManager->hasSelection()); - if (finalSelection.isEmpty()) { - // Selection has been empty already - QCOMPARE(spySelectionChanged.count(), 0); + + verifySelectionChange(spySelectionChanged, QSet(), finalSelection); +} + +void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy& spy, + const QSet& currentSelection, + const QSet& previousSelection) const +{ + QCOMPARE(m_selectionManager->selectedItems(), currentSelection); + QCOMPARE(m_selectionManager->hasSelection(), !currentSelection.isEmpty()); + for (int index = 0; index < m_selectionManager->model()->count(); ++index) { + if (currentSelection.contains(index)) { + QVERIFY(m_selectionManager->isSelected(index)); + } + else { + QVERIFY(!m_selectionManager->isSelected(index)); + } + } + + if (currentSelection == previousSelection) { + QCOMPARE(spy.count(), 0); } else { - QCOMPARE(spySelectionChanged.count(), 1); - QList arguments = spySelectionChanged.takeFirst(); - QCOMPARE(qvariant_cast >(arguments.at(0)), QSet()); - QCOMPARE(qvariant_cast >(arguments.at(1)), finalSelection); + QCOMPARE(spy.count(), 1); + QList arguments = spy.takeFirst(); + QCOMPARE(qvariant_cast >(arguments.at(0)), currentSelection); + QCOMPARE(qvariant_cast >(arguments.at(1)), previousSelection); } } -- 2.47.3