Add some simple layout logic for the icons

This commit is contained in:
Marco Pesenti Gritti 2006-08-29 16:07:23 +02:00
parent db4553184e
commit 8722255b17
4 changed files with 66 additions and 10 deletions

View File

@ -8,9 +8,7 @@ import Theme
class FriendIcon(IconItem):
def __init__(self, friend):
IconItem.__init__(self, icon_name='stock-buddy',
color=friend.get_color(), size=96,
x=random.random() * 2400,
y=random.random() * 1800)
color=friend.get_color(), size=96)
self._friend = friend
def get_friend(self):
@ -20,10 +18,12 @@ class FriendsGroup(goocanvas.Group):
WIDTH = 1200.0 * 1.9
HEIGHT = 900.0 * 1.9
def __init__(self, data_model):
def __init__(self, icon_layout, data_model):
goocanvas.Group.__init__(self)
self._friend_to_child = {}
self._icon_layout = icon_layout
self._theme = Theme.get_instance()
self._theme.connect("theme-changed", self.__theme_changed_cb)
@ -48,10 +48,12 @@ class FriendsGroup(goocanvas.Group):
icon = FriendIcon(friend)
self.add_child(icon)
self._friend_to_child[friend] = icon
self._icon_layout.add_icon(icon)
def remove_friend(self, friend):
icon = self._friend_to_child[friend]
self.remove_child(self._root.find_child(icon))
self._icon_layout.remove_icon(icon)
self.remove_child(self.find_child(icon))
del self._friend_to_child[friend]
def __friend_added_cb(self, data_model, friend):

View File

@ -5,6 +5,7 @@ import cairo
from home.MeshGroup import MeshGroup
from home.HomeGroup import HomeGroup
from home.FriendsGroup import FriendsGroup
from home.IconLayout import IconLayout
import sugar
class HomeWindow(gtk.Window):
@ -21,12 +22,26 @@ class HomeWindow(gtk.Window):
self._model = goocanvas.CanvasModelSimple()
root = self._model.get_root_item()
icon_layout = IconLayout(MeshGroup.WIDTH, MeshGroup.HEIGHT)
x1 = (self._width - FriendsGroup.WIDTH) / 2
y1 = (self._height - FriendsGroup.HEIGHT) / 2
x2 = x1 + FriendsGroup.WIDTH
y2 = y1 + FriendsGroup.HEIGHT
icon_layout.set_bounds(x1, y1, x2, y2)
data_model = model.get_mesh()
self._mesh_group = MeshGroup(data_model)
self._mesh_group = MeshGroup(icon_layout, data_model)
root.add_child(self._mesh_group)
icon_layout = IconLayout(FriendsGroup.WIDTH, FriendsGroup.HEIGHT)
x1 = (self._width - HomeGroup.WIDTH) / 2
y1 = (self._height - HomeGroup.HEIGHT) / 2
x2 = x1 + HomeGroup.WIDTH
y2 = y1 + HomeGroup.HEIGHT
icon_layout.set_bounds(x1, y1, x2, y2)
data_model = model.get_friends()
self._friends_group = FriendsGroup(data_model)
self._friends_group = FriendsGroup(icon_layout, data_model)
self._friends_group.translate((self._width - FriendsGroup.WIDTH) / 2,
(self._height - FriendsGroup.HEIGHT) / 2)
root.add_child(self._friends_group)

37
shell/home/IconLayout.py Normal file
View File

@ -0,0 +1,37 @@
import random
class IconLayout:
def __init__(self, width, height):
self._icons = []
self._width = width
self._height = height
def set_bounds(self, x1, y1, x2, y2):
self._x1 = x1
self._y1 = y1
self._x2 = x2
self._y2 = y2
def add_icon(self, icon):
self._icons.append(icon)
self._layout_icon(icon)
def remove_icon(self, icon):
self._icons.remove(icon)
def _is_valid_position(self, x, y):
if x < self._x1 or x > self._x2:
return True
if y < self._y1 or y > self._y2:
return True
return False
def _layout_icon(self, icon):
while True:
x = random.random() * self._width
y = random.random() * self._height
if self._is_valid_position(x, y):
break
icon.props.x = x
icon.props.y = y

View File

@ -26,8 +26,11 @@ class MeshGroup(goocanvas.Group):
WIDTH = 1200.0 * 3.5
HEIGHT = 900.0 * 3.5
def __init__(self, data_model):
def __init__(self, icon_layout, data_model):
goocanvas.Group.__init__(self)
self._icon_layout = icon_layout
self._theme = Theme.get_instance()
self._theme.connect("theme-changed", self.__theme_changed_cb)
@ -47,8 +50,7 @@ class MeshGroup(goocanvas.Group):
def add_activity(self, activity):
item = ActivityItem(activity)
item.set_property('x', random.random() * 1100)
item.set_property('y', random.random() * 800)
self._icon_layout.add_icon(item)
self.add_child(item)
def __activity_added_cb(self, data_model, activity):