From c7272df5c17c804fd379e4bac2758850f03695da Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Fri, 9 Sep 2011 20:34:55 +0200 Subject: [PATCH] Improve cache handling in KItemListWidget Provide a hook for derived KItemListWidget where the cache can be updated. --- src/kitemviews/kfileitemlistwidget.cpp | 32 +++++++++++++++---------- src/kitemviews/kfileitemlistwidget.h | 14 ++++++++++- src/views/dolphinfileitemlistwidget.cpp | 26 ++++++-------------- src/views/dolphinfileitemlistwidget.h | 3 +-- 4 files changed, 41 insertions(+), 34 deletions(-) diff --git a/src/kitemviews/kfileitemlistwidget.cpp b/src/kitemviews/kfileitemlistwidget.cpp index d162ed8a2..d9a1cbfd1 100644 --- a/src/kitemviews/kfileitemlistwidget.cpp +++ b/src/kitemviews/kfileitemlistwidget.cpp @@ -88,7 +88,7 @@ void KFileItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIte { KItemListWidget::paint(painter, option, widget); - const_cast(this)->updateCache(); + const_cast(this)->triggerCacheRefreshing(); // Draw expansion toggle '>' or 'V' if (m_isDir && !m_expansionArea.isEmpty()) { @@ -135,7 +135,7 @@ void KFileItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIte QRectF KFileItemListWidget::iconBoundingRect() const { - const_cast(this)->updateCache(); + const_cast(this)->triggerCacheRefreshing(); QRectF bounds = m_hoverPixmapRect; const qreal margin = styleOption().margin; @@ -145,16 +145,26 @@ QRectF KFileItemListWidget::iconBoundingRect() const QRectF KFileItemListWidget::textBoundingRect() const { - const_cast(this)->updateCache(); + const_cast(this)->triggerCacheRefreshing(); return m_textBoundingRect; } QRectF KFileItemListWidget::expansionToggleRect() const { - const_cast(this)->updateCache(); + const_cast(this)->triggerCacheRefreshing(); return m_isDir ? m_expansionArea : QRectF(); } +void KFileItemListWidget::invalidateCache() +{ + m_dirtyLayout = true; + m_dirtyContent = true; +} + +void KFileItemListWidget::refreshCache() +{ +} + void KFileItemListWidget::setTextColor(const QColor& color) { if (color != m_customTextColor) { @@ -171,13 +181,9 @@ QColor KFileItemListWidget::textColor() const void KFileItemListWidget::setOverlay(const QPixmap& overlay) { - const bool updateContent = (overlay.isNull() && !m_overlay.isNull()) || - (!overlay.isNull() && m_overlay.isNull()); - if (updateContent) { - m_overlay = overlay; - m_dirtyContent = true; - update(); - } + m_overlay = overlay; + m_dirtyContent = true; + update(); } QPixmap KFileItemListWidget::overlay() const @@ -263,12 +269,14 @@ void KFileItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event) m_dirtyLayout = true; } -void KFileItemListWidget::updateCache() +void KFileItemListWidget::triggerCacheRefreshing() { if ((!m_dirtyContent && !m_dirtyLayout) || index() < 0) { return; } + refreshCache(); + m_isDir = data()["isDir"].toBool(); updateExpansionArea(); diff --git a/src/kitemviews/kfileitemlistwidget.h b/src/kitemviews/kfileitemlistwidget.h index a16e75a8e..50e746fad 100644 --- a/src/kitemviews/kfileitemlistwidget.h +++ b/src/kitemviews/kfileitemlistwidget.h @@ -53,6 +53,18 @@ public: virtual QRectF expansionToggleRect() const; protected: + /** + * Invalidates the cache which results in calling KFileItemListWidget::refreshCache() as + * soon as the item need to gets repainted. + */ + void invalidateCache(); + + /** + * Is called if the cache got invalidated by KFileItemListWidget::invalidateCache(). + * The default implementation is empty. + */ + virtual void refreshCache(); + void setTextColor(const QColor& color); QColor textColor() const; @@ -80,7 +92,7 @@ private: TextIdCount // Mandatory last entry }; - void updateCache(); + void triggerCacheRefreshing(); void updateExpansionArea(); void updatePixmapCache(); diff --git a/src/views/dolphinfileitemlistwidget.cpp b/src/views/dolphinfileitemlistwidget.cpp index a3fac7b2e..d39d58cd0 100644 --- a/src/views/dolphinfileitemlistwidget.cpp +++ b/src/views/dolphinfileitemlistwidget.cpp @@ -34,16 +34,14 @@ DolphinFileItemListWidget::~DolphinFileItemListWidget() { } -void DolphinFileItemListWidget::dataChanged(const QHash& current, const QSet& roles) +void DolphinFileItemListWidget::refreshCache() { - KFileItemListWidget::dataChanged(current, roles); - QColor color; - QPixmap overlay; - if (roles.contains("version")) { + const QHash values = data(); + if (values.contains("version")) { // The item is under version control. Apply the text color corresponding // to its version state. - const KVersionControlPlugin::VersionState version = static_cast(current.value("version").toInt()); + const KVersionControlPlugin::VersionState version = static_cast(values.value("version").toInt()); const QColor textColor = styleOption().palette.text().color(); QColor tintColor = textColor; @@ -68,22 +66,12 @@ void DolphinFileItemListWidget::dataChanged(const QHash& c (tintColor.blue() + textColor.blue()) / 2, (tintColor.alpha() + textColor.alpha()) / 2); - overlay = overlayForState(version, styleOption().iconSize); + setOverlay(overlayForState(version, styleOption().iconSize)); + } else if (!overlay().isNull()) { + setOverlay(QPixmap()); } setTextColor(color); - setOverlay(overlay); -} - -void DolphinFileItemListWidget::styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous) -{ - KFileItemListWidget::styleOptionChanged(current, previous); - - if (!overlay().isNull() && current.iconSize != previous.iconSize) { - const KVersionControlPlugin::VersionState version = static_cast(data().value("version").toInt()); - const QPixmap newOverlay = overlayForState(version, current.iconSize); - setOverlay(newOverlay); - } } QPixmap DolphinFileItemListWidget::overlayForState(KVersionControlPlugin::VersionState version, int size) diff --git a/src/views/dolphinfileitemlistwidget.h b/src/views/dolphinfileitemlistwidget.h index b08b96428..8435b5c18 100644 --- a/src/views/dolphinfileitemlistwidget.h +++ b/src/views/dolphinfileitemlistwidget.h @@ -40,8 +40,7 @@ public: virtual ~DolphinFileItemListWidget(); protected: - virtual void dataChanged(const QHash& current, const QSet& roles = QSet()); - virtual void styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous); + virtual void refreshCache(); private: static QPixmap overlayForState(KVersionControlPlugin::VersionState state, int size); -- 2.47.3