diff options
author | Marco Pesenti Gritti <mpgritti@gmail.com> | 2008-09-13 11:33:30 (GMT) |
---|---|---|
committer | Marco Pesenti Gritti <mpgritti@gmail.com> | 2008-09-13 11:34:16 (GMT) |
commit | 91f9665dc3dac733224e464b26116f646853694c (patch) | |
tree | 803e620745e93dac11b0c17e215a17f318c6cf9d /src | |
parent | c24429b23c16aa4447fed47e4fde01a70380c954 (diff) |
Use the new grid in sugar-toolkit. #8372
Diffstat (limited to 'src')
-rw-r--r-- | src/view/home/grid.py | 40 | ||||
-rw-r--r-- | src/view/home/spreadlayout.py | 2 |
2 files changed, 16 insertions, 26 deletions
diff --git a/src/view/home/grid.py b/src/view/home/grid.py index e265e85..68a9844 100644 --- a/src/view/home/grid.py +++ b/src/view/home/grid.py @@ -15,18 +15,19 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -import numpy import random import gobject import gtk +from sugar import _sugarext + _PLACE_TRIALS = 20 _MAX_WEIGHT = 255 _REFRESH_RATE = 200 _MAX_COLLISIONS_PER_REFRESH = 20 -class Grid(gobject.GObject): +class Grid(_sugarext.Grid): __gsignals__ = { 'child-changed' : (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, @@ -43,12 +44,12 @@ class Grid(gobject.GObject): self._collisions = [] self._collisions_sid = 0 - self._array = numpy.zeros((width, height), dtype='b') + self.setup(self.width, self.height) def add(self, child, width, height, x=None, y=None, locked=False): if x is not None and y is not None: rect = gtk.gdk.Rectangle(x, y, width, height) - weight = self._compute_weight(rect) + weight = self.compute_weight(rect) else: trials = _PLACE_TRIALS weight = _MAX_WEIGHT @@ -57,7 +58,7 @@ class Grid(gobject.GObject): y = int(random.random() * (self.height - height)) rect = gtk.gdk.Rectangle(x, y, width, height) - new_weight = self._compute_weight(rect) + new_weight = self.compute_weight(rect) if weight > new_weight: weight = new_weight @@ -65,7 +66,7 @@ class Grid(gobject.GObject): self._child_rects[child] = rect self._children.append(child) - self._add_weight(self._child_rects[child]) + self.add_weight(self._child_rects[child]) if locked: self._locked_children.add(child) @@ -74,19 +75,19 @@ class Grid(gobject.GObject): def remove(self, child): self._children.remove(child) - self._remove_weight(self._child_rects[child]) + self.remove_weight(self._child_rects[child]) self._locked_children.discard(child) del self._child_rects[child] def move(self, child, x, y, locked=False): - self._remove_weight(self._child_rects[child]) + self.remove_weight(self._child_rects[child]) rect = self._child_rects[child] rect.x = x rect.y = y - weight = self._compute_weight(rect) - self._add_weight(self._child_rects[child]) + weight = self.compute_weight(rect) + self.add_weight(self._child_rects[child]) if locked: self._locked_children.add(child) @@ -140,7 +141,7 @@ class Grid(gobject.GObject): best_rect = None for new_rect in new_rects: - new_weight = self._compute_weight(new_rect) + new_weight = self.compute_weight(new_rect) if new_weight < weight: best_rect = new_rect weight = new_weight @@ -156,10 +157,10 @@ class Grid(gobject.GObject): collision = self._collisions.pop(0) old_rect = self._child_rects[collision] - self._remove_weight(old_rect) - weight = self._compute_weight(old_rect) + self.remove_weight(old_rect) + weight = self.compute_weight(old_rect) weight = self._shift_child(collision, weight) - self._add_weight(self._child_rects[collision]) + self.add_weight(self._child_rects[collision]) # TODO: we shouldn't give up the first time we failed to find a # better position. @@ -193,16 +194,5 @@ class Grid(gobject.GObject): self._collisions_sid = gobject.timeout_add(_REFRESH_RATE, self.__solve_collisions_cb, priority=gobject.PRIORITY_LOW) - def _add_weight(self, rect): - self._array[rect.x:rect.x+rect.width, rect.y:rect.y+rect.width] += 1 - - def _remove_weight(self, rect): - self._array[rect.x:rect.x+rect.width, rect.y:rect.y+rect.width] -= 1 - - def _compute_weight(self, rect): - weight = self._array[rect.x:rect.x+rect.width, - rect.y:rect.y+rect.width].sum() - return weight - def get_child_rect(self, child): return self._child_rects[child] diff --git a/src/view/home/spreadlayout.py b/src/view/home/spreadlayout.py index f28da96..0faabca 100644 --- a/src/view/home/spreadlayout.py +++ b/src/view/home/spreadlayout.py @@ -33,7 +33,7 @@ class SpreadLayout(gobject.GObject, hippo.CanvasLayout): min_width, width = self.do_get_width_request() min_height, height = self.do_get_height_request(width) - self._grid = Grid(width / _CELL_SIZE, height / _CELL_SIZE) + self._grid = Grid(int(width / _CELL_SIZE), int(height / _CELL_SIZE)) self._grid.connect('child-changed', self._grid_child_changed_cb) def add(self, child): |