From: Peter Penz Date: Sun, 13 May 2012 21:29:28 +0000 (+0200) Subject: PlacesItemModel: Remove setItemHidden and isItemHidden X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/6e9713c558ad07b0baa2a79ae69328fda7b61a15 PlacesItemModel: Remove setItemHidden and isItemHidden This should be handled automatically when the hidden-state of the item itself gets toggled. --- diff --git a/src/kitemviews/kstandarditem.cpp b/src/kitemviews/kstandarditem.cpp index 22c3a03e9..dc03a0d25 100644 --- a/src/kitemviews/kstandarditem.cpp +++ b/src/kitemviews/kstandarditem.cpp @@ -108,6 +108,7 @@ void KStandardItem::setDataValue(const QByteArray& role, const QVariant& value) const int index = m_model->index(this); QSet changedRoles; changedRoles.insert(role); + m_model->onItemChanged(index, changedRoles); emit m_model->itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles); } } diff --git a/src/kitemviews/kstandarditemmodel.cpp b/src/kitemviews/kstandarditemmodel.cpp index ffacd3c59..545a06b5d 100644 --- a/src/kitemviews/kstandarditemmodel.cpp +++ b/src/kitemviews/kstandarditemmodel.cpp @@ -49,14 +49,14 @@ 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 = this; QSet changedRoles; - KStandardItem* oldItem= m_items[index]; + KStandardItem* oldItem = m_items[index]; const QHash oldData = oldItem->data(); const QHash newData = item->data(); @@ -78,7 +78,7 @@ void KStandardItemModel::replaceItem(int index, KStandardItem* item) m_items[index] = item; m_indexesForItems.insert(item, index); - onItemReplaced(index); + onItemChanged(index, changedRoles); emit itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles); } else { kWarning() << "No item available to replace on the given index" << index; @@ -196,7 +196,7 @@ void KStandardItemModel::onItemInserted(int index) Q_UNUSED(index); } -void KStandardItemModel::onItemReplaced(int index) +void KStandardItemModel::onItemChanged(int index, const QSet& changedRoles) { Q_UNUSED(index); } diff --git a/src/kitemviews/kstandarditemmodel.h b/src/kitemviews/kstandarditemmodel.h index 510342a81..be5b7c438 100644 --- a/src/kitemviews/kstandarditemmodel.h +++ b/src/kitemviews/kstandarditemmodel.h @@ -52,11 +52,11 @@ public: void insertItem(int index, KStandardItem* item); /** - * Replaces the item on the index \a index by \a item. + * Changes the item on the index \a index to \a item. * KStandardItemModel takes the ownership of the item. The * old item gets deleted. */ - void replaceItem(int index, KStandardItem* item); + void changeItem(int index, KStandardItem* item); void removeItem(int index); KStandardItem* item(int index) const; @@ -84,10 +84,10 @@ protected: virtual void onItemInserted(int index); /** - * Is invoked after an item has been replaced and before the signal - * itemsChanged() gets emitted. + * Is invoked after an item or one of its roles has been changed and + * before the signal itemsChanged() gets emitted. */ - virtual void onItemReplaced(int index); + virtual void onItemChanged(int index, const QSet& changedRoles); /** * Is invoked after an item has been removed and before the signal diff --git a/src/panels/places/placesitemmodel.cpp b/src/panels/places/placesitemmodel.cpp index 69236e920..358652d4d 100644 --- a/src/panels/places/placesitemmodel.cpp +++ b/src/panels/places/placesitemmodel.cpp @@ -35,6 +35,7 @@ #include "placesitem.h" #include #include +#include #include #include @@ -56,7 +57,8 @@ PlacesItemModel::PlacesItemModel(QObject* parent) : m_bookmarkManager(0), m_systemBookmarks(), m_systemBookmarksIndexes(), - m_hiddenItems() + m_hiddenItems(), + m_hiddenItemToRemove(-1) { #ifdef HAVE_NEPOMUK m_nepomukRunning = (Nepomuk::ResourceManager::instance()->initialized()); @@ -98,29 +100,6 @@ int PlacesItemModel::hiddenCount() const return itemCount; } -void PlacesItemModel::setItemHidden(int index, bool hide) -{ - if (index >= 0 && index < count()) { - PlacesItem* shownItem = placesItem(index); - shownItem->setHidden(true); - if (!m_hiddenItemsShown && hide) { - const int newIndex = hiddenIndex(index); - PlacesItem* hiddenItem = new PlacesItem(*shownItem); - removeItem(index); - m_hiddenItems.insert(newIndex, hiddenItem); - } -#ifdef PLACESITEMMODEL_DEBUG - kDebug() << "Changed hide-state from" << index << "to" << hide; - showModelState(); -#endif - } -} - -bool PlacesItemModel::isItemHidden(int index) const -{ - return (index >= 0 && index < count()) ? m_hiddenItems[index] != 0 : false; -} - void PlacesItemModel::setHiddenItemsShown(bool show) { if (m_hiddenItemsShown == show) { @@ -145,7 +124,7 @@ void PlacesItemModel::setHiddenItemsShown(bool show) } else { // Move all items of the model, where the "isHidden" property is true, to // m_hiddenItems. - Q_ASSERT(m_hiddenItems.count() == count()); + //Q_ASSERT(m_hiddenItems.count() == count()); for (int i = count() - 1; i >= 0; --i) { PlacesItem* visibleItem = placesItem(i); if (visibleItem->isHidden()) { @@ -305,13 +284,12 @@ void PlacesItemModel::onItemInserted(int index) return; } - int modelIndex = 0; + int modelIndex = -1; int hiddenIndex = 0; while (hiddenIndex < m_hiddenItems.count()) { if (!m_hiddenItems[hiddenIndex]) { ++modelIndex; if (modelIndex + 1 == index) { - ++hiddenIndex; break; } } @@ -336,9 +314,18 @@ void PlacesItemModel::onItemRemoved(int index) #endif } -void PlacesItemModel::onItemReplaced(int index) +void PlacesItemModel::onItemChanged(int index, const QSet& changedRoles) { - Q_UNUSED(index); + if (changedRoles.contains("isHidden")) { + const PlacesItem* shownItem = placesItem(index); + Q_ASSERT(shownItem); + const bool hide = shownItem->isHidden(); + + if (!m_hiddenItemsShown && hide) { + m_hiddenItemToRemove = index; + QTimer::singleShot(0, this, SLOT(removeHiddenItem())); + } + } } void PlacesItemModel::slotDeviceAdded(const QString& udi) @@ -381,6 +368,18 @@ void PlacesItemModel::slotStorageTeardownDone(Solid::ErrorType error, const QVar } } +void PlacesItemModel::removeHiddenItem() +{ + const PlacesItem* shownItem = placesItem(m_hiddenItemToRemove); + const int newIndex = hiddenIndex(m_hiddenItemToRemove); + if (shownItem && newIndex >= 0) { + PlacesItem* hiddenItem = new PlacesItem(*shownItem); + removeItem(m_hiddenItemToRemove); + m_hiddenItems.insert(newIndex, hiddenItem); + } + m_hiddenItemToRemove = -1; +} + void PlacesItemModel::loadBookmarks() { KBookmarkGroup root = m_bookmarkManager->root(); @@ -614,7 +613,11 @@ void PlacesItemModel::showModelState() if (m_hiddenItems[i]) { kDebug() << i << "(Hidden) " << " " << m_hiddenItems[i]->dataValue("text").toString(); } else { - kDebug() << i << " " << j << " " << item(j)->dataValue("text").toString(); + if (item(j)) { + kDebug() << i << " " << j << " " << item(j)->dataValue("text").toString(); + } else { + kDebug() << i << " " << j << " " << "(not available yet)"; + } ++j; } } diff --git a/src/panels/places/placesitemmodel.h b/src/panels/places/placesitemmodel.h index 14d0accc1..e9604ad22 100644 --- a/src/panels/places/placesitemmodel.h +++ b/src/panels/places/placesitemmodel.h @@ -35,7 +35,7 @@ class KBookmarkManager; class PlacesItem; class QAction; -#define PLACESITEMMODEL_DEBUG +// #define PLACESITEMMODEL_DEBUG /** * @brief Model for maintaining the bookmarks of the places panel. @@ -58,9 +58,6 @@ public: int hiddenCount() const; - void setItemHidden(int index, bool hide); - bool isItemHidden(int index) const; - /** * Search the item which is equal to the URL or at least * is a parent URL. If there are more than one possible @@ -90,12 +87,13 @@ signals: protected: virtual void onItemInserted(int index); virtual void onItemRemoved(int index); - virtual void onItemReplaced(int index); + virtual void onItemChanged(int index, const QSet& changedRoles); private slots: void slotDeviceAdded(const QString& udi); void slotDeviceRemoved(const QString& udi); void slotStorageTeardownDone(Solid::ErrorType error, const QVariant& errorData); + void removeHiddenItem(); private: void loadBookmarks(); @@ -155,6 +153,12 @@ private: QHash m_systemBookmarksIndexes; QList m_hiddenItems; + + // Index of the hidden item that should be removed in + // removeHiddenItem(). The removing must be done + // asynchronously as in the scope of onItemChanged() + // removing an item is not allowed. + int m_hiddenItemToRemove; }; #endif diff --git a/src/panels/places/placespanel.cpp b/src/panels/places/placespanel.cpp index 0ddb05bf7..bc639c0fb 100644 --- a/src/panels/places/placespanel.cpp +++ b/src/panels/places/placespanel.cpp @@ -131,7 +131,7 @@ void PlacesPanel::slotItemMiddleClicked(int index) void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos) { - const PlacesItem* item = m_model->placesItem(index); + PlacesItem* item = m_model->placesItem(index); if (!item) { return; } @@ -219,7 +219,7 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos) m_model->removeItem(index); m_model->save(); } else if (action == hideAction) { - m_model->setItemHidden(index, hideAction->isChecked()); + item->setHidden(hideAction->isChecked()); m_model->save(); } else if (action == openInNewTabAction) { const KUrl url = m_model->item(index)->dataValue("url").value(); @@ -359,7 +359,7 @@ void PlacesPanel::editEntry(int index) // that another group should be assigned, we still apply the old // group to keep the same position for the item. item->setGroup(oldItem->group()); - m_model->replaceItem(index, item); + m_model->changeItem(index, item); } }