]> cloud.milkyroute.net Git - dolphin.git/commitdiff
KStandardItemModel: Fix inconsistent internal state
authorPeter Penz <peter.penz19@gmail.com>
Thu, 17 May 2012 11:32:09 +0000 (13:32 +0200)
committerPeter Penz <peter.penz19@gmail.com>
Thu, 17 May 2012 11:34:31 +0000 (13:34 +0200)
Assure that the index-cache is kept consistent with the items when
a removing or inserting is done. A unit-test will be created as soon
as possible.

src/kitemviews/kstandarditemmodel.cpp
src/panels/places/placesitemmodel.cpp

index e3d40038d9f443f8861c2e34791f978287dc9c1f..11d72a4ac443d64e4821d31e66602fa604fee252 100644 (file)
@@ -42,6 +42,13 @@ void KStandardItemModel::insertItem(int index, KStandardItem* item)
         item->m_model = this;
         m_items.insert(index, item);
         m_indexesForItems.insert(item, index);
+
+        // Inserting an item requires to update the indexes
+        // afterwards from m_indexesForItems.
+        for (int i = index + 1; i < m_items.count(); ++i) {
+            m_indexesForItems.insert(m_items[i], i);
+        }
+
         // TODO: no hierarchical items are handled yet
 
         onItemInserted(index);
@@ -94,12 +101,19 @@ void KStandardItemModel::removeItem(int index)
         m_indexesForItems.remove(item);
         m_items.removeAt(index);
 
+        // Removing an item requires to update the indexes
+        // afterwards from m_indexesForItems.
+        for (int i = index; i < m_items.count(); ++i) {
+            m_indexesForItems.insert(m_items[i], i);
+        }
+
         onItemRemoved(index, item);
-        emit itemsRemoved(KItemRangeList() << KItemRange(index, 1));
 
         delete item;
         item = 0;
 
+        emit itemsRemoved(KItemRangeList() << KItemRange(index, 1));
+
         // TODO: no hierarchical items are handled yet
     }
 }
index 21cbf9ed30325054a5b235f97dd57d53d2978920..14ec54be81f495ab80ba4974fbd2b4bc1c014215 100644 (file)
@@ -395,7 +395,7 @@ void PlacesItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRo
 {
     const PlacesItem* changedItem = placesItem(index);
     if (changedItem) {
-        // Take care to apply the PlacesItemModel-order of the inserted item
+        // Take care to apply the PlacesItemModel-order of the changed item
         // also to the bookmark-manager.
         const KBookmark insertedBookmark = changedItem->bookmark();
 
@@ -409,9 +409,7 @@ void PlacesItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRo
     }
 
     if (changedRoles.contains("isHidden")) {
-        const PlacesItem* shownItem = placesItem(index);
-        Q_ASSERT(shownItem);
-        if (!m_hiddenItemsShown && shownItem->isHidden()) {
+        if (!m_hiddenItemsShown && changedItem->isHidden()) {
             m_hiddenItemToRemove = index;
             QTimer::singleShot(0, this, SLOT(hideItem()));
         }