Port mesh and friends view to hippo, needs work

This commit is contained in:
Marco Pesenti Gritti 2006-10-03 16:31:32 +02:00
parent a951b36347
commit e14aa30f07
6 changed files with 95 additions and 112 deletions

View File

@ -1,45 +1,33 @@
import goocanvas
import BuddyIcon
from sugar.canvas.IconItem import IconItem
from sugar.presence import PresenceService
import conf
import hippo
import gobject
import BuddyIcon
from sugar.graphics.canvasicon import CanvasIcon
from sugar.presence import PresenceService
import conf
class BuddyActivityView(goocanvas.Group):
class BuddyActivityView(hippo.CanvasBox):
def __init__(self, shell, menu_shell, buddy, **kwargs):
goocanvas.Group.__init__(self, **kwargs)
hippo.CanvasBox.__init__(self, **kwargs)
self._pservice = PresenceService.get_instance()
self._activity_registry = conf.get_activity_registry()
self._buddy = buddy
self._buddy_icon = BuddyIcon.BuddyIcon(shell, menu_shell, buddy)
self.add_child(self._buddy_icon)
self.append(self._buddy_icon)
buddy_size = self._buddy_icon.props.size
offset_y = buddy_size
offset_x = (buddy_size - 48) / 2
self._activity_icon = IconItem(x=offset_x, y=offset_y, size=48)
self._activity_icon = CanvasIcon(size=48)
self._activity_icon_visible = False
if self._buddy.is_present():
self.__buddy_appeared_cb(buddy)
self._buddy_appeared_cb(buddy)
self._buddy.connect('current-activity-changed', self.__buddy_activity_changed_cb)
self._buddy.connect('appeared', self.__buddy_appeared_cb)
self._buddy.connect('disappeared', self.__buddy_disappeared_cb)
self._buddy.connect('color-changed', self.__buddy_color_changed_cb)
def get_size_request(self):
bi_size = self._buddy_icon.props.size
acti_size = self._activity_icon.props.size
width = bi_size
height = bi_size + acti_size
return [width, height]
self._buddy.connect('current-activity-changed', self._buddy_activity_changed_cb)
self._buddy.connect('appeared', self._buddy_appeared_cb)
self._buddy.connect('disappeared', self._buddy_disappeared_cb)
self._buddy.connect('color-changed', self._buddy_color_changed_cb)
def _get_new_icon_name(self, activity):
# FIXME: do something better here; we probably need to use "flagship"
@ -51,14 +39,14 @@ class BuddyActivityView(goocanvas.Group):
return act.get_icon()
return None
def __remove_activity_icon(self):
def _remove_activity_icon(self):
if self._activity_icon_visible:
self.remove_child(self._activity_icon)
self.remove(self._activity_icon)
self._activity_icon_visible = False
def __buddy_activity_changed_cb(self, buddy, activity=None):
def _buddy_activity_changed_cb(self, buddy, activity=None):
if not activity:
self.__remove_activity_icon()
self._remove_activity_icon()
return
# FIXME: use some sort of "unknown activity" icon rather
@ -68,17 +56,17 @@ class BuddyActivityView(goocanvas.Group):
self._activity_icon.props.icon_name = name
self._activity_icon.props.color = buddy.get_color()
if not self._activity_icon_visible:
self.add_child(self._activity_icon)
self.append(self._activity_icon)
self._activity_icon_visible = True
else:
self.__remove_activity_icon()
self._remove_activity_icon()
def __buddy_appeared_cb(self, buddy):
def _buddy_appeared_cb(self, buddy):
activity = self._buddy.get_current_activity()
self.__buddy_activity_changed_cb(buddy, activity)
self._buddy_activity_changed_cb(buddy, activity)
def __buddy_disappeared_cb(self, buddy):
self.__buddy_activity_changed_cb(buddy, None)
def _buddy_disappeared_cb(self, buddy):
self._buddy_activity_changed_cb(buddy, None)
def __buddy_color_changed_cb(self, buddy, color):
def _buddy_color_changed_cb(self, buddy, color):
self._activity_icon.props.color = buddy.get_color()

View File

@ -1,4 +1,4 @@
from sugar.canvas.MenuIcon import MenuIcon
from sugar.graphics.menuicon import MenuIcon
from view.BuddyMenu import BuddyMenu
class BuddyIcon(MenuIcon):
@ -8,11 +8,11 @@ class BuddyIcon(MenuIcon):
self._shell = shell
self._buddy = buddy
self._buddy.connect('appeared', self.__buddy_presence_change_cb)
self._buddy.connect('disappeared', self.__buddy_presence_change_cb)
self._buddy.connect('color-changed', self.__buddy_presence_change_cb)
self._buddy.connect('appeared', self._buddy_presence_change_cb)
self._buddy.connect('disappeared', self._buddy_presence_change_cb)
self._buddy.connect('color-changed', self._buddy_presence_change_cb)
def __buddy_presence_change_cb(self, buddy, color=None):
def _buddy_presence_change_cb(self, buddy, color=None):
# Update the icon's color when the buddy comes and goes
self.set_property('color', buddy.get_color())

View File

@ -1,24 +1,25 @@
import random
import goocanvas
import hippo
from view.home.IconLayout import IconLayout
from sugar.graphics.spreadlayout import SpreadLayout
from view.home.MyIcon import MyIcon
from view.BuddyActivityView import BuddyActivityView
class FriendsGroup(goocanvas.Group):
class FriendsBox(hippo.CanvasBox, hippo.CanvasItem):
__gtype_name__ = 'SugarFriendsBox'
def __init__(self, shell, menu_shell):
goocanvas.Group.__init__(self)
hippo.CanvasBox.__init__(self, background_color=0xe2e2e2ff)
self._shell = shell
self._menu_shell = menu_shell
self._icon_layout = IconLayout(shell.get_grid())
self._layout = SpreadLayout()
self._friends = {}
me = MyIcon(112)
me.translate(600 - (me.get_property('size') / 2),
450 - (me.get_property('size') / 2))
self.add_child(me)
#me = MyIcon(112)
#me.translate(600 - (me.get_property('size') / 2),
# 450 - (me.get_property('size') / 2))
#self.add_child(me)
friends = self._shell.get_model().get_friends()
@ -30,8 +31,7 @@ class FriendsGroup(goocanvas.Group):
def add_friend(self, buddy_info):
icon = BuddyActivityView(self._shell, self._menu_shell, buddy_info)
self.add_child(icon)
self._icon_layout.add_icon(icon)
self.append(icon, hippo.PACK_FIXED)
self._friends[buddy_info.get_name()] = icon
@ -39,5 +39,9 @@ class FriendsGroup(goocanvas.Group):
self.add_friend(buddy_info)
def _friend_removed_cb(self, data_model, name):
self.remove_child(self._friends[name])
self.remove(self._friends[name])
del self._friends[name]
def do_allocate(self, width, height):
hippo.CanvasBox.do_allocate(self, width, height)
self._layout.layout(self)

View File

@ -1,12 +1,13 @@
import gtk
import goocanvas
import hippo
import cairo
from sugar.canvas.CanvasView import CanvasView
from sugar.canvas.MenuShell import MenuShell
from view.home.MeshGroup import MeshGroup
from view.home.MeshBox import MeshBox
from view.home.HomeGroup import HomeGroup
from view.home.FriendsGroup import FriendsGroup
from view.home.FriendsBox import FriendsBox
import sugar
class HomeWindow(gtk.Window):
@ -27,8 +28,18 @@ class HomeWindow(gtk.Window):
menu_shell = MenuShell(shell.get_grid())
self._add_page(HomeGroup(shell))
self._add_page(FriendsGroup(shell, menu_shell))
self._add_page(MeshGroup(shell, menu_shell))
canvas = hippo.Canvas()
box = FriendsBox(shell, menu_shell)
canvas.set_root(box)
self._nb.append_page(canvas)
canvas.show()
canvas = hippo.Canvas()
box = MeshBox(shell, menu_shell)
canvas.set_root(box)
self._nb.append_page(canvas)
canvas.show()
def _add_page(self, group):
view = CanvasView()

View File

@ -1,26 +1,26 @@
import random
import goocanvas
import hippo
import conf
from sugar.canvas.IconItem import IconItem
from view.home.IconLayout import IconLayout
from sugar.graphics.spreadlayout import SpreadLayout
from sugar.graphics.canvasicon import CanvasIcon
from view.BuddyIcon import BuddyIcon
from sugar.canvas.SnowflakeLayout import SnowflakeLayout
import conf
class ActivityView(goocanvas.Group):
class ActivityView(hippo.CanvasBox):
def __init__(self, shell, menu_shell, model):
goocanvas.Group.__init__(self)
hippo.CanvasBox.__init__(self)
self._shell = shell
self._model = model
self._layout = SnowflakeLayout()
self._icons = {}
icon = IconItem(icon_name=model.get_icon_name(),
color=model.get_color(), size=80)
icon.connect('clicked', self._clicked_cb)
self.add_child(icon)
icon = CanvasIcon(icon_name=model.get_icon_name(),
color=model.get_color(), size=80)
icon.connect('activated', self._clicked_cb)
self.append(icon, hippo.PACK_FIXED)
self._layout.set_root(icon)
def has_buddy_icon(self, name):
@ -28,13 +28,11 @@ class ActivityView(goocanvas.Group):
def add_buddy_icon(self, name, icon):
self._icons[name] = icon
self.add_child(icon)
self._layout.add_child(icon)
self.append(icon, hippo.PACK_FIXED)
def remove_buddy_icon(self, name):
icon = self._icons[name]
self._layout.remove_child(icon)
self.remove_child(icon)
self.remove(icon)
del self._icons[name]
def get_size_request(self):
@ -47,15 +45,15 @@ class ActivityView(goocanvas.Group):
bundle = registry.get_activity_from_type(default_type)
self._shell.join_activity(bundle.get_id(), self._model.get_id())
class MeshGroup(goocanvas.Group):
class MeshBox(hippo.CanvasBox, hippo.CanvasItem):
__gtype_name__ = 'SugarMeshBox'
def __init__(self, shell, menu_shell):
goocanvas.Group.__init__(self)
hippo.CanvasBox.__init__(self, background_color=0xe2e2e2ff)
self._shell = shell
self._menu_shell = menu_shell
self._model = shell.get_model().get_mesh()
self._layout = IconLayout(shell.get_grid())
self._layout = SpreadLayout()
self._buddies = {}
self._activities = {}
self._buddy_to_activity = {}
@ -91,15 +89,13 @@ class MeshGroup(goocanvas.Group):
def _add_alone_buddy(self, buddy_model):
icon = BuddyIcon(self._shell, self._menu_shell, buddy_model)
icon.props.size = 80
self.add_child(icon)
self.append(icon, hippo.PACK_FIXED)
self._buddies[buddy_model.get_name()] = icon
self._layout.add_icon(icon)
def _remove_alone_buddy(self, buddy_model):
icon = self._buddies[buddy_model.get_name()]
self.remove_child(icon)
self._layout.remove_icon(icon)
self.remove(icon)
del self._buddies[buddy_model.get_name()]
def _remove_buddy(self, buddy_model):
@ -110,7 +106,6 @@ class MeshGroup(goocanvas.Group):
for activity in self._activities.values():
if activity.has_buddy_icon(name):
activity.remove_buddy_icon(name)
self._layout.update()
def _move_buddy(self, buddy_model, activity_model):
name = buddy_model.get_name()
@ -126,17 +121,17 @@ class MeshGroup(goocanvas.Group):
icon.props.size = 60
activity.add_buddy_icon(buddy_model.get_name(), icon)
self._layout.update()
def _add_activity(self, activity_model):
icon = ActivityView(self._shell, self._menu_shell, activity_model)
self.add_child(icon)
self.append(icon, hippo.PACK_FIXED)
self._activities[activity_model.get_id()] = icon
self._layout.add_icon(icon)
def _remove_activity(self, activity_model):
icon = self._activities[activity_model.get_id()]
self.remove_child(icon)
self._layout.remove_icon(icon)
self.remove(icon)
del self._activities[activity_model.get_id()]
def do_allocate(self, width, height):
hippo.CanvasBox.do_allocate(self, width, height)
self._layout.layout(self)

View File

@ -3,26 +3,11 @@ import math
import cairo
class IconLayout:
class SpreadLayout:
DISTANCE_THRESHOLD = 120.0
def __init__(self, grid):
self._icons = []
self._constraints = {}
self._grid = grid
[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):
self._icons.append(icon)
self._layout_icon(icon)
self.update()
def remove_icon(self, icon):
self._icons.remove(icon)
del self._constraints[icon]
self.update()
def __init__(self):
pass
def _get_distance(self, icon1, icon2):
[icon1_x, icon1_y] = self._constraints[icon1]
@ -83,9 +68,9 @@ class IconLayout:
self._spread_icons()
tries -= 1
def _layout_icon(self, icon):
[width, height] = icon.get_size_request()
x = random.random() * (self._x2 - self._x1 - width)
y = random.random() * (self._y2 - self._y1 - height)
self._constraints[icon] = [x, y]
def layout(self, box):
[width, height] = box.get_allocation()
for item in box.get_children():
x = int(random.random() * width)
y = int(random.random() * height)
box.move(item, x, y)