From 2b1a11fb66ea3704c60d3a43ca2ba855190dc2fa Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sat, 9 Sep 2006 12:23:01 +0200 Subject: [PATCH] Improved layout strategy --- sugar/canvas/GridBox.py | 33 +++++++++++++++++++++++++++++++ sugar/canvas/GridLayout.py | 40 ++++++++++++++++++++------------------ 2 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 sugar/canvas/GridBox.py diff --git a/sugar/canvas/GridBox.py b/sugar/canvas/GridBox.py new file mode 100644 index 00000000..f965512e --- /dev/null +++ b/sugar/canvas/GridBox.py @@ -0,0 +1,33 @@ +import goocanvas + +from sugar.canvas.GridLayout import GridGroup +from sugar.canvas.GridLayout import GridConstraints + +class GridBox(GridGroup, goocanvas.Item): + VERTICAL = 0 + HORIZONTAL = 1 + + def __init__(self, direction, size, padding): + if direction == GridBox.VERTICAL: + GridGroup.__init__(self, 1, size) + else: + GridGroup.__init__(self, size, 1) + + self._direction = direction + self._padding = padding + + def add_child(self, item, position=-1): + if position == -1: + position = self.get_n_children() + + if self._direction == GridBox.HORIZONTAL: + col = position + row = 0 + else: + col = 0 + row = position + + constraints = GridConstraints(col, row, 1, 1, self._padding) + self._layout.set_constraints(item, constraints) + + GridGroup.add_child(self, item, position) diff --git a/sugar/canvas/GridLayout.py b/sugar/canvas/GridLayout.py index b61a1853..3c965a8b 100644 --- a/sugar/canvas/GridLayout.py +++ b/sugar/canvas/GridLayout.py @@ -43,23 +43,18 @@ class GridLayout: return [x, y, width, height] - def layout_canvas_group(self, group): - i = 0 - while i < group.get_n_children(): - item = group.get_child(i) + def layout_canvas_item(self, item): + group = item.get_parent() + [x, y, width, height] = self._get_geometry(group, item) - [x, y, width, height] = self._get_geometry(group, item) + item.props.x = x + item.props.y = y - item.props.x = x - item.props.y = y - - try: - item.props.width = width - item.props.height = height - except: - item.props.size = width - - i += 1 + try: + item.props.width = width + item.props.height = height + except: + item.props.size = width def layout_screen(self, screen): for window in screen.get_windows(): @@ -85,13 +80,19 @@ class GridGroup(goocanvas.Group): matrix.translate(self._x, self._y) self.set_transform(matrix) + def _update_layout(self): + i = 0 + while i < self.get_n_children(): + self._layout.layout_canvas_item(self.get_child(i)) + i += 1 + def do_set_property(self, pspec, value): if pspec.name == 'width': self._width = value - self._layout.layout_canvas_group(self) + self._update_layout() elif pspec.name == 'height': self._height = value - self._layout.layout_canvas_group(self) + self._update_layout() elif pspec.name == 'x': self._x = value self._update_position() @@ -127,5 +128,6 @@ class GridGroup(goocanvas.Group): def get_layout(self): return self._layout - def __child_added_cb(self, child, position): - self._layout.layout_canvas_group(self) + def __child_added_cb(self, group, position): + item = group.get_child(position) + self._layout.layout_canvas_item(item)