basic (and probably wrong) Canvas::Grid child placement
authorPaul Davis <paul@linuxaudiosystems.com>
Sat, 14 Jan 2017 12:56:26 +0000 (12:56 +0000)
committerPaul Davis <paul@linuxaudiosystems.com>
Sun, 15 Jan 2017 12:13:03 +0000 (12:13 +0000)
libs/canvas/canvas/grid.h
libs/canvas/grid.cc

index a13cdf0a229bff2befb8b10d2c431a75f538f979..435a4e5eb13f41fb4b443bfe9875f7ca62eb1c89 100644 (file)
@@ -19,6 +19,8 @@
 #ifndef __CANVAS_GRID_H__
 #define __CANVAS_GRID_H__
 
+#include <map>
+
 #include "canvas/item.h"
 
 namespace ArdourCanvas
@@ -58,6 +60,9 @@ public:
 
        void child_changed ();
   private:
+       typedef std::map<Item*,Duple> CoordsByItem;
+       CoordsByItem coords_by_item;
+
        Rectangle *self;
        bool collapse_on_hide;
        bool homogenous;
index cc12feb8db67c60e57129f1fbcc07a76a48f3bf4..de1602f33a04a0a116d30aa65a74d14c29fc9aac 100644 (file)
 */
 
 #include <algorithm>
+#include <vector>
 
 #include "canvas/grid.h"
 #include "canvas/rectangle.h"
 
 using namespace ArdourCanvas;
+using std::vector;
+using std::max;
 
 Grid::Grid (Canvas* canvas)
        : Item (canvas)
@@ -157,23 +160,48 @@ Grid::reset_self ()
 void
 Grid::reposition_children ()
 {
-       Duple previous_edge (0, 0);
-       Distance largest_width = 0;
-       Distance largest_height = 0;
-
-       if (homogenous) {
-
-               for (std::list<Item*>::iterator i = _items.begin(); ++i != _items.end(); ++i) {
-                       boost::optional<Rect> bb = (*i)->bounding_box();
-                       if (bb) {
-                               largest_height = std::max (largest_height, bb.get().height());
-                               largest_width = std::max (largest_width, bb.get().width());
-                       }
+       uint32_t max_row = 0;
+       uint32_t max_col = 0;
+
+       for (CoordsByItem::const_iterator c = coords_by_item.begin(); c != coords_by_item.end(); ++c) {
+               max_col = max (max_col, (uint32_t) c->second.x);
+               max_row = max (max_row, (uint32_t) c->second.y);
+       }
+
+       vector<double> row_dimens;
+       vector<double> col_dimens;
+
+       row_dimens.assign (0, max_row);
+       col_dimens.assign (0, max_col);
+
+       for (std::list<Item*>::iterator i = _items.begin(); ++i != _items.end(); ++i) {
+               boost::optional<Rect> bb = (*i)->bounding_box();
+
+               if (!bb) {
+                       continue;
                }
+
+               CoordsByItem::const_iterator c = coords_by_item.find (*i);
+
+               row_dimens[c->second.x] = max (row_dimens[c->second.x], bb.get().width());
+               col_dimens[c->second.y] = max (col_dimens[c->second.y], bb.get().height());
        }
 
        for (std::list<Item*>::iterator i = _items.begin(); ++i != _items.end(); ++i) {
+               CoordsByItem::const_iterator c = coords_by_item.find (*i);
+
+               if (c == coords_by_item.end()) {
+                       continue;
+               }
+
+               Duple pos (0,0);
 
+               for (uint32_t n = 0; n < c->second.x; ++n) {
+                       pos.x += row_dimens[n];
+                       pos.y += col_dimens[n];
+               }
+
+               (*i)->set_position (pos);
        }
 
        _bounding_box_dirty = true;
@@ -183,7 +211,8 @@ Grid::reposition_children ()
 void
 Grid::place (Item* i, Duple at)
 {
-
+       add (i);
+       coords_by_item.insert (std::make_pair (i, at));
 }
 
 void