From 6828d212bbec771ff6b856275fdac448332456b5 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Mon, 10 Sep 2007 20:38:59 +0000 Subject: [PATCH] Add FreeLayout layout, which resizes its children to their sizeHint() whenever it is invalidated. Useful for desktop-like workspaces so that widgets can be agnostic of whether they are being used in a panel or a 'constraint free' area. svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=710761 --- widgets/freelayout.cpp | 62 ++++++++++++++++++++++++++++++++++++++ widgets/freelayout.h | 67 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 widgets/freelayout.cpp create mode 100644 widgets/freelayout.h diff --git a/widgets/freelayout.cpp b/widgets/freelayout.cpp new file mode 100644 index 000000000..ea3c3d431 --- /dev/null +++ b/widgets/freelayout.cpp @@ -0,0 +1,62 @@ + +#include "freelayout.h" + +using namespace Plasma; + +class FreeLayout::Private +{ +public: + QList children; + QRectF geometry; +}; + +FreeLayout::FreeLayout(LayoutItem *parent) + : Layout(parent) + , d(new Private) +{ +} +Qt::Orientations FreeLayout::expandingDirections() const +{ + return Qt::Horizontal | Qt::Vertical; +} +void FreeLayout::addItem(LayoutItem *item) +{ + d->children << item; + item->setManagingLayout(this); +} +void FreeLayout::removeItem(LayoutItem *item) +{ + d->children.removeAll(item); +} +int FreeLayout::indexOf(LayoutItem *item) const +{ + return d->children.indexOf(item); +} +LayoutItem * FreeLayout::itemAt(int i) const +{ + return d->children[i]; +} +int FreeLayout::count() const +{ + return d->children.count(); +} +LayoutItem * FreeLayout::takeAt(int i) +{ + return d->children.takeAt(i); +} +void FreeLayout::setGeometry(const QRectF& geometry) +{ + foreach( LayoutItem *child , d->children ) { + child->setGeometry( QRectF(child->geometry().topLeft(),child->sizeHint()) ); + } + d->geometry = geometry; +} +QRectF FreeLayout::geometry() const +{ + return d->geometry; +} +QSizeF FreeLayout::sizeHint() const +{ + return maximumSize(); +} + diff --git a/widgets/freelayout.h b/widgets/freelayout.h new file mode 100644 index 000000000..8e17b5f3a --- /dev/null +++ b/widgets/freelayout.h @@ -0,0 +1,67 @@ +/* + * Copyright 2007 by Robert Knight + * + * 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_FREE_LAYOUT +#define PLASMA_FREE_LAYOUT + +#include + +#include +#include + +namespace Plasma +{ + +/** + * The FreeLayout class is a layout for use in desktop-like workspaces where + * I items can be moved around freely and applets and widgets are allowed + * to determine their own size. + * + * Whenever this layout is updated, all child items are resized to + * their sizeHint() and left in their existing positions. + */ +class PLASMA_EXPORT FreeLayout : public Layout +{ + public: + + /** + * Creates a new free layout + */ + explicit FreeLayout(LayoutItem *parent = 0); + + // reimplemented from Layout + virtual void addItem(LayoutItem *l); + virtual void removeItem(LayoutItem *l); + virtual int indexOf(LayoutItem *l) const; + virtual LayoutItem *itemAt(int i) const; + virtual LayoutItem *takeAt(int i); + virtual Qt::Orientations expandingDirections() const; + virtual QRectF geometry() const; + virtual void setGeometry(const QRectF& geometry); + virtual int count() const; + + virtual QSizeF sizeHint() const; + + private: + class Private; + Private *const d; +}; + +} + +#endif /* PLASMA_FREE_LAYOUT */