From e614df56d0e195aeb343b63ab18988afe83a36c3 Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Mon, 19 Nov 2007 23:45:56 +0000 Subject: [PATCH] * consolidate geometry() and setGeometry() from the layouts into Layout * introduce relayout() which does only that * place guards around relayout() calls in Layout class to prevent recursion svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=738923 --- applet.cpp | 4 +-- layouts/borderlayout.cpp | 49 ++++++++++--------------------- layouts/borderlayout.h | 7 ++--- layouts/boxlayout.cpp | 21 +++++-------- layouts/boxlayout.h | 5 ++-- layouts/flowlayout.cpp | 38 ++++++++++-------------- layouts/flowlayout.h | 10 +++---- layouts/freelayout.cpp | 2 +- layouts/freelayout.h | 4 ++- layouts/layout.cpp | 22 ++++++++++++-- layouts/layout.h | 19 ++++++++++-- layouts/layoutitem.h | 7 +++++ layouts/nodelayout.cpp | 25 ++++------------ layouts/nodelayout.h | 6 ++-- uiloader.cpp | 21 +++++++------ widgets/lineedit.cpp | 11 +++++++ widgets/lineedit.h | 1 + widgets/tests/testLayouts.cpp | 12 ++++---- widgets/tests/testProgressBar.cpp | 7 ++--- 19 files changed, 136 insertions(+), 135 deletions(-) diff --git a/applet.cpp b/applet.cpp index eca0417c1..d5c84250f 100644 --- a/applet.cpp +++ b/applet.cpp @@ -57,10 +57,10 @@ #include "plasma/theme.h" #include "plasma/view.h" +#include "plasma/layouts/boxlayout.h" #include "plasma/widgets/widget.h" #include "plasma/widgets/lineedit.h" #include "plasma/widgets/pushbutton.h" -#include "plasma/layouts/boxlayout.h" //#define DYNAMIC_SHADOWS namespace Plasma @@ -715,7 +715,7 @@ void Applet::flushUpdatedConstraints() constraintsUpdated(c); if (layout()) { - layout()->update(); + layout()->updateGeometry(); } } diff --git a/layouts/borderlayout.cpp b/layouts/borderlayout.cpp index 01b7786d6..632458519 100644 --- a/layouts/borderlayout.cpp +++ b/layouts/borderlayout.cpp @@ -41,7 +41,6 @@ public: } QMap< Position, LayoutItem * > itemPositions; - QRectF geometry; QMap< Position, qreal > sizes; }; @@ -65,39 +64,23 @@ Qt::Orientations BorderLayout::expandingDirections() const return Qt::Horizontal | Qt::Vertical; } -QRectF BorderLayout::geometry() const +void BorderLayout::relayout() { - return d->geometry; -} + QRectF rect = geometry(); + rect.setTopLeft(rect.topLeft() + QPointF(margin(LeftMargin), margin(TopMargin))); + rect.setBottomRight(rect.bottomRight() - QPointF(margin(RightMargin), margin(BottomMargin))); -void BorderLayout::setGeometry(const QRectF& geometry) -{ - if (!geometry.isValid() || geometry.isEmpty()) { - return; - } - - d->geometry = geometry; - - invalidate(); -} - -void BorderLayout::invalidate() -{ - QRectF geometry = d->geometry; - geometry.setTopLeft(geometry.topLeft() + QPointF(margin(LeftMargin), margin(TopMargin))); - geometry.setBottomRight(geometry.bottomRight() - QPointF(margin(RightMargin), margin(BottomMargin))); - - QPointF origin = geometry.topLeft(); + QPointF origin = rect.topLeft(); qreal top, bottom, left, right; top = (d->sizes[TopPositioned] >= 0) ? d->sizes[TopPositioned] : 0; left = (d->sizes[LeftPositioned] >= 0) ? d->sizes[LeftPositioned] : 0; - bottom = geometry.height() - ((d->sizes[BottomPositioned] >= 0) ? d->sizes[BottomPositioned] : 0); - right = geometry.width() - ((d->sizes[RightPositioned] >= 0) ? d->sizes[RightPositioned] : 0); + bottom = rect.height() - ((d->sizes[BottomPositioned] >= 0) ? d->sizes[BottomPositioned] : 0); + right = rect.width() - ((d->sizes[RightPositioned] >= 0) ? d->sizes[RightPositioned] : 0); if (d->itemPositions[TopPositioned] /*&& d->itemPositions[TopPositioned]->isVisible()*/) { top = (d->sizes[TopPositioned] >= 0) ? d->sizes[TopPositioned] : d->itemPositions[TopPositioned]->sizeHint().height(); d->itemPositions[TopPositioned]->setGeometry(QRectF(origin, QSizeF( - geometry.width(), top))); + rect.width(), top))); top += spacing(); } @@ -105,9 +88,9 @@ void BorderLayout::invalidate() bottom = (d->sizes[BottomPositioned] >= 0) ? d->sizes[BottomPositioned] : d->itemPositions[BottomPositioned]->sizeHint().height(); d->itemPositions[BottomPositioned]->setGeometry(QRectF(origin + QPointF(0, - geometry.height() - bottom), QSizeF(geometry.width(), + rect.height() - bottom), QSizeF(rect.width(), bottom))); - bottom = geometry.height() - bottom - spacing(); + bottom = rect.height() - bottom - spacing(); } if (d->itemPositions[LeftPositioned] /*&& d->itemPositions[LeftPositioned]->isVisible()*/) { @@ -120,8 +103,8 @@ void BorderLayout::invalidate() if (d->itemPositions[RightPositioned] /*&& d->itemPositions[RightPositioned]->isVisible()*/) { right = (d->sizes[RightPositioned] >= 0) ? d->sizes[RightPositioned] : d->itemPositions[RightPositioned]->sizeHint().width(); d->itemPositions[RightPositioned]->setGeometry(QRectF(origin + QPointF( - geometry.width() - right, top), QSizeF(right, bottom - top))); - right = geometry.width() - right - spacing(); + rect.width() - right, top), QSizeF(right, bottom - top))); + right = rect.width() - right - spacing(); } if (d->itemPositions[CenterPositioned] /*&& d->itemPositions[CenterPositioned]->isVisible()*/) { @@ -169,7 +152,7 @@ void BorderLayout::addItem(Plasma::LayoutItem * item, Position position) removeItem(item); d->itemPositions[position] = item; item->setManagingLayout(this); - update(); + updateGeometry(); } void BorderLayout::removeItem(LayoutItem * item) @@ -182,7 +165,7 @@ void BorderLayout::removeItem(LayoutItem * item) item->unsetManagingLayout(this); } } - update(); + updateGeometry(); } int BorderLayout::count() const @@ -236,13 +219,13 @@ Plasma::LayoutItem * BorderLayout::takeAt(int i) void BorderLayout::setSize(qreal size, Position border) { d->sizes[border] = size; - update(); + updateGeometry(); } void BorderLayout::setAutoSize(Position border) { d->sizes[border] = -1; - update(); + updateGeometry(); } qreal BorderLayout::size(Position border) diff --git a/layouts/borderlayout.h b/layouts/borderlayout.h index b3ccdb355..f6fd4e6ae 100644 --- a/layouts/borderlayout.h +++ b/layouts/borderlayout.h @@ -41,10 +41,6 @@ public: virtual ~BorderLayout(); virtual Qt::Orientations expandingDirections() const; - virtual QRectF geometry() const; - void setGeometry(const QRectF& geometry); - - void invalidate(); QSizeF sizeHint() const; @@ -88,6 +84,9 @@ public: */ qreal size(Position border); +protected: + void relayout(); + private: class Private; Private * const d; diff --git a/layouts/boxlayout.cpp b/layouts/boxlayout.cpp index 4eb74c376..bfb872a78 100644 --- a/layouts/boxlayout.cpp +++ b/layouts/boxlayout.cpp @@ -36,7 +36,6 @@ class BoxLayout::Private public: BoxLayout *const q; Direction direction; - QRectF geometry; QList children; Private(BoxLayout *parent) @@ -255,7 +254,7 @@ BoxLayout::BoxLayout(Direction direction , LayoutItem *parent) void BoxLayout::setDirection(Direction direction) { d->direction = direction; - update(); + updateGeometry(); } BoxLayout::Direction BoxLayout::direction() const { @@ -285,11 +284,6 @@ Qt::Orientations BoxLayout::expandingDirections() const } } -QRectF BoxLayout::geometry() const -{ - return d->geometry; -} - int BoxLayout::count() const { return d->children.count(); @@ -313,7 +307,7 @@ void BoxLayout::insertItem(int index, LayoutItem *item) animator()->setCurrentState(item,LayoutAnimator::InsertedState); } - update(); + updateGeometry(); } void BoxLayout::addItem(LayoutItem *item) @@ -333,7 +327,7 @@ void BoxLayout::removeItem(LayoutItem *item) if ( animator() ) animator()->setCurrentState(item,LayoutAnimator::RemovedState); - update(); + updateGeometry(); } int BoxLayout::indexOf(LayoutItem *l) const @@ -350,12 +344,12 @@ LayoutItem *BoxLayout::takeAt(int i) { return d->children.takeAt(i); - update(); + updateGeometry(); } -void BoxLayout::setGeometry(const QRectF& geo) +void BoxLayout::relayout() { - QRectF margined = geo.adjusted(margin(LeftMargin), margin(TopMargin), -margin(RightMargin), -margin(BottomMargin)); + QRectF margined = geometry().adjusted(margin(LeftMargin), margin(TopMargin), -margin(RightMargin), -margin(BottomMargin)); //qDebug() << "geo before " << geo << "and with margins" << margined << "margins" << margin(LeftMargin) // << margin(TopMargin) << -margin(RightMargin) << -margin(BottomMargin); @@ -426,7 +420,7 @@ void BoxLayout::setGeometry(const QRectF& geo) } // set items' geometry according to new sizes - qreal pos = d->startPos(geo); + qreal pos = d->startPos(geometry()); for ( int i = 0 ; i < sizes.count() ; i++ ) { //QObject *obj = dynamic_cast(d->children[i]); @@ -436,7 +430,6 @@ void BoxLayout::setGeometry(const QRectF& geo) pos = d->layoutItem(margined, d->children[i], pos , sizes[i]); } - d->geometry = geo; startAnimation(); } diff --git a/layouts/boxlayout.h b/layouts/boxlayout.h index bc35eb916..b6aab5016 100644 --- a/layouts/boxlayout.h +++ b/layouts/boxlayout.h @@ -74,14 +74,15 @@ class PLASMA_EXPORT BoxLayout : public Layout virtual LayoutItem *itemAt(int i) const; virtual LayoutItem *takeAt(int i); virtual Qt::Orientations expandingDirections() const; - virtual QRectF geometry() const; - virtual void setGeometry(const QRectF& geometry); virtual int count() const; virtual QSizeF minimumSize() const; virtual QSizeF maximumSize() const; virtual QSizeF sizeHint() const; + protected: + void relayout(); + private: class Private; Private *const d; diff --git a/layouts/flowlayout.cpp b/layouts/flowlayout.cpp index a9223dc02..afb5e35f1 100644 --- a/layouts/flowlayout.cpp +++ b/layouts/flowlayout.cpp @@ -36,7 +36,6 @@ class FlowLayout::Private public: Private() : columnWidth( -1 ) {} QList items; - QRectF geometry; qreal columnWidth; }; @@ -103,11 +102,6 @@ LayoutItem* FlowLayout::takeAt(int i) return d->items.takeAt(i); } -QRectF FlowLayout::geometry() const -{ - return d->geometry; -} - template T qSum(const QList& container) { @@ -118,11 +112,11 @@ T qSum(const QList& container) return total; } -void FlowLayout::setGeometry(const QRectF& geo) +void FlowLayout::relayout() { - QRectF geometry = geo.adjusted(margin(LeftMargin), margin(TopMargin), -margin(RightMargin), -margin(BottomMargin)); + QRectF rect = geometry().adjusted(margin(LeftMargin), margin(TopMargin), -margin(RightMargin), -margin(BottomMargin)); - qDebug() << "Flow layout geometry set to " << geo; + qDebug() << "Flow layout geometry set to " << geometry(); // calculate average size of items qreal totalWidth = 0; @@ -138,12 +132,13 @@ void FlowLayout::setGeometry(const QRectF& geo) // average width, this provides the spacing between the items and // also allows some tolerance for small differences in item widths qreal averageWidth; - if( d->columnWidth == -1 ) - averageWidth = totalWidth / d->items.count() + 2*spacing(); - else + if (d->columnWidth == -1) { + averageWidth = totalWidth / d->items.count() + 2*spacing(); + } else { averageWidth = d->columnWidth; - - const int columnCount = (int)(geometry.width() / averageWidth); + } + + const int columnCount = (int)(rect.width() / averageWidth); int insertColumn = 0; qreal rowPos = 0; @@ -183,17 +178,16 @@ void FlowLayout::setGeometry(const QRectF& geo) // try to restrict the item width to the available geometry's // width - if ( itemWidth > geometry.width() ) { - itemWidth = qMax(geometry.width(),item->minimumSize().width()); + if ( itemWidth > rect.width() ) { + itemWidth = qMax(rect.width(),item->minimumSize().width()); offset = 0; } // position the item - const QRectF newGeometry( geometry.left() + - insertColumn * averageWidth + offset, - geometry.top() + rowPos , - itemWidth , - itemSize.height() ); + const QRectF newGeometry(rect.left() + insertColumn * averageWidth + offset, + rect.top() + rowPos, + itemWidth, + itemSize.height()); rowHeight = qMax(rowHeight,itemSize.height()); insertColumn += columnSpan; @@ -204,8 +198,6 @@ void FlowLayout::setGeometry(const QRectF& geo) item->setGeometry( newGeometry ); } - d->geometry = geo; - startAnimation(); } diff --git a/layouts/flowlayout.h b/layouts/flowlayout.h index 4a6fbc473..0507960dc 100644 --- a/layouts/flowlayout.h +++ b/layouts/flowlayout.h @@ -20,14 +20,11 @@ #define __FLOWLAYOUT__ #include - -#include "layout.h" +#include namespace Plasma { -class LayoutItem; - /** * A layout which lays items out left-to-right , top-to-bottom. * @@ -49,12 +46,13 @@ public: virtual LayoutItem* takeAt(int i); virtual QSizeF sizeHint() const; - virtual QRectF geometry() const; - virtual void setGeometry(const QRectF& geometry); virtual Qt::Orientations expandingDirections() const; virtual void setColumnWidth( const qreal width ); virtual qreal columnWidth() const; +protected: + void relayout(); + private: class Private; Private *const d; diff --git a/layouts/freelayout.cpp b/layouts/freelayout.cpp index 0e4f8557f..23a3ec70c 100644 --- a/layouts/freelayout.cpp +++ b/layouts/freelayout.cpp @@ -85,7 +85,7 @@ LayoutItem * FreeLayout::takeAt(int i) return d->children.takeAt(i); } -void FreeLayout::setGeometry(const QRectF &) +void FreeLayout::relayout() { foreach (LayoutItem *child , d->children) { if (child->geometry().size() != child->sizeHint()) { diff --git a/layouts/freelayout.h b/layouts/freelayout.h index b24300c55..1746a32d0 100644 --- a/layouts/freelayout.h +++ b/layouts/freelayout.h @@ -54,11 +54,13 @@ class PLASMA_EXPORT FreeLayout : public Layout virtual LayoutItem *takeAt(int i); virtual Qt::Orientations expandingDirections() const; virtual QRectF geometry() const; - virtual void setGeometry(const QRectF& geometry); virtual int count() const; virtual QSizeF sizeHint() const; + protected: + void relayout(); + private: class Private; Private *const d; diff --git a/layouts/layout.cpp b/layouts/layout.cpp index 0766da9a5..6c7882b2a 100644 --- a/layouts/layout.cpp +++ b/layouts/layout.cpp @@ -59,6 +59,7 @@ class Layout::Private LayoutAnimator *animator; bool relayouting; + QRectF geometry; }; @@ -88,9 +89,24 @@ bool Layout::isEmpty() const return count() == 0; } -void Layout::update() +void Layout::updateGeometry() { - setGeometry(geometry()); + relayout(); +} + +QRectF Layout::geometry() const +{ + return d->geometry; +} + +void Layout::setGeometry(const QRectF &geometry) +{ + if (!geometry.isValid() || geometry == d->geometry) { + return; + } + + d->geometry = geometry; + invalidate(); } void Layout::invalidate() @@ -111,7 +127,7 @@ void Layout::invalidate() } } while (parentLayout); - layout->update(); + layout->relayout(); d->relayouting = false; } diff --git a/layouts/layout.h b/layouts/layout.h index 5baa6b2fa..28b662580 100644 --- a/layouts/layout.h +++ b/layouts/layout.h @@ -143,8 +143,18 @@ class PLASMA_EXPORT Layout : public LayoutItem */ virtual void setAnimator( LayoutAnimator* animator ); + /** + * Returns the current geometry for this layout + */ + virtual QRectF geometry() const; + + /** + * Changes the geometry of this layout + */ + void setGeometry(const QRectF &geometry); + /** Triggers an update of the layout. */ - void update(); + void updateGeometry(); /** * Returns the minimum size of this layout. @@ -161,9 +171,14 @@ class PLASMA_EXPORT Layout : public LayoutItem void invalidate(); protected: + /** + * Triggers a layout, usually after a change in geometry + */ + virtual void relayout() = 0; + /** * Starts a layout animation. Subclasses may call this - * at the end of their setGeometry() implementation to + * at the end of their relayout() implementation to * start the timeline associated with the layout's animator() * if there is one. If an animation is already in progress then * the timeline is reset to 0ms and the animation continues. diff --git a/layouts/layoutitem.h b/layouts/layoutitem.h index 74645b297..761f1c2e0 100644 --- a/layouts/layoutitem.h +++ b/layouts/layoutitem.h @@ -104,6 +104,13 @@ class PLASMA_EXPORT LayoutItem */ virtual void setGeometry(const QRectF& geometry) = 0; + /** + * Updates the layouting of the item without first changing its geometry. + * Calling this may result in a geometry change, but may not, depending + * on the managing layout if any. + */ + virtual void updateGeometry() = 0; + /** * Returns the most appropriate size of this Item to hold whatever contents it has. */ diff --git a/layouts/nodelayout.cpp b/layouts/nodelayout.cpp index 358f32748..cc2605bfe 100644 --- a/layouts/nodelayout.cpp +++ b/layouts/nodelayout.cpp @@ -114,15 +114,6 @@ public: return result; } - void invalidate() - { - foreach (LayoutItem * item, items.keys()) { - if (item) { - item->setGeometry(calculateRectangle(item)); - } - } - } - void calculateSizeHint(LayoutItem * item = NULL) { if (item == NULL) { // Recalculate the sizeHint using all items @@ -167,19 +158,13 @@ Qt::Orientations NodeLayout::expandingDirections() const return Qt::Horizontal | Qt::Vertical; } -QRectF NodeLayout::geometry() const +void NodeLayout::relayout() { - return d->geometry; -} - -void NodeLayout::setGeometry(const QRectF& geometry) -{ - if (!geometry.isValid() || geometry.isEmpty()) { - return; + foreach (LayoutItem * item, d->items.keys()) { + if (item) { + item->setGeometry(d->calculateRectangle(item)); + } } - - d->geometry = geometry; - d->invalidate(); } QSizeF NodeLayout::sizeHint() const diff --git a/layouts/nodelayout.h b/layouts/nodelayout.h index 7c6fc5049..ac91dad90 100644 --- a/layouts/nodelayout.h +++ b/layouts/nodelayout.h @@ -76,9 +76,6 @@ public: explicit NodeLayout(LayoutItem * parent = 0); virtual ~NodeLayout(); - virtual QRectF geometry() const; - void setGeometry(const QRectF& geometry); - QSizeF sizeHint() const; /** @@ -109,6 +106,9 @@ public: virtual LayoutItem * itemAt(int i) const; virtual LayoutItem * takeAt(int i); +protected: + void relayout(); + private: class Private; Private * const d; diff --git a/uiloader.cpp b/uiloader.cpp index acae5d1f3..de15abd89 100644 --- a/uiloader.cpp +++ b/uiloader.cpp @@ -21,18 +21,17 @@ #include -#include "widgets/checkbox.h" -#include "widgets/flash.h" -#include "widgets/icon.h" -#include "widgets/label.h" -#include "widgets/pushbutton.h" -#include "widgets/radiobutton.h" -#include "widgets/meter.h" -//#include "widgets/rectangle.h" +#include "plasma/widgets/checkbox.h" +#include "plasma/widgets/flash.h" +#include "plasma/widgets/icon.h" +#include "plasma/widgets/label.h" +#include "plasma/widgets/pushbutton.h" +#include "plasma/widgets/radiobutton.h" +#include "plasma/widgets/meter.h" -#include "layouts/hboxlayout.h" -#include "layouts/vboxlayout.h" -#include "layouts/flowlayout.h" +#include "plasma/layouts/hboxlayout.h" +#include "plasma/layouts/vboxlayout.h" +#include "plasma/layouts/flowlayout.h" namespace Plasma { diff --git a/widgets/lineedit.cpp b/widgets/lineedit.cpp index 068b9d93c..88e5fb686 100644 --- a/widgets/lineedit.cpp +++ b/widgets/lineedit.cpp @@ -27,6 +27,8 @@ #include +#include "plasma/layouts/layout.h" + namespace Plasma { @@ -184,6 +186,15 @@ void LineEdit::setGeometry(const QRectF& geometry) update(); } +void LineEdit::updateGeometry() +{ + if (managingLayout()) { + managingLayout()->invalidate(); + } else { + setGeometry(QRectF(pos(), sizeHint())); + } +} + QSizeF LineEdit::sizeHint() const { return document()->size(); diff --git a/widgets/lineedit.h b/widgets/lineedit.h index 32838d7bd..39ec6c813 100644 --- a/widgets/lineedit.h +++ b/widgets/lineedit.h @@ -71,6 +71,7 @@ class PLASMA_EXPORT LineEdit : public QGraphicsTextItem, public LayoutItem QRectF geometry() const; void setGeometry(const QRectF& geometry); + void updateGeometry(); QSizeF sizeHint() const; void setDefaultText(const QString &text); diff --git a/widgets/tests/testLayouts.cpp b/widgets/tests/testLayouts.cpp index da4d1a2f8..c7c9cfbb7 100644 --- a/widgets/tests/testLayouts.cpp +++ b/widgets/tests/testLayouts.cpp @@ -8,11 +8,11 @@ #include #include -#include "../pushbutton.h" -#include "../lineedit.h" -#include "../../layouts/boxlayout.h" -#include "../widget.h" -#include "../label.h" +#include "plasma/layouts/boxlayout.h" +#include "plasma/widgets/pushbutton.h" +#include "plasma/widgets/lineedit.h" +#include "plasma/widgets/widget.h" +#include "plasma/widgets/label.h" int main(int argc, char **argv) { @@ -62,7 +62,7 @@ int main(int argc, char **argv) h2->addItem(labelTwo); scene.addItem(labelTwo); - widgetLayout->update(); + widgetLayout->updateGeometry(); view.show(); return app.exec(); diff --git a/widgets/tests/testProgressBar.cpp b/widgets/tests/testProgressBar.cpp index 5bbeb8e44..0f1893646 100644 --- a/widgets/tests/testProgressBar.cpp +++ b/widgets/tests/testProgressBar.cpp @@ -10,9 +10,8 @@ #include -#include "../../layouts/boxlayout.h" -#include "../widget.h" -#include "../progressbar.h" +#include "plasma/layouts/boxlayout.h" +#include "plasma/widgets/progressbar.h" class Counter : QObject { @@ -89,7 +88,7 @@ int main(int argc, char **argv) h1->addItem(progressBar); scene.addItem(progressBar); - widgetLayout->update(); + widgetLayout->updateGeometry(); view.show();