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);
}
}
}
}
-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();
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;
Q_UNUSED(index);
}
-void KStandardItemModel::onItemReplaced(int index)
+void KStandardItemModel::onItemChanged(int index, const QSet<QByteArray>& changedRoles)
{
Q_UNUSED(index);
}
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;
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
#include "placesitem.h"
#include <QAction>
#include <QDate>
+#include <QTimer>
#include <Solid/Device>
#include <Solid/DeviceNotifier>
m_bookmarkManager(0),
m_systemBookmarks(),
m_systemBookmarksIndexes(),
- m_hiddenItems()
+ m_hiddenItems(),
+ m_hiddenItemToRemove(-1)
{
#ifdef HAVE_NEPOMUK
m_nepomukRunning = (Nepomuk::ResourceManager::instance()->initialized());
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) {
} 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()) {
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;
}
}
#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)
}
}
+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();
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;
}
}
class PlacesItem;
class QAction;
-#define PLACESITEMMODEL_DEBUG
+// #define PLACESITEMMODEL_DEBUG
/**
* @brief Model for maintaining the bookmarks of the places panel.
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
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();
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
void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
{
- const PlacesItem* item = m_model->placesItem(index);
+ PlacesItem* item = m_model->placesItem(index);
if (!item) {
return;
}
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>();
// 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);
}
}