]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemlistselectionmanager.cpp
Remove unneded function KItemListSelectionManager::anchorItem()
[dolphin.git] / src / kitemviews / kitemlistselectionmanager.cpp
index ee8ba929eec30e3bd62b76924ece7e0b317f64ef..63306a3f26440093479790aad3a43e8f81d9dfef 100644 (file)
@@ -1,5 +1,6 @@
 /***************************************************************************
  *   Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com>             *
+ *   Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com>    *
  *                                                                         *
  *   Based on the Itemviews NG project from Trolltech Labs:                *
  *   http://qt.gitorious.org/qt-labs/itemviews-ng                          *
@@ -30,6 +31,7 @@ KItemListSelectionManager::KItemListSelectionManager(QObject* parent) :
     m_currentItem(-1),
     m_anchorItem(-1),
     m_selectedItems(),
+    m_isAnchoredSelectionActive(false),
     m_model(0)
 {
 }
@@ -41,6 +43,8 @@ KItemListSelectionManager::~KItemListSelectionManager()
 void KItemListSelectionManager::setCurrentItem(int current)
 {
     const int previous = m_currentItem;
+    const QSet<int> previousSelection = selectedItems();
+
     if (m_model && current >= 0 && current < m_model->count()) {
         m_currentItem = current;
     } else {
@@ -49,6 +53,13 @@ void KItemListSelectionManager::setCurrentItem(int current)
 
     if (m_currentItem != previous) {
         emit currentChanged(m_currentItem, previous);
+
+        if (m_isAnchoredSelectionActive) {
+            const QSet<int> selection = selectedItems();
+            if (selection != previousSelection) {
+                emit selectionChanged(selection, previousSelection);
+            }
+        }
     }
 }
 
@@ -68,12 +79,25 @@ void KItemListSelectionManager::setSelectedItems(const QSet<int>& items)
 
 QSet<int> KItemListSelectionManager::selectedItems() const
 {
-    return m_selectedItems;
+    QSet<int> selectedItems = m_selectedItems;
+
+    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);
+
+        for (int index = from; index <= to; ++index) {
+            selectedItems.insert(index);
+        }
+    }
+
+    return selectedItems;
 }
 
 bool KItemListSelectionManager::hasSelection() const
 {
-    return !m_selectedItems.isEmpty();
+    return !m_selectedItems.isEmpty() || (m_isAnchoredSelectionActive && m_anchorItem != m_currentItem);
 }
 
 void KItemListSelectionManager::setSelected(int index, int count, SelectionMode mode)
@@ -82,7 +106,8 @@ void KItemListSelectionManager::setSelected(int index, int count, SelectionMode
         return;
     }
 
-    const QSet<int> previous = m_selectedItems;
+    endAnchoredSelection();
+    const QSet<int> previous = selectedItems();
 
     count = qMin(count, m_model->count() - index);
 
@@ -115,47 +140,49 @@ void KItemListSelectionManager::setSelected(int index, int count, SelectionMode
         break;
     }
 
-    if (m_selectedItems != previous) {
-        emit selectionChanged(m_selectedItems, previous);
+    const QSet<int> selection = selectedItems();
+    if (selection != previous) {
+        emit selectionChanged(selection, previous);
     }
 }
 
 void KItemListSelectionManager::clearSelection()
 {
-    if (!m_selectedItems.isEmpty()) {
-        const QSet<int> previous = m_selectedItems;
+    const QSet<int> previous = selectedItems();
+    if (!previous.isEmpty()) {
         m_selectedItems.clear();
-        emit selectionChanged(m_selectedItems, previous);
+        m_isAnchoredSelectionActive = false;
+        emit selectionChanged(QSet<int>(), previous);
     }
 }
 
-void KItemListSelectionManager::beginAnchoredSelection(int anchor, SelectionMode mode)
+void KItemListSelectionManager::beginAnchoredSelection(int anchor)
 {
-    Q_UNUSED(anchor);
-    Q_UNUSED(mode);
+    if (anchor >= 0 && m_model && anchor < m_model->count()) {
+        m_isAnchoredSelectionActive = true;
+        m_anchorItem = anchor;
+    }
 }
 
 void KItemListSelectionManager::endAnchoredSelection()
 {
-}
-
-void KItemListSelectionManager::setAnchorItem(int anchor)
-{
-    const int previous = m_anchorItem;
-    if (m_model && anchor < m_model->count()) {
-        m_anchorItem = anchor;
-    } else {
-        m_anchorItem = -1;
+    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);
+
+        for (int index = from; index <= to; ++index) {
+            m_selectedItems.insert(index);
+        }
     }
 
-    if (m_anchorItem != previous) {
-        emit anchorChanged(m_anchorItem, previous);
-    }
+    m_isAnchoredSelectionActive = false;
 }
 
-int KItemListSelectionManager::anchorItem() const
+bool KItemListSelectionManager::isAnchoredSelectionActive() const
 {
-    return m_anchorItem;
+    return m_isAnchoredSelectionActive;
 }
 
 KItemModelBase* KItemListSelectionManager::model() const
@@ -173,10 +200,14 @@ void KItemListSelectionManager::setModel(KItemModelBase* model)
 
 void KItemListSelectionManager::itemsInserted(const KItemRangeList& itemRanges)
 {
+    // Store the current selection (needed in the selectionChanged() signal)
+    const QSet<int> previousSelection = selectedItems();
+
     // Update the current item
     if (m_currentItem < 0) {
         setCurrentItem(0);
     } else {
+        const int previousCurrent = m_currentItem;
         int inc = 0;
         foreach (const KItemRange& itemRange, itemRanges) {
             if (m_currentItem < itemRange.index) {
@@ -184,16 +215,31 @@ void KItemListSelectionManager::itemsInserted(const KItemRangeList& itemRanges)
             }
             inc += itemRange.count;
         }
-        setCurrentItem(m_currentItem + inc);
+        // 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 += inc;
+        emit currentChanged(m_currentItem, previousCurrent);
+    }
+
+    // Update the anchor item
+    if (m_anchorItem < 0) {
+        m_anchorItem = 0;
+    } else {
+        int inc = 0;
+        foreach (const KItemRange& itemRange, itemRanges) {
+            if (m_anchorItem < itemRange.index) {
+                break;
+            }
+            inc += itemRange.count;
+        }
+        m_anchorItem += inc;
     }
 
     // Update the selections
     if (!m_selectedItems.isEmpty()) {
         const QSet<int> previous = m_selectedItems;
-
-        QSet<int> current;
-        current.reserve(m_selectedItems.count());
-        QSetIterator<int> it(m_selectedItems);
+        m_selectedItems.clear();
+        QSetIterator<int> it(previous);
         while (it.hasNext()) {
             const int index = it.next();
             int inc = 0;
@@ -203,20 +249,24 @@ void KItemListSelectionManager::itemsInserted(const KItemRangeList& itemRanges)
                 }
                 inc += itemRange.count;
             }
-            current.insert(index + inc);
+            m_selectedItems.insert(index + inc);
         }
+    }
 
-        if (current != previous) {
-            m_selectedItems = current;
-            emit selectionChanged(current, previous);
-        }
+    const QSet<int> selection = selectedItems();
+    if (selection != previousSelection) {
+        emit selectionChanged(selection, previousSelection);
     }
 }
 
 void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
 {
+    // Store the current selection (needed in the selectionChanged() signal)
+    const QSet<int> previousSelection = selectedItems();
+
     // 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) {
@@ -228,16 +278,36 @@ void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
                 currentItem = m_model->count() - 1;
             }
         }
-        setCurrentItem(currentItem);
+        // 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;
+        emit currentChanged(m_currentItem, previousCurrent);
+    }
+
+    // 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;
+        if (m_anchorItem < 0) {
+            m_isAnchoredSelectionActive = false;
+        }
     }
 
     // Update the selections
     if (!m_selectedItems.isEmpty()) {
         const QSet<int> previous = m_selectedItems;
-
-        QSet<int> current;
-        current.reserve(m_selectedItems.count());
-        QSetIterator<int> it(m_selectedItems);
+        m_selectedItems.clear();
+        QSetIterator<int> it(previous);
         while (it.hasNext()) {
             int index = it.next();
             int dec = 0;
@@ -257,14 +327,14 @@ void KItemListSelectionManager::itemsRemoved(const KItemRangeList& itemRanges)
             }
             index -= dec;
             if (index >= 0)  {
-                current.insert(index);
+                m_selectedItems.insert(index);
             }
         }
+    }
 
-        if (current != previous) {
-            m_selectedItems = current;
-            emit selectionChanged(current, previous);
-        }
+    const QSet<int> selection = selectedItems();
+    if (selection != previousSelection) {
+        emit selectionChanged(selection, previousSelection);
     }
 }