Write a mesh model and start using it in the mesh view

This commit is contained in:
Marco Pesenti Gritti 2006-09-25 16:01:11 +02:00
parent 73be416c67
commit 56229aad36
5 changed files with 129 additions and 72 deletions

View File

@ -5,4 +5,5 @@ sugar_PYTHON = \
Friends.py \
Invites.py \
Owner.py \
MeshModel.py \
ShellModel.py

108
shell/model/MeshModel.py Normal file
View File

@ -0,0 +1,108 @@
import gobject
import conf
from sugar.canvas.IconItem import IconColor
from sugar.presence import PresenceService
from model.BuddyModel import BuddyModel
class ActivityModel:
def __init__(self, activity, service):
self._service = service
self._activity = activity
def get_id(self):
return self._activity.get_id()
def get_icon_name(self):
registry = conf.get_activity_registry()
info = registry.get_activity_from_type(self._service.get_type())
return info.get_icon()
def get_color(self):
return IconColor(self._activity.get_color())
def get_service(self):
return self._service
class MeshModel(gobject.GObject):
__gsignals__ = {
'activity-added': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
'activity-removed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
'buddy-added': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
'buddy-moved': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT,
gobject.TYPE_PYOBJECT])),
'buddy-removed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT]))
}
def __init__(self):
gobject.GObject.__init__(self)
self._activities = {}
self._buddies = {}
self._pservice = PresenceService.get_instance()
self._pservice.connect("service-appeared",
self._service_appeared_cb)
self._pservice.connect('activity-disappeared',
self._activity_disappeared_cb)
self._pservice.connect("buddy-appeared",
self._buddy_appeared_cb)
self._pservice.connect("buddy-disappeared",
self._buddy_disappeared_cb)
for service in self._pservice.get_services():
self._check_service(service)
def get_activities(self):
return self._activities
def get_buddies(self):
return self._buddies
def _buddy_activity_changed_cb(self, buddy, cur_activity):
buddy_model = self._buddies[buddy.get_name()]
activity_model = self._activities[cur_activity.get_id()]
self.emit('buddy-moved', buddy_model, activity_model)
def _buddy_appeared_cb(self, pservice, buddy):
model = BuddyModel(buddy=buddy)
model.connect('current-activity-changed',
self._buddy_activity_changed_cb)
self._buddies[model.get_name()] = model
self.emit('buddy-added', model)
def _buddy_disappeared_cb(self, pservice, buddy):
self.emit('buddy-removed', buddy)
del self._buddies[buddy.get_name()]
def _service_appeared_cb(self, pservice, service):
self._check_service(service)
def _check_service(self, service):
registry = conf.get_activity_registry()
if registry.get_activity_from_type(service.get_type()) != None:
activity_id = service.get_activity_id()
if not self.has_activity(activity_id):
activity = self._pservice.get_activity(activity_id)
if activity != None:
self.add_activity(activity, service)
def has_activity(self, activity_id):
return self._activities.has_key(activity_id)
def add_activity(self, activity, service):
model = ActivityModel(activity, service)
self._activities[model.get_id()] = model
self.emit('activity-added', model)
def _activity_disappeared_cb(self, pservice, activity):
if self._activities.has_key(activity.get_id()):
activity_model = self._activities[activity.get_id()]
self.emit('activity-removed', activity_model)
del self._activities[activity.get_id()]

View File

@ -1,5 +1,6 @@
from sugar.presence import PresenceService
from model.Friends import Friends
from model.MeshModel import MeshModel
from model.Owner import ShellOwner
class ShellModel:
@ -12,6 +13,10 @@ class ShellModel:
self._owner = ShellOwner()
self._owner.announce()
self._friends = Friends()
self._mesh = MeshModel()
def get_mesh(self):
return self._mesh
def get_friends(self):
return self._friends

View File

@ -28,7 +28,7 @@ class HomeWindow(gtk.Window):
self._add_page(HomeGroup(shell))
self._add_page(FriendsGroup(shell, menu_shell))
self._add_page(MeshGroup(shell))
self._add_page(MeshGroup(shell, menu_shell))
def _add_page(self, group):
view = CanvasView()

View File

@ -4,84 +4,27 @@ import goocanvas
import conf
from sugar.canvas.IconItem import IconItem
from sugar.canvas.IconItem import IconColor
from sugar.presence import PresenceService
from view.home.IconLayout import IconLayout
class ActivityItem(IconItem):
def __init__(self, activity, service):
self._service = service
self._activity = activity
IconItem.__init__(self, icon_name=self.get_icon_name(),
color=self.get_color(), size=112)
def get_id(self):
return self._activity.get_id()
def get_icon_name(self):
registry = conf.get_activity_registry()
info = registry.get_activity_from_type(self._service.get_type())
return info.get_icon()
def get_color(self):
return IconColor(self._activity.get_color())
def get_service(self):
return self._service
from view.BuddyIcon import BuddyIcon
class MeshGroup(goocanvas.Group):
def __init__(self, shell):
def __init__(self, shell, menu_shell):
goocanvas.Group.__init__(self)
self._shell = shell
self._menu_shell = menu_shell
self._model = shell.get_model().get_mesh()
self._layout = IconLayout(shell.get_grid())
self._icon_layout = IconLayout(shell.get_grid())
self._activities = {}
for buddy_model in self._model.get_buddies():
self._add_buddy(buddy_model)
self._pservice = PresenceService.get_instance()
self._pservice.connect("service-appeared",
self._service_appeared_cb)
self._pservice.connect('activity-disappeared',
self._activity_disappeared_cb)
self._model.connect('buddy-added', self._buddy_added_cb)
for service in self._pservice.get_services():
self._check_service(service)
def _buddy_added_cb(self, model, buddy_model):
self._add_buddy(buddy_model)
def _service_appeared_cb(self, pservice, service):
self._check_service(service)
def _check_service(self, service):
registry = conf.get_activity_registry()
if registry.get_activity_from_type(service.get_type()) != None:
activity_id = service.get_activity_id()
if not self.has_activity(activity_id):
activity = self._pservice.get_activity(activity_id)
if activity != None:
self.add_activity(activity, service)
def has_activity(self, activity_id):
return self._activities.has_key(activity_id)
def add_activity(self, activity, service):
item = ActivityItem(activity, service)
item.connect('clicked', self._activity_clicked_cb)
self._icon_layout.add_icon(item)
self.add_child(item)
self._activities[item.get_id()] = item
def _activity_disappeared_cb(self, pservice, activity):
if self._activities.has_key(activity.get_id()):
self.remove_child(self._activities[activity.get_id()])
del self._activities[activity.get_id()]
def _activity_clicked_cb(self, item):
default_type = item.get_service().get_type()
registry = conf.get_activity_registry()
bundle_id = registry.get_activity_from_type(default_type).get_id()
activity_id = item.get_id()
self._shell.join_activity(bundle_id, activity_id)
def _add_buddy(self, buddy_model):
icon = BuddyIcon(self._shell, self._menu_shell, buddy_model)
self.add_child(icon)
self._layout.add_icon(icon)