Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / libs / plasma / layouts / freelayout.cpp
blob4b69430ec8535bd61b504c6bcba8f9a7293e640e
1 /*
2 * Copyright 2007 by Robert Knight <robertknight@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details
15 * You should have received a copy of the GNU Library General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "freelayout.h"
23 #include <KDebug>
25 namespace Plasma
28 class FreeLayout::Private
30 public:
31 QList<QGraphicsLayoutItem*> children;
34 FreeLayout::FreeLayout(QGraphicsLayoutItem *parent)
35 : QGraphicsLayout(parent),
36 d(new Private)
38 setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding,QSizePolicy::DefaultType);
41 FreeLayout::~FreeLayout()
43 delete d;
46 void FreeLayout::addItem(QGraphicsLayoutItem *item)
48 if (d->children.contains(item)) {
49 return;
52 d->children << item;
55 void FreeLayout::removeItem(QGraphicsLayoutItem *item)
57 if (!item) {
58 return;
61 d->children.removeAll(item);
64 int FreeLayout::indexOf(QGraphicsLayoutItem *item) const
66 return d->children.indexOf(item);
69 QGraphicsLayoutItem * FreeLayout::itemAt(int i) const
71 return d->children[i];
74 int FreeLayout::count() const
76 return d->children.count();
79 QGraphicsLayoutItem * FreeLayout::takeAt(int i)
81 return d->children.takeAt(i);
84 void FreeLayout::removeAt(int i)
86 d->children.removeAt(i);
89 void FreeLayout::relayout()
91 foreach (QGraphicsLayoutItem *child , d->children) {
92 if (child->geometry().size() != child->effectiveSizeHint(Qt::PreferredSize)) {
93 const QSizeF newSize = child->effectiveSizeHint(Qt::PreferredSize).expandedTo(minimumSize()).boundedTo(maximumSize());
94 child->setGeometry(QRectF(child->geometry().topLeft(), newSize));
99 QRectF FreeLayout::geometry() const
101 if (parentLayoutItem()) {
102 return parentLayoutItem()->geometry();
105 return QRectF(QPointF(0, 0), maximumSize());
108 void FreeLayout::setGeometry(const QRectF &geom)
110 if (!geom.isValid() || geom == geometry()) {
111 return;
114 // QRectF newGeom = geom;
116 // if (d->parent && !dynamic_cast<QGraphicsLayout*>(d->parent)) {
117 // newGeom = d->parent->adjustToMargins(newGeom);
118 // //kDebug() << "parent rect is" << d->parent->topLeft() << d->parent->size()
119 // // << "and we are" << geometry() << "but aiming for"
120 // // << newGeom << "from" << geom;
121 // }
123 // d->pos = newGeom.topLeft();
124 setPreferredSize(geom.size());
125 // TODO: respect minimum and maximum sizes: is it possible?
126 //setSize(newGeom.size().expandedTo(minimumSize()).boundedTo(maximumSize()));
128 //kDebug() << "geometry is now" << geometry();
129 invalidate();
132 QSizeF FreeLayout::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
134 if (parentLayoutItem()) {
135 //kDebug() << "returning size hint from freelayout of" << parent()->geometry().size();
136 return parentLayoutItem()->geometry().size();
139 //kDebug() << "returning size hint from freelayout of" << maximumSize();
140 return maximumSize();