From 0006ce0ec435893a4fc44191ab63308f420febe7 Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Mon, 19 Nov 2007 03:30:24 +0000 Subject: [PATCH] introduce per-side margins to layouts. you can still just call setMargin(qreal) if you wish, but now this is much more useful for, say, panels. svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=738526 --- widgets/borderlayout.cpp | 6 ++--- widgets/boxlayout.cpp | 31 +++++++++++++------------ widgets/flowlayout.cpp | 5 ++-- widgets/layout.cpp | 50 ++++++++++++++++++++++++++++++++++++---- widgets/layout.h | 8 ++++++- 5 files changed, 73 insertions(+), 27 deletions(-) diff --git a/widgets/borderlayout.cpp b/widgets/borderlayout.cpp index c40fa5b66..b9234f9db 100644 --- a/widgets/borderlayout.cpp +++ b/widgets/borderlayout.cpp @@ -84,8 +84,8 @@ void BorderLayout::setGeometry(const QRectF& geometry) void BorderLayout::invalidate() { QRectF geometry = d->geometry; - geometry.setTopLeft(geometry.topLeft() + QPointF(margin(), margin())); - geometry.setBottomRight(geometry.bottomRight() - QPointF(margin(), margin())); + geometry.setTopLeft(geometry.topLeft() + QPointF(margin(LeftMargin), margin(TopMargin))); + geometry.setBottomRight(geometry.bottomRight() - QPointF(margin(RightMargin), margin(BottomMargin))); QPointF origin = geometry.topLeft(); qreal top, bottom, left, right; @@ -157,7 +157,7 @@ QSizeF BorderLayout::sizeHint() const hintWidth += d->itemPositions[CenterPositioned]->sizeHint().width(); } - return QSizeF(hintWidth + 2 * margin(), hintHeight + 2 * margin()); + return QSizeF(hintWidth + 2 + margin(LeftMargin) + margin(RightMargin), hintHeight + 2 + margin(TopMargin) + margin(BottomMargin)); } void BorderLayout::addItem(Plasma::LayoutItem * item) diff --git a/widgets/boxlayout.cpp b/widgets/boxlayout.cpp index dd664dba2..c1e60ccc2 100644 --- a/widgets/boxlayout.cpp +++ b/widgets/boxlayout.cpp @@ -85,12 +85,13 @@ public: { switch ( direction ) { case LeftToRight: + return q->margin(LeftMargin); case TopToBottom: - return q->margin(); + return q->margin(TopMargin); case RightToLeft: - return geometry.width() - q->margin(); + return geometry.width() - q->margin(RightMargin); case BottomToTop: - return geometry.height() - q->margin(); + return geometry.height() - q->margin(BottomMargin); default: Q_ASSERT(false); return 0; @@ -155,7 +156,7 @@ public: // qDebug() << "Item geometry: " << newGeometry; if ( q->animator() ) - q->animator()->setGeometry(item,newGeometry); + q->animator()->setGeometry(item, newGeometry); else item->setGeometry(newGeometry); @@ -216,7 +217,6 @@ public: { QSizeF result; - const qreal totalMargin = q->margin() * 2; const qreal totalSpacing = q->spacing() * (children.count()-1); switch ( direction ) { @@ -225,8 +225,8 @@ public: result = QSizeF(calculateSize(calculateSizeType,Qt::Horizontal,sum), calculateSize(calculateSizeType,Qt::Vertical,qMax)); - result.rwidth() += totalMargin + totalSpacing; - result.rheight() += totalMargin; + result.rwidth() += q->margin(LeftMargin) + q->margin(RightMargin) + totalSpacing; + result.rheight() += q->margin(TopMargin) + q->margin(BottomMargin); break; case TopToBottom: @@ -234,8 +234,8 @@ public: result = QSizeF(calculateSize(calculateSizeType,Qt::Horizontal,qMax), calculateSize(calculateSizeType,Qt::Vertical,sum)); - result.rheight() += totalMargin + totalSpacing; - result.rwidth() += totalMargin; + result.rheight() += q->margin(TopMargin) + q->margin(BottomMargin) + totalSpacing; + result.rwidth() += q->margin(LeftMargin) + q->margin(RightMargin); break; } @@ -353,16 +353,17 @@ LayoutItem *BoxLayout::takeAt(int i) void BoxLayout::setGeometry(const QRectF& geo) { - QRectF geometry = geo.adjusted(margin(),margin(),-margin(),-margin()); + QRectF margined = geo.adjusted(margin(LeftMargin), margin(TopMargin), -margin(RightMargin), -margin(BottomMargin)); - //qDebug() << "geo before " << geo << "after" << geometry << "margin" << margin(); + //qDebug() << "geo before " << geo << "and with margins" << margined << "margins" << margin(LeftMargin) + // << margin(TopMargin) << -margin(RightMargin) << -margin(BottomMargin); //qDebug() << "Box layout beginning with geo" << geometry; //qDebug() << "This box max size" << maximumSize(); QVector sizes(count()); QVector expansionSpace(count()); - qreal available = d->size(geometry.size()) - spacing()*(d->children.count()-1); + qreal available = d->size(margined.size()) - spacing() * (d->children.count()-1); qreal perItemSize = available / count(); // initial distribution of space to items @@ -423,17 +424,17 @@ void BoxLayout::setGeometry(const QRectF& geo) } // set items' geometry according to new sizes - qreal pos = d->startPos(geometry); + qreal pos = d->startPos(geo); for ( int i = 0 ; i < sizes.count() ; i++ ) { //QObject *obj = dynamic_cast(d->children[i]); //if ( obj ) //qDebug() << "Item " << i << obj->metaObject()->className() << "size:" << sizes[i]; - pos = d->layoutItem( geometry , d->children[i] , pos , sizes[i] ); + pos = d->layoutItem(margined, d->children[i], pos , sizes[i]); } - d->geometry = geometry; + d->geometry = geo; startAnimation(); } diff --git a/widgets/flowlayout.cpp b/widgets/flowlayout.cpp index 7552e8532..6bf129379 100644 --- a/widgets/flowlayout.cpp +++ b/widgets/flowlayout.cpp @@ -112,9 +112,8 @@ T qSum(const QList& container) void FlowLayout::setGeometry(const QRectF& geo) { - QRectF geometry(geo); - geometry.adjust(margin(),margin(),-margin(),-margin()); - + QRectF geometry = geo.adjusted(margin(LeftMargin), margin(TopMargin), -margin(RightMargin), -margin(BottomMargin)); + qDebug() << "Flow layout geometry set to " << geo; // calculate average size of items diff --git a/widgets/layout.cpp b/widgets/layout.cpp index 2ad46063b..096258a76 100644 --- a/widgets/layout.cpp +++ b/widgets/layout.cpp @@ -36,7 +36,10 @@ class Layout::Private { public: Private(LayoutItem* p) - : margin(12.0), + : leftMargin(12.0), + rightMargin(12.0), + topMargin(12.0), + bottomMargin(12.0), spacing(6.0), parent(p), animator(0) @@ -45,7 +48,10 @@ class Layout::Private ~Private() {} - qreal margin; + qreal leftMargin; + qreal rightMargin; + qreal topMargin; + qreal bottomMargin; qreal spacing; LayoutItem *parent; @@ -111,14 +117,48 @@ void Layout::setAnimator(LayoutAnimator *animator) d->animator = animator; } -qreal Layout::margin() const +qreal Layout::margin(MarginEdge edge) const { - return d->margin; + switch (edge) { + case LeftMargin: + return d->leftMargin; + break; + case RightMargin: + return d->rightMargin; + break; + case TopMargin: + return d->topMargin; + break; + case BottomMargin: + return d->bottomMargin; + break; + } +} + +void Layout::setMargin(MarginEdge edge, qreal m) +{ + switch (edge) { + case LeftMargin: + d->leftMargin = m; + break; + case RightMargin: + d->rightMargin = m; + break; + case TopMargin: + d->topMargin = m; + break; + case BottomMargin: + d->bottomMargin = m; + break; + } } void Layout::setMargin(qreal m) { - d->margin = m; + d->leftMargin = m; + d->rightMargin = m; + d->topMargin = m; + d->bottomMargin = m; } qreal Layout::spacing() const diff --git a/widgets/layout.h b/widgets/layout.h index e59430b77..6b154fa2d 100644 --- a/widgets/layout.h +++ b/widgets/layout.h @@ -44,6 +44,7 @@ class LayoutAnimator; class PLASMA_EXPORT Layout : public LayoutItem { public: + enum MarginEdge { TopMargin, BottomMargin, LeftMargin, RightMargin }; /** * Constructor. @@ -58,11 +59,16 @@ class PLASMA_EXPORT Layout : public LayoutItem /** * Returns the margin of this Layout. */ - qreal margin() const; + qreal margin(MarginEdge edge) const; /** * Sets the margin of this Layout. */ + void setMargin(MarginEdge edge, qreal m); + + /** + * Sets all the margins of this Layout. + */ void setMargin(qreal m); /**