2007-07-26 00:20:15 +02:00
|
|
|
/*
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2007 by Matias Valdenegro T. <mvaldenegro@informatica.utem.cl>
|
2007-09-01 14:34:22 +02:00
|
|
|
* Copyright 2007 by Robert Knight <robertknight@gmail.com>
|
2008-01-20 15:14:35 +01:00
|
|
|
* Copyright 2008 by Olivier Goffart <ogoffart@kde.org>
|
2007-07-26 00:20:15 +02:00
|
|
|
*
|
|
|
|
* 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-07-26 00:20:15 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "boxlayout.h"
|
|
|
|
|
2008-01-20 15:14:35 +01:00
|
|
|
#include <math.h>
|
|
|
|
|
2007-07-26 00:20:15 +02:00
|
|
|
#include <QtCore/QList>
|
2007-09-01 14:34:22 +02:00
|
|
|
#include <QtCore/QTimeLine>
|
2007-07-26 00:20:15 +02:00
|
|
|
|
|
|
|
#include <KDebug>
|
|
|
|
|
2008-01-20 15:14:35 +01:00
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
#include "layoutanimator.h"
|
|
|
|
|
2007-07-26 00:20:15 +02:00
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class BoxLayout::Private
|
|
|
|
{
|
2007-09-01 14:34:22 +02:00
|
|
|
public:
|
|
|
|
BoxLayout *const q;
|
|
|
|
Direction direction;
|
|
|
|
QList<LayoutItem*> children;
|
2008-01-28 12:55:56 +01:00
|
|
|
bool expandingBoth;
|
2008-01-20 15:14:35 +01:00
|
|
|
bool multiRow;
|
|
|
|
int rowCount;
|
2008-01-23 08:01:03 +01:00
|
|
|
int colCount() const {
|
|
|
|
return ((children.count() - 1) / rowCount) + 1;
|
2008-01-20 15:14:35 +01:00
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
|
|
|
|
Private(BoxLayout *parent)
|
|
|
|
: q(parent)
|
2008-01-28 12:55:56 +01:00
|
|
|
, direction(LeftToRight), expandingBoth(false), multiRow(false) , rowCount(1)
|
2007-09-01 14:34:22 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-09-28 07:43:15 +02:00
|
|
|
// returns the component of 'size' in the expanding direction
|
2007-09-01 14:34:22 +02:00
|
|
|
// of this layout
|
|
|
|
qreal size(const QSizeF& size) const
|
|
|
|
{
|
|
|
|
switch (direction) {
|
|
|
|
case LeftToRight:
|
|
|
|
case RightToLeft:
|
|
|
|
return size.width();
|
|
|
|
case TopToBottom:
|
|
|
|
case BottomToTop:
|
|
|
|
return size.height();
|
|
|
|
default:
|
|
|
|
Q_ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2008-01-20 18:11:56 +01:00
|
|
|
|
|
|
|
// returns the component of 'size' in the other direction
|
|
|
|
qreal size_o(const QSizeF& size) const
|
|
|
|
{
|
|
|
|
switch (direction) {
|
|
|
|
case LeftToRight:
|
|
|
|
case RightToLeft:
|
|
|
|
return size.height();
|
|
|
|
case TopToBottom:
|
|
|
|
case BottomToTop:
|
|
|
|
return size.width();
|
|
|
|
default:
|
|
|
|
Q_ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-01-28 12:55:56 +01:00
|
|
|
// returns the directions in which this layout expands
|
2007-09-01 14:34:22 +02:00
|
|
|
// or shrinks
|
2008-01-28 12:55:56 +01:00
|
|
|
Qt::Orientations expandingDirections() const
|
2007-09-01 14:34:22 +02:00
|
|
|
{
|
2008-01-28 12:55:56 +01:00
|
|
|
if (expandingBoth) {
|
|
|
|
return Qt::Horizontal|Qt::Vertical;
|
|
|
|
}
|
|
|
|
|
2008-01-23 08:01:03 +01:00
|
|
|
switch (direction) {
|
2007-09-01 14:34:22 +02:00
|
|
|
case LeftToRight:
|
|
|
|
case RightToLeft:
|
|
|
|
return Qt::Horizontal;
|
|
|
|
case TopToBottom:
|
|
|
|
case BottomToTop:
|
|
|
|
return Qt::Vertical;
|
|
|
|
default:
|
|
|
|
Q_ASSERT(false);
|
|
|
|
return Qt::Horizontal;
|
|
|
|
}
|
2007-09-28 07:43:15 +02:00
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
|
|
|
|
// returns the position from which layouting should
|
|
|
|
// begin depending on the direction of this layout
|
2007-09-28 07:43:15 +02:00
|
|
|
qreal startPos(const QRectF& geometry) const
|
2007-09-01 14:34:22 +02:00
|
|
|
{
|
2008-01-23 08:01:03 +01:00
|
|
|
switch (direction) {
|
2007-09-01 14:34:22 +02:00
|
|
|
case LeftToRight:
|
2007-11-22 07:00:02 +01:00
|
|
|
return geometry.left() + q->margin(LeftMargin);
|
2007-09-01 14:34:22 +02:00
|
|
|
case TopToBottom:
|
2007-11-22 07:00:02 +01:00
|
|
|
return geometry.top() + q->margin(TopMargin);
|
2007-09-01 14:34:22 +02:00
|
|
|
case RightToLeft:
|
2007-11-22 07:00:02 +01:00
|
|
|
return geometry.right() - q->margin(RightMargin);
|
2007-09-01 14:34:22 +02:00
|
|
|
case BottomToTop:
|
2007-11-22 07:00:02 +01:00
|
|
|
return geometry.bottom() - q->margin(BottomMargin);
|
2007-09-01 14:34:22 +02:00
|
|
|
default:
|
|
|
|
Q_ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-28 07:43:15 +02:00
|
|
|
// lays out an item
|
2007-09-01 14:34:22 +02:00
|
|
|
//
|
|
|
|
// 'geometry' the geometry of the layout
|
|
|
|
// 'item' the item whoose geometry should be altered
|
|
|
|
// 'pos' the position of the item (in the expanding direction of the layout)
|
|
|
|
// 'size' the size of the item (in the expanding direction of the layout)
|
|
|
|
//
|
|
|
|
// returns the position for the next item in the layout
|
|
|
|
//
|
2008-01-20 15:14:35 +01:00
|
|
|
qreal layoutItem(const QRectF& geometry , LayoutItem *item , const qreal pos , qreal size, int row)
|
2007-09-01 14:34:22 +02:00
|
|
|
{
|
2008-02-09 18:10:40 +01:00
|
|
|
//kDebug() << "layoutItem: " << direction << "item size" << size;
|
2007-09-01 14:34:22 +02:00
|
|
|
|
|
|
|
QRectF newGeometry;
|
|
|
|
qreal newPos = 0;
|
|
|
|
|
|
|
|
qreal top = 0;
|
|
|
|
qreal height = 0;
|
|
|
|
|
2008-02-09 18:10:40 +01:00
|
|
|
const QSizeF minSize = item->minimumSize();
|
|
|
|
const QSizeF maxSize = item->maximumSize();
|
2008-01-23 08:01:03 +01:00
|
|
|
switch (direction) {
|
2007-09-01 14:34:22 +02:00
|
|
|
case LeftToRight:
|
|
|
|
case RightToLeft:
|
2008-01-23 08:01:03 +01:00
|
|
|
height = (geometry.height() - q->spacing() * (rowCount - 1)) / rowCount;
|
|
|
|
top = geometry.top() + row * (height + q->spacing());
|
2008-01-20 15:14:35 +01:00
|
|
|
height = qBound(minSize.height(), height, maxSize.height());
|
2008-02-18 00:02:46 +01:00
|
|
|
if (size > geometry.width()) {
|
|
|
|
size = geometry.width();
|
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
break;
|
|
|
|
case TopToBottom:
|
|
|
|
case BottomToTop:
|
2008-01-23 08:01:03 +01:00
|
|
|
height = (geometry.width() - q->spacing() * (rowCount - 1)) / rowCount;
|
|
|
|
top = geometry.left() + row * (height + q->spacing());
|
2008-01-20 15:14:35 +01:00
|
|
|
height = qBound(minSize.width(), height, maxSize.width());
|
2008-02-18 00:02:46 +01:00
|
|
|
if (size > geometry.height()) {
|
|
|
|
size = geometry.height();
|
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-23 08:01:03 +01:00
|
|
|
switch (direction) {
|
2007-09-01 14:34:22 +02:00
|
|
|
case LeftToRight:
|
2008-01-23 08:01:03 +01:00
|
|
|
newGeometry = QRectF(pos, top, size, height);
|
|
|
|
newPos = pos + size + q->spacing();
|
2007-09-01 14:34:22 +02:00
|
|
|
break;
|
|
|
|
case RightToLeft:
|
2008-01-23 08:01:03 +01:00
|
|
|
newGeometry = QRectF(geometry.width() - pos - size, top, size, height);
|
|
|
|
newPos = pos - size - q->spacing();
|
2007-09-01 14:34:22 +02:00
|
|
|
break;
|
|
|
|
case TopToBottom:
|
2008-01-23 08:01:03 +01:00
|
|
|
newGeometry = QRectF(top, pos, height, size);
|
|
|
|
newPos = pos + size + q->spacing();
|
2007-09-01 14:34:22 +02:00
|
|
|
break;
|
|
|
|
case BottomToTop:
|
2008-01-23 08:01:03 +01:00
|
|
|
newGeometry = QRectF(top, geometry.height() - pos - size, height, size);
|
|
|
|
newPos = pos - size - q->spacing();
|
2007-09-01 14:34:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-02-09 18:10:40 +01:00
|
|
|
// kDebug() << "Item geometry: " << newGeometry;
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2007-11-20 01:47:20 +01:00
|
|
|
if (q->animator()) {
|
2007-11-19 04:30:24 +01:00
|
|
|
q->animator()->setGeometry(item, newGeometry);
|
2007-11-20 01:47:20 +01:00
|
|
|
} else {
|
2007-09-01 14:34:22 +02:00
|
|
|
item->setGeometry(newGeometry);
|
2007-11-20 01:47:20 +01:00
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2007-09-28 07:43:15 +02:00
|
|
|
return newPos;
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum SizeType
|
|
|
|
{
|
|
|
|
MinSize,
|
|
|
|
MaxSize,
|
|
|
|
HintSize
|
|
|
|
};
|
2007-09-28 07:43:15 +02:00
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
// this provides a + function which can be passed as the 'op'
|
2007-09-28 07:43:15 +02:00
|
|
|
// argument to calculateSize
|
2008-01-23 08:01:03 +01:00
|
|
|
static qreal sum(const qreal a, const qreal b)
|
2007-09-01 14:34:22 +02:00
|
|
|
{
|
2008-01-23 08:01:03 +01:00
|
|
|
return a + b;
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// calcualtes a size hint or value for this layout
|
2007-09-28 07:43:15 +02:00
|
|
|
// 'sizeType' - The item size ( minimum , maximum , hint ) to use
|
2007-09-01 14:34:22 +02:00
|
|
|
// 'dir' - The direction component of the item size to use
|
|
|
|
// 'op' - A function to apply to the size of each item in the layout
|
|
|
|
// , usually qMax,qMin or sum
|
2007-09-28 07:43:15 +02:00
|
|
|
template <class T>
|
2008-01-23 08:01:03 +01:00
|
|
|
qreal calculateSize(SizeType sizeType, Qt::Orientation dir, T (*op)(const T, const T)) const
|
2007-09-01 14:34:22 +02:00
|
|
|
{
|
|
|
|
qreal value = 0;
|
2008-01-23 08:01:03 +01:00
|
|
|
for (int i = 0; i < children.count(); i++) {
|
2007-09-01 14:34:22 +02:00
|
|
|
|
|
|
|
QSizeF itemSize;
|
2008-01-23 08:01:03 +01:00
|
|
|
switch (sizeType) {
|
2007-09-01 14:34:22 +02:00
|
|
|
case MinSize:
|
|
|
|
itemSize = children[i]->minimumSize();
|
|
|
|
break;
|
|
|
|
case MaxSize:
|
|
|
|
itemSize = children[i]->maximumSize();
|
|
|
|
break;
|
|
|
|
case HintSize:
|
|
|
|
itemSize = children[i]->sizeHint();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-01-23 08:01:03 +01:00
|
|
|
if (dir == Qt::Horizontal) {
|
|
|
|
value = op(value, itemSize.width());
|
2007-09-01 14:34:22 +02:00
|
|
|
} else {
|
2008-01-23 08:01:03 +01:00
|
|
|
value = op(value, itemSize.height());
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculates a size hint or value for this layout
|
|
|
|
// 'calculateSizeType' specifies the value to be calculated
|
|
|
|
QSizeF calculateSize(SizeType calculateSizeType) const
|
|
|
|
{
|
|
|
|
QSizeF result;
|
|
|
|
|
2008-01-25 18:16:38 +01:00
|
|
|
const qreal totalSpacingC = q->spacing() * (colCount() - 1);
|
|
|
|
const qreal totalSpacingR = q->spacing() * (rowCount - 1);
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-01-23 08:01:03 +01:00
|
|
|
switch (direction) {
|
2007-09-01 14:34:22 +02:00
|
|
|
case LeftToRight:
|
|
|
|
case RightToLeft:
|
2008-01-23 08:01:03 +01:00
|
|
|
result = QSizeF(calculateSize(calculateSizeType, Qt::Horizontal, sum) / rowCount,
|
|
|
|
calculateSize(calculateSizeType, Qt::Vertical, qMax<qreal>) * rowCount);
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-01-20 15:14:35 +01:00
|
|
|
result.rwidth() += q->margin(LeftMargin) + q->margin(RightMargin) + totalSpacingC;
|
|
|
|
result.rheight() += q->margin(TopMargin) + q->margin(BottomMargin) + totalSpacingR;
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2007-09-28 07:43:15 +02:00
|
|
|
break;
|
2007-09-01 14:34:22 +02:00
|
|
|
case TopToBottom:
|
|
|
|
case BottomToTop:
|
2008-01-23 08:01:03 +01:00
|
|
|
result = QSizeF(calculateSize(calculateSizeType, Qt::Horizontal, qMax<qreal>) / rowCount,
|
|
|
|
calculateSize(calculateSizeType, Qt::Vertical, sum) * rowCount);
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-01-20 15:14:35 +01:00
|
|
|
result.rheight() += q->margin(TopMargin) + q->margin(BottomMargin) + totalSpacingC;
|
|
|
|
result.rwidth() += q->margin(LeftMargin) + q->margin(RightMargin) + totalSpacingR;
|
2007-09-01 14:34:22 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-09-28 07:43:15 +02:00
|
|
|
return result;
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
2007-07-26 00:20:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-01-23 08:01:03 +01:00
|
|
|
BoxLayout::BoxLayout(Direction direction, LayoutItem *parent)
|
2007-07-26 00:20:15 +02:00
|
|
|
: Layout(parent),
|
2007-09-01 14:34:22 +02:00
|
|
|
d(new Private(this))
|
2007-07-26 00:20:15 +02:00
|
|
|
{
|
2007-09-01 14:34:22 +02:00
|
|
|
d->direction = direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BoxLayout::setDirection(Direction direction)
|
|
|
|
{
|
|
|
|
d->direction = direction;
|
2007-11-20 00:45:56 +01:00
|
|
|
updateGeometry();
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
2008-01-23 08:01:03 +01:00
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
BoxLayout::Direction BoxLayout::direction() const
|
|
|
|
{
|
|
|
|
return d->direction;
|
2007-07-26 00:20:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BoxLayout::~BoxLayout()
|
|
|
|
{
|
2008-02-11 06:50:47 +01:00
|
|
|
releaseManagedItems();
|
2007-07-26 00:20:15 +02:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::Orientations BoxLayout::expandingDirections() const
|
|
|
|
{
|
2008-01-28 12:55:56 +01:00
|
|
|
return d->expandingDirections();
|
2007-07-26 00:20:15 +02:00
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
int BoxLayout::count() const
|
2007-07-26 00:20:15 +02:00
|
|
|
{
|
2007-09-01 14:34:22 +02:00
|
|
|
return d->children.count();
|
2007-07-26 00:20:15 +02:00
|
|
|
}
|
|
|
|
|
2008-01-13 09:22:04 +01:00
|
|
|
void BoxLayout::setAnimator(LayoutAnimator *animator)
|
|
|
|
{
|
|
|
|
Layout::setAnimator(animator);
|
|
|
|
|
|
|
|
if (animator) {
|
|
|
|
foreach (LayoutItem *item, d->children) {
|
|
|
|
animator->setGeometry(item, item->geometry());
|
|
|
|
animator->setCurrentState(item, LayoutAnimator::StandardState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
void BoxLayout::insertItem(int index, LayoutItem *item)
|
2007-07-26 00:20:15 +02:00
|
|
|
{
|
2007-11-19 23:04:30 +01:00
|
|
|
if (!item || d->children.contains(item)) {
|
2007-08-31 01:44:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
item->setManagingLayout(this);
|
2007-08-31 01:44:08 +02:00
|
|
|
|
2007-11-19 23:04:30 +01:00
|
|
|
if (index == -1) {
|
2007-09-01 14:34:22 +02:00
|
|
|
index = d->children.size();
|
2007-11-19 23:04:30 +01:00
|
|
|
}
|
2007-08-31 01:44:08 +02:00
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
d->children.insert(index, item);
|
2007-08-31 01:44:08 +02:00
|
|
|
|
2007-11-19 23:04:30 +01:00
|
|
|
if (animator()) {
|
2007-11-20 02:19:59 +01:00
|
|
|
animator()->setCurrentState(item, LayoutAnimator::InsertedState);
|
2007-11-19 23:04:30 +01:00
|
|
|
}
|
2007-08-31 01:44:08 +02:00
|
|
|
|
2007-11-20 00:45:56 +01:00
|
|
|
updateGeometry();
|
2007-07-26 00:20:15 +02:00
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
void BoxLayout::addItem(LayoutItem *item)
|
2007-08-31 01:44:08 +02:00
|
|
|
{
|
2007-12-04 00:53:56 +01:00
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-19 23:04:30 +01:00
|
|
|
insertItem(-1, item);
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
2007-08-31 01:44:08 +02:00
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
void BoxLayout::removeItem(LayoutItem *item)
|
|
|
|
{
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
2007-08-31 01:44:08 +02:00
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
item->unsetManagingLayout(this);
|
|
|
|
d->children.removeAll(item);
|
2007-08-31 01:44:08 +02:00
|
|
|
|
2007-12-04 00:53:56 +01:00
|
|
|
if (animator()) {
|
|
|
|
animator()->setCurrentState(item, LayoutAnimator::RemovedState);
|
|
|
|
}
|
2007-08-31 01:44:08 +02:00
|
|
|
|
2007-11-20 00:45:56 +01:00
|
|
|
updateGeometry();
|
2007-08-31 01:44:08 +02:00
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
int BoxLayout::indexOf(LayoutItem *l) const
|
2007-07-26 00:20:15 +02:00
|
|
|
{
|
2007-09-01 14:34:22 +02:00
|
|
|
return d->children.indexOf(l);
|
2007-07-26 00:20:15 +02:00
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
LayoutItem *BoxLayout::itemAt(int i) const
|
2007-07-26 00:20:15 +02:00
|
|
|
{
|
2007-12-04 00:53:56 +01:00
|
|
|
if (i >= d->children.count()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
return d->children[i];
|
2007-07-26 00:20:15 +02:00
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
LayoutItem *BoxLayout::takeAt(int i)
|
2007-07-26 00:20:15 +02:00
|
|
|
{
|
2007-12-04 00:53:56 +01:00
|
|
|
if (i >= d->children.count()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-07-26 00:20:15 +02:00
|
|
|
|
2007-12-04 00:53:56 +01:00
|
|
|
return d->children.takeAt(i);
|
2007-12-29 08:14:21 +01:00
|
|
|
// FIXME: This is never reached. Should it be called?
|
2007-11-20 00:45:56 +01:00
|
|
|
updateGeometry();
|
2007-07-26 00:20:15 +02:00
|
|
|
}
|
|
|
|
|
2007-11-20 00:45:56 +01:00
|
|
|
void BoxLayout::relayout()
|
2007-07-26 00:20:15 +02:00
|
|
|
{
|
2008-02-09 18:10:40 +01:00
|
|
|
const QRectF margined = geometry().adjusted(margin(LeftMargin), margin(TopMargin), -margin(RightMargin), -margin(BottomMargin));
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-02-09 18:10:40 +01:00
|
|
|
//kDebug() << "geo before " << geo << "and with margins" << margined << "margins" << margin(LeftMargin)
|
2007-11-19 04:30:24 +01:00
|
|
|
// << margin(TopMargin) << -margin(RightMargin) << -margin(BottomMargin);
|
2008-02-09 18:10:40 +01:00
|
|
|
//kDebug() << "Box layout beginning with geo" << geometry;
|
|
|
|
//kDebug() << "This box max size" << maximumSize();
|
2008-01-20 15:14:35 +01:00
|
|
|
d->rowCount = 1;
|
2008-01-23 08:01:03 +01:00
|
|
|
if (d->multiRow) {
|
|
|
|
qreal minRowSize = 1;
|
|
|
|
qreal minWidth = 0;
|
2008-01-20 18:11:56 +01:00
|
|
|
for(int i = 0; i < d->children.count(); i++) {
|
|
|
|
minRowSize = qMax(minRowSize, d->size_o(d->children[i]->minimumSize()));
|
|
|
|
minWidth += d->size(d->children[i]->minimumSize());
|
|
|
|
}
|
2008-01-23 08:01:03 +01:00
|
|
|
|
2008-01-20 15:14:35 +01:00
|
|
|
const qreal ratio = 2.25; //maybe this should not be hardcoded
|
2008-01-20 18:11:56 +01:00
|
|
|
//we want the height of items be larger than the minimum size
|
|
|
|
int maxRow = (d->size_o(margined.size()) + spacing()) /
|
|
|
|
(minRowSize + spacing());
|
|
|
|
//we want enough rows to be able to fit each items width.
|
|
|
|
int minRow = (minWidth + d->children.count() * spacing()) /
|
|
|
|
(d->size(margined.size()) + spacing() + 0.1);
|
2008-01-20 15:14:35 +01:00
|
|
|
//FIXME: this formula doesn't take the cellspacing in account
|
|
|
|
// it should also try to "fill" before adding a row
|
2008-01-20 18:11:56 +01:00
|
|
|
d->rowCount = 1 + sqrt(ratio * count() * d->size_o(margined.size()) /
|
2008-01-23 08:01:03 +01:00
|
|
|
(d->size(margined.size()) + 1));
|
|
|
|
|
|
|
|
d->rowCount = qMax(minRow, d->rowCount);
|
|
|
|
d->rowCount = qMin(maxRow, d->rowCount);
|
|
|
|
d->rowCount = qMax(1, d->rowCount);
|
2008-01-20 15:14:35 +01:00
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-02-09 18:10:40 +01:00
|
|
|
const int colCount = d->colCount();
|
2008-01-23 08:01:03 +01:00
|
|
|
|
|
|
|
QVector<qreal> sizes(colCount, 0);
|
|
|
|
QVector<qreal> expansionSpace(colCount, 0);
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-01-20 15:14:35 +01:00
|
|
|
qreal available = d->size(margined.size()) - spacing() * colCount;
|
|
|
|
qreal perItemSize = available / colCount;
|
2008-01-23 08:01:03 +01:00
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
// initial distribution of space to items
|
2008-01-23 08:01:03 +01:00
|
|
|
for (int i = 0; i < colCount; i++) {
|
|
|
|
qreal minItemSize = 0;
|
|
|
|
qreal maxItemSize = 65536;
|
|
|
|
bool isExpanding = true;
|
2008-01-20 15:14:35 +01:00
|
|
|
qreal hint = 0;
|
2008-01-23 08:01:03 +01:00
|
|
|
|
|
|
|
for (int f = i * d->rowCount; f < (i + 1) * d->rowCount; f++) {
|
|
|
|
if (f >= count()) {
|
2008-01-20 15:14:35 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
const LayoutItem *item = d->children[f];
|
2008-01-28 12:55:56 +01:00
|
|
|
const bool itemExp = (item->expandingDirections() & d->expandingDirections());
|
2008-01-20 15:14:35 +01:00
|
|
|
isExpanding = isExpanding && itemExp;
|
2008-01-23 08:01:03 +01:00
|
|
|
minItemSize = qMax(minItemSize, d->size(item->minimumSize()));
|
|
|
|
maxItemSize = qMin(maxItemSize, d->size(item->maximumSize()));
|
|
|
|
if (!itemExp) {
|
|
|
|
hint = qMax(hint, d->size(item->sizeHint()));
|
|
|
|
}
|
2008-01-20 15:14:35 +01:00
|
|
|
}
|
2007-09-28 07:43:15 +02:00
|
|
|
|
2008-01-23 08:01:03 +01:00
|
|
|
if (isExpanding) {
|
2007-09-28 07:43:15 +02:00
|
|
|
sizes[i] = perItemSize;
|
2008-01-20 15:14:35 +01:00
|
|
|
} else {
|
|
|
|
sizes[i] = hint;
|
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-02-09 18:10:40 +01:00
|
|
|
// kDebug() << "Layout max item " << i << "size: " << maxItemSize;
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-01-23 08:01:03 +01:00
|
|
|
sizes[i] = qMin(sizes[i], maxItemSize);
|
|
|
|
sizes[i] = qMax(sizes[i], minItemSize);
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-02-09 18:10:40 +01:00
|
|
|
// kDebug() << "Available: " << available << "per item:" << perItemSize <<
|
2007-09-01 14:34:22 +02:00
|
|
|
// "Initial size: " << sizes[i];
|
|
|
|
|
2008-01-23 08:01:03 +01:00
|
|
|
if (isExpanding) {
|
|
|
|
expansionSpace[i] = maxItemSize - sizes[i];
|
2008-01-20 15:14:35 +01:00
|
|
|
} else {
|
2007-09-01 14:34:22 +02:00
|
|
|
expansionSpace[i] = 0;
|
2008-01-20 15:14:35 +01:00
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2007-10-09 09:05:37 +02:00
|
|
|
available -= sizes[i];
|
2007-09-01 14:34:22 +02:00
|
|
|
// adjust the per-item size if the space was over or under used
|
2008-01-23 08:01:03 +01:00
|
|
|
if (sizes[i] != perItemSize && i != sizes.count() - 1) {
|
|
|
|
perItemSize = available / (sizes.count() - i - 1);
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
2007-07-26 00:20:15 +02:00
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
// distribute out any remaining space to items which can still expand
|
|
|
|
//
|
|
|
|
// space is distributed equally amongst remaining items until we run
|
|
|
|
// out of space or items to expand
|
|
|
|
int expandable = sizes.count();
|
|
|
|
const qreal threshold = 1.0;
|
2008-01-23 08:01:03 +01:00
|
|
|
while (available > threshold && expandable > 0) {
|
2007-09-28 07:43:15 +02:00
|
|
|
|
2008-02-09 18:10:40 +01:00
|
|
|
const qreal extraSpace = available / expandable;
|
2008-01-23 08:01:03 +01:00
|
|
|
for (int i = 0; i < colCount; i++) {
|
|
|
|
if (expansionSpace[i] > threshold) {
|
2007-09-01 14:34:22 +02:00
|
|
|
qreal oldSize = sizes[i];
|
|
|
|
|
2008-01-23 08:01:03 +01:00
|
|
|
sizes[i] += qMin(extraSpace, expansionSpace[i]);
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-01-23 08:01:03 +01:00
|
|
|
expansionSpace[i] -= sizes[i] - oldSize;
|
|
|
|
available -= sizes[i] - oldSize;
|
2007-09-01 14:34:22 +02:00
|
|
|
} else {
|
|
|
|
expandable--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-26 00:20:15 +02:00
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
// set items' geometry according to new sizes
|
2007-11-20 00:45:56 +01:00
|
|
|
qreal pos = d->startPos(geometry());
|
2008-01-23 08:01:03 +01:00
|
|
|
for (int col = 0; col < colCount; col++) {
|
2008-01-20 15:14:35 +01:00
|
|
|
int newPos = pos;
|
2008-01-23 08:01:03 +01:00
|
|
|
for (int row = 0; row < d->rowCount; row++) {
|
|
|
|
int i = col * d->rowCount + row;
|
|
|
|
if (i >= count()) {
|
2008-01-20 15:14:35 +01:00
|
|
|
break;
|
2008-01-23 08:01:03 +01:00
|
|
|
}
|
|
|
|
|
2008-01-20 15:14:35 +01:00
|
|
|
//QObject *obj = dynamic_cast<QObject*>(d->children[i]);
|
|
|
|
//if ( obj )
|
2008-02-09 18:10:40 +01:00
|
|
|
//kDebug() << "Item " << i << obj->metaObject()->className() << "size:" << sizes[i];
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-01-23 08:01:03 +01:00
|
|
|
int p = d->layoutItem(margined, d->children[i], pos, sizes[col], row);
|
|
|
|
newPos = (row != 0 && p < pos) ? qMin(p, newPos) : qMax(p, newPos);
|
2008-01-20 15:14:35 +01:00
|
|
|
}
|
2008-01-23 08:01:03 +01:00
|
|
|
pos = newPos;
|
2007-07-27 22:00:07 +02:00
|
|
|
}
|
2007-12-29 08:14:21 +01:00
|
|
|
|
|
|
|
startAnimation();
|
2007-07-26 00:20:15 +02:00
|
|
|
}
|
|
|
|
|
2008-02-11 06:50:47 +01:00
|
|
|
void BoxLayout::releaseManagedItems()
|
|
|
|
{
|
|
|
|
foreach (LayoutItem* item, d->children) {
|
|
|
|
item->unsetManagingLayout(this);
|
|
|
|
}
|
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
|
|
|
|
QSizeF BoxLayout::maximumSize() const
|
2007-07-26 00:20:15 +02:00
|
|
|
{
|
2007-09-01 14:34:22 +02:00
|
|
|
return Layout::maximumSize();
|
2007-07-26 00:20:15 +02:00
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
QSizeF BoxLayout::minimumSize() const
|
2007-07-26 00:20:15 +02:00
|
|
|
{
|
2007-09-01 14:34:22 +02:00
|
|
|
return d->calculateSize(Private::MinSize);
|
2007-07-26 00:20:15 +02:00
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
QSizeF BoxLayout::sizeHint() const
|
2007-07-26 00:20:15 +02:00
|
|
|
{
|
2007-09-28 07:43:15 +02:00
|
|
|
return d->calculateSize(Private::HintSize);
|
2007-07-26 00:20:15 +02:00
|
|
|
}
|
|
|
|
|
2008-01-20 15:14:35 +01:00
|
|
|
void BoxLayout::setMultiRow(bool b)
|
|
|
|
{
|
|
|
|
d->multiRow = b;
|
|
|
|
}
|
|
|
|
|
2008-01-28 12:55:56 +01:00
|
|
|
void BoxLayout::setExpandingBoth(bool both)
|
|
|
|
{
|
|
|
|
d->expandingBoth = both;
|
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
HBoxLayout::HBoxLayout(LayoutItem *parent)
|
2008-01-23 08:01:03 +01:00
|
|
|
: BoxLayout(LeftToRight, parent)
|
2007-07-26 00:20:15 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
VBoxLayout::VBoxLayout(LayoutItem *parent)
|
2008-01-23 08:01:03 +01:00
|
|
|
: BoxLayout(TopToBottom, parent)
|
2007-07-26 00:20:15 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
} // Plasma namespace
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-01-20 15:14:35 +01:00
|
|
|
|