]> cloud.milkyroute.net Git - dolphin.git/commitdiff
PlacesItemModel: Remove setItemHidden and isItemHidden
authorPeter Penz <peter.penz19@gmail.com>
Sun, 13 May 2012 21:29:28 +0000 (23:29 +0200)
committerPeter Penz <peter.penz19@gmail.com>
Sun, 13 May 2012 21:30:38 +0000 (23:30 +0200)
This should be handled automatically when the hidden-state of
the item itself gets toggled.

src/kitemviews/kstandarditem.cpp
src/kitemviews/kstandarditemmodel.cpp
src/kitemviews/kstandarditemmodel.h
src/panels/places/placesitemmodel.cpp
src/panels/places/placesitemmodel.h
src/panels/places/placespanel.cpp

index 22c3a03e99959e98d3f0160a87d6f0e9e94f43f2..dc03a0d251f888c7b59cae396c77b33e7fa8ebac 100644 (file)
@@ -108,6 +108,7 @@ void KStandardItem::setDataValue(const QByteArray& role, const QVariant& value)
         const int index = m_model->index(this);
         QSet<QByteArray> changedRoles;
         changedRoles.insert(role);
+        m_model->onItemChanged(index, changedRoles);
         emit m_model->itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles);
     }
 }
index ffacd3c596d83a1bf7338e101ce37529e0629913..545a06b5da90081055895546d478b90ada576d1e 100644 (file)
@@ -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<QByteArray> changedRoles;
 
-        KStandardItem* oldItem= m_items[index];
+        KStandardItem* oldItem = m_items[index];
         const QHash<QByteArray, QVariant> oldData = oldItem->data();
         const QHash<QByteArray, QVariant> 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<QByteArray>& changedRoles)
 {
     Q_UNUSED(index);
 }
index 510342a818bccf8de32b27ab908dbe09c991dfc3..be5b7c438575b21b80363a0cc446f6ac69421471 100644 (file)
@@ -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<QByteArray>& changedRoles);
 
     /**
      * Is invoked after an item has been removed and before the signal
index 69236e9203b9a56eb799fad3755dfc536ea6daf4..358652d4d7fd5cf323589ff610add4baff08af5e 100644 (file)
@@ -35,6 +35,7 @@
 #include "placesitem.h"
 #include <QAction>
 #include <QDate>
+#include <QTimer>
 
 #include <Solid/Device>
 #include <Solid/DeviceNotifier>
@@ -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<QByteArray>& 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;
         }
     }
index 14d0accc1a589ada356cea3f2e547fc4cf520b3b..e9604ad22199c278d081488c4f7ef5c752c4d599 100644 (file)
@@ -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<QByteArray>& 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<KUrl, int> m_systemBookmarksIndexes;
 
     QList<PlacesItem*> 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
index 0ddb05bf70bf7ac52099b6483c7bcd8a8e7953e1..bc639c0fb6fbee9434088d850d263657096c52c7 100644 (file)
@@ -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<KUrl>();
@@ -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);
         }
     }