* moves the idea of margins up to LayoutItem, allowing any LayoutItem to have a margin

* moves the idea of the size of the item up to LayoutItem, as both Widget and Layout implemented it separately
* provides a way in LayoutItem to adjust a rect within an item to its margins (that is what LayoutItem::topLeft() is there for)
* fix several bugs, both painting and sizing, in LineEdit
* Applet sets margins now based on the existence of setDrawBackground


svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=776380
This commit is contained in:
Aaron J. Seigo 2008-02-17 23:02:46 +00:00
parent b01fa6947c
commit 777b8083f8
10 changed files with 238 additions and 112 deletions

View File

@ -601,11 +601,13 @@ void Applet::setDrawStandardBackground(bool drawBackground)
int left, top, right, bottom;
d->getBorderSize(left, top, right, bottom);
setMargins(0, 0, right, bottom);
updateGeometry();
}
} else if (d->background) {
delete d->background;
d->background = 0;
setMargins(0, 0, 0, 0);
updateGeometry();
}
}
@ -648,7 +650,7 @@ void Applet::setFailedToLaunch(bool failed, const QString& reason)
if (failed) {
setDrawStandardBackground(true);
Layout* failureLayout = new BoxLayout(BoxLayout::TopToBottom, this);
failureLayout->setMargin(0);
failureLayout->setMargins(0, 0, 0, 0);
d->failureText = new LineEdit(this);
d->failureText->setTextInteractionFlags( Qt::TextSelectableByMouse );
d->failureText->setStyled(false);
@ -787,7 +789,7 @@ QRectF Applet::boundingRect() const
int top;
int bottom;
d->getBorderSize(left,top,right,bottom);
d->getBorderSize(left, top, right, bottom);
//kDebug() << "Background , Border size" << d->background << left << top << right << bottom;
return rect.adjusted(-left, -top, right, bottom);
}

View File

@ -155,12 +155,18 @@ public:
height = (geometry.height() - q->spacing() * (rowCount - 1)) / rowCount;
top = geometry.top() + row * (height + q->spacing());
height = qBound(minSize.height(), height, maxSize.height());
if (size > geometry.width()) {
size = geometry.width();
}
break;
case TopToBottom:
case BottomToTop:
height = (geometry.width() - q->spacing() * (rowCount - 1)) / rowCount;
top = geometry.left() + row * (height + q->spacing());
height = qBound(minSize.width(), height, maxSize.width());
if (size > geometry.height()) {
size = geometry.height();
}
break;
}

View File

@ -140,8 +140,7 @@ T qSum(const QList<T>& container)
void FlowLayout::relayout()
{
const QRectF rect = geometry().adjusted(margin(LeftMargin), margin(TopMargin),
-margin(RightMargin), -margin(BottomMargin));
const QRectF rect = adjustToMargins(geometry());
const qreal space = spacing();
const qreal rectWidth = rect.width();

View File

@ -27,6 +27,8 @@
#include <QtCore/QTimeLine>
#include <QtDebug>
#include <KDebug>
#include "widgets/widget.h"
#include "layouts/layoutanimator.h"
@ -37,11 +39,7 @@ class Layout::Private
{
public:
Private()
: leftMargin(12.0),
rightMargin(12.0),
topMargin(12.0),
bottomMargin(12.0),
spacing(6.0),
: spacing(6.0),
parent(0),
animator(0),
relayouting(false)
@ -50,17 +48,13 @@ class Layout::Private
~Private() {}
qreal leftMargin;
qreal rightMargin;
qreal topMargin;
qreal bottomMargin;
qreal spacing;
LayoutItem *parent;
LayoutAnimator *animator;
bool relayouting;
QRectF geometry;
QPointF pos;
};
@ -68,6 +62,7 @@ Layout::Layout(LayoutItem *parent)
: LayoutItem(),
d(new Private)
{
setMargins(12, 12, 12, 12);
setParent(parent);
}
@ -117,16 +112,30 @@ void Layout::updateGeometry()
QRectF Layout::geometry() const
{
return d->geometry;
return QRectF(d->pos, size());
}
void Layout::setGeometry(const QRectF &geometry)
void Layout::setGeometry(const QRectF &geom)
{
if (!geometry.isValid() || geometry == d->geometry) {
if (!geom.isValid() || geom == geometry()) {
return;
}
d->geometry = geometry;
QRectF newGeom = geom;
if (d->parent && !dynamic_cast<Layout*>(d->parent)) {
newGeom = d->parent->adjustToMargins(newGeom);
//kDebug() << "parent rect is" << d->parent->topLeft() << d->parent->size()
// << "and we are" << geometry() << "but aiming for"
// << newGeom << "from" << geom;
}
d->pos = newGeom.topLeft();
setSize(newGeom.size());
// TODO: respect minimum and maximum sizes: is it possible?
//setSize(newGeom.size().expandedTo(minimumSize()).boundedTo(maximumSize()));
//kDebug() << "geometry is now" << geometry();
invalidate();
}
@ -166,52 +175,6 @@ void Layout::setAnimator(LayoutAnimator *animator)
d->animator = animator;
}
qreal Layout::margin(Plasma::MarginEdge edge) const
{
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;
}
return 0;
}
void Layout::setMargin(Plasma::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->leftMargin = m;
d->rightMargin = m;
d->topMargin = m;
d->bottomMargin = m;
}
qreal Layout::spacing() const
{
return d->spacing;

View File

@ -55,21 +55,6 @@ class PLASMA_EXPORT Layout : public LayoutItem
*/
virtual ~Layout();
/**
* Returns the margin of this Layout.
*/
qreal margin(Plasma::MarginEdge edge) const;
/**
* Sets the margin of this Layout.
*/
void setMargin(Plasma::MarginEdge edge, qreal m);
/**
* Sets all the margins of this Layout.
*/
void setMargin(qreal m);
/**
* Returns the spacing between Layout elements of this Layout.
*/

View File

@ -32,7 +32,11 @@ class LayoutItem::Private
public:
Private()
: layout(0),
managingLayout(0)
managingLayout(0),
leftMargin(0),
rightMargin(0),
topMargin(0),
bottomMargin(0)
{
}
@ -40,9 +44,13 @@ class LayoutItem::Private
Layout* layout;
Layout* managingLayout;
QSizeF size;
qreal leftMargin;
qreal rightMargin;
qreal topMargin;
qreal bottomMargin;
};
LayoutItem::LayoutItem()
: d(new Private())
{
@ -135,9 +143,109 @@ void LayoutItem::managingLayoutChanged()
{
}
QPointF LayoutItem::topLeft() const
{
return QPointF(0, 0);
}
Layout* LayoutItem::managingLayout() const
{
return d->managingLayout;
}
qreal LayoutItem::margin(Plasma::MarginEdge edge) const
{
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;
}
return 0;
}
QRectF LayoutItem::adjustToMargins(const QRectF &rect) const
{
QRectF r(rect);
if (r.x() <= d->leftMargin) {
r.setX(d->leftMargin);
}
if (r.y() <= d->topMargin) {
r.setY(d->topMargin);
}
QPointF tl = topLeft();
qreal maxWidth = d->size.width() + tl.x() - d->leftMargin - d->rightMargin;
if (r.width() > maxWidth) {
r.setWidth(maxWidth);
}
qreal maxHeight = d->size.height() + tl.y() - d->topMargin - d->bottomMargin;
if (r.height() > maxHeight) {
r.setHeight(maxHeight);
}
return r;
}
void LayoutItem::setMargin(Plasma::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;
}
if (d->layout) {
d->layout->setGeometry(d->layout->geometry());
}
}
void LayoutItem::setMargin(qreal m)
{
setMargins(m, m, m, m);
}
void LayoutItem::setMargins(qreal left, qreal top, qreal right, qreal bottom)
{
d->leftMargin = left;
d->rightMargin = right;
d->topMargin = top;
d->bottomMargin = bottom;
if (d->layout) {
d->layout->setGeometry(d->layout->geometry());
}
}
QSizeF LayoutItem::size() const
{
return d->size;
}
void LayoutItem::setSize(const QSizeF &size)
{
d->size = size;
}
}

View File

@ -25,6 +25,7 @@
#include <QtCore/QSizeF>
#include <plasma/plasma_export.h>
#include <plasma/plasma.h>
class QGraphicsItem;
@ -158,6 +159,41 @@ class PLASMA_EXPORT LayoutItem
**/
Layout* managingLayout() const;
/**
* Returns the margin of this Layout.
*/
qreal margin(Plasma::MarginEdge edge) const;
/**
* @return the rect adjust to the margins of this item
*/
QRectF adjustToMargins(const QRectF &rect) const;
/**
* Sets the margin of this Layout.
*/
void setMargin(Plasma::MarginEdge edge, qreal m);
/**
* Sets all the margins of this Layout.
*/
void setMargin(qreal m);
/**
* Sets all the margins of this Layout.
*/
void setMargins(qreal left, qreal top, qreal right, qreal bottom);
/**
* Sets the size of the layout item
*/
void setSize(const QSizeF &size);
/**
* @return the size of this item
*/
QSizeF size() const;
/**
* Returns the graphics item associated with this layout item or 0
* if there is no associated graphics item.
@ -166,6 +202,11 @@ class PLASMA_EXPORT LayoutItem
*/
virtual QGraphicsItem* graphicsItem();
/**
* Get the topLeft of the item in its coordinate space
*/
virtual QPointF topLeft() const;
protected:
/**
* Reimplement to respond to a change in managing layout

View File

@ -35,9 +35,13 @@ class AbstractRunner;
class PLASMA_EXPORT SearchMatch
{
public:
enum Type { InformationalMatch,
ExactMatch,
PossibleMatch };
/**
* The type of match. Value is important here as it is used for sorting
*/
enum Type { PossibleMatch = 0 /**< Something that may match the query */,
InformationalMatch = 50 /**< A purely informational, non-actionable match,
such as the answer to a question or calculation*/,
ExactMatch = 100 /**< An exact matcht to the query */};
SearchMatch(const SearchContext *search, AbstractRunner *runner);
~SearchMatch();

View File

@ -36,11 +36,13 @@
#include <KDebug>
#include "plasma/applet.h"
#include "layouts/freelayout.h"
#include "plasma/plasma.h"
#include "plasma/view.h"
#include "plasma/containment.h"
#include "tooltip_p.h"
#include "plasma/widgets/tooltip_p.h"
namespace Plasma
{
@ -62,7 +64,6 @@ class Widget::Private
delete toolTip;
}
QSizeF size;
QSizeF minimumSize;
QSizeF maximumSize;
@ -173,11 +174,12 @@ Qt::Orientations Widget::expandingDirections() const
return Qt::Horizontal | Qt::Vertical;
}
void Widget::setMinimumSize(const QSizeF& size)
void Widget::setMinimumSize(const QSizeF& newMin)
{
d->minimumSize = size;
if (d->size != d->size.expandedTo(size)) {
setGeometry(QRectF(pos(), d->size.expandedTo(size)));
d->minimumSize = newMin;
QSizeF s = size();
if (s != s.expandedTo(newMin)) {
setGeometry(QRectF(pos(), s.expandedTo(newMin)));
}
}
@ -186,11 +188,12 @@ QSizeF Widget::minimumSize() const
return d->minimumSize;
}
void Widget::setMaximumSize(const QSizeF& size)
void Widget::setMaximumSize(const QSizeF& newMax)
{
d->maximumSize = size;
if (d->size != d->size.boundedTo(size)) {
setGeometry(QRectF(pos(), d->size.boundedTo(size)));
d->maximumSize = newMax;
QSizeF s = size();
if (s != s.boundedTo(newMax)) {
setGeometry(QRectF(pos(), s.boundedTo(newMax)));
}
}
@ -225,20 +228,45 @@ qreal Widget::widthForHeight(qreal h) const
QRectF Widget::geometry() const
{
return QRectF(pos(), d->size);
return QRectF(pos(), size());
}
void Widget::setSize(const QSizeF &s)
{
LayoutItem::setSize(s);
}
void Widget::setGeometry(const QRectF& geometry)
{
if (geometry.size().width() > 0 && geometry.size().height() > 0 && d->size != geometry.size()) {
setPos(geometry.topLeft());
if (geometry.size().width() > 0 && geometry.size().height() > 0 && size() != geometry.size()) {
prepareGeometryChange();
qreal width = qBound(d->minimumSize.width(), geometry.size().width(), d->maximumSize.width());
qreal height = qBound(d->minimumSize.height(), geometry.size().height(), d->maximumSize.height());
d->size = QSizeF(width, height);
setSize(QSizeF(width, height));
qreal xd = topLeft().x();
qreal yd = topLeft().y();
if (xd < 0) {
width -= xd;
xd = 0;
}
if (yd < 0) {
height -= yd;
yd = 0;
}
if (layout()) {
layout()->setGeometry(QRectF(QPointF(0, 0), d->size));
QRectF r(QPointF(xd, yd), QSizeF(width, height));
r = adjustToMargins(r);
layout()->setGeometry(r);
/*if (qobject_cast<Plasma::Applet*>(this)) {
kDebug() << (QObject*)this << this->geometry() << this->topLeft()
<< "layout geometry is now" << r << margin(RightMargin);
}*/
}
if (managingLayout()) {
@ -246,7 +274,6 @@ void Widget::setGeometry(const QRectF& geometry)
}
}
setPos(geometry.topLeft());
update();
}
@ -264,15 +291,10 @@ QSizeF Widget::sizeHint() const
if (layout()) {
return layout()->sizeHint();
} else {
return d->size;
return size();
}
}
QSizeF Widget::size() const
{
return d->size;
}
QFont Widget::font() const
{
return QApplication::font();
@ -280,7 +302,7 @@ QFont Widget::font() const
QRectF Widget::boundingRect() const
{
return QRectF(QPointF(0,0), d->size);
return QRectF(QPointF(0,0), size());
}
void Widget::resize(const QSizeF& size)

View File

@ -182,11 +182,6 @@ public:
*/
virtual QSizeF sizeHint() const;
/**
* @return the size of this Plasma::Widget
*/
QSizeF size() const;
/**
* @return the font currently set for this widget
**/
@ -308,11 +303,12 @@ protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value);
void managingLayoutChanged();
virtual bool sceneEvent(QEvent *event);
private:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
void setSize(const QSizeF &);
class Private;
Private *const d;