X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/d76b113ad10fe207ef23d5dd44c63ee076c71521..894232ebda5b2cf155a4f4e5bf1287eb700faa18:/src/kitemviews/kstandarditemmodel.cpp diff --git a/src/kitemviews/kstandarditemmodel.cpp b/src/kitemviews/kstandarditemmodel.cpp index d0be1325f..dbf608c92 100644 --- a/src/kitemviews/kstandarditemmodel.cpp +++ b/src/kitemviews/kstandarditemmodel.cpp @@ -38,10 +38,22 @@ KStandardItemModel::~KStandardItemModel() void KStandardItemModel::insertItem(int index, KStandardItem* item) { - if (item && !m_indexesForItems.contains(item) && !item->m_model) { + if (index < 0 || index > count() || !item) { + delete item; + return; + } + + if (!m_indexesForItems.contains(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); @@ -49,42 +61,41 @@ void KStandardItemModel::insertItem(int index, KStandardItem* item) } } -void KStandardItemModel::replaceItem(int index, KStandardItem* item) +void KStandardItemModel::changeItem(int index, KStandardItem* item) { - if (item && index >= 0 && index < count() && !item->m_model) { - item->m_model = this; + if (index < 0 || index >= count() || !item) { + delete item; + return; + } - QSet changedRoles; - - KStandardItem* oldItem= m_items[index]; - const QHash oldData = oldItem->data(); - const QHash newData = item->data(); - - // Determine which roles have been changed - QHashIterator it(oldData); - while (it.hasNext()) { - it.next(); - const QByteArray role = it.key(); - const QVariant oldValue = it.value(); - if (newData.contains(role) && newData.value(role) != oldValue) { - changedRoles.insert(role); - } - } + item->m_model = this; - m_indexesForItems.remove(oldItem); - delete oldItem; - oldItem = 0; + QSet changedRoles; - m_items[index] = item; - m_indexesForItems.insert(item, index); + KStandardItem* oldItem = m_items[index]; + const QHash oldData = oldItem->data(); + const QHash newData = item->data(); - onItemReplaced(index); - emit itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles); - } else { - kWarning() << "No item available to replace on the given index" << index; - delete item; - item = 0; + // Determine which roles have been changed + QHashIterator it(oldData); + while (it.hasNext()) { + it.next(); + const QByteArray role = it.key(); + const QVariant oldValue = it.value(); + if (newData.contains(role) && newData.value(role) != oldValue) { + changedRoles.insert(role); + } } + + m_indexesForItems.remove(oldItem); + delete oldItem; + oldItem = 0; + + m_items[index] = item; + m_indexesForItems.insert(item, index); + + onItemChanged(index, changedRoles); + emit itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles); } void KStandardItemModel::removeItem(int index) @@ -93,11 +104,20 @@ void KStandardItemModel::removeItem(int index) KStandardItem* item = m_items[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); + delete item; item = 0; - onItemRemoved(index); emit itemsRemoved(KItemRangeList() << KItemRange(index, 1)); + // TODO: no hierarchical items are handled yet } } @@ -175,7 +195,7 @@ QList > KStandardItemModel::groups() const { QList > groups; - const QByteArray role = sortRole(); + const QByteArray role = sortRole().isEmpty() ? "group" : sortRole(); bool isFirstGroupValue = true; QString groupValue; const int maxIndex = count() - 1; @@ -196,14 +216,16 @@ void KStandardItemModel::onItemInserted(int index) Q_UNUSED(index); } -void KStandardItemModel::onItemReplaced(int index) +void KStandardItemModel::onItemChanged(int index, const QSet& changedRoles) { Q_UNUSED(index); + Q_UNUSED(changedRoles); } -void KStandardItemModel::onItemRemoved(int index) +void KStandardItemModel::onItemRemoved(int index, KStandardItem* removedItem) { Q_UNUSED(index); + Q_UNUSED(removedItem); }