[Panel-devel] Patch to cache standard applet background background

Thomas Georgiou tageorgiou at gmail.com
Sun Jul 29 18:24:11 CEST 2007


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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: appletBackgroundCache.diff
Type: text/x-diff
Size: 6144 bytes
Desc: not available
Url : http://mail.kde.org/pipermail/panel-devel/attachments/20070729/92be7e98/attachment.bin 


More information about the Panel-devel mailing list