From 43c338928a98f6a765e95011d3808c26cc6597ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Nusser?= Date: Fri, 7 Apr 2017 00:17:17 +0200 Subject: Implement GridLayout --- plugingui/layout.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 8 deletions(-) (limited to 'plugingui/layout.h') diff --git a/plugingui/layout.h b/plugingui/layout.h index 37a7d6d..763f616 100644 --- a/plugingui/layout.h +++ b/plugingui/layout.h @@ -28,6 +28,7 @@ #include #include +#include #include @@ -56,8 +57,7 @@ private: }; //! \brief Abtract Layout class. -class Layout - : public Listener +class Layout : public Listener { public: Layout(LayoutItem* parent); @@ -80,8 +80,7 @@ protected: }; //! \brief Abstract box layout -class BoxLayout - : public Layout +class BoxLayout : public Layout { public: BoxLayout(LayoutItem* parent); @@ -107,8 +106,7 @@ enum class HAlignment }; //! \brief A Layout that lays out its elements vertically. -class VBoxLayout - : public BoxLayout +class VBoxLayout : public BoxLayout { public: VBoxLayout(LayoutItem* parent); @@ -130,8 +128,7 @@ enum class VAlignment }; //! \brief A Layout that lays out its elements vertically. -class HBoxLayout - : public BoxLayout +class HBoxLayout : public BoxLayout { public: HBoxLayout(LayoutItem* parent); @@ -145,4 +142,51 @@ protected: VAlignment align; }; +//! \brief A Layout class which places the items in a regular grid. An item can +//! span multiple rows/columns. +class GridLayout : public BoxLayout +{ +public: + // The range is open, i.e. end is one past the last one. + struct GridRange + { + int column_begin; + int column_end; + int row_begin; + int row_end; + }; + + GridLayout(LayoutItem* parent, std::size_t number_of_columns, + std::size_t number_of_rows); + + virtual ~GridLayout() + { + } + + // From Layout: + virtual void removeItem(LayoutItem* item); + virtual void layout(); + + void setPosition(LayoutItem* item, GridRange const& range); + +protected: + std::size_t number_of_columns; + std::size_t number_of_rows; + + // Note: Yes, this is somewhat redundant to the LayoutItemList of the Layout + // class. However, this was the best idea I had such that I could still + // derive from Layout. If you find this ugly, feel free to fix it. + std::unordered_map grid_ranges; + +private: + struct CellSize { + std::size_t width; + std::size_t height; + }; + + CellSize calculateCellSize() const; + void moveAndResize( + LayoutItem& item, GridRange const& range, CellSize cell_size) const; +}; + } // GUI:: -- cgit v1.2.3