diff options
| -rw-r--r-- | plugingui/layout.cc | 82 | ||||
| -rw-r--r-- | plugingui/layout.h | 47 | 
2 files changed, 90 insertions, 39 deletions
| diff --git a/plugingui/layout.cc b/plugingui/layout.cc index f12887e..65042a4 100644 --- a/plugingui/layout.cc +++ b/plugingui/layout.cc @@ -51,8 +51,13 @@ void LayoutItem::setLayoutParent(Layout *p)  }  Layout::Layout(LayoutItem *parent) +	: parent(parent)  { -	this->parent = parent; +	auto widget = dynamic_cast<Widget*>(parent); +	if(widget) +	{ +		CONNECT(widget, sizeChangeNotifier, this, &Layout::sizeChanged); +	}  }  void Layout::addItem(LayoutItem *item) @@ -78,6 +83,11 @@ void Layout::removeItem(LayoutItem *item)  	layout();  } +void Layout::sizeChanged(int width, int height) +{ +	layout(); +} +  //  // BoxLayout  // @@ -85,12 +95,18 @@ void Layout::removeItem(LayoutItem *item)  BoxLayout::BoxLayout(LayoutItem *parent)  	: Layout(parent)  { -	resize_children = true;  } -void BoxLayout::setResizeChildren(bool resize_children) +void BoxLayout::setResizeChildren(bool resizeChildren)  { -	this->resize_children = resize_children; +	this->resizeChildren = resizeChildren; +	layout(); +} + +void BoxLayout::setSpacing(size_t spacing) +{ +	this->spacing = spacing; +	layout();  }  // @@ -98,7 +114,8 @@ void BoxLayout::setResizeChildren(bool resize_children)  //  VBoxLayout::VBoxLayout(LayoutItem *parent) -	: BoxLayout(parent), align(HALIGN_CENTER) +	: BoxLayout(parent) +	, align(HAlignment::center)  {  } @@ -106,32 +123,59 @@ void VBoxLayout::layout()  {  	size_t y = 0;  	size_t w = parent->width(); -	size_t h = parent->height() / items.size(); +	//size_t h = parent->height() / items.size();  	LayoutItemList::iterator i = items.begin();  	while(i != items.end())  	{  		LayoutItem *item = *i; -		if(resize_children)item->resize(w, h); -		item->move(0, y); -		y += h; +		if(resizeChildren) +		{ +			item->resize(w, parent->height() / items.size()); +		} + +		size_t x = 0; +		switch(align) { +		case HAlignment::left: +			x = 0; +			break; +		case HAlignment::center: +			x = (w / 2) - (item->width() / 2); +			break; +		case HAlignment::right: +			x = w - item->width(); +			break; +		} + +		item->move(x, y); +		y += item->height() + spacing;  		++i;  	}  } +void VBoxLayout::setHAlignment(HAlignment alignment) +{ +	align = alignment; +} +  //  // HBoxLayout  //  HBoxLayout::HBoxLayout(LayoutItem *parent)  	: BoxLayout(parent) -	, align(VALIGN_CENTER) +	, align(VAlignment::center)  {  }  void HBoxLayout::layout()  { -	size_t w = parent->width() / items.size(); +	if(items.empty()) +	{ +		return; +	} + +//	size_t w = parent->width() / items.size();  	size_t h = parent->height();  	size_t x = 0; @@ -139,35 +183,35 @@ void HBoxLayout::layout()  	while(i != items.end())  	{  		LayoutItem *item = *i; -		if(resize_children) +		if(resizeChildren)  		{ -			item->resize(w, h); +			item->resize(parent->width() / items.size(), h);  			item->move(x, 0);  		}  		else  		{  			size_t y = 0;  			switch(align) { -			case VALIGN_TOP: +			case VAlignment::top:  				y = 0;  				break; -			case VALIGN_CENTER: +			case VAlignment::center:  				y = (h / 2) - (item->height() / 2);  				break; -			case VALIGN_BOTTOM: +			case VAlignment::bottom:  				y = h - item->height();  				break;  			} -			int diff = w - item->width(); +			int diff = 0;//w - item->width();  			item->move(x + diff / 2, y);  		} -		x += w; +		x += item->width() + spacing;  		++i;  	}  } -void HBoxLayout::setVAlignment(alignment_t alignment) +void HBoxLayout::setVAlignment(VAlignment alignment)  {  	align = alignment;  } diff --git a/plugingui/layout.h b/plugingui/layout.h index 72e4fc8..949c1ca 100644 --- a/plugingui/layout.h +++ b/plugingui/layout.h @@ -29,6 +29,8 @@  #include <list>  #include <cstdlib> +#include "notifier.h" +  namespace GUI {  class Layout; @@ -42,8 +44,8 @@ public:  	virtual void resize(int width, int height) = 0;  	virtual void move(size_t x, size_t y) = 0; -	virtual size_t x() = 0; -	virtual size_t y() = 0; +	virtual int x() = 0; +	virtual int y() = 0;  	virtual size_t width() = 0;  	virtual size_t height() = 0; @@ -52,7 +54,7 @@ private:  };  //! \brief Abtract Layout class. -class Layout +class Layout : public Listener  {  public:  	Layout(LayoutItem *parent); @@ -65,6 +67,8 @@ public:  	virtual void layout() = 0;  protected: +	void sizeChanged(int width, int height); +  	LayoutItem *parent;  	typedef std::list<LayoutItem *> LayoutItemList;  	LayoutItemList items; @@ -78,11 +82,20 @@ public:  	//! \brief Set to false to only move the items, not scale them.  	void setResizeChildren(bool resize_children); +	void setSpacing(size_t spacing); +  	// From Layout:  	virtual void layout() override  = 0;  protected: -	bool resize_children; +	bool resizeChildren{false}; +	size_t spacing{0}; +}; + +enum class HAlignment { +	left, +	center, +	right,  };  //! \brief A Layout that lays out its elements vertically. @@ -90,19 +103,19 @@ class VBoxLayout : public BoxLayout {  public:  	VBoxLayout(LayoutItem *parent); -	typedef enum { -		HALIGN_LEFT, -		HALIGN_CENTER, -		HALIGN_RIGHT, -	} alignment_t; - -	void setHAlignment(alignment_t alignment); +	void setHAlignment(HAlignment alignment);  	// From BoxLayout:  	virtual void layout() override;  protected: -	alignment_t align; +	HAlignment align; +}; + +enum class VAlignment { +	top, +	center, +	bottom,  };  //! \brief A Layout that lays out its elements vertically. @@ -110,19 +123,13 @@ class HBoxLayout : public BoxLayout {  public:  	HBoxLayout(LayoutItem *parent); -	typedef enum { -		VALIGN_TOP, -		VALIGN_CENTER, -		VALIGN_BOTTOM, -	} alignment_t; - -	void setVAlignment(alignment_t alignment); +	void setVAlignment(VAlignment alignment);  	// From BoxLayout:  	virtual void layout() override;  protected: -	alignment_t align; +	VAlignment align;  };  } // GUI:: | 
