]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Simplify KItemListSelectionManager
authorFrank Reininghaus <frank78ac@googlemail.com>
Fri, 26 Aug 2011 16:39:32 +0000 (18:39 +0200)
committerFrank Reininghaus <frank78ac@googlemail.com>
Fri, 26 Aug 2011 16:39:32 +0000 (18:39 +0200)
1. The anchorChanged() signal is not needed.
2. The only place where setAnchorItem() is called is in
   beginAnchoredSelection() -> merge both functions.

src/kitemviews/kitemlistselectionmanager.cpp
src/kitemviews/kitemlistselectionmanager.h
src/tests/kitemlistselectionmanagertest.cpp

index 764227004528bd5dbf15941db43e92b83273e941..3c7b94965674ed5d1f392a9cfc1e14ff9bfa3861 100644 (file)
@@ -160,8 +160,7 @@ void KItemListSelectionManager::beginAnchoredSelection(int anchor)
 {
     if (anchor >= 0 && m_model && anchor < m_model->count()) {
         m_isAnchoredSelectionActive = true;
-        setAnchorItem(anchor);
-        Q_ASSERT(m_anchorItem >= 0);
+        m_anchorItem = anchor;
     }
 }
 
@@ -181,24 +180,6 @@ void KItemListSelectionManager::endAnchoredSelection()
     m_isAnchoredSelectionActive = false;
 }
 
-void KItemListSelectionManager::setAnchorItem(int anchor)
-{
-    if (!m_isAnchoredSelectionActive) {
-        return;
-    }
-
-    if (anchor < 0 || (m_model && anchor >= m_model->count())) {
-        // Index is out of range
-        return;
-    }
-
-    if (m_anchorItem != anchor) {
-        const int previous = m_anchorItem;
-        m_anchorItem = anchor;
-        emit anchorChanged(m_anchorItem, previous);
-    }
-}
-
 int KItemListSelectionManager::anchorItem() const
 {
     return m_anchorItem;
@@ -247,9 +228,8 @@ void KItemListSelectionManager::itemsInserted(const KItemRangeList& itemRanges)
 
     // Update the anchor item
     if (m_anchorItem < 0) {
-        setAnchorItem(0);
+        m_anchorItem = 0;
     } else {
-        const int previousAnchor = m_anchorItem;
         int inc = 0;
         foreach (const KItemRange& itemRange, itemRanges) {
             if (m_anchorItem < itemRange.index) {
@@ -258,7 +238,6 @@ void KItemListSelectionManager::itemsInserted(const KItemRangeList& itemRanges)
             inc += itemRange.count;
         }
         m_anchorItem += inc;
-        emit anchorChanged(m_anchorItem, previousAnchor);
     }
 
     // Update the selections
@@ -312,7 +291,6 @@ void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
 
     // Update the anchor item
     if (m_anchorItem >= 0) {
-        const int previousAnchor = m_anchorItem;
         int anchorItem = m_anchorItem;
         foreach (const KItemRange& itemRange, itemRanges) {
             if (anchorItem < itemRange.index) {
@@ -328,8 +306,6 @@ void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
         if (m_anchorItem < 0) {
             m_isAnchoredSelectionActive = false;
         }
-
-        emit anchorChanged(m_anchorItem, previousAnchor);
     }
 
     // Update the selections
index f8c33bb79f1b182678d7bc6caeeb3c03f73998bc..2761940f54b15555f89b4710578b943537a9aa18 100644 (file)
@@ -61,16 +61,7 @@ public:
 
     void beginAnchoredSelection(int anchor);
     void endAnchoredSelection();
-
-    /**
-     * Sets the anchor to \a anchor and emits anchorChanged() if the
-     * anchor differs from the current anchor value. If no anchor selection is active (see
-     * KItemListSelectionManager::beginAnchoredSelection()) or the index
-     * is not within the available model items the anchor will not be modified.
-     */
-    void setAnchorItem(int anchor);
     int anchorItem() const;
-
     bool isAnchoredSelectionActive() const;
 
     KItemModelBase* model() const;
@@ -78,7 +69,6 @@ public:
 signals:
     void currentChanged(int current, int previous);
     void selectionChanged(const QSet<int>& current, const QSet<int>& previous);
-    void anchorChanged(int anchor, int previous);
 
 private:
     void setModel(KItemModelBase* model);
index 0e09a5dde4358cc3e98e7636ac650b71d12e9465..0602d2798771f2ceb87e2b620cbdafa8fcafe6b3 100644 (file)
@@ -96,7 +96,6 @@ void KItemListSelectionManagerTest::testConstructor()
 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
 {
     QSignalSpy spyCurrent(m_selectionManager, SIGNAL(currentChanged(int,int)));
-    QSignalSpy spyAnchor(m_selectionManager, SIGNAL(anchorChanged(int,int)));
 
     // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
     m_selectionManager->setCurrentItem(4);
@@ -105,20 +104,10 @@ void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
     QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 4);
     spyCurrent.takeFirst();
 
-    // Set anchor item and check that the selection manager emits the anchorChanged(int,int) signal correctly.
-    m_selectionManager->beginAnchoredSelection(3);
+    // Begin an anchored selection.
+    m_selectionManager->beginAnchoredSelection(5);
     QVERIFY(m_selectionManager->isAnchoredSelectionActive());
-    QCOMPARE(m_selectionManager->anchorItem(), 3);
-    QCOMPARE(spyAnchor.count(), 1);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 3);
-    spyAnchor.takeFirst();
-
-    m_selectionManager->setAnchorItem(5);
     QCOMPARE(m_selectionManager->anchorItem(), 5);
-    QCOMPARE(spyAnchor.count(), 1);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 5);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 3);
-    spyAnchor.takeFirst();
 
     // Items between current and anchor should be selected now
     QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
@@ -148,10 +137,6 @@ void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
     spyCurrent.takeFirst();
 
     QCOMPARE(m_selectionManager->anchorItem(), 8);
-    QCOMPARE(spyAnchor.count(), 1);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 8);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 5);
-    spyAnchor.takeFirst();
 
     QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8);
     QVERIFY(m_selectionManager->hasSelection());
@@ -169,10 +154,6 @@ void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
     spyCurrent.takeFirst();
 
     QCOMPARE(m_selectionManager->anchorItem(), 5);
-    QCOMPARE(spyAnchor.count(), 1);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 5);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 8);
-    spyAnchor.takeFirst();
 
     QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
     QVERIFY(m_selectionManager->hasSelection());