From 1c3e743d2a44b461ec0447f0c20a30b202728907 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 22 May 2020 08:46:04 -0600 Subject: [PATCH] Canvas: continued work on constraint packer --- libs/canvas/canvas/constraint_packer.h | 18 +++++++++ libs/canvas/constraint_packer.cc | 51 ++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/libs/canvas/canvas/constraint_packer.h b/libs/canvas/canvas/constraint_packer.h index 70ef55c91a..1fd42a821e 100644 --- a/libs/canvas/canvas/constraint_packer.h +++ b/libs/canvas/canvas/constraint_packer.h @@ -20,11 +20,13 @@ #define __CANVAS_CONSTRAINT_PACKER_H__ #include "canvas/item.h" +#include "kiwi/kiwi.h" namespace ArdourCanvas { class Rectangle; +class ConstrainedItem; class LIBCANVAS_API ConstraintPacker : public Item { @@ -32,13 +34,29 @@ public: ConstraintPacker (Canvas *); ConstraintPacker (Item *); + void add (Item *); + void remove (Item *); + void constrain (kiwi::Constraint const &); + + void solve (); + void apply (); + void compute_bounding_box () const; void render (Rect const & area, Cairo::RefPtr context) const; + void size_allocate (Rect const &); + protected: void child_changed (); private: + typedef std::map ConstraintMap; + ConstraintMap constraint_map; + + kiwi::Solver _solver; + kiwi::Variable width; + kiwi::Variable height; + Rectangle *self; bool collapse_on_hide; diff --git a/libs/canvas/constraint_packer.cc b/libs/canvas/constraint_packer.cc index dc8b24ea88..a29b65abd3 100644 --- a/libs/canvas/constraint_packer.cc +++ b/libs/canvas/constraint_packer.cc @@ -16,16 +16,25 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include "pbd/i18n.h" + + #include "kiwi/kiwi.h" #include "canvas/constraint_packer.h" +#include "canvas/constrained_item.h" +#include "canvas/item.h" #include "canvas/rectangle.h" using namespace ArdourCanvas; ConstraintPacker::ConstraintPacker (Canvas* canvas) : Item (canvas) + , width (X_("width")) + , height (X_("height")) { + _solver.addEditVariable (width, kiwi::strength::strong); + _solver.addEditVariable (height, kiwi::strength::strong); } ConstraintPacker::ConstraintPacker (Item* parent) @@ -88,3 +97,45 @@ ConstraintPacker::child_changed () reposition_children (); } +void +ConstraintPacker::constrain (kiwi::Constraint const &c) +{ + _solver.addConstraint (c); +} + +void +ConstraintPacker::size_allocate (Rect const & r) +{ + Item::size_allocate (r); + + _solver.suggestValue (width, r.width()); + _solver.suggestValue (height, r.height()); + + solve (); +} + +void +ConstraintPacker::add (Item* item) +{ + Item::add (item); + ConstrainedItem* ci = new ConstrainedItem (*item); + constraint_map.insert (std::make_pair (item, ci)); +} + +void +ConstraintPacker::remove (Item* item) +{ + Item::remove (item); + constraint_map.erase (item); +} + +void +ConstraintPacker::solve () +{ + _solver.updateVariables (); + + for (ConstraintMap::iterator x = constraint_map.begin(); x != constraint_map.end(); ++x) { + x->first->constrained (*(x->second)); + } +} +