Fix the spread box test and fix the box layout logic too
This commit is contained in:
parent
75fb1a33de
commit
d1205dd255
@ -14,6 +14,7 @@ class SpreadBox(hippo.CanvasBox, hippo.CanvasItem):
|
||||
|
||||
self._items_to_position = []
|
||||
self._spread_on_add = False
|
||||
self._stable = False
|
||||
|
||||
def add(self, item):
|
||||
self._items_to_position.append(item)
|
||||
@ -56,7 +57,8 @@ class SpreadBox(hippo.CanvasBox, hippo.CanvasItem):
|
||||
x = max(0, x)
|
||||
y = max(0, y)
|
||||
|
||||
[item_w, item_h] = icon.get_allocation()
|
||||
item_w = icon.get_width_request()
|
||||
item_h = icon.get_height_request(item_w)
|
||||
[box_w, box_h] = self.get_allocation()
|
||||
|
||||
x = min(box_w - item_w, x)
|
||||
@ -65,37 +67,35 @@ class SpreadBox(hippo.CanvasBox, hippo.CanvasItem):
|
||||
return [x, y]
|
||||
|
||||
def _spread_icons(self):
|
||||
stable = True
|
||||
self._stable = True
|
||||
|
||||
for icon1 in self.get_children():
|
||||
vx = 0
|
||||
vy = 0
|
||||
|
||||
[x, y] = self.get_position(icon1)
|
||||
|
||||
for icon2 in self.get_children():
|
||||
if icon1 != icon2:
|
||||
distance = self._get_distance(icon1, icon2)
|
||||
if distance <= _DISTANCE_THRESHOLD:
|
||||
stable = False
|
||||
self._stable = False
|
||||
[f_x, f_y] = self._get_repulsion(icon1, icon2)
|
||||
vx += f_x
|
||||
vy += f_y
|
||||
|
||||
new_x = x + vx
|
||||
new_y = y + vy
|
||||
if vx != 0 or vy != 0:
|
||||
[x, y] = self.get_position(icon1)
|
||||
new_x = x + vx
|
||||
new_y = y + vy
|
||||
|
||||
[new_x, new_y] = self._clamp_position(icon1, new_x, new_y)
|
||||
[new_x, new_y] = self._clamp_position(icon1, new_x, new_y)
|
||||
|
||||
self.move(icon1, new_x, new_y)
|
||||
|
||||
return stable
|
||||
self.move(icon1, new_x, new_y)
|
||||
|
||||
def do_allocate(self, width, height):
|
||||
hippo.CanvasBox.do_allocate(self, width, height)
|
||||
|
||||
tries = 10
|
||||
stable = self._spread_icons()
|
||||
while not stable and tries > 0:
|
||||
stable = self._spread_icons()
|
||||
self._spread_icons()
|
||||
while not self._stable and tries > 0:
|
||||
self._spread_icons()
|
||||
tries -= 1
|
||||
|
@ -1,55 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
import pygtk
|
||||
pygtk.require('2.0')
|
||||
import gobject
|
||||
|
||||
from sugar.session.UITestSession import UITestSession
|
||||
|
||||
session = UITestSession()
|
||||
session.start()
|
||||
|
||||
import sys
|
||||
import random
|
||||
|
||||
import gtk
|
||||
import goocanvas
|
||||
|
||||
from view.home.IconLayout import IconLayout
|
||||
from sugar.graphics import IconColor
|
||||
from sugar.graphics.IconItem import IconItem
|
||||
from sugar.graphics.CanvasView import CanvasView
|
||||
from sugar.graphics.Grid import Grid
|
||||
|
||||
def _create_icon():
|
||||
color = IconColor.IconColor()
|
||||
|
||||
icon = IconItem(size=125, color=color,
|
||||
icon_name='stock-buddy')
|
||||
root.add_child(icon)
|
||||
|
||||
icon_layout.add_icon(icon)
|
||||
|
||||
return (root.get_n_children() < 20)
|
||||
|
||||
window = gtk.Window()
|
||||
window.connect("destroy", lambda w: gtk.main_quit())
|
||||
window.show()
|
||||
|
||||
canvas = CanvasView()
|
||||
canvas.show()
|
||||
window.add(canvas)
|
||||
|
||||
canvas_model = goocanvas.CanvasModelSimple()
|
||||
root = canvas_model.get_root_item()
|
||||
|
||||
item = goocanvas.Rect(x=0, y=0, width=1200, height=900,
|
||||
line_width=0.0, fill_color='#e2e2e2')
|
||||
root.add_child(item)
|
||||
|
||||
icon_layout = IconLayout(Grid())
|
||||
|
||||
gobject.timeout_add(500, _create_icon)
|
||||
|
||||
canvas.set_model(canvas_model)
|
||||
|
||||
gtk.main()
|
45
tests/test-spread-box.py
Executable file
45
tests/test-spread-box.py
Executable file
@ -0,0 +1,45 @@
|
||||
#!/usr/bin/python
|
||||
import pygtk
|
||||
pygtk.require('2.0')
|
||||
import gobject
|
||||
|
||||
from sugar.session.UITestSession import UITestSession
|
||||
|
||||
session = UITestSession()
|
||||
session.start()
|
||||
|
||||
import sys
|
||||
import random
|
||||
|
||||
import gtk
|
||||
import hippo
|
||||
|
||||
from sugar.graphics.spreadbox import SpreadBox
|
||||
from sugar.graphics.iconcolor import IconColor
|
||||
from sugar.graphics.canvasicon import CanvasIcon
|
||||
|
||||
def _create_icon():
|
||||
color = IconColor()
|
||||
|
||||
icon = CanvasIcon(size=100, color=color,
|
||||
icon_name='stock-buddy')
|
||||
box.add(icon)
|
||||
|
||||
return (len(box.get_children()) < 20)
|
||||
|
||||
window = gtk.Window()
|
||||
window.connect("destroy", lambda w: gtk.main_quit())
|
||||
window.show()
|
||||
|
||||
canvas = hippo.Canvas()
|
||||
|
||||
box = SpreadBox(background_color=0xe2e2e2ff)
|
||||
canvas.set_root(box)
|
||||
box.spread()
|
||||
|
||||
canvas.show()
|
||||
window.add(canvas)
|
||||
|
||||
gobject.timeout_add(500, _create_icon)
|
||||
|
||||
gtk.main()
|
Loading…
Reference in New Issue
Block a user