]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Update the anchor item when items are added or removed
authorFrank Reininghaus <frank78ac@googlemail.com>
Wed, 10 Aug 2011 08:38:24 +0000 (10:38 +0200)
committerFrank Reininghaus <frank78ac@googlemail.com>
Wed, 10 Aug 2011 08:38:24 +0000 (10:38 +0200)
If items are added or removed in the model, not only the
current item, but also the anchor item, which is the
starting point for any selections via Shift+Click or
Shift+Key, needs to be updated.
BUG: 262638
FIXED-IN: 4.8.0

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

index ee8ba929eec30e3bd62b76924ece7e0b317f64ef..cdc6dbc7dc26f75f02146485bf71c6028775674f 100644 (file)
@@ -187,6 +187,20 @@ void KItemListSelectionManager::itemsInserted(const KItemRangeList& itemRanges)
         setCurrentItem(m_currentItem + inc);
     }
 
+    // Update the anchor item
+    if (m_anchorItem < 0) {
+        setAnchorItem(0);
+    } else {
+        int inc = 0;
+        foreach (const KItemRange& itemRange, itemRanges) {
+            if (m_anchorItem < itemRange.index) {
+                break;
+            }
+            inc += itemRange.count;
+        }
+        setAnchorItem(m_anchorItem + inc);
+    }
+
     // Update the selections
     if (!m_selectedItems.isEmpty()) {
         const QSet<int> previous = m_selectedItems;
@@ -231,6 +245,22 @@ void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
         setCurrentItem(currentItem);
     }
 
+    // Update the anchor item
+    if (m_anchorItem >= 0) {
+        int anchorItem = m_anchorItem;
+        foreach (const KItemRange& itemRange, itemRanges) {
+            if (anchorItem < itemRange.index) {
+                break;
+            }
+            if (anchorItem >= itemRange.index + itemRange.count) {
+                anchorItem -= itemRange.count;
+            } else if (anchorItem >= m_model->count()) {
+                anchorItem = m_model->count() - 1;
+            }
+        }
+        setAnchorItem(anchorItem);
+    }
+
     // Update the selections
     if (!m_selectedItems.isEmpty()) {
         const QSet<int> previous = m_selectedItems;
index 3d0c318ffc9467ec59d20fecfe53746b8c63000a..6b0059a5c7a29d33caaa644f24834feb4f6410d6 100644 (file)
@@ -121,6 +121,42 @@ void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
     QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 5);
     QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 3);
     spyAnchor.takeFirst();
+
+    // Inserting items should update current item and anchor item.
+    m_selectionManager->itemsInserted(KItemRangeList() <<
+                                      KItemRange(0, 1) <<
+                                      KItemRange(2, 2) <<
+                                      KItemRange(6, 3));
+
+    QCOMPARE(m_selectionManager->currentItem(), 5);
+    QCOMPARE(spyCurrent.count(), 1);
+    QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 5);
+    QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 2);
+    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();
+
+    // Removing items should update current item and anchor item.
+    m_selectionManager->itemsRemoved(KItemRangeList() <<
+                                     KItemRange(0, 2) <<
+                                     KItemRange(2, 1) <<
+                                     KItemRange(9, 2));
+
+    QCOMPARE(m_selectionManager->currentItem(), 2);
+    QCOMPARE(spyCurrent.count(), 1);
+    QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 2);
+    QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 5);
+    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();
 }
 
 void KItemListSelectionManagerTest::testSetSelected_data()