canvas::BoxLayout: fix parent ref on add/remove.

This commit is contained in:
Thomas Geymayer 2014-07-17 15:05:13 +02:00
parent 1d93e8d61e
commit e415b08da4
2 changed files with 32 additions and 20 deletions

View File

@ -70,8 +70,12 @@ namespace canvas
item_data.layout_item = item;
item_data.stretch = std::max(0, stretch);
item->setCanvas(_canvas);
item->setParent(this);
if( SGWeakReferenced::count(this) )
item->setParent(this);
else
SG_LOG( SG_GUI,
SG_WARN,
"Adding item to expired or non-refcounted layout" );
if( index < 0 )
_layout_items.push_back(item_data);
@ -121,6 +125,7 @@ namespace canvas
LayoutItems::iterator it = _layout_items.begin() + index;
LayoutItemRef item = it->layout_item;
item->setParent(LayoutItemWeakRef());
item->onRemove();
_layout_items.erase(it);
@ -136,6 +141,7 @@ namespace canvas
it != _layout_items.end();
++it )
{
it->layout_item->setParent(LayoutItemWeakRef());
it->layout_item->onRemove();
}
_layout_items.clear();

View File

@ -295,35 +295,41 @@ BOOST_AUTO_TEST_CASE( vertical_layout)
//------------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE( boxlayout_insert_remove )
{
sc::HBoxLayout hbox;
sc::BoxLayoutRef hbox( new sc::HBoxLayout );
BOOST_CHECK_EQUAL(hbox.count(), 0);
BOOST_CHECK(!hbox.itemAt(0));
BOOST_CHECK(!hbox.takeAt(0));
BOOST_CHECK_EQUAL(hbox->count(), 0);
BOOST_CHECK(!hbox->itemAt(0));
BOOST_CHECK(!hbox->takeAt(0));
TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
SGVec2i(32, 32),
SGVec2i(9999, 32) ) ),
w2( new TestWidget(*w1) );
hbox.addItem(w1);
BOOST_CHECK_EQUAL(hbox.count(), 1);
BOOST_CHECK_EQUAL(hbox.itemAt(0), w1);
hbox->addItem(w1);
BOOST_CHECK_EQUAL(hbox->count(), 1);
BOOST_CHECK_EQUAL(hbox->itemAt(0), w1);
BOOST_CHECK_EQUAL(w1->getParent(), hbox);
hbox.insertItem(0, w2);
BOOST_CHECK_EQUAL(hbox.count(), 2);
BOOST_CHECK_EQUAL(hbox.itemAt(0), w2);
BOOST_CHECK_EQUAL(hbox.itemAt(1), w1);
hbox->insertItem(0, w2);
BOOST_CHECK_EQUAL(hbox->count(), 2);
BOOST_CHECK_EQUAL(hbox->itemAt(0), w2);
BOOST_CHECK_EQUAL(hbox->itemAt(1), w1);
BOOST_CHECK_EQUAL(w2->getParent(), hbox);
hbox.removeItem(w2);
BOOST_CHECK_EQUAL(hbox.count(), 1);
BOOST_CHECK_EQUAL(hbox.itemAt(0), w1);
hbox->removeItem(w2);
BOOST_CHECK_EQUAL(hbox->count(), 1);
BOOST_CHECK_EQUAL(hbox->itemAt(0), w1);
BOOST_CHECK( !w2->getParent() );
hbox.addItem(w2);
BOOST_CHECK_EQUAL(hbox.count(), 2);
hbox->addItem(w2);
BOOST_CHECK_EQUAL(hbox->count(), 2);
BOOST_CHECK_EQUAL(w2->getParent(), hbox);
hbox.clear();
BOOST_CHECK_EQUAL(hbox.count(), 0);
hbox->clear();
BOOST_CHECK_EQUAL(hbox->count(), 0);
BOOST_CHECK( !w1->getParent() );
BOOST_CHECK( !w2->getParent() );
}
//------------------------------------------------------------------------------