From dd2bf418b958c96cd7cdd3d831e84823aa3f3f77 Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Thu, 12 Jun 2014 00:35:17 +0200 Subject: [PATCH] canvas::Layout: remove/get child items. --- simgear/canvas/layout/BoxLayout.cxx | 30 ++++++++++++++++++++ simgear/canvas/layout/BoxLayout.hxx | 8 +++++- simgear/canvas/layout/Layout.cxx | 13 +++++++++ simgear/canvas/layout/Layout.hxx | 26 +++++++++++++++++ simgear/canvas/layout/canvas_layout_test.cxx | 27 ++++++++++++++++++ 5 files changed, 103 insertions(+), 1 deletion(-) diff --git a/simgear/canvas/layout/BoxLayout.cxx b/simgear/canvas/layout/BoxLayout.cxx index 5ba2ad36..a4dbd54e 100644 --- a/simgear/canvas/layout/BoxLayout.cxx +++ b/simgear/canvas/layout/BoxLayout.cxx @@ -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) { diff --git a/simgear/canvas/layout/BoxLayout.hxx b/simgear/canvas/layout/BoxLayout.hxx index 10537274..4bf9fd1a 100644 --- a/simgear/canvas/layout/BoxLayout.hxx +++ b/simgear/canvas/layout/BoxLayout.hxx @@ -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 _layout_items; + typedef std::vector LayoutItems; + + mutable LayoutItems _layout_items; mutable ItemData _layout_data; void updateSizeHints() const; diff --git a/simgear/canvas/layout/Layout.cxx b/simgear/canvas/layout/Layout.cxx index 8c10a520..a61d2780 100644 --- a/simgear/canvas/layout/Layout.cxx +++ b/simgear/canvas/layout/Layout.cxx @@ -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() { diff --git a/simgear/canvas/layout/Layout.hxx b/simgear/canvas/layout/Layout.hxx index 6d4aa575..dc563023 100644 --- a/simgear/canvas/layout/Layout.hxx +++ b/simgear/canvas/layout/Layout.hxx @@ -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 { diff --git a/simgear/canvas/layout/canvas_layout_test.cxx b/simgear/canvas/layout/canvas_layout_test.cxx index b05afd48..4750aae8 100644 --- a/simgear/canvas/layout/canvas_layout_test.cxx +++ b/simgear/canvas/layout/canvas_layout_test.cxx @@ -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 ) { -- 2.39.5