From bd02faa982c263205b5cc21d6ceb4225a83c76b5 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Thu, 17 May 2012 13:32:09 +0200 Subject: [PATCH] KStandardItemModel: Fix inconsistent internal state 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 | 16 +++++++++++++++- src/panels/places/placesitemmodel.cpp | 6 ++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/kitemviews/kstandarditemmodel.cpp b/src/kitemviews/kstandarditemmodel.cpp index e3d40038d..11d72a4ac 100644 --- a/src/kitemviews/kstandarditemmodel.cpp +++ b/src/kitemviews/kstandarditemmodel.cpp @@ -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 } } diff --git a/src/panels/places/placesitemmodel.cpp b/src/panels/places/placesitemmodel.cpp index 21cbf9ed3..14ec54be8 100644 --- a/src/panels/places/placesitemmodel.cpp +++ b/src/panels/places/placesitemmodel.cpp @@ -395,7 +395,7 @@ void PlacesItemModel::onItemChanged(int index, const QSet& 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& 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())); } -- 2.47.3