diff options
-rw-r--r-- | plugingui/layout.cc | 68 | ||||
-rw-r--r-- | plugingui/layout.h | 30 |
2 files changed, 48 insertions, 50 deletions
diff --git a/plugingui/layout.cc b/plugingui/layout.cc index 509e32d..7e3c6e8 100644 --- a/plugingui/layout.cc +++ b/plugingui/layout.cc @@ -28,10 +28,12 @@ #include "widget.h" -namespace GUI { +#include <algorithm> -LayoutItem::LayoutItem() - : parent(nullptr) +namespace GUI +{ + +LayoutItem::LayoutItem() : parent(nullptr) { } @@ -40,7 +42,7 @@ LayoutItem::~LayoutItem() setLayoutParent(nullptr); // Will disconnect from layout if any. } -void LayoutItem::setLayoutParent(Layout *p) +void LayoutItem::setLayoutParent(Layout* p) { if(this->parent) { @@ -50,8 +52,7 @@ void LayoutItem::setLayoutParent(Layout *p) this->parent = p; } -Layout::Layout(LayoutItem *parent) - : parent(parent) +Layout::Layout(LayoutItem* parent) : parent(parent) { auto widget = dynamic_cast<Widget*>(parent); if(widget) @@ -60,26 +61,16 @@ Layout::Layout(LayoutItem *parent) } } -void Layout::addItem(LayoutItem *item) +void Layout::addItem(LayoutItem* item) { items.push_back(item); item->setLayoutParent(this); layout(); } -void Layout::removeItem(LayoutItem *item) +void Layout::removeItem(LayoutItem* item) { - LayoutItemList::iterator i = items.begin(); - while(i != items.end()) - { - if(*i == item) - { - i = items.erase(i); - continue; - } - ++i; - } - + std::remove(items.begin(), items.end(), item); layout(); } @@ -92,8 +83,7 @@ void Layout::sizeChanged(int width, int height) // BoxLayout // -BoxLayout::BoxLayout(LayoutItem *parent) - : Layout(parent) +BoxLayout::BoxLayout(LayoutItem* parent) : Layout(parent) { } @@ -113,9 +103,8 @@ void BoxLayout::setSpacing(size_t spacing) // VBoxLayout // -VBoxLayout::VBoxLayout(LayoutItem *parent) - : BoxLayout(parent) - , align(HAlignment::center) +VBoxLayout::VBoxLayout(LayoutItem* parent) + : BoxLayout(parent), align(HAlignment::center) { } @@ -123,7 +112,7 @@ 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()) @@ -135,18 +124,21 @@ void VBoxLayout::layout() auto empty_space = (num_items - 1) * spacing; auto available_space = parent->height(); - if (available_space >= empty_space) { + if(available_space >= empty_space) + { auto item_height = (available_space - empty_space) / num_items; item->resize(w, item_height); } - else { + else + { // TODO: Should this case be handled differently? item->resize(w, 0); } } size_t x = 0; - switch(align) { + switch(align) + { case HAlignment::left: x = 0; break; @@ -173,9 +165,8 @@ void VBoxLayout::setHAlignment(HAlignment alignment) // HBoxLayout // -HBoxLayout::HBoxLayout(LayoutItem *parent) - : BoxLayout(parent) - , align(VAlignment::center) +HBoxLayout::HBoxLayout(LayoutItem* parent) + : BoxLayout(parent), align(VAlignment::center) { } @@ -186,25 +177,27 @@ void HBoxLayout::layout() return; } -// size_t w = parent->width() / items.size(); + // size_t w = parent->width() / items.size(); size_t h = parent->height(); size_t x = 0; LayoutItemList::iterator i = items.begin(); while(i != items.end()) { - LayoutItem *item = *i; + LayoutItem* item = *i; if(resizeChildren) { auto num_items = items.size(); auto empty_space = (num_items - 1) * spacing; auto available_space = parent->width(); - if (available_space >= empty_space) { + if(available_space >= empty_space) + { auto item_width = (available_space - empty_space) / num_items; item->resize(item_width, h); } - else { + else + { // TODO: Should this case be handled differently? item->resize(0, h); } @@ -214,7 +207,8 @@ void HBoxLayout::layout() else { size_t y = 0; - switch(align) { + switch(align) + { case VAlignment::top: y = 0; break; @@ -226,7 +220,7 @@ void HBoxLayout::layout() break; } - int diff = 0;//w - item->width(); + int diff = 0; // w - item->width(); item->move(x + diff / 2, y); } x += item->width() + spacing; diff --git a/plugingui/layout.h b/plugingui/layout.h index 4d20eb6..37a7d6d 100644 --- a/plugingui/layout.h +++ b/plugingui/layout.h @@ -26,8 +26,8 @@ */ #pragma once -#include <list> #include <cstdlib> +#include <list> #include <notifier.h> @@ -60,11 +60,13 @@ class Layout : public Listener { public: - Layout(LayoutItem *parent); - virtual ~Layout() {} + Layout(LayoutItem* parent); + virtual ~Layout() + { + } - virtual void addItem(LayoutItem *item); - virtual void removeItem(LayoutItem *item); + virtual void addItem(LayoutItem* item); + virtual void removeItem(LayoutItem* item); //! \brief Reimplement this method to create a new Layout rule. virtual void layout() = 0; @@ -72,8 +74,8 @@ public: protected: void sizeChanged(int width, int height); - LayoutItem *parent; - typedef std::list<LayoutItem *> LayoutItemList; + LayoutItem* parent; + typedef std::list<LayoutItem*> LayoutItemList; LayoutItemList items; }; @@ -82,7 +84,7 @@ class BoxLayout : public Layout { public: - BoxLayout(LayoutItem *parent); + BoxLayout(LayoutItem* parent); //! \brief Set to false to only move the items, not scale them. void setResizeChildren(bool resize_children); @@ -90,14 +92,15 @@ public: void setSpacing(size_t spacing); // From Layout: - virtual void layout() override = 0; + virtual void layout() override = 0; protected: bool resizeChildren{false}; size_t spacing{0}; }; -enum class HAlignment { +enum class HAlignment +{ left, center, right, @@ -108,7 +111,7 @@ class VBoxLayout : public BoxLayout { public: - VBoxLayout(LayoutItem *parent); + VBoxLayout(LayoutItem* parent); void setHAlignment(HAlignment alignment); @@ -119,7 +122,8 @@ protected: HAlignment align; }; -enum class VAlignment { +enum class VAlignment +{ top, center, bottom, @@ -130,7 +134,7 @@ class HBoxLayout : public BoxLayout { public: - HBoxLayout(LayoutItem *parent); + HBoxLayout(LayoutItem* parent); void setVAlignment(VAlignment alignment); |