sugar-toolkit-gtk3/shell/view/home/MeshBox.py

130 lines
3.7 KiB
Python
Raw Normal View History

import random
import hippo
import gobject
from sugar.graphics.spreadbox import SpreadBox
from sugar.graphics.snowflakebox import SnowflakeBox
from sugar.graphics.canvasicon import CanvasIcon
from view.BuddyIcon import BuddyIcon
import conf
2006-09-25 16:40:15 +02:00
class ActivityView(SnowflakeBox):
2006-09-25 16:40:15 +02:00
def __init__(self, shell, menu_shell, model):
SnowflakeBox.__init__(self)
2006-09-25 16:40:15 +02:00
2006-09-25 18:38:01 +02:00
self._shell = shell
2006-09-25 16:40:15 +02:00
self._model = model
2006-09-25 18:38:01 +02:00
self._icons = {}
2006-09-25 16:40:15 +02:00
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.set_root(icon)
2006-09-25 16:40:15 +02:00
2006-09-25 18:38:01 +02:00
def has_buddy_icon(self, name):
return self._icons.has_key(name)
def add_buddy_icon(self, name, icon):
self._icons[name] = icon
self.append(icon, hippo.PACK_FIXED)
2006-09-25 18:38:01 +02:00
def remove_buddy_icon(self, name):
icon = self._icons[name]
self.remove(icon)
2006-09-25 18:38:01 +02:00
del self._icons[name]
def _clicked_cb(self, item):
registry = conf.get_activity_registry()
default_type = self._model.get_service().get_type()
bundle = registry.get_activity_from_type(default_type)
self._shell.join_activity(bundle.get_id(), self._model.get_id())
class MeshBox(SpreadBox):
def __init__(self, shell, menu_shell):
SpreadBox.__init__(self, background_color=0xe2e2e2ff)
self._shell = shell
self._menu_shell = menu_shell
self._model = shell.get_model().get_mesh()
2006-09-25 16:40:15 +02:00
self._buddies = {}
self._activities = {}
2006-09-25 18:38:01 +02:00
self._buddy_to_activity = {}
for buddy_model in self._model.get_buddies():
self._add_buddy(buddy_model)
self._model.connect('buddy-added', self._buddy_added_cb)
2006-09-25 16:40:15 +02:00
self._model.connect('buddy-removed', self._buddy_removed_cb)
2006-09-25 18:38:01 +02:00
self._model.connect('buddy-moved', self._buddy_moved_cb)
2006-09-25 16:40:15 +02:00
for activity_model in self._model.get_activities():
self._add_activity(activity_model)
self._model.connect('activity-added', self._activity_added_cb)
self._model.connect('activity-removed', self._activity_removed_cb)
gobject.idle_add(self.spread)
def _buddy_added_cb(self, model, buddy_model):
2006-09-25 18:55:35 +02:00
self._add_alone_buddy(buddy_model)
2006-09-25 16:40:15 +02:00
def _buddy_removed_cb(self, model, buddy_model):
self._remove_buddy(buddy_model)
2006-09-25 18:38:01 +02:00
def _buddy_moved_cb(self, model, buddy_model, activity_model):
self._move_buddy(buddy_model, activity_model)
2006-09-25 16:40:15 +02:00
def _activity_added_cb(self, model, activity_model):
self._add_activity(activity_model)
def _activity_removed_cb(self, model, activity_model):
self._remove_activity(activity_model)
2006-09-25 18:55:35 +02:00
def _add_alone_buddy(self, buddy_model):
icon = BuddyIcon(self._shell, self._menu_shell, buddy_model)
2006-09-25 16:40:15 +02:00
icon.props.size = 80
self.add(icon)
2006-09-25 16:40:15 +02:00
self._buddies[buddy_model.get_name()] = icon
2006-09-25 18:55:35 +02:00
def _remove_alone_buddy(self, buddy_model):
2006-09-25 16:40:15 +02:00
icon = self._buddies[buddy_model.get_name()]
self.remove(icon)
2006-09-25 16:40:15 +02:00
del self._buddies[buddy_model.get_name()]
2006-09-25 18:55:35 +02:00
def _remove_buddy(self, buddy_model):
name = buddy_model.get_name()
if self._buddies.has_key(name):
self._remove_alone_buddy(buddy_model)
else:
for activity in self._activities.values():
if activity.has_buddy_icon(name):
activity.remove_buddy_icon(name)
2006-09-25 18:38:01 +02:00
def _move_buddy(self, buddy_model, activity_model):
name = buddy_model.get_name()
2006-09-25 18:55:35 +02:00
self._remove_buddy(buddy_model)
2006-09-25 18:38:01 +02:00
if activity_model == None:
2006-09-25 19:41:08 +02:00
self._add_alone_buddy(buddy_model)
2006-09-25 18:38:01 +02:00
else:
activity = self._activities[activity_model.get_id()]
icon = BuddyIcon(self._shell, self._menu_shell, buddy_model)
icon.props.size = 60
activity.add_buddy_icon(buddy_model.get_name(), icon)
2006-09-25 16:40:15 +02:00
def _add_activity(self, activity_model):
icon = ActivityView(self._shell, self._menu_shell, activity_model)
self.add(icon)
2006-09-25 16:40:15 +02:00
self._activities[activity_model.get_id()] = icon
def _remove_activity(self, activity_model):
icon = self._activities[activity_model.get_id()]
self.remove(icon)
2006-09-25 16:40:15 +02:00
del self._activities[activity_model.get_id()]