From cc11d5ebd5777ca258f2f821daa493f6d2f235e5 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Thu, 5 Jul 2007 23:50:22 +0200 Subject: [PATCH] Implement items shifting, not quite working yet. --- sugar/graphics/spreadlayout.py | 61 ++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/sugar/graphics/spreadlayout.py b/sugar/graphics/spreadlayout.py index 5c6dc37a..d85e69bb 100644 --- a/sugar/graphics/spreadlayout.py +++ b/sugar/graphics/spreadlayout.py @@ -26,8 +26,15 @@ _PLACE_TRIALS = 20 _MAX_WEIGHT = 255 _CELL_SIZE = 4 -class _Grid(object): +class _Grid(gobject.GObject): + __gsignals__ = { + 'child-changed' : (gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, + ([gobject.TYPE_PYOBJECT])) + } def __init__(self, width, height): + gobject.GObject.__init__(self) + self.width = width self.height = height self._children = [] @@ -74,8 +81,51 @@ class _Grid(object): self._children.append(child) self._add_weight(child.grid_rect) + def _move_child(self, child, new_rect): + child.grid_rect = new_rect + self.emit('child-changed', child) + + def _shift_child(self, child): + rect = child.grid_rect + weight = self._compute_weight(rect) + new_rects = [] + + if (rect.x + rect.width < self.width - 1): + new_rects.append(gtk.gdk.Rectangle(rect.x + 1, rect.y, + rect.width, rect.height)) + + if (rect.x - 1 > 0): + new_rects.append(gtk.gdk.Rectangle(rect.x - 1, rect.y, + rect.width, rect.height)) + + if (rect.y + rect.height < self.height - 1): + new_rects.append(gtk.gdk.Rectangle(rect.x, rect.y + 1, + rect.width, rect.height)) + + if (rect.y - 1 > 0): + new_rects.append(gtk.gdk.Rectangle(rect.x, rect.y - 1, + rect.width, rect.height)) + + best_rect = None + for new_rect in new_rects: + new_weight = self._compute_weight(new_rect) + if new_weight < weight: + best_rect = new_rect + weight = new_weight + + if best_rect: + self._move_child(child, best_rect) + + return weight + + def _solve_collisions(self): - return False + for collision in self._collisions[:]: + weight = self._shift_child(collision) + if not weight: + self._collisions.remove(collision) + + return (len(self._collisions) > 0) def _detect_collisions(self, child): for c in self._children: @@ -122,6 +172,7 @@ class SpreadLayout(gobject.GObject,hippo.CanvasLayout): min_height, height = self.do_get_height_request(width) self._grid = _Grid(width / _CELL_SIZE, height / _CELL_SIZE) + self._grid.connect('child-changed', self._grid_child_changed_cb) def add_center(self, child): self._box.append(child) @@ -170,3 +221,9 @@ class SpreadLayout(gobject.GObject,hippo.CanvasLayout): min_height, height = child.get_height_request(width) return int(width / _CELL_SIZE), int(height / _CELL_SIZE) + + def _grid_child_changed_cb(self, grid, box_child): + # FIXME box_child->item is not exposed in the python bindings + for item in self._box.get_children(): + if self._box.find_box_child(item) == box_child: + item.emit_request_changed()