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
This commit is contained in:
Aaron J. Seigo 2007-07-25 22:20:15 +00:00
parent eddb8f294f
commit f82a4cfb55
6 changed files with 253 additions and 134 deletions

View File

@ -28,6 +28,7 @@ set(plasma_LIB_SRCS
svg.cpp svg.cpp
theme.cpp theme.cpp
karambamanager.cpp karambamanager.cpp
widgets/boxlayout.cpp
widgets/checkbox.cpp widgets/checkbox.cpp
widgets/icon.cpp widgets/icon.cpp
widgets/lineedit.cpp widgets/lineedit.cpp
@ -92,6 +93,7 @@ install(FILES
DESTINATION ${INCLUDE_INSTALL_DIR}/plasma) DESTINATION ${INCLUDE_INSTALL_DIR}/plasma)
install(FILES install(FILES
widgets/boxlayout.h
widgets/icon.h widgets/icon.h
widgets/layout.h widgets/layout.h
widgets/layoutitem.h widgets/layoutitem.h

142
widgets/boxlayout.cpp Normal file
View File

@ -0,0 +1,142 @@
/*
* 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 "boxlayout.h"
#include <QtCore/QList>
#include <KDebug>
namespace Plasma
{
class BoxLayout::Private
{
public:
QRectF geometry;
QList<LayoutItem *> 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<LayoutItem *> BoxLayout::children() const
{
return d->childList;
}
} // Plasma namespace

88
widgets/boxlayout.h Normal 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 PLASMA_BOX_LAYOUT
#define PLASMA_BOX_LAYOUT
#include <QtCore/QList>
#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 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<LayoutItem *> children() const;
private:
class Private;
Private *const d;
};
}
#endif /* _H_BOX_LAYOUT__ */

View File

@ -44,7 +44,7 @@ class PLASMA_EXPORT Layout : public LayoutItem
/** /**
* Constructor. * Constructor.
*/ */
Layout(LayoutItem *parent); explicit Layout(LayoutItem *parent);
/** /**
* Virtual Destructor. * Virtual Destructor.

View File

@ -25,30 +25,14 @@
namespace Plasma namespace Plasma
{ {
class VBoxLayout::Private
{
public:
Private() {}
~Private() {}
QRectF geometry;
QList<LayoutItem *> childList;
};
VBoxLayout::VBoxLayout(LayoutItem *parent) VBoxLayout::VBoxLayout(LayoutItem *parent)
: Layout(parent), : BoxLayout(parent),
d(new Private) d(0)
{ {
} }
VBoxLayout::~VBoxLayout() VBoxLayout::~VBoxLayout()
{ {
foreach (LayoutItem *l, d->childList) {
l->resetLayout();
}
delete d;
} }
Qt::Orientations VBoxLayout::expandingDirections() const Qt::Orientations VBoxLayout::expandingDirections() const
@ -56,16 +40,6 @@ Qt::Orientations VBoxLayout::expandingDirections() const
return Qt::Vertical; return Qt::Vertical;
} }
QSizeF VBoxLayout::minimumSize() const
{
return QSizeF();
}
QSizeF VBoxLayout::maximumSize() const
{
return QSizeF();
}
bool VBoxLayout::hasHeightForWidth() const bool VBoxLayout::hasHeightForWidth() const
{ {
return true; return true;
@ -73,14 +47,10 @@ bool VBoxLayout::hasHeightForWidth() const
qreal VBoxLayout::heightForWidth(qreal w) const qreal VBoxLayout::heightForWidth(qreal w) const
{ {
Q_UNUSED(w);
return qreal(); return qreal();
} }
QRectF VBoxLayout::geometry() const
{
return d->geometry;
}
void VBoxLayout::setGeometry(const QRectF& geometry) void VBoxLayout::setGeometry(const QRectF& geometry)
{ {
if (!geometry.isValid() || geometry.isEmpty()) { if (!geometry.isValid() || geometry.isEmpty()) {
@ -88,24 +58,23 @@ void VBoxLayout::setGeometry(const QRectF& geometry)
return; return;
} }
kDebug() << this << " Geometry process " << geometry << " for " << d->childList.count() << " childrens"<< endl; kDebug() << this << " Geometry process " << geometry << " for " << children().count() << " childrens"<< endl;
QList<LayoutItem *> children; QList<LayoutItem *> fixedChildren;
QList<LayoutItem *> expandingChildren; QList<LayoutItem *> expandingChildren;
QList<QSizeF> sizes; QList<QSizeF> sizes;
QSizeF available = geometry.size() - QSizeF(2 * margin(), 2 * margin()); QSizeF available = geometry.size() - QSizeF(2 * margin(), 2 * margin());
foreach (LayoutItem *l, d->childList) { foreach (LayoutItem *l, children()) {
kDebug() << "testing layout item " << l << endl; kDebug() << "testing layout item " << l << endl;
if (l->expandingDirections() & Qt::Vertical) { if (l->expandingDirections() & Qt::Vertical) {
expandingChildren += l; expandingChildren += l;
} else { } else {
fixedChildren += l;
children += l;
} }
} }
foreach (LayoutItem *l, children) { foreach (LayoutItem *l, fixedChildren) {
QSizeF hint = l->sizeHint(); QSizeF hint = l->sizeHint();
sizes.insert(indexOf(l), QSizeF(available.width(), hint.height())); sizes.insert(indexOf(l), QSizeF(available.width(), hint.height()));
available -= QSizeF(0.0, hint.height() + spacing()); 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(); qreal expandHeight = (available.height() - ((expandingChildren.count() - 1) * spacing())) / expandingChildren.count();
foreach (LayoutItem *l, expandingChildren) { foreach (LayoutItem *l, expandingChildren) {
sizes.insert(indexOf(l),QSizeF(available.width(), expandHeight)); sizes.insert(indexOf(l),QSizeF(available.width(), expandHeight));
} }
@ -123,7 +91,6 @@ void VBoxLayout::setGeometry(const QRectF& geometry)
start += QPointF(margin(), spacing()); start += QPointF(margin(), spacing());
for (int i = 0; i < sizes.size(); i++) { for (int i = 0; i < sizes.size(); i++) {
LayoutItem *l = itemAt(i); LayoutItem *l = itemAt(i);
kDebug() << "Setting Geometry for child " << l << " to " << QRectF(start, sizes[i]) << endl; 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()); start += QPointF(0.0, sizes[i].height() + spacing());
} }
d->geometry = geometry; BoxLayout::setGeometry(geometry);
} }
QSizeF VBoxLayout::sizeHint() const QSizeF VBoxLayout::sizeHint() const
@ -140,7 +107,7 @@ QSizeF VBoxLayout::sizeHint() const
qreal hintHeight = 0.0; qreal hintHeight = 0.0;
qreal hintWidth = 0.0; qreal hintWidth = 0.0;
foreach(LayoutItem *l, d->childList) { foreach(LayoutItem *l, children()) {
QSizeF hint = l->sizeHint(); QSizeF hint = l->sizeHint();
@ -151,64 +118,4 @@ QSizeF VBoxLayout::sizeHint() const
return QSizeF(hintWidth, hintHeight); 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();
}
} }

View File

@ -16,14 +16,11 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
#ifndef __V_BOX_LAYOUT__ #ifndef PLASMA_VBOXLAYOUT
#define __V_BOX_LAYOUT__ #define PLASMA_VBOXLAYOUT
#include <QtCore/QRectF>
#include <QtCore/QSizeF>
#include <plasma/plasma_export.h> #include <plasma/plasma_export.h>
#include <plasma/widgets/layout.h> #include <plasma/widgets/boxlayout.h>
namespace Plasma 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. * 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: public:
/** /**
* Constructor. * Constructor.
*/ */
VBoxLayout(LayoutItem *parent = 0); explicit VBoxLayout(LayoutItem *parent = 0);
/** /**
* Virtual Destructor. * Virtual Destructor.
*/ */
virtual ~VBoxLayout(); ~VBoxLayout();
Qt::Orientations expandingDirections() const; Qt::Orientations expandingDirections() const;
QSizeF minimumSize() const;
QSizeF maximumSize() const;
bool hasHeightForWidth() const; bool hasHeightForWidth() const;
qreal heightForWidth(qreal w) const; qreal heightForWidth(qreal w) const;
QRectF geometry() const;
void setGeometry(const QRectF& geometry); void setGeometry(const QRectF& geometry);
QSizeF sizeHint() const; 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; QSizeF size() const;
private: private:
@ -85,4 +65,4 @@ class PLASMA_EXPORT VBoxLayout : public Layout
} }
#endif /* __V_BOX_LAYOUT__ */ #endif /* PLASMA_VBOXLAYOUT */