Add Horizontal Layout class.

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=692743
This commit is contained in:
Matias Valdenegro Toro 2007-07-26 06:58:33 +00:00
parent 569e685e66
commit 4f778d6e2d
2 changed files with 301 additions and 0 deletions

213
widgets/hboxlayout.cpp Executable file
View File

@ -0,0 +1,213 @@
/*
* Copyright (C) 2007 by Matias Valdenegro T. <mvaldenegro@informatica.utem.cl>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 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
*
* 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 "hboxlayout.h"
#include <QtCore/QList>
#include <KDebug>
namespace Plasma
{
class HBoxLayout::Private
{
public:
Private() {}
~Private() {}
QRectF geometry;
QList<LayoutItem *> childList;
};
HBoxLayout::HBoxLayout(LayoutItem *parent)
: Layout(parent),
d(new Private)
{
}
HBoxLayout::~HBoxLayout()
{
foreach (LayoutItem *l, d->childList) {
l->resetLayout();
}
delete d;
}
Qt::Orientations HBoxLayout::expandingDirections() const
{
return Qt::Horizontal;
}
QSizeF HBoxLayout::minimumSize() const
{
return QSizeF();
}
QSizeF HBoxLayout::maximumSize() const
{
return QSizeF();
}
bool HBoxLayout::hasHeightForWidth() const
{
return true;
}
qreal HBoxLayout::heightForWidth(qreal w) const
{
return qreal();
}
QRectF HBoxLayout::geometry() const
{
return d->geometry;
}
void HBoxLayout::setGeometry(const QRectF& geometry)
{
if (!geometry.isValid() || geometry.isEmpty()) {
kDebug() << "Invalid Geometry " << geometry << endl;
return;
}
kDebug() << this << " Geometry process " << geometry << " for " << d->childList.count() << " childrens"<< endl;
QList<LayoutItem *> children;
QList<LayoutItem *> expandingChildren;
QList<QSizeF> sizes;
QSizeF available = geometry.size() - QSizeF(2 * margin(), 2 * margin());
foreach (LayoutItem *l, d->childList) {
kDebug() << "testing layout item " << l << endl;
if (l->expandingDirections() & Qt::Horizontal) {
expandingChildren += l;
} else {
children += l;
}
}
foreach (LayoutItem *l, children) {
QSizeF hint = l->sizeHint();
sizes.insert(indexOf(l), QSizeF(available.width(), hint.height()));
available -= QSizeF(hint.width() + spacing(), 0.0f);
}
qreal expandWidth = (available.width() - ((expandingChildren.count() - 1) * spacing())) / expandingChildren.count();
foreach (LayoutItem *l, expandingChildren) {
sizes.insert(indexOf(l), QSizeF(expandWidth, available.height()));
}
QPointF start = QPointF(0.0f, 0.0f);
start += QPointF(margin(), spacing());
for (int i = 0; i < sizes.size(); i++) {
LayoutItem *l = itemAt(i);
kDebug() << "Setting Geometry for child " << l << " to " << QRectF(start, sizes[i]) << endl;
l->setGeometry(QRectF(start, sizes[i]));
start += QPointF(sizes[i].width() + spacing(), 0.0);
}
d->geometry = geometry;
}
QSizeF HBoxLayout::sizeHint() const
{
qreal hintHeight = 0.0;
qreal hintWidth = 0.0;
foreach(LayoutItem *l, d->childList) {
QSizeF hint = l->sizeHint();
hintHeight = qMax(hint.height(), hintHeight);
hintWidth += hint.width() + spacing();
}
return QSizeF(hintWidth, hintHeight);
}
int HBoxLayout::count() const
{
return d->childList.count();
}
bool HBoxLayout::isEmpty() const
{
return count() == 0;
}
void HBoxLayout::insertItem(int index, LayoutItem *l)
{
if (!l) {
return;
}
l->setLayout(this);
d->childList.insert(index, l);
setGeometry(geometry());
}
void HBoxLayout::addItem(LayoutItem *l)
{
if (!l) {
return;
}
l->setLayout(this);
d->childList.append(l);
qDebug("Added Child LayoutItem : %p", l);
setGeometry(geometry());
}
void HBoxLayout::removeItem(LayoutItem *l)
{
d->childList.removeAll(l);
}
int HBoxLayout::indexOf(LayoutItem *l) const
{
return d->childList.indexOf(l);
}
LayoutItem *HBoxLayout::itemAt(int i) const
{
return d->childList[i];
}
LayoutItem *HBoxLayout::takeAt(int i)
{
return d->childList.takeAt(i);
setGeometry(geometry());
}
QSizeF HBoxLayout::size() const
{
return geometry().size();
}
}

88
widgets/hboxlayout.h Executable file
View File

@ -0,0 +1,88 @@
/*
* Copyright (C) 2007 by Matias Valdenegro T. <mvaldenegro@informatica.utem.cl>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2 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
*
* 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.
*/
#ifndef __H_BOX_LAYOUT__
#define __H_BOX_LAYOUT__
#include <QtCore/QRectF>
#include <QtCore/QSizeF>
#include <plasma/plasma_export.h>
#include <plasma/widgets/layout.h>
namespace Plasma
{
/**
* Vertical Box Layout
*
* @author Matias Valdenegro T. <mvaldenegro@informatica.utem.cl>
*
* This class implements a Horizontal Box Layout, it just lays items horizontally, from left to right.
*/
class PLASMA_EXPORT HBoxLayout : public Layout
{
public:
/**
* Constructor.
*/
HBoxLayout(LayoutItem *parent = 0);
/**
* Virtual Destructor.
*/
virtual ~HBoxLayout();
Qt::Orientations expandingDirections() const;
QSizeF minimumSize() const;
QSizeF maximumSize() const;
bool hasHeightForWidth() const;
qreal heightForWidth(qreal w) const;
QRectF geometry() const;
void setGeometry(const QRectF& geometry);
QSizeF sizeHint() const;
int count() const;
bool isEmpty() const;
void insertItem(int index, LayoutItem *l);
void addItem(LayoutItem *l);
void removeItem(LayoutItem *l);
int indexOf(LayoutItem *l) const;
LayoutItem *itemAt(int i) const;
LayoutItem *takeAt(int i);
QSizeF size() const;
private:
class Private;
Private *const d;
};
}
#endif /* __H_BOX_LAYOUT__ */