From 4f778d6e2d0b6adc469c5a0f809e974771d504a2 Mon Sep 17 00:00:00 2001 From: Matias Valdenegro Toro Date: Thu, 26 Jul 2007 06:58:33 +0000 Subject: [PATCH] Add Horizontal Layout class. svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=692743 --- widgets/hboxlayout.cpp | 213 +++++++++++++++++++++++++++++++++++++++++ widgets/hboxlayout.h | 88 +++++++++++++++++ 2 files changed, 301 insertions(+) create mode 100755 widgets/hboxlayout.cpp create mode 100755 widgets/hboxlayout.h diff --git a/widgets/hboxlayout.cpp b/widgets/hboxlayout.cpp new file mode 100755 index 000000000..209630f9c --- /dev/null +++ b/widgets/hboxlayout.cpp @@ -0,0 +1,213 @@ +/* + * Copyright (C) 2007 by Matias Valdenegro T. + * + * 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 + +#include + +namespace Plasma +{ + +class HBoxLayout::Private +{ + public: + Private() {} + ~Private() {} + + QRectF geometry; + QList 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 children; + QList expandingChildren; + QList 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(); +} + +} diff --git a/widgets/hboxlayout.h b/widgets/hboxlayout.h new file mode 100755 index 000000000..2c207d1e1 --- /dev/null +++ b/widgets/hboxlayout.h @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2007 by Matias Valdenegro T. + * + * 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 +#include + +#include +#include + +namespace Plasma +{ + + +/** + * Vertical Box Layout + * + * @author Matias Valdenegro T. + * + * 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__ */