2007-09-01 14:34:22 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2007 by Robert Knight <robertknight@gmail.com>
|
2008-02-10 02:59:56 +01:00
|
|
|
* Copyright 2008 by William Egert <begert@gmail.com>
|
2007-09-01 14:34:22 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-11-23 01:03:27 +01:00
|
|
|
* it under the terms of the GNU Library General Public License
|
|
|
|
* as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
2007-09-01 14:34:22 +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 "flowlayout.h"
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include <QtCore/QList>
|
|
|
|
#include <QtCore/QRectF>
|
|
|
|
#include <QtCore/QTimeLine>
|
|
|
|
|
2008-02-09 15:29:03 +01:00
|
|
|
#include <KDebug>
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
#include "layoutanimator.h"
|
2007-09-01 14:34:22 +02:00
|
|
|
|
|
|
|
using namespace Plasma;
|
|
|
|
|
|
|
|
class FlowLayout::Private
|
|
|
|
{
|
|
|
|
public:
|
2008-02-29 18:50:57 +01:00
|
|
|
Private() : columnWidth( -1 ) {}
|
|
|
|
QList<LayoutItem*> items;
|
2007-11-06 03:46:01 +01:00
|
|
|
qreal columnWidth;
|
2007-09-01 14:34:22 +02:00
|
|
|
};
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
FlowLayout::FlowLayout(LayoutItem* parent)
|
|
|
|
: Layout(parent)
|
2007-09-01 14:34:22 +02:00
|
|
|
, d(new Private)
|
|
|
|
{
|
|
|
|
}
|
2008-02-11 06:50:47 +01:00
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
FlowLayout::~FlowLayout()
|
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
releaseManagedItems();
|
2007-09-01 14:34:22 +02:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FlowLayout::count() const
|
|
|
|
{
|
|
|
|
return d->items.count();
|
|
|
|
}
|
2007-11-19 23:04:30 +01:00
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
void FlowLayout::addItem(LayoutItem* item)
|
2007-09-01 14:34:22 +02:00
|
|
|
{
|
2007-12-04 00:53:56 +01:00
|
|
|
if (!item || d->items.contains(item)) {
|
2007-11-19 23:04:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
item->setManagingLayout(this);
|
2007-09-01 14:34:22 +02:00
|
|
|
d->items << item;
|
2008-02-29 18:50:57 +01:00
|
|
|
|
|
|
|
if (animator()) {
|
|
|
|
animator()->setCurrentState(item,LayoutAnimator::InsertedState);
|
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2007-12-15 10:08:09 +01:00
|
|
|
updateGeometry();
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
2008-02-29 18:50:57 +01:00
|
|
|
void FlowLayout::removeItem(LayoutItem* item)
|
2007-09-01 14:34:22 +02:00
|
|
|
{
|
2007-12-04 00:53:56 +01:00
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
item->unsetManagingLayout(this);
|
2007-09-01 14:34:22 +02:00
|
|
|
d->items.removeAll(item);
|
2008-02-29 18:50:57 +01:00
|
|
|
|
|
|
|
if (animator()) {
|
|
|
|
animator()->setCurrentState(item,LayoutAnimator::RemovedState);
|
|
|
|
}
|
2007-12-15 10:08:09 +01:00
|
|
|
|
|
|
|
updateGeometry();
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
2008-02-29 18:50:57 +01:00
|
|
|
int FlowLayout::indexOf(LayoutItem* item) const
|
2007-09-01 14:34:22 +02:00
|
|
|
{
|
2007-12-04 00:53:56 +01:00
|
|
|
if (!item) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
return d->items.indexOf(item);
|
|
|
|
}
|
2008-02-29 18:50:57 +01:00
|
|
|
LayoutItem* FlowLayout::itemAt(int i) const
|
2007-09-01 14:34:22 +02:00
|
|
|
{
|
2007-12-04 00:53:56 +01:00
|
|
|
if (i >= d->items.count()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
return d->items[i];
|
|
|
|
}
|
2007-12-04 00:53:56 +01:00
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
QSizeF FlowLayout::sizeHint() const
|
2007-09-01 14:34:22 +02:00
|
|
|
{
|
|
|
|
// TODO A proper algorithm here
|
|
|
|
//
|
|
|
|
// Idea: Return a size hint based on the golden ratio to
|
|
|
|
// make it aesthetically good
|
|
|
|
// eg. Longer side is 1.61x the length of the shorter side
|
|
|
|
//
|
|
|
|
|
|
|
|
// testing
|
|
|
|
return QSizeF(500,500);
|
|
|
|
}
|
2007-12-04 00:53:56 +01:00
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
LayoutItem* FlowLayout::takeAt(int i)
|
2007-09-01 14:34:22 +02:00
|
|
|
{
|
2007-12-04 00:53:56 +01:00
|
|
|
if (i >= d->items.count()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-01 14:34:22 +02:00
|
|
|
return d->items.takeAt(i);
|
2007-12-29 08:14:21 +01:00
|
|
|
// FIXME: Should updateGeometry() be called?
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
T qSum(const QList<T>& container)
|
|
|
|
{
|
|
|
|
T total = 0;
|
|
|
|
foreach( const T& item , container ) {
|
|
|
|
total += item;
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2007-11-20 00:45:56 +01:00
|
|
|
void FlowLayout::relayout()
|
2007-09-01 14:34:22 +02:00
|
|
|
{
|
2008-02-29 18:50:57 +01:00
|
|
|
const QRectF rect = adjustToMargins(geometry());
|
2007-11-19 04:30:24 +01:00
|
|
|
|
2008-02-09 15:29:03 +01:00
|
|
|
const qreal space = spacing();
|
|
|
|
const qreal rectWidth = rect.width();
|
|
|
|
const qreal rectHeight = rect.height();
|
|
|
|
const int count = d->items.count();
|
|
|
|
//kDebug() << "Flow layout geometry set to " << geometry();
|
2007-09-01 14:34:22 +02:00
|
|
|
|
|
|
|
// calculate average size of items
|
2008-02-09 15:29:03 +01:00
|
|
|
qreal colWidth = 0;
|
|
|
|
qreal rowHeight = 0;
|
|
|
|
qreal maxItemWidth = 0;
|
2008-02-20 04:00:26 +01:00
|
|
|
qreal minItemWidth = 0;
|
2008-02-09 15:29:03 +01:00
|
|
|
//qreal maxItemHeight = 0;
|
|
|
|
qreal minItemHeight = 0;
|
|
|
|
int colCnt = 0;
|
|
|
|
int rowCnt = 0;
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
foreach(LayoutItem *item , d->items) {
|
2008-02-09 15:29:03 +01:00
|
|
|
maxItemWidth = (maxItemWidth < item->maximumSize().width()) ?
|
|
|
|
item->maximumSize().width() : maxItemWidth;
|
2008-02-20 04:00:26 +01:00
|
|
|
minItemWidth = (minItemWidth < item->minimumSize().width()) ?
|
|
|
|
item->minimumSize().width() : minItemWidth;
|
2008-02-09 15:29:03 +01:00
|
|
|
//maxItemHeight = (maxItemHeight < item->maximumSize().height()) ?
|
|
|
|
// item->maximumSize().height() : maxItemHeight;
|
|
|
|
minItemHeight = (minItemHeight < item->minimumSize().height()) ?
|
|
|
|
item->minimumSize().height() : minItemHeight;
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
|
|
|
|
2008-02-11 04:22:41 +01:00
|
|
|
const int rowMax = ((minItemHeight != 0) && (minItemHeight != rectHeight)) ?
|
|
|
|
(int)(rectHeight / (minItemHeight + space)) : 1;
|
2007-11-20 00:45:56 +01:00
|
|
|
|
2008-02-20 04:00:26 +01:00
|
|
|
if( maxItemWidth == 0 && minItemWidth != 0 ) {
|
|
|
|
kDebug() << "******POSSIBLE DIVIDE BY ZERO: maxItemWidth = minItemWidth ********";
|
|
|
|
maxItemWidth = minItemWidth + space;
|
|
|
|
} else if( maxItemWidth == 0 && minItemWidth == 0 ) {
|
|
|
|
kDebug() << "******POSSIBLE DIVIDE BY ZERO: maxItemWidth = rectWidth ********";
|
|
|
|
maxItemWidth = rectWidth + space;
|
2008-02-09 15:29:03 +01:00
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-02-09 15:29:03 +01:00
|
|
|
// try to use the maxwidth if there is room
|
2008-02-10 02:59:56 +01:00
|
|
|
// need usedSpace so we don't try to use the leftover
|
|
|
|
// area of our rect to make a item location.
|
2008-02-09 15:29:03 +01:00
|
|
|
maxItemWidth += space;
|
|
|
|
qreal usedSpace = floor( (rectWidth / maxItemWidth ) ) * maxItemWidth;
|
|
|
|
for(int i = 1; (i <= rowMax) && (colWidth == 0); i++) {
|
|
|
|
if( i * (usedSpace / maxItemWidth ) >= count) {
|
|
|
|
colWidth = maxItemWidth;
|
2008-02-11 02:32:00 +01:00
|
|
|
rowHeight = (rectHeight + space) / i;
|
2008-02-09 15:29:03 +01:00
|
|
|
rowCnt = i;
|
|
|
|
colCnt = (int)(usedSpace / colWidth);
|
|
|
|
}
|
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-02-10 02:59:56 +01:00
|
|
|
//crazy algorithm to make items fit in available space
|
2008-02-09 15:29:03 +01:00
|
|
|
if( colWidth == 0) {
|
2008-02-10 02:59:56 +01:00
|
|
|
// These gave me the most trouble and should
|
|
|
|
// be taken into account if you try and change this:
|
|
|
|
// - maxRow = 3 with 9 items and 3 columns.
|
|
|
|
// - maxRow = 5 with 8 items and 3 colums.
|
2008-02-11 04:22:41 +01:00
|
|
|
// - maxRow = 1 with odd number columns.
|
2008-02-10 02:59:56 +01:00
|
|
|
|
|
|
|
const qreal tmp = (qreal)(count + (count % 2)) / rowMax;
|
|
|
|
if( (tmp - floor(tmp)) > 0.5) {
|
|
|
|
colCnt = (int)ceil(tmp) + 1;
|
|
|
|
} else {
|
|
|
|
colCnt = (int)ceil(tmp);
|
|
|
|
}
|
|
|
|
rowCnt = (int)ceil((qreal)count / colCnt);
|
2008-02-11 04:22:41 +01:00
|
|
|
if( (rowCnt == 1) && (colCnt&2) ) {
|
|
|
|
colCnt--;
|
|
|
|
}
|
2008-02-09 15:56:04 +01:00
|
|
|
colWidth = rectWidth / colCnt;
|
2008-02-11 02:32:00 +01:00
|
|
|
rowHeight = (rectHeight + space) / rowCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( minItemHeight > (rowHeight - space) ) {
|
|
|
|
rowHeight = minItemHeight + space;
|
2008-02-09 15:29:03 +01:00
|
|
|
}
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-02-11 04:22:41 +01:00
|
|
|
// kDebug() << "colWidth: " << colWidth << "rowHeight: " << rowHeight
|
|
|
|
// << "rowCnt: " << rowCnt << "rowMax: " << rowMax << "colCnt: " << colCnt;
|
2007-09-01 14:34:22 +02:00
|
|
|
|
|
|
|
|
2008-02-09 15:29:03 +01:00
|
|
|
// lay the items out in left-to-right , top-to-bottom order
|
|
|
|
int insertColumn = 0;
|
2008-02-09 15:56:04 +01:00
|
|
|
qreal rowPos = 0;
|
2008-02-29 18:50:57 +01:00
|
|
|
foreach(LayoutItem *item , d->items) {
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-02-09 15:29:03 +01:00
|
|
|
if(insertColumn >= colCnt) {
|
|
|
|
insertColumn = 0;
|
2008-02-09 15:56:04 +01:00
|
|
|
rowPos += rowHeight;
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// position the item
|
2008-02-09 15:29:03 +01:00
|
|
|
const QRectF newGeometry(rect.left() + (insertColumn * colWidth),
|
2007-11-20 00:45:56 +01:00
|
|
|
rect.top() + rowPos,
|
2008-02-09 15:29:03 +01:00
|
|
|
colWidth - space,
|
2008-02-09 15:56:04 +01:00
|
|
|
rowHeight - space);
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-02-09 15:29:03 +01:00
|
|
|
//kDebug() << "newGeometry: " << newGeometry;
|
|
|
|
insertColumn++;
|
2007-09-01 14:34:22 +02:00
|
|
|
|
2008-02-29 18:50:57 +01:00
|
|
|
if ( animator() ){
|
2007-09-01 14:34:22 +02:00
|
|
|
animator()->setGeometry( item , newGeometry );
|
2008-02-29 18:50:57 +01:00
|
|
|
} else {
|
2007-09-01 14:34:22 +02:00
|
|
|
item->setGeometry( newGeometry );
|
2008-02-29 18:50:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlowLayout::releaseManagedItems()
|
|
|
|
{
|
|
|
|
foreach (LayoutItem *item, d->items) {
|
|
|
|
item->unsetManagingLayout(this);
|
2008-02-11 06:50:47 +01:00
|
|
|
}
|
2008-02-29 18:50:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Qt::Orientations FlowLayout::expandingDirections() const
|
|
|
|
{
|
|
|
|
return Qt::Vertical | Qt::Horizontal;
|
2007-09-01 14:34:22 +02:00
|
|
|
}
|
|
|
|
|
2007-11-06 03:46:01 +01:00
|
|
|
qreal FlowLayout::columnWidth() const
|
|
|
|
{
|
|
|
|
return d->columnWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlowLayout::setColumnWidth( const qreal width )
|
|
|
|
{
|
|
|
|
d->columnWidth = width;
|
|
|
|
}
|