From 22e290332edc221f3020cb1d3f69dddc0c830af5 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sat, 26 Aug 2006 14:59:19 +0200 Subject: [PATCH] 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. --- shell/home/FriendsModel.py | 17 +++++++++++---- shell/home/FriendsView.py | 42 +++++++++++++++++++++++++++----------- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/shell/home/FriendsModel.py b/shell/home/FriendsModel.py index 791aa815..87f2625e 100644 --- a/shell/home/FriendsModel.py +++ b/shell/home/FriendsModel.py @@ -17,7 +17,8 @@ class Friend: icolor = IconColor(color) except RuntimeError: 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 class FriendsModel(gobject.GObject): @@ -31,21 +32,29 @@ class FriendsModel(gobject.GObject): def __init__(self): gobject.GObject.__init__(self) - self._friends = [] + self._friends = {} self._pservice = PresenceService.get_instance() 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(): self.add_friend(buddy) def add_friend(self, buddy): friend = Friend(buddy) - self._friends.append(friend) + self._friends[buddy.get_name()] = 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): - return self._friends.__iter__() + return self._friends.values().__iter__() def __buddy_appeared_cb(self, pservice, buddy): self.add_friend(buddy) + + def __buddy_disappeared_cb(self, pservice, buddy): + self.remove_friend(buddy) diff --git a/shell/home/FriendsView.py b/shell/home/FriendsView.py index c1d3e627..4f0f365d 100644 --- a/shell/home/FriendsView.py +++ b/shell/home/FriendsView.py @@ -3,38 +3,52 @@ import random import goocanvas from sugar.canvas.IconItem import IconItem - 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): def __init__(self, data_model): goocanvas.CanvasModelSimple.__init__(self) + self._friend_to_child = {} + self._theme = Theme.get_instance() 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() self._mesh_rect = goocanvas.Rect(width=1200, height=900, fill_color=color) - root.add_child(self._mesh_rect) + self._root.add_child(self._mesh_rect) color = self._theme.get_home_friends_color() self._friends_rect = goocanvas.Rect(x=100, y=100, width=1000, height=700, line_width=0, fill_color=color, 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() self._home_rect = goocanvas.Rect(x=400, y=300, width=400, height=300, line_width=0, fill_color=color, radius_x=30, radius_y=30) - root.add_child(self._home_rect) + self._root.add_child(self._home_rect) for friend in data_model: self.add_friend(friend) data_model.connect('friend-added', self.__friend_added_cb) + data_model.connect('friend-removed', self.__friend_removed_cb) def __theme_changed_cb(self, theme): color = self._theme.get_home_activities_color() @@ -45,16 +59,20 @@ class Model(goocanvas.CanvasModelSimple): self._mesh_rect.set_property("fill-color", color) 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) - icon.set_property('x', random.random() * 1100) - icon.set_property('y', random.random() * 800) - - root.add_child(icon) + def remove_friend(self, friend): + icon = self._friend_to_child[friend] + self._root.remove_child(self._root.find_child(icon)) + del self._friend_to_child[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): def __init__(self, shell, data_model):