* 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
This commit is contained in:
Aaron J. Seigo 2007-11-19 23:45:56 +00:00
parent ff0b3da511
commit e614df56d0
19 changed files with 136 additions and 135 deletions

View File

@ -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();
}
}

View File

@ -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)

View File

@ -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;

View File

@ -36,7 +36,6 @@ class BoxLayout::Private
public:
BoxLayout *const q;
Direction direction;
QRectF geometry;
QList<LayoutItem*> 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<QObject*>(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();
}

View File

@ -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;

View File

@ -36,7 +36,6 @@ class FlowLayout::Private
public:
Private() : columnWidth( -1 ) {}
QList<LayoutItem*> 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 <class T>
T qSum(const QList<T>& container)
{
@ -118,11 +112,11 @@ T qSum(const QList<T>& 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();
}

View File

@ -20,14 +20,11 @@
#define __FLOWLAYOUT__
#include <plasma/plasma_export.h>
#include "layout.h"
#include <plasma/layouts/layout.h>
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;

View File

@ -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()) {

View File

@ -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;

View File

@ -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;
}

View File

@ -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.

View File

@ -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.
*/

View File

@ -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

View File

@ -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;

View File

@ -21,18 +21,17 @@
#include <QStringList>
#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
{

View File

@ -27,6 +27,8 @@
#include <KDebug>
#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();

View File

@ -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);

View File

@ -8,11 +8,11 @@
#include <KAboutData>
#include <KIcon>
#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();

View File

@ -10,9 +10,8 @@
#include <KIcon>
#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();