Merge branch 'master' of git+ssh://dev.laptop.org/git/sugar

Conflicts:

	shell/view/home/IconLayout.py
This commit is contained in:
Dan Williams 2006-09-24 21:37:46 -04:00
commit e2f82f02a0
5 changed files with 69 additions and 29 deletions

View File

@ -12,7 +12,7 @@ class FriendsGroup(goocanvas.Group):
self._shell = shell self._shell = shell
self._menu_shell = menu_shell self._menu_shell = menu_shell
self._icon_layout = IconLayout(1200, 900) self._icon_layout = IconLayout(shell.get_grid())
self._friends = {} self._friends = {}
me = MyIcon(112) me = MyIcon(112)

View File

@ -1,10 +1,15 @@
import random import random
import math
class IconLayout: class IconLayout:
def __init__(self, width, height): DISTANCE_THRESHOLD = 120.0
def __init__(self, grid):
self._icons = [] self._icons = []
self._width = width self._grid = grid
self._height = height
[self._x1, self._y1] = self._grid.convert_to_canvas(1, 1)
[self._x2, self._y2] = self._grid.convert_to_canvas(78, 59)
def add_icon(self, icon): def add_icon(self, icon):
self._icons.append(icon) self._icons.append(icon)
@ -13,22 +18,47 @@ class IconLayout:
def remove_icon(self, icon): def remove_icon(self, icon):
self._icons.remove(icon) self._icons.remove(icon)
def _is_valid_position(self, icon, x, y): def _distance(self, icon1, icon2):
icon_size = icon.get_property('size') a = icon2.props.x - icon1.props.x
border = 20 b = icon2.props.y - icon1.props.y
return math.sqrt(a * a + b * b)
if not (border < x < self._width - icon_size - border and \ def _spread_icons(self):
border < y < self._height - icon_size - border): self._stable = True
return False
return True for icon1 in self._icons:
vx = 0
vy = 0
for icon2 in self._icons:
if icon1 != icon2:
distance = self._distance(icon1, icon2)
if distance <= IconLayout.DISTANCE_THRESHOLD:
self._stable = False
vx += icon1.props.x - icon2.props.x
vy += icon1.props.y - icon2.props.y
new_x = icon1.props.x + vx
new_y = icon1.props.y + vy
new_x = max(self._x1, new_x)
new_y = max(self._y1, new_y)
new_x = min(self._x2 - icon1.props.size, new_x)
new_y = min(self._y2 - icon1.props.size, new_y)
icon1.props.x = new_x
icon1.props.y = new_y
def _layout_icon(self, icon): def _layout_icon(self, icon):
while True: x = random.random() * (self._x2 - self._x1 - icon.props.size)
x = random.random() * self._width y = random.random() * (self._y2 - self._y1 - icon.props.size)
y = random.random() * self._height
if self._is_valid_position(icon, x, y):
break
icon.set_property('x', x) icon.props.x = x + self._x1
icon.set_property('y', y) icon.props.y = y + self._y1
tries = 10
self._spread_icons()
while not self._stable and tries > 0:
self._spread_icons()
tries -= 1

View File

@ -37,7 +37,7 @@ class MeshGroup(goocanvas.Group):
self._shell = shell self._shell = shell
self._icon_layout = IconLayout(1200, 900) self._icon_layout = IconLayout(shell.get_grid())
self._activities = {} self._activities = {}
self._pservice = PresenceService.get_instance() self._pservice = PresenceService.get_instance()

View File

@ -31,6 +31,10 @@ class Grid:
return [grid_x, grid_y] return [grid_x, grid_y]
def convert_to_canvas(self, grid_x, grid_y):
scale = 1200 / Grid.COLS
return [grid_x * scale, grid_y * scale]
def set_constraints(self, component, x, y, width=-1, height=-1): def set_constraints(self, component, x, y, width=-1, height=-1):
if isinstance(component, gtk.Window): if isinstance(component, gtk.Window):
self._layout_window(component, x, y, width, height) self._layout_window(component, x, y, width, height)

View File

@ -1,6 +1,7 @@
#!/usr/bin/python #!/usr/bin/python
import pygtk import pygtk
pygtk.require('2.0') pygtk.require('2.0')
import gobject
from sugar.session.UITestSession import UITestSession from sugar.session.UITestSession import UITestSession
@ -17,6 +18,18 @@ from view.home.IconLayout import IconLayout
from sugar.canvas import IconColor from sugar.canvas import IconColor
from sugar.canvas.IconItem import IconItem from sugar.canvas.IconItem import IconItem
from sugar.canvas.CanvasView import CanvasView from sugar.canvas.CanvasView import CanvasView
from sugar.canvas.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 = gtk.Window()
window.connect("destroy", lambda w: gtk.main_quit()) window.connect("destroy", lambda w: gtk.main_quit())
@ -33,16 +46,9 @@ item = goocanvas.Rect(x=0, y=0, width=1200, height=900,
line_width=0.0, fill_color='#e2e2e2') line_width=0.0, fill_color='#e2e2e2')
root.add_child(item) root.add_child(item)
icon_layout = IconLayout(1200, 900) icon_layout = IconLayout(Grid())
for i in range(0, 20): gobject.timeout_add(500, _create_icon)
color = IconColor.IconColor()
icon = IconItem(size=75, color=color,
icon_name='stock-buddy')
root.add_child(icon)
icon_layout.add_icon(icon)
canvas.set_model(canvas_model) canvas.set_model(canvas_model)