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

List:       kde-panel-devel
Subject:    [Panel-devel] Propsed change: boundingRect() vs. contentSize()
From:       Alex Merry <huntedhacker () tiscali ! co ! uk>
Date:       2007-07-25 13:54:35
Message-ID: 200707251454.40121.huntedhacker () tiscali ! co ! uk
[Download RAW message or body]

[Attachment #2 (multipart/signed)]

[Attachment #4 (multipart/mixed)]


Attached is a proposed API change.  I've got a test applet (which I will 
commit, because it's very useful for debugging), the digital clock and 
the dictionary applets working correctly with it (well, the dictionary 
one needs work to make it display the arrows, but...)

Basically, rather than specifying the boundingRect(), applets implement 
a virtual method
QSizeF contentSize() const
detailing what size they need for content.  If no background is being 
drawn, Plasma::Applet::boundingRect() just returns a rectangle of the 
requested size anchored at (0,0).

If a background is drawn, the boundingRect() may be larger to take this 
into account.  However, the area for content will be anchored at (0,0) 
and of the requested size.

So the following works as expected:
MyApplet::MyApplet(...) : ..., m_edit(new Plasma::LineEdit(this)) {
  setDrawDefaultBackground(true);
}
QSizeF contentSize() const {
  m_edit->sizeHint();
}

(in fact, this is exactly what the test applet I wrote does).

Comments?  OK to commit?

Alex


-- 
KDE: http://www.kde.org
Ubuntu/Kubuntu: http://www.ubuntu.org http://www.kubuntu.org
Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html

["plasma-rects.diff" (text/x-diff)]

Index: applet.cpp
===================================================================
--- applet.cpp	(revision 692327)
+++ applet.cpp	(working copy)
@@ -131,9 +131,9 @@ public:
     void paintBackground(QPainter* p, Applet* q)
     {
         //TODO: we should cache this background rather that repaint it over and over
-        QRect rect = q->boundingRect().toRect();
-        const int w = rect.width();
-        const int h = rect.height();
+        QSize contents = q->contentSize().toSize();
+        const int contentWidth = contents.width();
+        const int contentHeight = contents.height();
 #if 0
         // this could be used to draw a dynamic shadow
         QImage image(w, h, QImage::Format_ARGB32_Premultiplied);
@@ -149,13 +149,21 @@ public:
         const int leftHeight = background->elementSize("left").height();
         const int rightWidth = background->elementSize("right").width();
         const int bottomHeight = background->elementSize("bottom").height();
-        const int lrWidths = leftWidth + rightWidth;
-        const int tbHeights = topHeight + bottomHeight;
+        //const int lrWidths = leftWidth + rightWidth;
+        //const int tbHeights = topHeight + bottomHeight;
 
-        background->paint(p, QRect(0, 0, leftWidth, topHeight), "topleft");
-        background->paint(p, QRect(w - rightWidth, 0, rightWidth, topHeight), \
                "topright");
-        background->paint(p, QRect(0, h - bottomHeight, leftWidth, bottomHeight), \
                "bottomleft");
-        background->paint(p, QRect(w - rightWidth, h - bottomHeight, rightWidth, \
bottomHeight), "bottomright"); +        // contents top-left corner is (0,0).  We \
need to draw up and left of that +        const int topOffset = 0 - topHeight;
+        const int leftOffset = 0 - leftWidth;
+        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);
         {
@@ -163,7 +171,7 @@ public:
             sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
             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);
         {
@@ -171,7 +179,7 @@ public:
             sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
             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);
@@ -180,7 +188,7 @@ public:
             sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
             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);
         {
@@ -188,9 +196,9 @@ public:
             sidePainter.setCompositionMode(QPainter::CompositionMode_Source);
             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
         // this could be used to draw a dynamic shadow
@@ -446,12 +454,19 @@ int Applet::type() const
 
 QRectF Applet::boundingRect () const
 {
-    if (d->scriptEngine) {
-        return QRectF(QPointF(0, 0), d->scriptEngine->size());
+    QRectF rect = QRectF(QPointF(0,0), contentSize());
+    if (!d->background) {
+        return rect;
     }
 
-    //FIXME: this should be big enough to allow for the failure text?
-    return QRectF(300, 300, 300, 300);
+    const int topHeight = d->background->elementSize("top").height();
+    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, leftWidth + rightWidth, topHeight + \
bottomHeight); +    return rect;
+
 }
 
 void Applet::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, \
QWidget *widget) @@ -466,7 +481,7 @@ void Applet::paint(QPainter *painter, co
         return;
     }
 
-    paintInterface(painter, option, contentsRect());
+    paintInterface(painter, option, QRect(QPoint(0,0), contentSize().toSize()));
 
     //TODO: interface overlays on hover
 }
@@ -501,20 +516,14 @@ Location Applet::location() const
     return static_cast<Corona*>(scene())->location();
 }
 
-QRect Applet::contentsRect() const
+QSizeF Applet::contentSize() const
 {
-    QRect rect = boundingRect().toRect();
-    if (!d->background) {
-        return rect;
+    if (d->scriptEngine) {
+        return d->scriptEngine->size();
     }
 
-    const int topHeight = d->background->elementSize("top").height();
-    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(leftWidth, topHeight, 0 - leftWidth - rightWidth, 0 - topHeight - \
                bottomHeight);
-    return rect;
+    //FIXME: this should be big enough to allow for the failure text?
+    return QSizeF(300, 300);
 }
 
 QString Applet::globalName() const
Index: applet.h
===================================================================
--- applet.h	(revision 692327)
+++ applet.h	(working copy)
@@ -174,7 +174,7 @@ class PLASMA_EXPORT Applet : public QObj
          * Returns the area within which contents can be painted. If there is no
          * background, then this is equivalent to boundingRect().
          **/
-        QRect contentsRect() const;
+        virtual QSizeF contentSize() const;
 
         /**
          * Returns a list of all known applets in a hash keyed by a unique


["signature.asc" (application/pgp-signature)]

_______________________________________________
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