keep track of what layout we belong to so that we can remove ourselves from the layout when we exit; prevents crashes when items are added then deleted later. since we aren't guaranteed a QObject, we can't do the obvious thing (e.g. listen for the destroyed() signal)

svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=668928
This commit is contained in:
Aaron J. Seigo 2007-05-28 05:46:24 +00:00
parent 10dd76c794
commit c300ff48bc
4 changed files with 106 additions and 63 deletions

View File

@ -17,6 +17,7 @@
*/
#include "layoutitem.h"
#include "layout.h"
namespace Plasma
{
@ -24,8 +25,14 @@ namespace Plasma
class LayoutItem::Private
{
public:
Private() {}
Private()
: layout(0)
{
}
~Private() {}
Layout* layout;
};
@ -36,6 +43,10 @@ LayoutItem::LayoutItem()
LayoutItem::~LayoutItem()
{
if (d->layout) {
d->layout->removeItem(this);
}
delete d;
}
@ -59,4 +70,18 @@ qreal LayoutItem::widthForHeight(qreal h) const
return 0.0;
}
void LayoutItem::setLayout(Layout* layout)
{
if (d->layout) {
d->layout->removeItem(this);
}
d->layout = layout;
}
Layout* LayoutItem::layout()
{
return d->layout;
}
}

View File

@ -27,6 +27,8 @@
namespace Plasma
{
class Layout;
/**
* Base class for Plasma layout-managed items
*/
@ -52,6 +54,9 @@ class KDE_EXPORT LayoutItem
virtual QSizeF sizeHint() const = 0;
void setLayout(Layout* layout);
Layout* layout();
private:
class Private;
Private *const d;

View File

@ -20,6 +20,8 @@
#include <QtCore/QList>
#include <KDebug>
namespace Plasma
{
@ -77,8 +79,7 @@ QRectF VBoxLayout::geometry() const
void VBoxLayout::setGeometry(const QRectF& geometry)
{
if (!geometry.isValid() || geometry.isEmpty()) {
qDebug("Invalid Geometry!");
kDebug() << "Invalid Geometry!" << endl;
return;
}
@ -90,9 +91,8 @@ void VBoxLayout::setGeometry(const QRectF& geometry)
QSizeF available = geometry.size() - QSizeF(2 * margin(), 2 * margin());
foreach (LayoutItem *l, d->childList) {
kDebug() << "testing layout item " << l << endl;
if (l->expandingDirections() & Qt::Vertical) {
expandingChilds += l;
} else {
@ -101,9 +101,7 @@ void VBoxLayout::setGeometry(const QRectF& geometry)
}
foreach (LayoutItem *l, childs) {
QSizeF hint = l->sizeHint();
sizes.insert(indexOf(l), QSizeF(available.width(), hint.height()));
available -= QSizeF(0.0, hint.height() + spacing());
}
@ -155,12 +153,26 @@ 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());
}

View File

@ -57,6 +57,7 @@ class KDE_EXPORT VBoxLayout : public Layout
bool isEmpty() const;
void insertItem(int index, LayoutItem *l);
void addItem(LayoutItem *l);
void removeItem(LayoutItem *l);