]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Improve cache handling in KItemListWidget
authorPeter Penz <peter.penz19@gmail.com>
Fri, 9 Sep 2011 18:34:55 +0000 (20:34 +0200)
committerPeter Penz <peter.penz19@gmail.com>
Fri, 9 Sep 2011 18:35:57 +0000 (20:35 +0200)
Provide a hook for derived KItemListWidget where the cache
can be updated.

src/kitemviews/kfileitemlistwidget.cpp
src/kitemviews/kfileitemlistwidget.h
src/views/dolphinfileitemlistwidget.cpp
src/views/dolphinfileitemlistwidget.h

index d162ed8a2c13ef8036f8c0a13905342d91ce9eae..d9a1cbfd1ca7d5baefb3c279f9c5edcc2a410881 100644 (file)
@@ -88,7 +88,7 @@ void KFileItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsIte
 {
     KItemListWidget::paint(painter, option, widget);
 
-    const_cast<KFileItemListWidget*>(this)->updateCache();
+    const_cast<KFileItemListWidget*>(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<KFileItemListWidget*>(this)->updateCache();
+    const_cast<KFileItemListWidget*>(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<KFileItemListWidget*>(this)->updateCache();
+    const_cast<KFileItemListWidget*>(this)->triggerCacheRefreshing();
     return m_textBoundingRect;
 }
 
 QRectF KFileItemListWidget::expansionToggleRect() const
 {
-    const_cast<KFileItemListWidget*>(this)->updateCache();
+    const_cast<KFileItemListWidget*>(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();
index a16e75a8e0b5dafe4724d6868fe918767acb4fa0..50e746fad63ada02a40bbca13d5d62b5ccd49f43 100644 (file)
@@ -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();
 
index a3fac7b2e46aa0f3af35d727effd84f002f82ada..d39d58cd03ecff7183a6d976a49ce72ae94b4fa7 100644 (file)
@@ -34,16 +34,14 @@ DolphinFileItemListWidget::~DolphinFileItemListWidget()
 {
 }
 
-void DolphinFileItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current, const QSet<QByteArray>& roles)
+void DolphinFileItemListWidget::refreshCache()
 {
-    KFileItemListWidget::dataChanged(current, roles);
-
     QColor color;
-    QPixmap overlay;
-    if (roles.contains("version")) {
+    const QHash<QByteArray, QVariant> 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<KVersionControlPlugin::VersionState>(current.value("version").toInt());
+        const KVersionControlPlugin::VersionState version = static_cast<KVersionControlPlugin::VersionState>(values.value("version").toInt());
         const QColor textColor = styleOption().palette.text().color();
         QColor tintColor = textColor;
 
@@ -68,22 +66,12 @@ void DolphinFileItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& 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<KVersionControlPlugin::VersionState>(data().value("version").toInt());
-        const QPixmap newOverlay = overlayForState(version, current.iconSize);
-        setOverlay(newOverlay);
-    }
 }
 
 QPixmap DolphinFileItemListWidget::overlayForState(KVersionControlPlugin::VersionState version, int size)
index b08b96428f4f262bccd192c9f5f41fec8c6bc2d4..8435b5c18bd387bead782c4e31ea91405e4718e9 100644 (file)
@@ -40,8 +40,7 @@ public:
     virtual ~DolphinFileItemListWidget();
 
 protected:
-    virtual void dataChanged(const QHash<QByteArray, QVariant>& current, const QSet<QByteArray>& roles = QSet<QByteArray>());
-    virtual void styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous);
+    virtual void refreshCache();
 
 private:
     static QPixmap overlayForState(KVersionControlPlugin::VersionState state, int size);