Commit d6b154e4 authored by Thomas Zander's avatar Thomas Zander

Fixes: doing a setParent() on a shape that already had a parent ended up...

Fixes:   doing a setParent() on a shape that already had a parent ended up having two collections own the shape.
Details: Make sure that setParent() on a shape removes itself from the old container before adding it to the new one.
Autotest included.

svn path=/trunk/koffice/; revision=777427
parent eeec21a8
......@@ -353,6 +353,8 @@ void KoShape::setParent(KoShapeContainer *parent) {
if(d->parent == parent)
return;
if(parent && dynamic_cast<KoShape*>(parent) != this) {
if (d->parent)
d->parent->removeChild(this);
d->parent = parent;
parent->addChild(this);
}
......
......@@ -140,7 +140,7 @@ void KoShapeContainer::addChild(KoShape *shape) {
return;
if(d->children == 0)
d->children = new ChildrenData();
if( shape->parent() )
if( shape->parent() && shape->parent() != this)
shape->parent()->removeChild( shape );
d->children->add(shape);
shape->setParent(this);
......
......@@ -38,5 +38,29 @@ void TestShapeContainer::testModel() {
QCOMPARE(model->proposeMoveCalled(), 0);
}
void TestShapeContainer::testSetParent()
{
MockContainerModel *model1 = new MockContainerModel();
MockContainer container1(model1);
MockContainerModel *model2 = new MockContainerModel();
MockContainer container2(model2);
MockShape shape;
// init test
QCOMPARE(model1->iterator().count(), 0);
QCOMPARE(model2->iterator().count(), 0);
shape.setParent(&container1);
QCOMPARE(model1->iterator().count(), 1);
QCOMPARE(model2->iterator().count(), 0);
QCOMPARE(shape.parent(), &container1);
shape.setParent(&container2);
QCOMPARE(model1->iterator().count(), 0);
QCOMPARE(container1.iterator().count(), 0);
QCOMPARE(model2->iterator().count(), 1);
QCOMPARE(container2.iterator().count(), 1);
QCOMPARE(shape.parent(), &container2);
}
QTEST_MAIN(TestShapeContainer)
#include "TestShapeContainer.moc"
......@@ -9,6 +9,7 @@ class TestShapeContainer : public QObject
private slots:
// tests
void testModel();
void testSetParent();
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment