]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistselectionmanager.cpp
Merge remote-tracking branch 'origin/KDE/4.10'
[dolphin.git] / src / kitemviews / kitemlistselectionmanager.cpp
index 131ee46e6278a4d7598d5bfbfd9bd75f82bdd4eb..833f7aad07f6d0e985f6fe6dfa59f5a5c8c0daf9 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);
@@ -264,70 +284,36 @@ void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
 {
     // Store the current selection (needed in the selectionChanged() signal)
     const QSet<int> previousSelection = selectedItems();
+    const int previousCurrent = m_currentItem;
 
     // Update the current item
-    if (m_currentItem >= 0) {
-        const int previousCurrent = m_currentItem;
-        int currentItem = m_currentItem;
-        foreach (const KItemRange& itemRange, itemRanges) {
-            if (currentItem < itemRange.index) {
-                break;
-            }
-            if (currentItem >= itemRange.index + itemRange.count) {
-                currentItem -= itemRange.count;
-            } else if (currentItem >= m_model->count()) {
-                currentItem = m_model->count() - 1;
-            }
-        }
-        // Calling setCurrentItem would trigger the selectionChanged signal, but we want to
-        // emit it only once in this function -> change the current item manually and emit currentChanged
-        m_currentItem = currentItem;
+    m_currentItem = indexAfterRangesRemoving(m_currentItem, itemRanges, DiscardRemovedIndex);
+    if (m_currentItem != previousCurrent) {
         emit currentChanged(m_currentItem, previousCurrent);
+        if (m_currentItem < 0) {
+            // Calling setCurrentItem() would trigger the selectionChanged signal, but we want to
+            // emit it only once in this function -> change the current item manually and emit currentChanged
+            m_currentItem = indexAfterRangesRemoving(previousCurrent, itemRanges, AdjustRemovedIndex);
+            emit currentChanged(m_currentItem, -1);
+        }
     }
 
     // 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;
-            }
-        }
-        m_anchorItem = anchorItem;
+        m_anchorItem = indexAfterRangesRemoving(m_anchorItem, itemRanges, DiscardRemovedIndex);
         if (m_anchorItem < 0) {
             m_isAnchoredSelectionActive = false;
         }
     }
 
-    // Update the selections
+    // Update the selections and the anchor item
     if (!m_selectedItems.isEmpty()) {
         const QSet<int> previous = m_selectedItems;
         m_selectedItems.clear();
         m_selectedItems.reserve(previous.count());
         QSetIterator<int> it(previous);
         while (it.hasNext()) {
-            int index = it.next();
-            int dec = 0;
-            foreach (const KItemRange& itemRange, itemRanges) {
-                if (index < itemRange.index) {
-                    break;
-                }
-
-                if (index < itemRange.index + itemRange.count) {
-                    // The selection is part of the removed range
-                    // and will get deleted
-                    index = -1;
-                    break;
-                }
-
-                dec += itemRange.count;
-            }
-            index -= dec;
+            const int index = indexAfterRangesRemoving(it.next(), itemRanges, DiscardRemovedIndex);
             if (index >= 0)  {
                 m_selectedItems.insert(index);
             }
@@ -338,6 +324,9 @@ void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
     if (selection != previousSelection) {
         emit selectionChanged(selection, previousSelection);
     }
+
+    Q_ASSERT(m_currentItem < m_model->count());
+    Q_ASSERT(m_anchorItem < m_model->count());
 }
 
 void KItemListSelectionManager::itemsMoved(const KItemRange& itemRange, const QList<int>& movedToIndexes)
@@ -384,4 +373,29 @@ void KItemListSelectionManager::itemsMoved(const KItemRange& itemRange, const QL
     }
 }
 
+int KItemListSelectionManager::indexAfterRangesRemoving(int index, const KItemRangeList& itemRanges,
+                                                        const RangesRemovingBehaviour behaviour) const
+{
+    int dec = 0;
+    foreach (const KItemRange& itemRange, itemRanges) {
+        if (index < itemRange.index) {
+            break;
+        }
+
+        dec += itemRange.count;
+
+        const int firstIndexAfterRange = itemRange.index + itemRange.count;
+        if (index < firstIndexAfterRange) {
+            // The index is part of the removed range
+            if (behaviour == DiscardRemovedIndex) {
+                return -1;
+            } else {
+                // Use the first item after the range as new index
+                index = firstIndexAfterRange;
+                break;
+            }
+        }
+    }
+    return qBound(-1, index - dec, m_model->count() - 1);
+}
 #include "kitemlistselectionmanager.moc"