Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/shell/view/home/MeshBox.py
blob: 9b3ce3cf3d7c8bd4f73002fd4d065ae9f0f88798 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import random

import hippo

from sugar.graphics.spreadlayout import SpreadLayout
from sugar.graphics.canvasicon import CanvasIcon
from view.BuddyIcon import BuddyIcon
from sugar.graphics.snowflakelayout import SnowflakeLayout
import conf

class ActivityView(hippo.CanvasBox, hippo.CanvasItem):
	__gtype_name__ = 'SugarActivityView'
	def __init__(self, shell, menu_shell, model):
		hippo.CanvasBox.__init__(self)

		self._shell = shell
		self._model = model
		self._layout = SnowflakeLayout()
		self._icons = {}

		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):
		return self._icons.has_key(name)

	def add_buddy_icon(self, name, icon):
		self._icons[name] = icon
		self.append(icon, hippo.PACK_FIXED)

	def remove_buddy_icon(self, name):
		icon = self._icons[name]
		self.remove(icon)
		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())

	def do_allocate(self, width, height):
		hippo.CanvasBox.do_allocate(self, width, height)
		self._layout.layout(self)

class MeshBox(hippo.CanvasBox, hippo.CanvasItem):
	__gtype_name__ = 'SugarMeshBox'
	def __init__(self, shell, menu_shell):
		hippo.CanvasBox.__init__(self, background_color=0xe2e2e2ff)

		self._shell = shell
		self._menu_shell = menu_shell
		self._model = shell.get_model().get_mesh()
		self._layout = SpreadLayout()
		self._buddies = {}
		self._activities = {}
		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)
		self._model.connect('buddy-removed', self._buddy_removed_cb)
		self._model.connect('buddy-moved', self._buddy_moved_cb)

		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)

	def _buddy_added_cb(self, model, buddy_model):
		self._add_alone_buddy(buddy_model)

	def _buddy_removed_cb(self, model, buddy_model):
		self._remove_buddy(buddy_model) 

	def _buddy_moved_cb(self, model, buddy_model, activity_model):
		self._move_buddy(buddy_model, activity_model)

	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) 

	def _add_alone_buddy(self, buddy_model):
		icon = BuddyIcon(self._shell, self._menu_shell, buddy_model)
		icon.props.size = 80
		self.append(icon, hippo.PACK_FIXED)

		self._buddies[buddy_model.get_name()] = icon

	def _remove_alone_buddy(self, buddy_model):
		icon = self._buddies[buddy_model.get_name()]
		self.remove(icon)
		del self._buddies[buddy_model.get_name()]

	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)

	def _move_buddy(self, buddy_model, activity_model):
		name = buddy_model.get_name()

		self._remove_buddy(buddy_model)

		if activity_model == None:
			self._add_alone_buddy(buddy_model)
		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)

	def _add_activity(self, activity_model):
		icon = ActivityView(self._shell, self._menu_shell, activity_model)
		self.append(icon, hippo.PACK_FIXED)

		self._activities[activity_model.get_id()] = icon

	def _remove_activity(self, activity_model):
		icon = self._activities[activity_model.get_id()]
		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)