Remove buddies when they disappear. The canvas model / data model

separation makes this more complicated than I'd like. Maybe
it's worth to merge the two levels, I dunno.
This commit is contained in:
Marco Pesenti Gritti 2006-08-26 14:59:19 +02:00
parent 83bbe54bc8
commit 22e290332e
2 changed files with 43 additions and 16 deletions

View File

@ -17,7 +17,8 @@ class Friend:
icolor = IconColor(color) icolor = IconColor(color)
except RuntimeError: except RuntimeError:
icolor = IconColor() icolor = IconColor()
logging.info("Buddy %s doesn't have an allowed color; using a random color instead." % self.get_name()) logging.info("Buddy %s doesn't have an allowed color; \
using a random color instead." % self.get_name())
return icolor return icolor
class FriendsModel(gobject.GObject): class FriendsModel(gobject.GObject):
@ -31,21 +32,29 @@ class FriendsModel(gobject.GObject):
def __init__(self): def __init__(self):
gobject.GObject.__init__(self) gobject.GObject.__init__(self)
self._friends = [] self._friends = {}
self._pservice = PresenceService.get_instance() self._pservice = PresenceService.get_instance()
self._pservice.connect("buddy-appeared", self.__buddy_appeared_cb) self._pservice.connect("buddy-appeared", self.__buddy_appeared_cb)
self._pservice.connect("buddy-disappeared", self.__buddy_disappeared_cb)
for buddy in self._pservice.get_buddies(): for buddy in self._pservice.get_buddies():
self.add_friend(buddy) self.add_friend(buddy)
def add_friend(self, buddy): def add_friend(self, buddy):
friend = Friend(buddy) friend = Friend(buddy)
self._friends.append(friend) self._friends[buddy.get_name()] = friend
self.emit('friend-added', friend) self.emit('friend-added', friend)
def remove_friend(self, buddy):
self.emit('friend-removed', self._friends[buddy.get_name()])
del self._friends[buddy.get_name()]
def __iter__(self): def __iter__(self):
return self._friends.__iter__() return self._friends.values().__iter__()
def __buddy_appeared_cb(self, pservice, buddy): def __buddy_appeared_cb(self, pservice, buddy):
self.add_friend(buddy) self.add_friend(buddy)
def __buddy_disappeared_cb(self, pservice, buddy):
self.remove_friend(buddy)

View File

@ -3,38 +3,52 @@ import random
import goocanvas import goocanvas
from sugar.canvas.IconItem import IconItem from sugar.canvas.IconItem import IconItem
import Theme import Theme
class FriendIcon(IconItem):
def __init__(self, friend):
IconItem.__init__(self, 'stock-buddy', friend.get_color(), 48)
self._friend = friend
self.set_property('x', random.random() * 1100)
self.set_property('y', random.random() * 800)
def get_friend(self):
return self._friend
class Model(goocanvas.CanvasModelSimple): class Model(goocanvas.CanvasModelSimple):
def __init__(self, data_model): def __init__(self, data_model):
goocanvas.CanvasModelSimple.__init__(self) goocanvas.CanvasModelSimple.__init__(self)
self._friend_to_child = {}
self._theme = Theme.get_instance() self._theme = Theme.get_instance()
self._theme.connect("theme-changed", self.__theme_changed_cb) self._theme.connect("theme-changed", self.__theme_changed_cb)
root = self.get_root_item() self._root = self.get_root_item()
color = self._theme.get_home_mesh_color() color = self._theme.get_home_mesh_color()
self._mesh_rect = goocanvas.Rect(width=1200, height=900, self._mesh_rect = goocanvas.Rect(width=1200, height=900,
fill_color=color) fill_color=color)
root.add_child(self._mesh_rect) self._root.add_child(self._mesh_rect)
color = self._theme.get_home_friends_color() color = self._theme.get_home_friends_color()
self._friends_rect = goocanvas.Rect(x=100, y=100, width=1000, height=700, self._friends_rect = goocanvas.Rect(x=100, y=100, width=1000, height=700,
line_width=0, fill_color=color, line_width=0, fill_color=color,
radius_x=30, radius_y=30) radius_x=30, radius_y=30)
root.add_child(self._friends_rect) self._root.add_child(self._friends_rect)
color = self._theme.get_home_activities_color() color = self._theme.get_home_activities_color()
self._home_rect = goocanvas.Rect(x=400, y=300, width=400, height=300, self._home_rect = goocanvas.Rect(x=400, y=300, width=400, height=300,
line_width=0, fill_color=color, line_width=0, fill_color=color,
radius_x=30, radius_y=30) radius_x=30, radius_y=30)
root.add_child(self._home_rect) self._root.add_child(self._home_rect)
for friend in data_model: for friend in data_model:
self.add_friend(friend) self.add_friend(friend)
data_model.connect('friend-added', self.__friend_added_cb) data_model.connect('friend-added', self.__friend_added_cb)
data_model.connect('friend-removed', self.__friend_removed_cb)
def __theme_changed_cb(self, theme): def __theme_changed_cb(self, theme):
color = self._theme.get_home_activities_color() color = self._theme.get_home_activities_color()
@ -45,17 +59,21 @@ class Model(goocanvas.CanvasModelSimple):
self._mesh_rect.set_property("fill-color", color) self._mesh_rect.set_property("fill-color", color)
def add_friend(self, friend): def add_friend(self, friend):
root = self.get_root_item() icon = FriendIcon(friend)
self._root.add_child(icon)
self._friend_to_child[friend] = icon
icon = IconItem('stock-buddy', friend.get_color(), 48) def remove_friend(self, friend):
icon.set_property('x', random.random() * 1100) icon = self._friend_to_child[friend]
icon.set_property('y', random.random() * 800) self._root.remove_child(self._root.find_child(icon))
del self._friend_to_child[friend]
root.add_child(icon)
def __friend_added_cb(self, data_model, friend): def __friend_added_cb(self, data_model, friend):
self.add_friend(friend) self.add_friend(friend)
def __friend_removed_cb(self, data_model, friend):
self.remove_friend(friend)
class FriendsView(goocanvas.CanvasView): class FriendsView(goocanvas.CanvasView):
def __init__(self, shell, data_model): def __init__(self, shell, data_model):
goocanvas.CanvasView.__init__(self) goocanvas.CanvasView.__init__(self)