[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-panel-devel
Subject:    [Panel-devel] Patch to cache standard applet background background
From:       Thomas Georgiou <tageorgiou () gmail ! com>
Date:       2007-07-29 16:24:11
Message-ID: 200707291224.11972.TAGeorgiou () gmail ! com
[Download RAW message or body]

I reworked the background caching without the shadows and made big 
improvements to it thanks to the help of people on this mailing list.

Index: applet.cpp
===================================================================
--- applet.cpp	(revision 693910)
+++ applet.cpp	(working copy)
@@ -133,7 +133,7 @@
         }
     }
 
-    void paintBackground(QPainter* p, Applet* q)
+    void paintBackground(QPainter* p2, Applet* q)
     {
         //TODO: we should cache this background rather that repaint it over 
and over
         QSize contents = contentSize(q).toSize();
@@ -145,7 +145,7 @@
         QPainter* p = new QPainter(&image);
         p->setCompositionMode(QPainter::CompositionMode_Source);
 #endif
-        p->setRenderHint(QPainter::SmoothPixmapTransform);
+
         background->resize();
 
         const int topHeight = background->elementSize("top").height();
@@ -165,49 +165,61 @@
         const int contentTop = 0;
         const int contentLeft = 0;
 
-        background->paint(p, QRect(leftOffset,  topOffset,    leftWidth,  
topHeight),    "topleft");
-        background->paint(p, QRect(rightOffset, topOffset,    rightWidth, 
topHeight),    "topright");
-        background->paint(p, QRect(leftOffset,  bottomOffset, leftWidth,  
bottomHeight), "bottomleft");
-        background->paint(p, QRect(rightOffset, bottomOffset, rightWidth, 
bottomHeight), "bottomright");
+        if (cachedBackground || cachedBackground->size() != QSize(leftWidth + 
contentWidth + rightWidth, topHeight + contentHeight + bottomHeight)) {
+            if (!cachedBackground) {
+                delete cachedBackground;
+            }
+            cachedBackground = new QPixmap(leftWidth + contentWidth + 
rightWidth, topHeight + contentHeight + bottomHeight);
+            cachedBackground->fill(Qt::transparent);
+            QPainter p(cachedBackground);
+            p.translate(leftWidth, topHeight);
+            p.setCompositionMode(QPainter::CompositionMode_Source);
+            p.setRenderHint(QPainter::SmoothPixmapTransform);
 
-        QPixmap left(leftWidth, leftHeight);
-        left.fill(Qt::transparent);
-        {
-            QPainter sidePainter(&left);
-            sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
-            background->paint(&sidePainter, QPoint(0, 0), "left");
-        }
-        p->drawTiledPixmap(QRect(leftOffset, contentTop, leftWidth, 
contentHeight), left);
+            background->paint(&p, QRect(leftOffset,  topOffset,    leftWidth,  
topHeight),    "topleft");
+            background->paint(&p, QRect(rightOffset, topOffset,    
rightWidth, topHeight),    "topright");
+            background->paint(&p, QRect(leftOffset,  bottomOffset, leftWidth,  
bottomHeight), "bottomleft");
+            background->paint(&p, QRect(rightOffset, bottomOffset, 
rightWidth, bottomHeight), "bottomright");
 
-        QPixmap right(rightWidth, leftHeight);
-        right.fill(Qt::transparent);
-        {
-            QPainter sidePainter(&right);
-            sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
-            background->paint(&sidePainter, QPoint(0, 0), "right");
-        }
-        p->drawTiledPixmap(QRect(rightOffset, contentTop, rightWidth, 
contentHeight), right);
+            QPixmap left(leftWidth, leftHeight);
+            left.fill(Qt::transparent);
+            {
+                QPainter sidePainter(&left);
+                
sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
+                background->paint(&sidePainter, QPoint(0, 0), "left");
+            }
+            p.drawTiledPixmap(QRect(leftOffset, contentTop, leftWidth, 
contentHeight), left);
 
+            QPixmap right(rightWidth, leftHeight);
+            right.fill(Qt::transparent);
+            {
+                QPainter sidePainter(&right);
+                
sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
+                background->paint(&sidePainter, QPoint(0, 0), "right");
+            }
+            p.drawTiledPixmap(QRect(rightOffset, contentTop, rightWidth, 
contentHeight), right);
 
-        QPixmap top(topWidth, topHeight);
-        top.fill(Qt::transparent);
-        {
-            QPainter sidePainter(&top);
-            sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
-            background->paint(&sidePainter, QPoint(0, 0), "top");
-        }
-        p->drawTiledPixmap(QRect(contentLeft, topOffset, contentWidth, 
topHeight), top);
+            QPixmap top(topWidth, topHeight);
+            top.fill(Qt::transparent);
+            {
+                QPainter sidePainter(&top);
+                
sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
+                background->paint(&sidePainter, QPoint(0, 0), "top");
+            }
+            p.drawTiledPixmap(QRect(contentLeft, topOffset, contentWidth, 
topHeight), top);
 
-        QPixmap bottom(topWidth, bottomHeight);
-        bottom.fill(Qt::transparent);
-        {
-            QPainter sidePainter(&bottom);
-            sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
-            background->paint(&sidePainter, QPoint(0, 0), "bottom");
+            QPixmap bottom(topWidth, bottomHeight);
+            bottom.fill(Qt::transparent);
+            {
+                QPainter sidePainter(&bottom);
+                
sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
+                background->paint(&sidePainter, QPoint(0, 0), "bottom");
+            }
+            p.drawTiledPixmap(QRect(contentLeft, bottomOffset, contentWidth, 
bottomHeight), bottom);
+
+            background->paint(&p, QRect(contentLeft, contentTop, contentWidth 
+ 1, contentHeight + 1), "center");
         }
-        p->drawTiledPixmap(QRect(contentLeft, bottomOffset, contentWidth, 
bottomHeight), bottom);
-
-        background->paint(p, QRect(contentLeft, contentTop, contentWidth + 1, 
contentHeight + 1), "center");
+        p2->drawPixmap(leftOffset, topOffset, *cachedBackground);
     }
 
     void paintHover(QPainter* painter, Applet* q)
@@ -251,6 +263,8 @@
     bool immutable : 1;
     bool hasConfigurationInterface : 1;
     bool failed : 1;
+private:
+    QPixmap* cachedBackground;
 };
 
 uint Applet::Private::s_maxAppletId = 0;

["appletBackgroundCache.diff" (text/x-diff)]

Index: applet.cpp
===================================================================
--- applet.cpp	(revision 693910)
+++ applet.cpp	(working copy)
@@ -133,7 +133,7 @@
         }
     }
 
-    void paintBackground(QPainter* p, Applet* q)
+    void paintBackground(QPainter* p2, Applet* q)
     {
         //TODO: we should cache this background rather that repaint it over and over
         QSize contents = contentSize(q).toSize();
@@ -145,7 +145,7 @@
         QPainter* p = new QPainter(&image);
         p->setCompositionMode(QPainter::CompositionMode_Source);
 #endif
-        p->setRenderHint(QPainter::SmoothPixmapTransform);
+
         background->resize();
 
         const int topHeight = background->elementSize("top").height();
@@ -165,49 +165,61 @@
         const int contentTop = 0;
         const int contentLeft = 0;
 
-        background->paint(p, QRect(leftOffset,  topOffset,    leftWidth,  \
                topHeight),    "topleft");
-        background->paint(p, QRect(rightOffset, topOffset,    rightWidth, \
                topHeight),    "topright");
-        background->paint(p, QRect(leftOffset,  bottomOffset, leftWidth,  \
                bottomHeight), "bottomleft");
-        background->paint(p, QRect(rightOffset, bottomOffset, rightWidth, \
bottomHeight), "bottomright"); +        if (cachedBackground || \
cachedBackground->size() != QSize(leftWidth + contentWidth + rightWidth, topHeight + \
contentHeight + bottomHeight)) { +            if (!cachedBackground) {
+                delete cachedBackground;
+            }
+            cachedBackground = new QPixmap(leftWidth + contentWidth + rightWidth, \
topHeight + contentHeight + bottomHeight); +            \
cachedBackground->fill(Qt::transparent); +            QPainter p(cachedBackground);
+            p.translate(leftWidth, topHeight);
+            p.setCompositionMode(QPainter::CompositionMode_Source);
+            p.setRenderHint(QPainter::SmoothPixmapTransform);
 
-        QPixmap left(leftWidth, leftHeight);
-        left.fill(Qt::transparent);
-        {
-            QPainter sidePainter(&left);
-            sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
-            background->paint(&sidePainter, QPoint(0, 0), "left");
-        }
-        p->drawTiledPixmap(QRect(leftOffset, contentTop, leftWidth, contentHeight), \
left); +            background->paint(&p, QRect(leftOffset,  topOffset,    leftWidth, \
topHeight),    "topleft"); +            background->paint(&p, QRect(rightOffset, \
topOffset,    rightWidth, topHeight),    "topright"); +            \
background->paint(&p, QRect(leftOffset,  bottomOffset, leftWidth,  bottomHeight), \
"bottomleft"); +            background->paint(&p, QRect(rightOffset, bottomOffset, \
rightWidth, bottomHeight), "bottomright");  
-        QPixmap right(rightWidth, leftHeight);
-        right.fill(Qt::transparent);
-        {
-            QPainter sidePainter(&right);
-            sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
-            background->paint(&sidePainter, QPoint(0, 0), "right");
-        }
-        p->drawTiledPixmap(QRect(rightOffset, contentTop, rightWidth, \
contentHeight), right); +            QPixmap left(leftWidth, leftHeight);
+            left.fill(Qt::transparent);
+            {
+                QPainter sidePainter(&left);
+                sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
+                background->paint(&sidePainter, QPoint(0, 0), "left");
+            }
+            p.drawTiledPixmap(QRect(leftOffset, contentTop, leftWidth, \
contentHeight), left);  
+            QPixmap right(rightWidth, leftHeight);
+            right.fill(Qt::transparent);
+            {
+                QPainter sidePainter(&right);
+                sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
+                background->paint(&sidePainter, QPoint(0, 0), "right");
+            }
+            p.drawTiledPixmap(QRect(rightOffset, contentTop, rightWidth, \
contentHeight), right);  
-        QPixmap top(topWidth, topHeight);
-        top.fill(Qt::transparent);
-        {
-            QPainter sidePainter(&top);
-            sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
-            background->paint(&sidePainter, QPoint(0, 0), "top");
-        }
-        p->drawTiledPixmap(QRect(contentLeft, topOffset, contentWidth, topHeight), \
top); +            QPixmap top(topWidth, topHeight);
+            top.fill(Qt::transparent);
+            {
+                QPainter sidePainter(&top);
+                sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
+                background->paint(&sidePainter, QPoint(0, 0), "top");
+            }
+            p.drawTiledPixmap(QRect(contentLeft, topOffset, contentWidth, \
topHeight), top);  
-        QPixmap bottom(topWidth, bottomHeight);
-        bottom.fill(Qt::transparent);
-        {
-            QPainter sidePainter(&bottom);
-            sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
-            background->paint(&sidePainter, QPoint(0, 0), "bottom");
+            QPixmap bottom(topWidth, bottomHeight);
+            bottom.fill(Qt::transparent);
+            {
+                QPainter sidePainter(&bottom);
+                sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
+                background->paint(&sidePainter, QPoint(0, 0), "bottom");
+            }
+            p.drawTiledPixmap(QRect(contentLeft, bottomOffset, contentWidth, \
bottomHeight), bottom); +
+            background->paint(&p, QRect(contentLeft, contentTop, contentWidth + 1, \
contentHeight + 1), "center");  }
-        p->drawTiledPixmap(QRect(contentLeft, bottomOffset, contentWidth, \
                bottomHeight), bottom);
-
-        background->paint(p, QRect(contentLeft, contentTop, contentWidth + 1, \
contentHeight + 1), "center"); +        p2->drawPixmap(leftOffset, topOffset, \
*cachedBackground);  }
 
     void paintHover(QPainter* painter, Applet* q)
@@ -251,6 +263,8 @@
     bool immutable : 1;
     bool hasConfigurationInterface : 1;
     bool failed : 1;
+private:
+    QPixmap* cachedBackground;
 };
 
 uint Applet::Private::s_maxAppletId = 0;



_______________________________________________
Panel-devel mailing list
Panel-devel@kde.org
https://mail.kde.org/mailman/listinfo/panel-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic