From edaecc53af848fdd598e36fca94691b41ac702d2 Mon Sep 17 00:00:00 2001 From: Matias Valdenegro Toro Date: Tue, 22 May 2007 16:48:34 +0000 Subject: [PATCH] First commit of my work, added classes : - LayoutItem - Layout - VBoxLayout svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=667376 --- widgets/layout.cpp | 75 ++++++++++++++++ widgets/layout.h | 66 ++++++++++++++ widgets/layoutitem.cpp | 62 +++++++++++++ widgets/layoutitem.h | 59 +++++++++++++ widgets/vboxlayout.cpp | 191 +++++++++++++++++++++++++++++++++++++++++ widgets/vboxlayout.h | 74 ++++++++++++++++ widgets/widget.cpp | 172 ++++++++++++++++++++++++++++++++++++- widgets/widget.h | 57 +++++++++++- 8 files changed, 748 insertions(+), 8 deletions(-) create mode 100644 widgets/layout.cpp create mode 100644 widgets/layout.h create mode 100644 widgets/layoutitem.cpp create mode 100644 widgets/layoutitem.h create mode 100644 widgets/vboxlayout.cpp create mode 100644 widgets/vboxlayout.h diff --git a/widgets/layout.cpp b/widgets/layout.cpp new file mode 100644 index 000000000..0f4865832 --- /dev/null +++ b/widgets/layout.cpp @@ -0,0 +1,75 @@ +/* + * 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 "layout.h" + +#include + +namespace Plasma +{ + +class Layout::Private +{ + public: + Private() : margin(10.0), spacing(10.0), parent(0) {} + ~Private() {} + + qreal margin; + qreal spacing; + + LayoutItem *parent; +}; + + +Layout::Layout(LayoutItem *parent) + : LayoutItem(), + d(new Private) +{ + d->parent = parent; +} + +Layout::~Layout() +{ +} + +qreal Layout::margin() const +{ + return d->margin; +} + +void Layout::setMargin(qreal m) +{ + d->margin = m; +} + +qreal Layout::spacing() const +{ + return d->spacing; +} + +void Layout::setSpacing(qreal s) +{ + d->spacing = s; +} + +LayoutItem *Layout::parent() const +{ + return d->parent; +} + +}; diff --git a/widgets/layout.h b/widgets/layout.h new file mode 100644 index 000000000..7dcf50e54 --- /dev/null +++ b/widgets/layout.h @@ -0,0 +1,66 @@ +/* + * 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 __LAYOUT__ +#define __LAYOUT__ + +#include +#include + +#include + +#include "layoutitem.h" + +namespace Plasma +{ + +class Widget; + +class KDE_EXPORT Layout : public LayoutItem +{ + public: + Layout(LayoutItem *parent); + virtual ~Layout(); + + qreal margin() const; + void setMargin(qreal m); + + qreal spacing() const; + void setSpacing(qreal s); + + LayoutItem *parent() const; + + virtual int count() const = 0; + virtual bool isEmpty() const = 0; + + virtual void addItem(LayoutItem *l) = 0; + + virtual void removeItem(LayoutItem *l) = 0; + + virtual int indexOf(LayoutItem *l) const = 0; + virtual LayoutItem *itemAt(int i) const = 0; + virtual LayoutItem *takeAt(int i) = 0; + + private: + class Private; + Private *const d; +}; + +}; + +#endif /* __LAYOUT__ */ diff --git a/widgets/layoutitem.cpp b/widgets/layoutitem.cpp new file mode 100644 index 000000000..12946a087 --- /dev/null +++ b/widgets/layoutitem.cpp @@ -0,0 +1,62 @@ +/* + * 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 "layoutitem.h" + +namespace Plasma +{ + +class LayoutItem::Private +{ + public: + Private() {} + ~Private() {} +}; + + +LayoutItem::LayoutItem() + : d(new Private) +{ +} + +LayoutItem::~LayoutItem() +{ + delete d; +} + +bool LayoutItem::hasHeightForWidth() const +{ + return false; +} + +qreal LayoutItem::heightForWidth(qreal w) const +{ + return 0.0; +} + +bool LayoutItem::hasWidthForHeight() const +{ + return false; +} + +qreal LayoutItem::widthForHeight(qreal h) const +{ + return 0.0; +} + +}; diff --git a/widgets/layoutitem.h b/widgets/layoutitem.h new file mode 100644 index 000000000..d73b43dda --- /dev/null +++ b/widgets/layoutitem.h @@ -0,0 +1,59 @@ +/* + * 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 __LAYOUT_ITEM__ +#define __LAYOUT_ITEM__ + +#include +#include + +#include + +namespace Plasma +{ + +class KDE_EXPORT LayoutItem +{ + public: + LayoutItem(); + virtual ~LayoutItem(); + + virtual Qt::Orientations expandingDirections() const = 0; + + virtual QSizeF minimumSize() const = 0; + virtual QSizeF maximumSize() const = 0; + + virtual bool hasHeightForWidth() const; + virtual qreal heightForWidth(qreal w) const; + + virtual bool hasWidthForHeight() const; + virtual qreal widthForHeight(qreal h) const; + + virtual QRectF geometry() const = 0; + virtual void setGeometry(const QRectF& geometry) = 0; + + virtual QSizeF sizeHint() const = 0; + + private: + class Private; + Private *const d; +}; + +}; + +#endif /* __LAYOUT_ITEM__ */ diff --git a/widgets/vboxlayout.cpp b/widgets/vboxlayout.cpp new file mode 100644 index 000000000..d45f34b5b --- /dev/null +++ b/widgets/vboxlayout.cpp @@ -0,0 +1,191 @@ +/* + * 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 "vboxlayout.h" + +#include + +namespace Plasma +{ + +class VBoxLayout::Private +{ + public: + Private() {} + ~Private() {} + + QRectF geometry; + QList childList; +}; + + +VBoxLayout::VBoxLayout(LayoutItem *parent) + : Layout(parent), + d(new Private) +{ +} + +VBoxLayout::~VBoxLayout() +{ +} + +Qt::Orientations VBoxLayout::expandingDirections() const +{ + return Qt::Vertical; +} + +QSizeF VBoxLayout::minimumSize() const +{ +} + +QSizeF VBoxLayout::maximumSize() const +{ +} + +bool VBoxLayout::hasHeightForWidth() const +{ + return true; +} + +qreal VBoxLayout::heightForWidth(qreal w) const +{ +} + +QRectF VBoxLayout::geometry() const +{ + return d->geometry; +} + +void VBoxLayout::setGeometry(const QRectF& geometry) +{ + if(!geometry.isValid() || geometry.isEmpty()) { + qDebug("Invalid Geometry!"); + + return; + } + + qDebug("Geometry %p : %f, %f by %f, %f", this, geometry.x(), geometry.y(), geometry.width(), geometry.height()); + + QList childs; + QList expandingChilds; + QList sizes; + QSizeF available = geometry.size() - QSizeF(2 * margin(), 2 * margin()); + + foreach(LayoutItem *l, d->childList) { + + if(l->expandingDirections() & Qt::Vertical) { + + expandingChilds += l; + } else { + + childs += l; + } + } + + foreach(LayoutItem *l, childs) { + + QSizeF hint = l->sizeHint(); + + sizes.insert(indexOf(l), QSizeF(available.width(), hint.height())); + available -= QSizeF(0.0, hint.height() + spacing()); + } + + qreal expandHeight = (available.height() - ((expandingChilds.count() - 1) * spacing())) / expandingChilds.count(); + + foreach(LayoutItem *l, expandingChilds) { + + sizes.insert(indexOf(l),QSizeF(available.width(), expandHeight)); + } + + QPointF start = geometry.topLeft(); + start += QPointF(margin(), spacing()); + + for(int i = 0; i < sizes.size(); i++) { + + LayoutItem *l = itemAt(i); + + l->setGeometry(QRectF(start, sizes[i])); + start += QPointF(0.0, sizes[i].height() + spacing()); + } + + d->geometry = geometry; +} + +QSizeF VBoxLayout::sizeHint() const +{ + qreal hintHeight = 0.0; + qreal hintWidth = 0.0; + + foreach(LayoutItem *l, d->childList) { + + QSizeF hint = l->sizeHint(); + + hintWidth = qMax(hint.width(), hintWidth); + hintHeight += hint.height() + spacing(); + } + + return QSizeF(hintWidth, hintHeight); +} + +int VBoxLayout::count() const +{ + return d->childList.count(); +} + +bool VBoxLayout::isEmpty() const +{ + return count() == 0; +} + +void VBoxLayout::addItem(LayoutItem *l) +{ + 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 new file mode 100644 index 000000000..480418616 --- /dev/null +++ b/widgets/vboxlayout.h @@ -0,0 +1,74 @@ +/* + * 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 __V_BOX_LAYOUT__ +#define __V_BOX_LAYOUT__ + +#include +#include + +#include + +#include "layout.h" + +namespace Plasma +{ + +class Widget; + +class KDE_EXPORT VBoxLayout : public Layout +{ + public: + VBoxLayout(LayoutItem *parent = 0); + virtual ~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 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 /* __V_BOX_LAYOUT__ */ diff --git a/widgets/widget.cpp b/widgets/widget.cpp index 930b8fa88..fc2010b1f 100644 --- a/widgets/widget.cpp +++ b/widgets/widget.cpp @@ -1,5 +1,6 @@ /* * Copyright (C) 2007 by Alexander Wiedenbruch + * and Matias Valdenegro * * 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 @@ -16,7 +17,13 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include + #include "widget.h" +#include "widget.moc" + +#include "layout.h" + namespace Plasma { @@ -24,16 +31,30 @@ namespace Plasma class Widget::Private { public: - Private() { } + Private() : parent(0) { } ~Private() { } + + QRectF geometry; + + Widget *parent; + Layout *layout; + QList childList; }; -Widget::Widget(QGraphicsItem *parent) - : QGraphicsItem(parent), - DataVisualization(), +Widget::Widget(Widget *parent) + : DataVisualization(), + QGraphicsItem(parent), d(new Private) { + d->parent = parent; + d->layout = 0; + + if(parent) { + parent->addChild(this); + + parent->setGeometry(QRectF(QPointF(0.0, 0.0), parent->size())); + } } Widget::~Widget() @@ -41,5 +62,148 @@ Widget::~Widget() delete d; } +Qt::Orientations Widget::expandingDirections() const +{ + return 0; +} + +QSizeF Widget::maximumSize() const +{ + return QSizeF(); +} + +QSizeF Widget::minimumSize() const +{ + return QSizeF(0.0, 0.0); +} + +bool Widget::hasHeightForWidth() const +{ + return false; +} + +qreal Widget::heightForWidth(qreal w) const +{ + Q_UNUSED(w); + + return -1.0; +} + +bool Widget::hasWidthForHeight() const +{ + return false; +} + +qreal Widget::widthForHeight(qreal h) const +{ + Q_UNUSED(h); + + return -1.0; +} + +QRectF Widget::geometry() const +{ + return d->geometry; +} + +void Widget::setGeometry(const QRectF& geometry) +{ + prepareGeometryChange(); + + d->geometry = geometry; + + updateGeometry(); + update(); +} + +void Widget::updateGeometry() +{ + if(layout()) { + layout()->setGeometry(QRectF(QPointF(0.0, 0.0), size())); + } +} + +void Widget::invalidate() +{ + updateGeometry(); + + if(parent()) { + parent()->updateGeometry(); + } +} + +QSizeF Widget::sizeHint() const +{ + return QSizeF(0.0, 0.0); +} + +QSizeF Widget::size() const +{ + return geometry().size(); +} + +QRectF Widget::boundingRect() const +{ + return geometry(); +} + +void Widget::resize(const QSizeF& size) +{ + setGeometry(QRectF(d->geometry.topLeft(), size)); +} + +void Widget::resize(qreal w, qreal h) +{ + resize(QSizeF(w, h)); +} + +void Widget::data(const DataSource::Data& data) +{ + Q_UNUSED(data); +} + +void Widget::setLayout(Layout *l) +{ + d->layout = l; +} + +Layout *Widget::layout() const +{ + return d->layout; +} + +Widget *Widget::parent() const +{ + return d->parent; +} + +void Widget::addChild(Widget *w) +{ + if(!w) { + return; + } + + w->reparent(this); + w->setParentItem(this); + + d->childList.append(w); + + qDebug("Added Child Widget : %p", w); + + if(layout()) { + + layout()->addItem(w); + + updateGeometry(); + } +} + +void Widget::reparent(Widget *w) +{ + d->parent = w; + + update(); +} + } // Plasma namespace diff --git a/widgets/widget.h b/widgets/widget.h index 76ba38007..d1e496506 100644 --- a/widgets/widget.h +++ b/widgets/widget.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2007 by Alexander Wiedenbruch + * and Matias Valdenegro * * 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 @@ -21,22 +22,70 @@ #include +#include +#include + #include #include "datavisualization.h" +#include "layoutitem.h" namespace Plasma { +class Layout; + /** * Class that emulates a QWidget inside plasma */ -class KDE_EXPORT Widget : public QGraphicsItem, - public DataVisualization +class KDE_EXPORT Widget : public DataVisualization, + public QGraphicsItem, + public LayoutItem { + Q_OBJECT + public: - Widget(QGraphicsItem *parent = 0); - virtual ~Widget(); + Widget(Widget *parent = 0); + virtual ~Widget(); + + virtual Qt::Orientations expandingDirections() const; + + virtual QSizeF minimumSize() const; + virtual QSizeF maximumSize() const; + + virtual bool hasHeightForWidth() const; + virtual qreal heightForWidth(qreal w) const; + + virtual bool hasWidthForHeight() const; + virtual qreal widthForHeight(qreal h) const; + + QRectF geometry() const; + void setGeometry(const QRectF& geometry); + + void updateGeometry(); + + virtual void invalidate(); + + virtual QSizeF sizeHint() const; + + QSizeF size() const; + + virtual QRectF boundingRect() const; + + void resize(const QSizeF& size); + void resize(qreal w, qreal h); + + void setLayout(Layout *l); + Layout *layout() const; + + Widget *parent() const; + void reparent(Widget *w); + + void addChild(Widget *w); + + public Q_SLOTS: + virtual void data(const DataSource::Data&); + private: class Private;