]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Add function KItemListSelectionManager::isSelected(int index)
authorFrank Reininghaus <frank78ac@googlemail.com>
Mon, 3 Oct 2011 14:42:02 +0000 (16:42 +0200)
committerFrank Reininghaus <frank78ac@googlemail.com>
Mon, 3 Oct 2011 14:42:02 +0000 (16:42 +0200)
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
src/kitemviews/kitemlistselectionmanager.h
src/kitemviews/kitemlistview.cpp
src/tests/kitemlistselectionmanagertest.cpp

index 131ee46e6278a4d7598d5bfbfd9bd75f82bdd4eb..fb279796f66b8e87e8f297b4a3a31c30756c00fa 100644 (file)
@@ -95,6 +95,26 @@ QSet<int> 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);
index a8ef5ca2df62b163fb080833b6ea1722d6ee989b..82ae13abb552bd66694102d73c5926a85c9d812a 100644 (file)
@@ -54,6 +54,7 @@ public:
 
     void setSelectedItems(const QSet<int>& items);
     QSet<int> selectedItems() const;
+    bool isSelected(int index) const;
     bool hasSelection() const;
 
     void setSelected(int index, int count = 1, SelectionMode mode = Select);
index d96cf14cec152d3286aedc46c23f3edd29dc34af..ad4221394356a7b7bbc69f6a229670333c446152 100644 (file)
@@ -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<int> 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));
 }
index 2a3601bb0a198dfeefcde7578b8191f351b55a7e..2fa6f677b4de579a33ce5e286fd460b893194102 100644 (file)
@@ -69,6 +69,8 @@ private slots:
     void testChangeSelection();
 
 private:
+    void verifySelectionChange(QSignalSpy& spy, const QSet<int>& currentSelection, const QSet<int>& 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<QVariant> arguments = spySelectionChanged.takeFirst();
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), initialSelection);
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), QSet<int>());
-    }
+
+    verifySelectionChange(spySelectionChanged, initialSelection, QSet<int>());
 
     // 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<QVariant> arguments = spySelectionChanged.takeFirst();
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), expectedSelection);
-        QCOMPARE(qvariant_cast<QSet<int> >(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<QVariant> arguments = spySelectionChanged.takeFirst();
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), finalSelection);
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), expectedSelection);
-    }
+    verifySelectionChange(spySelectionChanged, finalSelection, expectedSelection);
 
     // Finally, clear the selection
     m_selectionManager->clearSelection();
-    QCOMPARE(m_selectionManager->selectedItems(), QSet<int>());
-    QVERIFY(!m_selectionManager->hasSelection());
-    if (finalSelection.isEmpty()) {
-        // Selection has been empty already
-        QCOMPARE(spySelectionChanged.count(), 0);
+
+    verifySelectionChange(spySelectionChanged, QSet<int>(), finalSelection);
+}
+
+void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy& spy,
+                                                          const QSet<int>& currentSelection,
+                                                          const QSet<int>& 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<QVariant> arguments = spySelectionChanged.takeFirst();
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), QSet<int>());
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), finalSelection);
+        QCOMPARE(spy.count(), 1);
+        QList<QVariant> arguments = spy.takeFirst();
+        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), currentSelection);
+        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), previousSelection);
     }
 }