13
0

Canvas: continued work on constraint packer

This commit is contained in:
Paul Davis 2020-05-22 08:46:04 -06:00
parent 01137cbbf3
commit 1c3e743d2a
2 changed files with 69 additions and 0 deletions

View File

@ -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<Cairo::Context> context) const;
void size_allocate (Rect const &);
protected:
void child_changed ();
private:
typedef std::map<Item*,ConstrainedItem*> ConstraintMap;
ConstraintMap constraint_map;
kiwi::Solver _solver;
kiwi::Variable width;
kiwi::Variable height;
Rectangle *self;
bool collapse_on_hide;

View File

@ -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));
}
}