[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-panel-devel
Subject: Re: [Panel-devel] Patch to cache standard applet background
From: Thomas Georgiou <tageorgiou () gmail ! com>
Date: 2007-07-30 11:36:42
Message-ID: 200707300736.42887.TAGeorgiou () gmail ! com
[Download RAW message or body]
On Monday 30 July 2007 03:06:45 am Zack Rusin wrote:
> Well yeah, of course. You never initialize the cachedBackground pointer so
> it was random garbage and you recompiling with different codepaths made the
> random garbage randomly 0.
> So randomly it just happened to work in this scenario and you, randomly,
> decided that you fixed it =)
> So now maybe "deterministically" for a change, you can initialize the
> pointer and remove the silly "if" :)
Thanks! I forgot to initiliaze the pointer to null in the constructor.
Here is the revised patch:
Index: applet.cpp
===================================================================
--- applet.cpp (revision 694270)
+++ applet.cpp (working copy)
@@ -66,7 +66,8 @@
kioskImmutable(false),
immutable(false),
hasConfigurationInterface(false),
- failed(false)
+ failed(false),
+ cachedBackground(0)
{
if (appletId == 0) {
appletId = nextId();
@@ -133,9 +134,8 @@
}
}
- 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();
const int contentWidth = contents.width();
const int contentHeight = contents.height();
@@ -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,60 @@
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)) {
+ 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.end();
}
- 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 +262,8 @@
bool immutable : 1;
bool hasConfigurationInterface : 1;
bool failed : 1;
+private:
+ QPixmap* cachedBackground;
};
uint Applet::Private::s_maxAppletId = 0;
["appletBgCache.diff" (text/x-diff)]
Index: applet.cpp
===================================================================
--- applet.cpp (revision 694270)
+++ applet.cpp (working copy)
@@ -66,7 +66,8 @@
kioskImmutable(false),
immutable(false),
hasConfigurationInterface(false),
- failed(false)
+ failed(false),
+ cachedBackground(0)
{
if (appletId == 0) {
appletId = nextId();
@@ -133,9 +134,8 @@
}
}
- 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();
const int contentWidth = contents.width();
const int contentHeight = contents.height();
@@ -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,60 @@
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)) { + 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.end();
}
- 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 +262,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