2007-09-13 07:40:37 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2007 by Robert Knight <robertknight@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-09-14 22:17:11 +02:00
|
|
|
* it under the terms of the GNU Library General Public License as
|
2007-09-14 21:06:18 +02:00
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
2007-09-13 07:40:37 +02:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2007-09-10 22:38:59 +02:00
|
|
|
#include "freelayout.h"
|
|
|
|
|
2007-11-11 00:13:42 +01:00
|
|
|
#include <KDebug>
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
#include <plasma/layouts/layout.h>
|
|
|
|
|
2007-09-13 07:40:37 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
2007-09-10 22:38:59 +02:00
|
|
|
|
|
|
|
class FreeLayout::Private
|
|
|
|
{
|
|
|
|
public:
|
2008-02-29 18:50:57 +01:00
|
|
|
QList<LayoutItem*> children;
|
2007-09-10 22:38:59 +02:00
|
|
|
};
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
FreeLayout::FreeLayout(LayoutItem *parent)
|
|
|
|
: Layout(parent),
|
2007-09-13 07:40:37 +02:00
|
|
|
d(new Private)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeLayout::~FreeLayout()
|
2007-09-10 22:38:59 +02:00
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
releaseManagedItems();
|
2007-09-13 07:40:37 +02:00
|
|
|
delete d;
|
2007-09-10 22:38:59 +02:00
|
|
|
}
|
2007-09-13 07:40:37 +02:00
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
Qt::Orientations FreeLayout::expandingDirections() const
|
|
|
|
{
|
|
|
|
return Qt::Horizontal | Qt::Vertical;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreeLayout::addItem(LayoutItem *item)
|
2007-09-10 22:38:59 +02:00
|
|
|
{
|
2007-11-19 23:04:30 +01:00
|
|
|
if (d->children.contains(item)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-10 22:38:59 +02:00
|
|
|
d->children << item;
|
2008-02-29 18:50:57 +01:00
|
|
|
item->setManagingLayout(this);
|
2007-09-10 22:38:59 +02:00
|
|
|
}
|
2007-09-13 07:40:37 +02:00
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
void FreeLayout::removeItem(LayoutItem *item)
|
2007-09-10 22:38:59 +02:00
|
|
|
{
|
2007-12-03 17:11:27 +01:00
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-10 22:38:59 +02:00
|
|
|
d->children.removeAll(item);
|
2008-02-29 18:50:57 +01:00
|
|
|
item->unsetManagingLayout(this);
|
2007-09-10 22:38:59 +02:00
|
|
|
}
|
2007-09-13 07:40:37 +02:00
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
int FreeLayout::indexOf(LayoutItem *item) const
|
2007-09-10 22:38:59 +02:00
|
|
|
{
|
|
|
|
return d->children.indexOf(item);
|
|
|
|
}
|
2007-09-13 07:40:37 +02:00
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
LayoutItem * FreeLayout::itemAt(int i) const
|
2007-09-10 22:38:59 +02:00
|
|
|
{
|
|
|
|
return d->children[i];
|
|
|
|
}
|
2007-09-13 07:40:37 +02:00
|
|
|
|
2007-09-10 22:38:59 +02:00
|
|
|
int FreeLayout::count() const
|
|
|
|
{
|
|
|
|
return d->children.count();
|
|
|
|
}
|
2007-09-13 07:40:37 +02:00
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
LayoutItem * FreeLayout::takeAt(int i)
|
2007-09-10 22:38:59 +02:00
|
|
|
{
|
|
|
|
return d->children.takeAt(i);
|
|
|
|
}
|
2007-09-13 07:40:37 +02:00
|
|
|
|
2007-11-20 00:45:56 +01:00
|
|
|
void FreeLayout::relayout()
|
2007-09-10 22:38:59 +02:00
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
foreach (LayoutItem *child , d->children) {
|
|
|
|
if (child->geometry().size() != child->sizeHint()) {
|
2008-03-26 09:35:08 +01:00
|
|
|
/*
|
|
|
|
* We are setting the new geometry to have the desired size (sizeHint()),
|
|
|
|
* and we are changing the position of the topLeft corner so that the
|
|
|
|
* ratio
|
|
|
|
* d(widgetLeft, layoutLeft) / d(widgetRight, layoutRight)
|
|
|
|
* is kept constant (where d(X,Y) is the distance between X and Y)
|
|
|
|
*
|
|
|
|
* The same goes for the vertical distances.
|
|
|
|
*/
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
const QSizeF newSize = child->sizeHint().expandedTo(minimumSize()).boundedTo(maximumSize());
|
2008-03-26 09:35:08 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Since the layout doesn't have to be originated at (0, 0), we are switching
|
|
|
|
* to local coordinates.
|
|
|
|
*/
|
|
|
|
QRectF newGeometry = QRectF(child->geometry().topLeft() - geometry().topLeft(), newSize);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* newWidgetLeft layoutWidth - newWidgetWidth
|
|
|
|
* ------------- = ----------------------------
|
|
|
|
* oldWidgetLeft layoutWidth - oldWidgetWidth
|
|
|
|
*/
|
|
|
|
newGeometry.moveLeft(
|
|
|
|
newGeometry.left() *
|
|
|
|
(geometry().width() - newSize.width()) /
|
|
|
|
(geometry().width() - child->size().width())
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* newWidgetTop layoutHeight - newWidgetHeight
|
|
|
|
* ------------ = ------------------------------
|
|
|
|
* oldWidgetTop layoutHeight - oldWidgetHeight
|
|
|
|
*/
|
|
|
|
newGeometry.moveTop(
|
|
|
|
newGeometry.top() *
|
|
|
|
(geometry().height() - newSize.height()) /
|
|
|
|
(geometry().height() - child->size().height())
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Moving back from local coordinates to global
|
|
|
|
*/
|
|
|
|
newGeometry.moveTopLeft(newGeometry.topLeft() + geometry().topLeft());
|
|
|
|
|
|
|
|
child->setGeometry(newGeometry);
|
2007-09-15 06:16:11 +02:00
|
|
|
}
|
2007-09-10 22:38:59 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-13 07:40:37 +02:00
|
|
|
|
2008-03-26 09:35:08 +01:00
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
void FreeLayout::releaseManagedItems()
|
2008-02-11 06:50:47 +01:00
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
foreach (LayoutItem *item, d->children) {
|
|
|
|
item->unsetManagingLayout(this);
|
2008-02-11 06:50:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
QRectF FreeLayout::geometry() const
|
2007-09-10 22:38:59 +02:00
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
if (parent()) {
|
|
|
|
return parent()->geometry();
|
2007-11-17 19:53:16 +01:00
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
return QRectF(QPointF(0, 0), maximumSize());
|
2007-09-10 22:38:59 +02:00
|
|
|
}
|
2007-09-13 07:40:37 +02:00
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
QSizeF FreeLayout::sizeHint() const
|
2007-09-10 22:38:59 +02:00
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
if (parent()) {
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "returning size hint from freelayout of" << parent()->geometry().size();
|
2008-02-29 18:50:57 +01:00
|
|
|
return parent()->geometry().size();
|
2007-11-11 00:13:42 +01:00
|
|
|
}
|
|
|
|
|
2008-01-08 02:25:09 +01:00
|
|
|
//kDebug() << "returning size hint from freelayout of" << maximumSize();
|
2007-09-10 22:38:59 +02:00
|
|
|
return maximumSize();
|
|
|
|
}
|
|
|
|
|
2007-09-13 07:40:37 +02:00
|
|
|
}
|
|
|
|
|