canvas::Layout: remove/get child items.

This commit is contained in:
Thomas Geymayer
2014-06-12 00:35:17 +02:00
parent 97cc250047
commit dd2bf418b9
5 changed files with 103 additions and 1 deletions

View File

@@ -91,6 +91,36 @@ namespace canvas
insertItem(index, LayoutItemRef(new SpacerItem(size_hint, max_size)));
}
//----------------------------------------------------------------------------
size_t BoxLayout::count() const
{
return _layout_items.size();
}
//----------------------------------------------------------------------------
LayoutItemRef BoxLayout::itemAt(size_t index)
{
if( index >= _layout_items.size() )
return LayoutItemRef();
return _layout_items[index].layout_item;
}
//----------------------------------------------------------------------------
LayoutItemRef BoxLayout::takeAt(size_t index)
{
if( index >= _layout_items.size() )
return LayoutItemRef();
LayoutItems::iterator it = _layout_items.begin() + index;
LayoutItemRef item = it->layout_item;
_layout_items.erase(it);
invalidate();
return item;
}
//----------------------------------------------------------------------------
void BoxLayout::setStretch(size_t index, int stretch)
{

View File

@@ -55,6 +55,10 @@ namespace canvas
void insertSpacing(int index, int size);
virtual size_t count() const;
virtual LayoutItemRef itemAt(size_t index);
virtual LayoutItemRef takeAt(size_t index);
/**
* Set the stretch factor of the item at position @a index to @a stretch.
*/
@@ -86,7 +90,9 @@ namespace canvas
int _padding;
Direction _direction;
mutable std::vector<ItemData> _layout_items;
typedef std::vector<ItemData> LayoutItems;
mutable LayoutItems _layout_items;
mutable ItemData _layout_data;
void updateSizeHints() const;

View File

@@ -54,6 +54,19 @@ namespace canvas
update();
}
//----------------------------------------------------------------------------
void Layout::removeItem(const LayoutItemRef& item)
{
size_t i = 0;
while( LayoutItemRef child = itemAt(i) )
{
if( item == child )
return (void)takeAt(i);
++i;
}
}
//----------------------------------------------------------------------------
void Layout::ItemData::reset()
{

View File

@@ -40,6 +40,32 @@ namespace canvas
virtual void setSpacing(int spacing) = 0;
virtual int spacing() const = 0;
/**
* Get the number of items.
*/
virtual size_t count() const = 0;
/**
* Get the item at position @a index.
*
* If there is no such item the function must do nothing and return an
* empty reference.
*/
virtual LayoutItemRef itemAt(size_t index) = 0;
/**
* Remove and get the item at position @a index.
*
* If there is no such item the function must do nothing and return an
* empty reference.
*/
virtual LayoutItemRef takeAt(size_t index) = 0;
/**
* Remove the given @a item from the layout.
*/
void removeItem(const LayoutItemRef& item);
protected:
enum LayoutFlags
{

View File

@@ -255,6 +255,33 @@ BOOST_AUTO_TEST_CASE( vertical_layout)
vbox.setDirection(sc::BoxLayout::BottomToTop);
}
BOOST_AUTO_TEST_CASE( boxlayout_insert_remove )
{
sc::HBoxLayout hbox;
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.insertItem(0, w2);
BOOST_CHECK_EQUAL(hbox.count(), 2);
BOOST_CHECK_EQUAL(hbox.itemAt(0), w2);
BOOST_CHECK_EQUAL(hbox.itemAt(1), w1);
hbox.removeItem(w2);
BOOST_CHECK_EQUAL(hbox.count(), 1);
BOOST_CHECK_EQUAL(hbox.itemAt(0), w1);
}
//------------------------------------------------------------------------------
BOOST_AUTO_TEST_CASE( nasal_layout )
{