Move the view to his own module
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
import math
|
||||
|
||||
import goocanvas
|
||||
|
||||
from sugar.canvas.IconItem import IconItem
|
||||
|
||||
class PieceIcon(IconItem):
|
||||
def __init__(self, piece_item, **kwargs):
|
||||
IconItem.__init__(self, size=48, **kwargs)
|
||||
self._piece_item = piece_item
|
||||
|
||||
def construct(self):
|
||||
angle_start = self._piece_item.get_angle_start()
|
||||
angle_end = self._piece_item.get_angle_end()
|
||||
radius = self.get_parent().get_radius()
|
||||
inner_radius = self.get_parent().get_inner_radius()
|
||||
|
||||
icon_radius = (radius + inner_radius) / 2
|
||||
icon_angle = (angle_start + angle_end) / 2
|
||||
x = icon_radius * math.cos(icon_angle)
|
||||
y = - icon_radius * math.sin(icon_angle)
|
||||
|
||||
icon_width = self.get_property('size')
|
||||
icon_height = self.get_property('size')
|
||||
self.set_property('x', x - icon_width / 2)
|
||||
self.set_property('y', y - icon_height / 2)
|
||||
|
||||
class PieceItem(goocanvas.Path):
|
||||
def __init__(self, angle_start, angle_end, **kwargs):
|
||||
goocanvas.Path.__init__(self, **kwargs)
|
||||
self._angle_start = angle_start
|
||||
self._angle_end = angle_end
|
||||
|
||||
self.set_property('fill-color', '#ffffff')
|
||||
self.set_property('stroke-color', '#e2e2e2')
|
||||
self.set_property('line-width', 4)
|
||||
|
||||
def get_icon(self):
|
||||
return self._icon
|
||||
|
||||
def set_icon(self, icon_name, color):
|
||||
self._icon = PieceIcon(self, icon_name=icon_name, color=color)
|
||||
self.get_parent().add_child(self._icon)
|
||||
self._icon.construct()
|
||||
|
||||
def get_angle_start(self):
|
||||
return self._angle_start
|
||||
|
||||
def get_angle_end(self):
|
||||
return self._angle_end
|
||||
|
||||
def construct(self):
|
||||
r = self.get_parent().get_radius()
|
||||
|
||||
data = 'M0,0 '
|
||||
|
||||
dx = r * math.cos(self._angle_start)
|
||||
dy = - r * math.sin(self._angle_start)
|
||||
|
||||
data += 'l%f,%f ' % (dx, dy)
|
||||
|
||||
dx = r * math.cos(self._angle_end)
|
||||
dy = - r * math.sin(self._angle_end)
|
||||
|
||||
data += 'A%f,%f 0 0,0 %f,%f ' % (r, r, dx, dy)
|
||||
|
||||
data += 'z'
|
||||
|
||||
self.set_property('data', data)
|
||||
|
||||
class DonutItem(goocanvas.Group):
|
||||
def __init__(self, radius, **kwargs):
|
||||
goocanvas.Group.__init__(self, **kwargs)
|
||||
self._radius = radius
|
||||
self._angle_start = 0
|
||||
|
||||
bg = goocanvas.Ellipse(radius_x=radius, radius_y=radius,
|
||||
fill_color='#f1f1f1', line_width=0)
|
||||
self.add_child(bg)
|
||||
|
||||
self._inner_radius = radius / 2
|
||||
fg = goocanvas.Ellipse(radius_x=self._inner_radius,
|
||||
radius_y=self._inner_radius,
|
||||
fill_color='#e2e2e2', line_width=0)
|
||||
self.add_child(fg)
|
||||
|
||||
def add_piece(self, perc, icon_name, color):
|
||||
# FIXME can't override set_parent on the
|
||||
# PieceItem and there is no signal. So we
|
||||
# call a construct method on the childs for now.
|
||||
|
||||
angle_end = self._angle_start + perc * 2 * math.pi / 100
|
||||
piece_item = PieceItem(self._angle_start, angle_end)
|
||||
self._angle_start = angle_end
|
||||
|
||||
self.add_child(piece_item, 1)
|
||||
piece_item.construct()
|
||||
piece_item.set_icon(icon_name, color)
|
||||
|
||||
return piece_item
|
||||
|
||||
def remove_piece(self, piece_item):
|
||||
index = self.find_child(piece_item)
|
||||
self.remove_child(index)
|
||||
|
||||
icon = piece_item.get_icon()
|
||||
index = self.find_child(icon)
|
||||
self.remove_child(index)
|
||||
|
||||
def get_radius(self):
|
||||
return self._radius
|
||||
|
||||
def get_inner_radius(self):
|
||||
return self._inner_radius
|
||||
@@ -0,0 +1,33 @@
|
||||
import random
|
||||
|
||||
import goocanvas
|
||||
|
||||
from view.home.IconLayout import IconLayout
|
||||
from view.home.MyIcon import MyIcon
|
||||
from view.FriendIcon import FriendIcon
|
||||
|
||||
class FriendsGroup(goocanvas.Group):
|
||||
def __init__(self, shell_model):
|
||||
goocanvas.Group.__init__(self)
|
||||
|
||||
self._shell_model = shell_model
|
||||
self._icon_layout = IconLayout(1200, 900)
|
||||
self._friends = shell_model.get_friends()
|
||||
|
||||
me = MyIcon(100)
|
||||
me.translate(600 - (me.get_property('size') / 2),
|
||||
450 - (me.get_property('size') / 2))
|
||||
self.add_child(me)
|
||||
|
||||
for friend in self._friends:
|
||||
self.add_friend(friend)
|
||||
|
||||
self._friends.connect('friend-added', self._friend_added_cb)
|
||||
|
||||
def add_friend(self, friend):
|
||||
icon = FriendIcon(self._shell_model, friend)
|
||||
self.add_child(icon)
|
||||
self._icon_layout.add_icon(icon)
|
||||
|
||||
def _friend_added_cb(self, data_model, friend):
|
||||
self.add_friend(friend)
|
||||
@@ -0,0 +1,52 @@
|
||||
import goocanvas
|
||||
|
||||
from view.home.DonutItem import DonutItem
|
||||
from view.home.MyIcon import MyIcon
|
||||
|
||||
class TasksItem(DonutItem):
|
||||
def __init__(self, shell_model):
|
||||
DonutItem.__init__(self, 250)
|
||||
|
||||
self._items = {}
|
||||
|
||||
self._shell_model = shell_model
|
||||
self._shell_model.connect('activity_opened', self.__activity_opened_cb)
|
||||
self._shell_model.connect('activity_closed', self.__activity_closed_cb)
|
||||
|
||||
def __activity_opened_cb(self, model, activity):
|
||||
self._add(activity)
|
||||
|
||||
def __activity_closed_cb(self, model, activity):
|
||||
self._remove(activity)
|
||||
|
||||
def _remove(self, activity):
|
||||
item = self._items[activity.get_id()]
|
||||
self.remove_piece(item)
|
||||
del self._items[activity.get_id()]
|
||||
|
||||
def _add(self, activity):
|
||||
icon_name = activity.get_icon_name()
|
||||
icon_color = activity.get_icon_color()
|
||||
|
||||
item = self.add_piece(100 / 8, icon_name, icon_color)
|
||||
item.get_icon().connect('clicked',
|
||||
self.__activity_icon_clicked_cb,
|
||||
activity)
|
||||
|
||||
self._items[activity.get_id()] = item
|
||||
|
||||
def __activity_icon_clicked_cb(self, item, activity):
|
||||
activity.present()
|
||||
|
||||
class HomeGroup(goocanvas.Group):
|
||||
def __init__(self, shell_model):
|
||||
goocanvas.Group.__init__(self)
|
||||
|
||||
tasks = TasksItem(shell_model)
|
||||
tasks.translate(600, 450)
|
||||
self.add_child(tasks)
|
||||
|
||||
me = MyIcon(150)
|
||||
me.translate(600 - (me.get_property('size') / 2),
|
||||
450 - (me.get_property('size') / 2))
|
||||
self.add_child(me)
|
||||
@@ -0,0 +1,50 @@
|
||||
import gtk
|
||||
import goocanvas
|
||||
import cairo
|
||||
|
||||
from sugar.canvas.CanvasView import CanvasView
|
||||
from view.home.MeshGroup import MeshGroup
|
||||
from view.home.HomeGroup import HomeGroup
|
||||
from view.home.FriendsGroup import FriendsGroup
|
||||
import sugar
|
||||
|
||||
class HomeWindow(gtk.Window):
|
||||
def __init__(self, shell_model):
|
||||
gtk.Window.__init__(self)
|
||||
self._shell_model = shell_model
|
||||
|
||||
self.realize()
|
||||
self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DESKTOP)
|
||||
|
||||
self._nb = gtk.Notebook()
|
||||
self._nb.set_show_border(False)
|
||||
self._nb.set_show_tabs(False)
|
||||
|
||||
self.add(self._nb)
|
||||
self._nb.show()
|
||||
|
||||
self._add_page(HomeGroup(shell_model))
|
||||
self._add_page(FriendsGroup(shell_model))
|
||||
self._add_page(MeshGroup())
|
||||
|
||||
def _add_page(self, group):
|
||||
view = CanvasView()
|
||||
self._nb.append_page(view)
|
||||
view.show()
|
||||
|
||||
model = goocanvas.CanvasModelSimple()
|
||||
root = model.get_root_item()
|
||||
view.set_model(model)
|
||||
|
||||
bg = goocanvas.Rect(width=1900, height=1200,
|
||||
line_width=0, fill_color='#e2e2e2')
|
||||
root.add_child(bg)
|
||||
root.add_child(group)
|
||||
|
||||
def set_zoom_level(self, level):
|
||||
if level == sugar.ZOOM_HOME:
|
||||
self._nb.set_current_page(0)
|
||||
elif level == sugar.ZOOM_FRIENDS:
|
||||
self._nb.set_current_page(1)
|
||||
elif level == sugar.ZOOM_MESH:
|
||||
self._nb.set_current_page(2)
|
||||
@@ -0,0 +1,34 @@
|
||||
import random
|
||||
|
||||
class IconLayout:
|
||||
def __init__(self, width, height):
|
||||
self._icons = []
|
||||
self._width = width
|
||||
self._height = height
|
||||
|
||||
def add_icon(self, icon):
|
||||
self._icons.append(icon)
|
||||
self._layout_icon(icon)
|
||||
|
||||
def remove_icon(self, icon):
|
||||
self._icons.remove(icon)
|
||||
|
||||
def _is_valid_position(self, icon, x, y):
|
||||
icon_size = icon.props.size
|
||||
border = 20
|
||||
|
||||
if not (border < x < self._width - icon_size - border and \
|
||||
border < y < self._height - icon_size - border):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def _layout_icon(self, icon):
|
||||
while True:
|
||||
x = random.random() * self._width
|
||||
y = random.random() * self._height
|
||||
if self._is_valid_position(icon, x, y):
|
||||
break
|
||||
|
||||
icon.props.x = x
|
||||
icon.props.y = y
|
||||
@@ -0,0 +1,10 @@
|
||||
sugardir = $(pkgdatadir)/shell/view/home
|
||||
sugar_PYTHON = \
|
||||
__init__.py \
|
||||
DonutItem.py \
|
||||
FriendsGroup.py \
|
||||
IconLayout.py \
|
||||
HomeGroup.py \
|
||||
HomeWindow.py \
|
||||
MeshGroup.py \
|
||||
MyIcon.py
|
||||
@@ -0,0 +1,82 @@
|
||||
import random
|
||||
|
||||
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=96)
|
||||
|
||||
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 MeshGroup(goocanvas.Group):
|
||||
def __init__(self):
|
||||
goocanvas.Group.__init__(self)
|
||||
self._icon_layout = IconLayout(1200, 900)
|
||||
self._activities = {}
|
||||
|
||||
self._pservice = PresenceService.get_instance()
|
||||
self._pservice.connect("service-appeared", self._service_appeared_cb)
|
||||
self._pservice.connect('activity-disappeared', self._activity_disappeared_cb)
|
||||
|
||||
for service in self._pservice.get_services():
|
||||
self._check_service(service)
|
||||
|
||||
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)
|
||||
@@ -0,0 +1,10 @@
|
||||
import conf
|
||||
from sugar.canvas.IconItem import IconItem
|
||||
from sugar.canvas.IconColor import IconColor
|
||||
|
||||
class MyIcon(IconItem):
|
||||
def __init__(self, size):
|
||||
profile = conf.get_profile()
|
||||
|
||||
IconItem.__init__(self, icon_name='stock-buddy',
|
||||
color=profile.get_color(), size=size)
|
||||
Reference in New Issue
Block a user