2007-10-13 15:29:21 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-11-23 01:08:03 +01:00
|
|
|
* it under the terms of the GNU Lesser General Public License
|
2007-10-13 15:29:21 +02:00
|
|
|
* version 2, or (at your option) any later version, as published by the
|
|
|
|
* Free Software Foundation
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2007-11-23 01:08:03 +01:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2007-10-13 15:29:21 +02:00
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nodelayout.h"
|
|
|
|
|
|
|
|
#include <QPair>
|
|
|
|
#include <QMap>
|
|
|
|
|
2007-12-10 20:49:25 +01:00
|
|
|
#include <limits>
|
|
|
|
|
2007-10-13 15:29:21 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
NodeLayout::NodeCoordinate::NodeCoordinate(qreal xRelative, qreal yRelative, qreal xAbsolute, qreal yAbsolute)
|
2007-10-13 16:33:14 +02:00
|
|
|
: xr(xRelative), xa(xAbsolute), yr(yRelative), ya(yAbsolute) {}
|
2007-10-13 15:29:21 +02:00
|
|
|
|
|
|
|
NodeLayout::NodeCoordinate NodeLayout::NodeCoordinate::simple(qreal x, qreal y,
|
|
|
|
CoordinateType xType, CoordinateType yType)
|
|
|
|
{
|
|
|
|
NodeLayout::NodeCoordinate coo;
|
|
|
|
switch (xType) {
|
|
|
|
case Relative:
|
|
|
|
coo.xr = x;
|
|
|
|
coo.xa = 0;
|
|
|
|
break;
|
|
|
|
case Absolute:
|
|
|
|
coo.xr = 0;
|
|
|
|
coo.xa = x;
|
|
|
|
break;
|
|
|
|
case InnerRelative:
|
|
|
|
coo.xr = x;
|
2007-12-10 20:49:25 +01:00
|
|
|
coo.xa = std::numeric_limits<float>::infinity();
|
2007-10-13 15:29:21 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (yType) {
|
|
|
|
case Relative:
|
|
|
|
coo.yr = y;
|
|
|
|
coo.ya = 0;
|
|
|
|
break;
|
|
|
|
case Absolute:
|
|
|
|
coo.yr = 0;
|
|
|
|
coo.ya = y;
|
|
|
|
break;
|
|
|
|
case InnerRelative:
|
|
|
|
coo.yr = y;
|
2007-12-10 20:49:25 +01:00
|
|
|
coo.ya = std::numeric_limits<float>::infinity();
|
2007-10-13 15:29:21 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return coo;
|
|
|
|
}
|
|
|
|
|
|
|
|
class NodeLayout::Private {
|
|
|
|
public:
|
2008-02-29 18:50:57 +01:00
|
|
|
QMap <LayoutItem * , QPair < NodeCoordinate, NodeCoordinate > > items;
|
2007-12-10 20:04:16 +01:00
|
|
|
//QRectF geometry;
|
|
|
|
NodeLayout * parent;
|
2007-10-13 15:29:21 +02:00
|
|
|
QSizeF sizeHint;
|
|
|
|
|
2007-12-10 20:04:16 +01:00
|
|
|
Private(NodeLayout * parentLayout) {
|
|
|
|
parent = parentLayout;
|
|
|
|
}
|
|
|
|
|
2007-10-13 15:29:21 +02:00
|
|
|
qreal calculateXPosition(const NodeCoordinate & coo, const QRectF & parentGeometry) const
|
|
|
|
{
|
|
|
|
return parentGeometry.left() + (coo.xr * parentGeometry.width()) + coo.xa;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal calculateYPosition(const NodeCoordinate & coo, const QRectF & parentGeometry) const
|
|
|
|
{
|
|
|
|
return parentGeometry.top() + (coo.yr * parentGeometry.height()) + coo.ya;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF calculatePosition(const NodeCoordinate & coo, const QRectF & parentGeometry) const
|
|
|
|
{
|
2007-11-22 08:19:43 +01:00
|
|
|
Q_UNUSED( parentGeometry );
|
|
|
|
|
2007-10-13 15:29:21 +02:00
|
|
|
return QPointF(
|
2007-12-10 20:04:16 +01:00
|
|
|
calculateXPosition(coo, parent->geometry()),
|
|
|
|
calculateYPosition(coo, parent->geometry())
|
2007-10-13 15:29:21 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
QRectF calculateRectangle(LayoutItem * item, QRectF geometry = QRectF()) const
|
2007-10-13 15:29:21 +02:00
|
|
|
{
|
2008-02-09 18:40:07 +01:00
|
|
|
if (geometry == QRectF()) {
|
|
|
|
geometry = parent->geometry();
|
|
|
|
}
|
2007-10-13 15:29:21 +02:00
|
|
|
|
|
|
|
QRectF result;
|
2008-02-09 18:40:07 +01:00
|
|
|
if (!item || !items.contains(item)) {
|
|
|
|
return QRectF();
|
|
|
|
}
|
2007-10-13 15:29:21 +02:00
|
|
|
|
|
|
|
result.setTopLeft(calculatePosition(items[item].first, geometry));
|
|
|
|
|
2007-12-10 20:49:25 +01:00
|
|
|
if (items[item].second.xa != std::numeric_limits<float>::infinity()) {
|
2007-10-13 15:29:21 +02:00
|
|
|
result.setRight(calculateXPosition(items[item].second, geometry));
|
|
|
|
} else {
|
2008-02-29 18:50:57 +01:00
|
|
|
result.setWidth(item->sizeHint().width());
|
2007-10-13 15:29:21 +02:00
|
|
|
result.moveLeft(result.left() - items[item].second.xr * result.width());
|
|
|
|
}
|
|
|
|
|
2007-12-10 20:49:25 +01:00
|
|
|
if (items[item].second.ya != std::numeric_limits<float>::infinity()) {
|
2007-10-13 15:29:21 +02:00
|
|
|
result.setBottom(calculateYPosition(items[item].second, geometry));
|
|
|
|
} else {
|
2008-02-29 18:50:57 +01:00
|
|
|
result.setHeight(item->sizeHint().height());
|
2007-10-13 15:29:21 +02:00
|
|
|
result.moveTop(result.top() - items[item].second.yr * result.height());
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
void calculateSizeHint(LayoutItem * item = NULL) {
|
2007-10-13 15:29:21 +02:00
|
|
|
if (item == NULL) {
|
|
|
|
// Recalculate the sizeHint using all items
|
|
|
|
sizeHint = QSizeF();
|
2008-02-29 18:50:57 +01:00
|
|
|
foreach (LayoutItem * item, items.keys()) {
|
2007-10-13 15:29:21 +02:00
|
|
|
if (item) {
|
|
|
|
calculateSizeHint(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Calculate size hint for current item
|
2008-02-09 18:40:07 +01:00
|
|
|
const QRectF scaled = calculateRectangle(item, QRectF(0, 0, 1, 1));
|
2007-10-13 15:29:21 +02:00
|
|
|
|
|
|
|
// qMin(..., 1.0) so that for autosized elements we don't get smaller
|
|
|
|
// size than the item's size itself. The sizeHint for NodeLayout can
|
|
|
|
// not do anything smarter concerning the sizeHint when there are
|
|
|
|
// autosized elements.
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
qreal width = item->sizeHint().width() / qMin(scaled.width(), qreal(1.0));
|
|
|
|
qreal height = item->sizeHint().height() / qMin(scaled.height(), qreal(1.0));
|
2007-10-13 15:29:21 +02:00
|
|
|
|
|
|
|
if (width > sizeHint.width()) sizeHint.setWidth(width);
|
|
|
|
if (height > sizeHint.height()) sizeHint.setHeight(height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
NodeLayout::NodeLayout(LayoutItem * parent)
|
|
|
|
: Layout(parent), d(new Private(this))
|
2007-10-13 15:29:21 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeLayout::~NodeLayout()
|
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
releaseManagedItems();
|
2007-10-13 15:29:21 +02:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
Qt::Orientations NodeLayout::expandingDirections() const
|
|
|
|
{
|
|
|
|
return Qt::Horizontal | Qt::Vertical;
|
|
|
|
}
|
|
|
|
|
2007-11-20 00:45:56 +01:00
|
|
|
void NodeLayout::relayout()
|
2007-10-13 15:29:21 +02:00
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
foreach (LayoutItem * item, d->items.keys()) {
|
2007-11-20 00:45:56 +01:00
|
|
|
if (item) {
|
|
|
|
item->setGeometry(d->calculateRectangle(item));
|
|
|
|
}
|
2007-10-13 15:29:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
void NodeLayout::releaseManagedItems()
|
|
|
|
{
|
|
|
|
foreach (LayoutItem * item, d->items.keys()) {
|
|
|
|
item->unsetManagingLayout(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF NodeLayout::sizeHint() const
|
2007-10-13 15:29:21 +02:00
|
|
|
{
|
|
|
|
return d->sizeHint;
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
void NodeLayout::addItem(LayoutItem * item)
|
2007-10-13 15:29:21 +02:00
|
|
|
{
|
2007-12-04 00:53:56 +01:00
|
|
|
NodeLayout::addItem(item, NodeCoordinate());
|
2007-10-13 15:29:21 +02:00
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
void NodeLayout::addItem(LayoutItem * item, NodeCoordinate topLeft, NodeCoordinate bottomRight)
|
2007-10-13 15:29:21 +02:00
|
|
|
{
|
2007-11-19 23:04:30 +01:00
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-13 15:29:21 +02:00
|
|
|
d->items[item] = QPair<NodeCoordinate, NodeCoordinate>(topLeft, bottomRight);
|
2008-02-29 18:50:57 +01:00
|
|
|
item->setManagingLayout(this);
|
2007-10-13 15:29:21 +02:00
|
|
|
d->calculateSizeHint(item);
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
void NodeLayout::addItem(LayoutItem * item, NodeCoordinate node, qreal xr, qreal yr)
|
2007-10-13 15:29:21 +02:00
|
|
|
{
|
2007-11-19 23:04:30 +01:00
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-10-13 15:29:21 +02:00
|
|
|
d->items[item] = QPair<NodeCoordinate, NodeCoordinate>(node,
|
|
|
|
NodeCoordinate::simple(xr, yr, NodeCoordinate::InnerRelative, NodeCoordinate::InnerRelative));
|
2008-02-29 18:50:57 +01:00
|
|
|
item->setManagingLayout(this);
|
2007-10-13 15:29:21 +02:00
|
|
|
d->calculateSizeHint(item);
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
void NodeLayout::removeItem(LayoutItem * item)
|
2007-10-13 15:29:21 +02:00
|
|
|
{
|
2007-11-19 23:04:30 +01:00
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
item->unsetManagingLayout(this);
|
2007-10-13 15:29:21 +02:00
|
|
|
d->items.remove(item);
|
|
|
|
d->calculateSizeHint();
|
|
|
|
}
|
|
|
|
|
|
|
|
int NodeLayout::count() const
|
|
|
|
{
|
|
|
|
return d->items.count();
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
int NodeLayout::indexOf(LayoutItem * item) const
|
2007-10-13 15:29:21 +02:00
|
|
|
{
|
2007-12-04 00:53:56 +01:00
|
|
|
if (!item) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-10-13 15:29:21 +02:00
|
|
|
return d->items.keys().indexOf(item);
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
LayoutItem * NodeLayout::itemAt(int i) const
|
2007-10-13 15:29:21 +02:00
|
|
|
{
|
2007-12-04 00:53:56 +01:00
|
|
|
if (i >= d->items.count()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-13 15:29:21 +02:00
|
|
|
return d->items.keys()[i];
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
LayoutItem * NodeLayout::takeAt(int i)
|
2007-10-13 15:29:21 +02:00
|
|
|
{
|
2007-12-04 00:53:56 +01:00
|
|
|
if (i >= d->items.count()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
LayoutItem * item = itemAt(i);
|
2007-10-13 15:29:21 +02:00
|
|
|
removeItem(item);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|