2
* Copyright (C) 2007, 2008, 2009, 2010 Ivan Cukic <ivan.cukic(at)kde.org>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser/Library General Public License version 2,
6
* or (at your option) any later version, as published by the Free
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU Lesser/Library General Public License for more details
14
* You should have received a copy of the GNU Lesser/Library General Public
15
* License along with this program; if not, write to the
16
* Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
#include "NodeLayout.h"
26
#include <lancelot/lancelot.h>
33
NodeLayout::NodeCoordinate::NodeCoordinate(qreal xRelative, qreal yRelative, qreal xAbsolute, qreal yAbsolute)
34
: xr(xRelative), xa(xAbsolute), yr(yRelative), ya(yAbsolute) {}
36
NodeLayout::NodeCoordinate NodeLayout::NodeCoordinate::simple(qreal x, qreal y,
37
CoordinateType xType, CoordinateType yType)
39
NodeLayout::NodeCoordinate coo;
51
coo.xa = QREAL_INFINITY;
66
coo.ya = QREAL_INFINITY;
72
class NodeLayout::Private {
74
QMap <QGraphicsLayoutItem * , QPair < NodeCoordinate, NodeCoordinate > > items;
76
QMap < Qt::SizeHint, QSizeF > sizeHintCache;
78
Private(NodeLayout * parentLayout) {
79
parent = parentLayout;
84
foreach (QGraphicsLayoutItem * item, items.keys()) // krazy:exclude=foreach
87
item->setGeometry(calculateRectangle(item));
92
qreal calculateXPosition(const NodeCoordinate & coo, const QRectF & parentGeometry) const
94
return parentGeometry.left() + (coo.xr * parentGeometry.width()) + coo.xa;
97
qreal calculateYPosition(const NodeCoordinate & coo, const QRectF & parentGeometry) const
99
return parentGeometry.top() + (coo.yr * parentGeometry.height()) + coo.ya;
102
QPointF calculatePosition(const NodeCoordinate & coo, const QRectF & parentGeometry) const
104
Q_UNUSED( parentGeometry );
107
calculateXPosition(coo, parent->geometry()),
108
calculateYPosition(coo, parent->geometry())
112
QRectF calculateRectangle(QGraphicsLayoutItem * item, QRectF geometry = QRectF()) const
114
if (geometry == QRectF()) {
115
geometry = parent->geometry();
119
if (!item || !items.contains(item)) {
123
result.setTopLeft(calculatePosition(items[item].first, geometry));
125
if (items[item].second.xa != QREAL_INFINITY) {
126
result.setRight(calculateXPosition(items[item].second, geometry));
128
result.setWidth(item->preferredSize().width());
129
result.moveLeft(result.left() - items[item].second.xr * result.width());
132
if (items[item].second.ya != QREAL_INFINITY) {
133
result.setBottom(calculateYPosition(items[item].second, geometry));
135
result.setHeight(item->preferredSize().height());
136
result.moveTop(result.top() - items[item].second.yr * result.height());
141
void calculateSizeHint(QGraphicsLayoutItem * item = NULL) {
143
// Recalculate the sizeHint using all items
144
sizeHintCache[Qt::MinimumSize] = QSizeF();
145
sizeHintCache[Qt::MaximumSize] = QSizeF();
146
sizeHintCache[Qt::PreferredSize] = QSizeF();
147
foreach (QGraphicsLayoutItem * item, items.keys()) // krazy:exclude=foreach
150
calculateSizeHint(item);
154
// Calculate size hint for current item
155
const QRectF scaled = calculateRectangle(item, QRectF(0, 0, 1, 1));
157
// qMin(..., 1.0) so that for autosized elements we don't get smaller
158
// size than the item's size itself. The sizeHint for NodeLayout can
159
// not do anything smarter concerning the sizeHint when there are
160
// autosized elements.
164
foreach (const Qt::SizeHint &which, sizeHintCache.keys()) // krazy:exclude=foreach
166
size = item->effectiveSizeHint(which);
168
1 / qMin(scaled.width(), qreal(1.0)),
169
1 / qMin(scaled.height(), qreal(1.0)),
170
Qt::IgnoreAspectRatio
172
sizeHintCache[which] = sizeHintCache[which].expandedTo(size);
179
NodeLayout::NodeLayout(QGraphicsLayoutItem * parent)
180
: QGraphicsLayout(parent), d(new Private(this))
184
NodeLayout::~NodeLayout()
189
QSizeF NodeLayout::sizeHint(Qt::SizeHint which,
190
const QSizeF & constraint) const
193
Q_UNUSED(constraint);
197
case Qt::MaximumSize:
198
result = MAX_WIDGET_SIZE;
201
result = d->sizeHintCache[which];
203
if (constraint.isValid()) {
204
result = result.boundedTo(constraint);
209
void NodeLayout::addItem(QGraphicsLayoutItem * item)
211
NodeLayout::addItem(item, NodeCoordinate());
214
void NodeLayout::addItem(QGraphicsLayoutItem * item, NodeCoordinate topLeft, NodeCoordinate bottomRight)
220
d->items[item] = QPair<NodeCoordinate, NodeCoordinate>(topLeft, bottomRight);
221
d->calculateSizeHint(item);
225
void NodeLayout::addItem(QGraphicsLayoutItem * item, NodeCoordinate node, qreal xr, qreal yr)
231
d->items[item] = QPair<NodeCoordinate, NodeCoordinate>(node,
232
NodeCoordinate::simple(xr, yr, NodeCoordinate::InnerRelative, NodeCoordinate::InnerRelative));
233
d->calculateSizeHint(item);
237
int NodeLayout::count() const
239
return d->items.count();
242
QGraphicsLayoutItem * NodeLayout::itemAt(int i) const
244
if (i >= d->items.count()) {
248
return d->items.keys()[i];
251
void NodeLayout::removeAt(int i)
253
if (i >= d->items.count()) {
257
d->items.remove(itemAt(i));
260
void NodeLayout::setGeometry(const QRectF & rect)
262
QGraphicsLayout::setGeometry(rect);
266
} // namespace Lancelot