Change how boundingRect() is calculated and backgrounds are drawn.

Instead of implementing boundingRect(), applets should now implement
contentSize(), to tell Plasma::Applet how much space they need.

I wrote a long commit log, then realised it belonged in the apidocs.


svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=692519
This commit is contained in:
Alex Merry 2007-07-25 19:15:23 +00:00
parent 1dede63a8f
commit eddb8f294f
2 changed files with 52 additions and 31 deletions

View File

@ -131,9 +131,9 @@ public:
void paintBackground(QPainter* p, Applet* q) void paintBackground(QPainter* p, Applet* q)
{ {
//TODO: we should cache this background rather that repaint it over and over //TODO: we should cache this background rather that repaint it over and over
QRect rect = q->boundingRect().toRect(); QSize contents = q->contentSize().toSize();
const int w = rect.width(); const int contentWidth = contents.width();
const int h = rect.height(); const int contentHeight = contents.height();
#if 0 #if 0
// this could be used to draw a dynamic shadow // this could be used to draw a dynamic shadow
QImage image(w, h, QImage::Format_ARGB32_Premultiplied); QImage image(w, h, QImage::Format_ARGB32_Premultiplied);
@ -149,13 +149,21 @@ public:
const int leftHeight = background->elementSize("left").height(); const int leftHeight = background->elementSize("left").height();
const int rightWidth = background->elementSize("right").width(); const int rightWidth = background->elementSize("right").width();
const int bottomHeight = background->elementSize("bottom").height(); const int bottomHeight = background->elementSize("bottom").height();
const int lrWidths = leftWidth + rightWidth; //const int lrWidths = leftWidth + rightWidth;
const int tbHeights = topHeight + bottomHeight; //const int tbHeights = topHeight + bottomHeight;
background->paint(p, QRect(0, 0, leftWidth, topHeight), "topleft"); // contents top-left corner is (0,0). We need to draw up and left of that
background->paint(p, QRect(w - rightWidth, 0, rightWidth, topHeight), "topright"); const int topOffset = 0 - topHeight;
background->paint(p, QRect(0, h - bottomHeight, leftWidth, bottomHeight), "bottomleft"); const int leftOffset = 0 - leftWidth;
background->paint(p, QRect(w - rightWidth, h - bottomHeight, rightWidth, bottomHeight), "bottomright"); const int rightOffset = contentWidth;
const int bottomOffset = contentHeight;
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");
QPixmap left(leftWidth, leftHeight); QPixmap left(leftWidth, leftHeight);
{ {
@ -163,7 +171,7 @@ public:
sidePainter.setCompositionMode(QPainter::CompositionMode_Source); sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
background->paint(&sidePainter, QPoint(0, 0), "left"); background->paint(&sidePainter, QPoint(0, 0), "left");
} }
p->drawTiledPixmap(QRect(0, topHeight, leftWidth, h - tbHeights), left); p->drawTiledPixmap(QRect(leftOffset, contentTop, leftWidth, contentHeight), left);
QPixmap right(rightWidth, leftHeight); QPixmap right(rightWidth, leftHeight);
{ {
@ -171,7 +179,7 @@ public:
sidePainter.setCompositionMode(QPainter::CompositionMode_Source); sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
background->paint(&sidePainter, QPoint(0, 0), "right"); background->paint(&sidePainter, QPoint(0, 0), "right");
} }
p->drawTiledPixmap(QRect(w - rightWidth, topHeight, leftWidth, h - tbHeights), right); p->drawTiledPixmap(QRect(rightOffset, contentTop, rightWidth, contentHeight), right);
QPixmap top(topWidth, topHeight); QPixmap top(topWidth, topHeight);
@ -180,7 +188,7 @@ public:
sidePainter.setCompositionMode(QPainter::CompositionMode_Source); sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
background->paint(&sidePainter, QPoint(0, 0), "top"); background->paint(&sidePainter, QPoint(0, 0), "top");
} }
p->drawTiledPixmap(QRect(leftWidth, 0, w - lrWidths, topHeight), top); p->drawTiledPixmap(QRect(contentLeft, topOffset, contentWidth, topHeight), top);
QPixmap bottom(topWidth, bottomHeight); QPixmap bottom(topWidth, bottomHeight);
{ {
@ -188,9 +196,9 @@ public:
sidePainter.setCompositionMode(QPainter::CompositionMode_Source); sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
background->paint(&sidePainter, QPoint(0, 0), "bottom"); background->paint(&sidePainter, QPoint(0, 0), "bottom");
} }
p->drawTiledPixmap(QRect(leftWidth, h - bottomHeight, w - lrWidths, bottomHeight), bottom); p->drawTiledPixmap(QRect(contentLeft, bottomOffset, contentWidth, bottomHeight), bottom);
background->paint(p, QRect(leftWidth, topHeight, w - lrWidths + 1, h - tbHeights + 1), "center"); background->paint(p, QRect(contentLeft, contentTop, contentWidth + 1, contentHeight + 1), "center");
#if 0 #if 0
// this could be used to draw a dynamic shadow // this could be used to draw a dynamic shadow
@ -446,12 +454,19 @@ int Applet::type() const
QRectF Applet::boundingRect () const QRectF Applet::boundingRect () const
{ {
if (d->scriptEngine) { QRectF rect = QRectF(QPointF(0,0), contentSize());
return QRectF(QPointF(0, 0), d->scriptEngine->size()); if (!d->background) {
return rect;
} }
//FIXME: this should be big enough to allow for the failure text? const int topHeight = d->background->elementSize("top").height();
return QRectF(300, 300, 300, 300); const int leftWidth = d->background->elementSize("left").width();
const int rightWidth = d->background->elementSize("right").width();
const int bottomHeight = d->background->elementSize("bottom").height();
rect.adjust(0 - leftWidth, 0 - topHeight, rightWidth, bottomHeight);
return rect;
} }
void Applet::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) void Applet::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@ -466,7 +481,7 @@ void Applet::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QW
return; return;
} }
paintInterface(painter, option, contentsRect()); paintInterface(painter, option, QRect(QPoint(0,0), contentSize().toSize()));
//TODO: interface overlays on hover //TODO: interface overlays on hover
} }
@ -501,20 +516,14 @@ Location Applet::location() const
return static_cast<Corona*>(scene())->location(); return static_cast<Corona*>(scene())->location();
} }
QRect Applet::contentsRect() const QSizeF Applet::contentSize() const
{ {
QRect rect = boundingRect().toRect(); if (d->scriptEngine) {
if (!d->background) { return d->scriptEngine->size();
return rect;
} }
const int topHeight = d->background->elementSize("top").height(); //FIXME: this should be big enough to allow for the failure text?
const int leftWidth = d->background->elementSize("left").width(); return QSizeF(300, 300);
const int rightWidth = d->background->elementSize("right").width();
const int bottomHeight = d->background->elementSize("bottom").height();
rect.adjust(leftWidth, topHeight, 0 - rightWidth, 0 - bottomHeight);
return rect;
} }
QString Applet::globalName() const QString Applet::globalName() const

View File

@ -173,8 +173,20 @@ class PLASMA_EXPORT Applet : public QObject, public Widget
/** /**
* Returns the area within which contents can be painted. If there is no * Returns the area within which contents can be painted. If there is no
* background, then this is equivalent to boundingRect(). * background, then this is equivalent to boundingRect().
*
* Applets should implement contentSize() to tell Plasma::Applet how
* much space they need.
*
* If drawStandardBackground() == true, enough space will reserved
* within the borders for the content, and boundingRect() will the
* total size of the applet, including borders.
*
* If the applet has not asked Plasma::Applet to draw the default
* background, boundingRect().size() == contentSize(), and
* boundingRect().topLeft() == QPointF(0,0). Also, such applets will
* not break if they implement boundingRect() instead.
**/ **/
QRect contentsRect() const; virtual QSizeF contentSize() const;
/** /**
* Returns a list of all known applets in a hash keyed by a unique * Returns a list of all known applets in a hash keyed by a unique