Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sugar/graphics/spreadbox.py29
-rwxr-xr-xtests/test-snowflake-box.py29
2 files changed, 28 insertions, 30 deletions
diff --git a/sugar/graphics/spreadbox.py b/sugar/graphics/spreadbox.py
index f4cf5ea..8614cf6 100644
--- a/sugar/graphics/spreadbox.py
+++ b/sugar/graphics/spreadbox.py
@@ -21,7 +21,8 @@ from random import random
import hippo
_X_TILES = 120
-_NUM_TRIALS = 100
+_NUM_TRIALS = 20
+_MAX_WEIGHT = 255
class _Grid(object):
def __init__(self, x_tiles, y_tiles, cell_size):
@@ -84,29 +85,41 @@ class SpreadBox(hippo.CanvasBox, hippo.CanvasItem):
def remove_item(self, item):
self.remove(item)
+
+ def _place_item(self, item, x, y, w, h):
+ self._grid.add_weight_at(x, y, w, h)
+ self.set_position(item,
+ self._grid.cell_size * x,
+ self._grid.cell_size * y)
+
def _layout_item(self, item):
if not self._grid:
return
trials = _NUM_TRIALS
placed = False
+ best_weight = _MAX_WEIGHT
- while trials > 0 and not placed:
- [width, height] = item.get_allocation()
- cell_size = self._grid.cell_size
+ [width, height] = item.get_allocation()
+ w = int(width / self._grid.cell_size)
+ h = int(height / self._grid.cell_size)
- w = int(width / cell_size)
- h = int(height / cell_size)
+ while trials > 0 and not placed:
x = int(random() * (self._grid.x_tiles - w))
y = int(random() * (self._grid.y_tiles - h))
weight = self._grid.compute_weight_at(x, y, w, h)
if weight == 0:
- self._grid.add_weight_at(x, y, w, h)
- self.set_position(item, cell_size * x, cell_size * y)
+ self._place_item(item, x, y, w, h)
placed = True
+ elif weight < best_weight:
+ best_x = x
+ best_y = y
trials -= 1
+
+ if not placed:
+ self._place_item(item, best_x, best_y, w, h)
def _layout(self):
for item in self.get_children():
diff --git a/tests/test-snowflake-box.py b/tests/test-snowflake-box.py
index eb7efb9..45f3c0a 100755
--- a/tests/test-snowflake-box.py
+++ b/tests/test-snowflake-box.py
@@ -17,7 +17,7 @@
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import sys
-import random
+from random import random
import pygtk
pygtk.require('2.0')
@@ -33,14 +33,14 @@ from sugar.graphics.canvasicon import CanvasIcon
def _create_snowflake(parent, children):
color = XoColor()
- icon = CanvasIcon(scale=1.0, xo_color=color,
+ icon = CanvasIcon(scale=0.8, xo_color=color,
icon_name='theme:object-link')
parent.append(icon, hippo.PACK_FIXED)
parent.set_root(icon)
for i in range(0, children):
color = XoColor()
- icon = CanvasIcon(scale=0.5, xo_color=color,
+ icon = CanvasIcon(scale=0.4, xo_color=color,
icon_name='theme:stock-buddy')
parent.append(icon, hippo.PACK_FIXED)
@@ -58,25 +58,10 @@ box = SnowflakeBox()
snow_flake = _create_snowflake(box, 0)
root_box.set_center_item(box)
-box = SnowflakeBox()
-snow_flake = _create_snowflake(box, 30)
-root_box.add_item(box)
-
-box = SnowflakeBox()
-snow_flake = _create_snowflake(box, 15)
-root_box.add_item(box)
-
-box = SnowflakeBox()
-snow_flake = _create_snowflake(box, 10)
-root_box.add_item(box)
-
-box = SnowflakeBox()
-snow_flake = _create_snowflake(box, 5)
-root_box.add_item(box)
-
-box = SnowflakeBox()
-snow_flake = _create_snowflake(box, 2)
-root_box.add_item(box)
+for i in range(0, 30):
+ box = SnowflakeBox()
+ snow_flake = _create_snowflake(box, int(2 + random() * 8))
+ root_box.add_item(box)
canvas.show()
window.add(canvas)