From f82a4cfb5544b29fa4b7c3919a6ee41b94cbb7b9 Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Wed, 25 Jul 2007 22:20:15 +0000 Subject: [PATCH] separate out the stuff the hbox will need from vbox and put it into box. sprinkle "layout" and some capital letters throughout the above if the above sounds random to you ;) svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=692578 --- CMakeLists.txt | 2 + widgets/boxlayout.cpp | 142 +++++++++++++++++++++++++++++++++++++++++ widgets/boxlayout.h | 88 +++++++++++++++++++++++++ widgets/layout.h | 2 +- widgets/vboxlayout.cpp | 119 ++++------------------------------ widgets/vboxlayout.h | 34 ++-------- 6 files changed, 253 insertions(+), 134 deletions(-) create mode 100644 widgets/boxlayout.cpp create mode 100644 widgets/boxlayout.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 434b38d85..64c913669 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ set(plasma_LIB_SRCS svg.cpp theme.cpp karambamanager.cpp + widgets/boxlayout.cpp widgets/checkbox.cpp widgets/icon.cpp widgets/lineedit.cpp @@ -92,6 +93,7 @@ install(FILES DESTINATION ${INCLUDE_INSTALL_DIR}/plasma) install(FILES + widgets/boxlayout.h widgets/icon.h widgets/layout.h widgets/layoutitem.h diff --git a/widgets/boxlayout.cpp b/widgets/boxlayout.cpp new file mode 100644 index 000000000..db5851e81 --- /dev/null +++ b/widgets/boxlayout.cpp @@ -0,0 +1,142 @@ +/* + * 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 "boxlayout.h" + +#include + +#include + +namespace Plasma +{ + +class BoxLayout::Private +{ + public: + QRectF geometry; + QList childList; +}; + + +BoxLayout::BoxLayout(LayoutItem *parent) + : Layout(parent), + d(new Private) +{ +} + +BoxLayout::~BoxLayout() +{ + foreach (LayoutItem *l, d->childList) { + l->resetLayout(); + } + + delete d; +} + +Qt::Orientations BoxLayout::expandingDirections() const +{ + return Qt::Vertical | Qt::Horizontal; +} + +QSizeF BoxLayout::minimumSize() const +{ + return QSizeF(); +} + +QSizeF BoxLayout::maximumSize() const +{ + return QSizeF(); +} + +QRectF BoxLayout::geometry() const +{ + return d->geometry; +} + +void BoxLayout::setGeometry(const QRectF& geometry) +{ + d->geometry = geometry; +} + +int BoxLayout::count() const +{ + return d->childList.count(); +} + +bool BoxLayout::isEmpty() const +{ + return count() == 0; +} + +void BoxLayout::insertItem(int index, LayoutItem *l) +{ + if (!l) { + return; + } + + l->setLayout(this); + d->childList.insert(index, l); + setGeometry(geometry()); +} + +void BoxLayout::addItem(LayoutItem *l) +{ + if (!l) { + return; + } + + l->setLayout(this); + d->childList.append(l); + qDebug("Added Child LayoutItem : %p", l); + setGeometry(geometry()); +} + +void BoxLayout::removeItem(LayoutItem *l) +{ + d->childList.removeAll(l); + setGeometry(geometry()); +} + +int BoxLayout::indexOf(LayoutItem *l) const +{ + return d->childList.indexOf(l); +} + +LayoutItem *BoxLayout::itemAt(int i) const +{ + return d->childList[i]; +} + +LayoutItem *BoxLayout::takeAt(int i) +{ + return d->childList.takeAt(i); + + setGeometry(geometry()); +} + +QSizeF BoxLayout::size() const +{ + return d->geometry.size(); +} + +QList BoxLayout::children() const +{ + return d->childList; +} + +} // Plasma namespace diff --git a/widgets/boxlayout.h b/widgets/boxlayout.h new file mode 100644 index 000000000..92646e5f3 --- /dev/null +++ b/widgets/boxlayout.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 PLASMA_BOX_LAYOUT +#define PLASMA_BOX_LAYOUT + +#include + +#include +#include + +namespace Plasma +{ + + +/** + * Vertical Box Layout + * + * @author Matias Valdenegro T. + * + * This class implements a generic box Layout used as a common API for VBox and HBox implementations. + */ +class PLASMA_EXPORT BoxLayout : public Layout +{ + public: + + /** + * Constructor. + */ + explicit BoxLayout(LayoutItem *parent = 0); + + /** + * Destructor. + */ + ~BoxLayout(); + + virtual Qt::Orientations expandingDirections() const; + + QSizeF minimumSize() const; + QSizeF maximumSize() const; + + QRectF geometry() const; + void setGeometry(const QRectF& geometry); + + //TODO: if we turn this into an actually usable layout we need to implement this + //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; + + protected: + QList children() const; + + private: + class Private; + Private *const d; +}; + +} + +#endif /* _H_BOX_LAYOUT__ */ diff --git a/widgets/layout.h b/widgets/layout.h index b71009723..f9cf3cb2b 100644 --- a/widgets/layout.h +++ b/widgets/layout.h @@ -44,7 +44,7 @@ class PLASMA_EXPORT Layout : public LayoutItem /** * Constructor. */ - Layout(LayoutItem *parent); + explicit Layout(LayoutItem *parent); /** * Virtual Destructor. diff --git a/widgets/vboxlayout.cpp b/widgets/vboxlayout.cpp index 1c646586c..c7c461d2c 100644 --- a/widgets/vboxlayout.cpp +++ b/widgets/vboxlayout.cpp @@ -25,60 +25,30 @@ namespace Plasma { -class VBoxLayout::Private -{ - public: - Private() {} - ~Private() {} - - QRectF geometry; - QList childList; -}; - - VBoxLayout::VBoxLayout(LayoutItem *parent) - : Layout(parent), - d(new Private) + : BoxLayout(parent), + d(0) { } VBoxLayout::~VBoxLayout() { - foreach (LayoutItem *l, d->childList) { - l->resetLayout(); - } - - delete d; } Qt::Orientations VBoxLayout::expandingDirections() const { - return Qt::Vertical; -} - -QSizeF VBoxLayout::minimumSize() const -{ - return QSizeF(); -} - -QSizeF VBoxLayout::maximumSize() const -{ - return QSizeF(); + return Qt::Vertical; } bool VBoxLayout::hasHeightForWidth() const { - return true; + return true; } qreal VBoxLayout::heightForWidth(qreal w) const { - return qreal(); -} - -QRectF VBoxLayout::geometry() const -{ - return d->geometry; + Q_UNUSED(w); + return qreal(); } void VBoxLayout::setGeometry(const QRectF& geometry) @@ -88,24 +58,23 @@ void VBoxLayout::setGeometry(const QRectF& geometry) return; } - kDebug() << this << " Geometry process " << geometry << " for " << d->childList.count() << " childrens"<< endl; + kDebug() << this << " Geometry process " << geometry << " for " << children().count() << " childrens"<< endl; - QList children; + QList fixedChildren; QList expandingChildren; QList sizes; QSizeF available = geometry.size() - QSizeF(2 * margin(), 2 * margin()); - foreach (LayoutItem *l, d->childList) { + foreach (LayoutItem *l, children()) { kDebug() << "testing layout item " << l << endl; if (l->expandingDirections() & Qt::Vertical) { expandingChildren += l; } else { - - children += l; + fixedChildren += l; } } - foreach (LayoutItem *l, children) { + foreach (LayoutItem *l, fixedChildren) { QSizeF hint = l->sizeHint(); sizes.insert(indexOf(l), QSizeF(available.width(), hint.height())); available -= QSizeF(0.0, hint.height() + spacing()); @@ -114,7 +83,6 @@ void VBoxLayout::setGeometry(const QRectF& geometry) qreal expandHeight = (available.height() - ((expandingChildren.count() - 1) * spacing())) / expandingChildren.count(); foreach (LayoutItem *l, expandingChildren) { - sizes.insert(indexOf(l),QSizeF(available.width(), expandHeight)); } @@ -123,7 +91,6 @@ void VBoxLayout::setGeometry(const QRectF& geometry) 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; @@ -132,7 +99,7 @@ void VBoxLayout::setGeometry(const QRectF& geometry) start += QPointF(0.0, sizes[i].height() + spacing()); } - d->geometry = geometry; + BoxLayout::setGeometry(geometry); } QSizeF VBoxLayout::sizeHint() const @@ -140,7 +107,7 @@ QSizeF VBoxLayout::sizeHint() const qreal hintHeight = 0.0; qreal hintWidth = 0.0; - foreach(LayoutItem *l, d->childList) { + foreach(LayoutItem *l, children()) { QSizeF hint = l->sizeHint(); @@ -151,64 +118,4 @@ QSizeF VBoxLayout::sizeHint() const return QSizeF(hintWidth, hintHeight); } -int VBoxLayout::count() const -{ - return d->childList.count(); -} - -bool VBoxLayout::isEmpty() const -{ - return count() == 0; -} - -void VBoxLayout::insertItem(int index, LayoutItem *l) -{ - if (!l) { - return; - } - - l->setLayout(this); - d->childList.insert(index, l); - setGeometry(geometry()); -} - -void VBoxLayout::addItem(LayoutItem *l) -{ - if (!l) { - return; - } - - l->setLayout(this); - d->childList.append(l); - qDebug("Added Child LayoutItem : %p", l); - setGeometry(geometry()); -} - -void VBoxLayout::removeItem(LayoutItem *l) -{ - d->childList.removeAll(l); -} - -int VBoxLayout::indexOf(LayoutItem *l) const -{ - return d->childList.indexOf(l); -} - -LayoutItem *VBoxLayout::itemAt(int i) const -{ - return d->childList[i]; -} - -LayoutItem *VBoxLayout::takeAt(int i) -{ - return d->childList.takeAt(i); - - setGeometry(geometry()); -} - -QSizeF VBoxLayout::size() const -{ - return geometry().size(); -} - } diff --git a/widgets/vboxlayout.h b/widgets/vboxlayout.h index 18dcc24c8..b9449c85e 100644 --- a/widgets/vboxlayout.h +++ b/widgets/vboxlayout.h @@ -16,14 +16,11 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef __V_BOX_LAYOUT__ -#define __V_BOX_LAYOUT__ - -#include -#include +#ifndef PLASMA_VBOXLAYOUT +#define PLASMA_VBOXLAYOUT #include -#include +#include namespace Plasma { @@ -36,46 +33,29 @@ namespace Plasma * * This class implements a Vertical Box Layout, it just lays items in vertical, from up to down. */ -class PLASMA_EXPORT VBoxLayout : public Layout +class PLASMA_EXPORT VBoxLayout : public BoxLayout { public: /** * Constructor. */ - VBoxLayout(LayoutItem *parent = 0); + explicit VBoxLayout(LayoutItem *parent = 0); /** * Virtual Destructor. */ - virtual ~VBoxLayout(); + ~VBoxLayout(); 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: @@ -85,4 +65,4 @@ class PLASMA_EXPORT VBoxLayout : public Layout } -#endif /* __V_BOX_LAYOUT__ */ +#endif /* PLASMA_VBOXLAYOUT */