From 13a6573ecb0d2fc8109e53375842d6c5952f1afc Mon Sep 17 00:00:00 2001 From: David Rosca Date: Wed, 28 Dec 2016 20:26:03 +0100 Subject: [PATCH] BookmarkItem: Revert to caching icon for 20 seconds --- src/lib/bookmarks/bookmarkitem.cpp | 10 +++++++--- src/lib/bookmarks/bookmarkitem.h | 2 +- src/lib/bookmarks/bookmarksmodel.cpp | 19 +------------------ src/lib/bookmarks/bookmarksmodel.h | 1 - src/lib/bookmarks/bookmarkstreeview.cpp | 13 ------------- src/lib/bookmarks/bookmarkstreeview.h | 2 -- 6 files changed, 9 insertions(+), 38 deletions(-) diff --git a/src/lib/bookmarks/bookmarkitem.cpp b/src/lib/bookmarks/bookmarkitem.cpp index 283346f15..4c673d2e4 100644 --- a/src/lib/bookmarks/bookmarkitem.cpp +++ b/src/lib/bookmarks/bookmarkitem.cpp @@ -70,12 +70,16 @@ QList BookmarkItem::children() const return m_children; } -QIcon BookmarkItem::icon(bool load) +QIcon BookmarkItem::icon() { + // Cache icon for 20 seconds + const int iconCacheTime = 20 * 1000; + switch (m_type) { case Url: - if (load && m_icon.isNull()) { - setIcon(IconProvider::iconForUrl(m_url)); + if (m_iconTime.isNull() || m_iconTime.elapsed() > iconCacheTime) { + m_icon = IconProvider::iconForUrl(m_url); + m_iconTime.restart(); } return m_icon; case Folder: diff --git a/src/lib/bookmarks/bookmarkitem.h b/src/lib/bookmarks/bookmarkitem.h index 71707fb3e..25af10da5 100644 --- a/src/lib/bookmarks/bookmarkitem.h +++ b/src/lib/bookmarks/bookmarkitem.h @@ -50,7 +50,7 @@ public: BookmarkItem* parent() const; QList children() const; - QIcon icon(bool load = true); + QIcon icon(); void setIcon(const QIcon &icon); QString urlString() const; diff --git a/src/lib/bookmarks/bookmarksmodel.cpp b/src/lib/bookmarks/bookmarksmodel.cpp index 2ab05ee14..953145e73 100644 --- a/src/lib/bookmarks/bookmarksmodel.cpp +++ b/src/lib/bookmarks/bookmarksmodel.cpp @@ -131,7 +131,7 @@ QVariant BookmarksModel::data(const QModelIndex &index, int role) const } case Qt::DecorationRole: if (index.column() == 0) { - return itm->icon(false); + return itm->icon(); } return QVariant(); default: @@ -139,23 +139,6 @@ QVariant BookmarksModel::data(const QModelIndex &index, int role) const } } -bool BookmarksModel::setData(const QModelIndex &index, const QVariant &value, int role) -{ - BookmarkItem *itm = item(index); - - if (!itm) { - return false; - } - - if (role == IconRole) { - itm->setIcon(value.value()); - emit dataChanged(index, index); - return true; - } - - return false; -} - QVariant BookmarksModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { diff --git a/src/lib/bookmarks/bookmarksmodel.h b/src/lib/bookmarks/bookmarksmodel.h index 32c61de50..ad3b498a0 100644 --- a/src/lib/bookmarks/bookmarksmodel.h +++ b/src/lib/bookmarks/bookmarksmodel.h @@ -54,7 +54,6 @@ public: Qt::ItemFlags flags(const QModelIndex &index) const; QVariant data(const QModelIndex &index, int role) const; - bool setData(const QModelIndex &index, const QVariant &value, int role); QVariant headerData(int section, Qt::Orientation orientation, int role) const; int rowCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const; diff --git a/src/lib/bookmarks/bookmarkstreeview.cpp b/src/lib/bookmarks/bookmarkstreeview.cpp index bfb2997ad..030d1fa6e 100644 --- a/src/lib/bookmarks/bookmarkstreeview.cpp +++ b/src/lib/bookmarks/bookmarkstreeview.cpp @@ -289,16 +289,3 @@ void BookmarksTreeView::keyPressEvent(QKeyEvent* event) } } } - -void BookmarksTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &options, const QModelIndex &index) const -{ - bool itemIsUrl = BookmarkItem::Type(index.data(BookmarksModel::TypeRole).toInt()) == BookmarkItem::Url; - bool iconLoaded = !index.data(BookmarksModel::IconRole).value().isNull(); - - if (itemIsUrl && !iconLoaded) { - const QPersistentModelIndex idx = index; - model()->setData(idx, IconProvider::iconForUrl(index.data(BookmarksModel::UrlRole).toUrl()), BookmarksModel::IconRole); - } - - QTreeView::drawRow(painter, options, index); -} diff --git a/src/lib/bookmarks/bookmarkstreeview.h b/src/lib/bookmarks/bookmarkstreeview.h index b6ba0966c..ede6dba6e 100644 --- a/src/lib/bookmarks/bookmarkstreeview.h +++ b/src/lib/bookmarks/bookmarkstreeview.h @@ -82,8 +82,6 @@ private: void mouseDoubleClickEvent(QMouseEvent* event); void keyPressEvent(QKeyEvent* event); - void drawRow(QPainter* painter, const QStyleOptionViewItem &options, const QModelIndex &index) const; - Bookmarks* m_bookmarks; BookmarksModel* m_model; BookmarksFilterModel* m_filter;