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
{
@ -76,9 +78,8 @@ QRectF VBoxLayout::geometry() const
void VBoxLayout::setGeometry(const QRectF& geometry)
{
if(!geometry.isValid() || geometry.isEmpty()) {
qDebug("Invalid Geometry!");
if (!geometry.isValid() || geometry.isEmpty()) {
kDebug() << "Invalid Geometry!" << endl;
return;
}
@ -89,10 +90,9 @@ void VBoxLayout::setGeometry(const QRectF& geometry)
QList<QSizeF> sizes;
QSizeF available = geometry.size() - QSizeF(2 * margin(), 2 * margin());
foreach(LayoutItem *l, d->childList) {
if(l->expandingDirections() & Qt::Vertical) {
foreach (LayoutItem *l, d->childList) {
kDebug() << "testing layout item " << l << endl;
if (l->expandingDirections() & Qt::Vertical) {
expandingChilds += l;
} else {
@ -100,17 +100,15 @@ void VBoxLayout::setGeometry(const QRectF& geometry)
}
}
foreach(LayoutItem *l, childs) {
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) {
foreach (LayoutItem *l, expandingChilds) {
sizes.insert(indexOf(l),QSizeF(available.width(), expandHeight));
}
@ -118,7 +116,7 @@ void VBoxLayout::setGeometry(const QRectF& geometry)
QPointF start = geometry.topLeft();
start += QPointF(margin(), spacing());
for(int i = 0; i < sizes.size(); i++) {
for (int i = 0; i < sizes.size(); i++) {
LayoutItem *l = itemAt(i);
@ -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);