Reimplement the donut using hippo canvas

This commit is contained in:
Marco Pesenti Gritti 2006-10-04 14:23:15 +02:00
parent 1f8ff1db1f
commit 72a4aa6fe2
4 changed files with 119 additions and 69 deletions

View File

@ -0,0 +1,11 @@
import hippo
from view.home.activitiesdonut import ActivitiesDonut
class HomeBox(hippo.CanvasBox):
def __init__(self, shell):
hippo.CanvasBox.__init__(self, background_color=0xe2e2e2ff,
yalign=2)
donut = ActivitiesDonut(shell, box_width=300, box_height=300)
self.append(donut)

View File

@ -1,51 +0,0 @@
import goocanvas
from view.home.DonutItem import DonutItem
from view.home.MyIcon import MyIcon
class TasksItem(DonutItem):
def __init__(self, shell):
DonutItem.__init__(self, 250)
self._items = {}
shell.connect('activity_opened', self.__activity_opened_cb)
shell.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):
goocanvas.Group.__init__(self)
tasks = TasksItem(shell)
tasks.translate(600, 450)
self.add_child(tasks)
me = MyIcon(180)
me.translate(600 - (me.get_property('size') / 2),
450 - (me.get_property('size') / 2))
self.add_child(me)

View File

@ -1,12 +1,10 @@
import gtk import gtk
import goocanvas
import hippo import hippo
import cairo import cairo
from sugar.canvas.CanvasView import CanvasView
from sugar.graphics.menushell import MenuShell from sugar.graphics.menushell import MenuShell
from view.home.MeshBox import MeshBox from view.home.MeshBox import MeshBox
from view.home.HomeGroup import HomeGroup from view.home.HomeBox import HomeBox
from view.home.FriendsBox import FriendsBox from view.home.FriendsBox import FriendsBox
import sugar import sugar
@ -27,7 +25,11 @@ class HomeWindow(gtk.Window):
menu_shell = MenuShell() menu_shell = MenuShell()
self._add_page(HomeGroup(shell)) canvas = hippo.Canvas()
box = HomeBox(shell)
canvas.set_root(box)
self._nb.append_page(canvas)
canvas.show()
canvas = hippo.Canvas() canvas = hippo.Canvas()
box = FriendsBox(shell, menu_shell) box = FriendsBox(shell, menu_shell)
@ -41,20 +43,6 @@ class HomeWindow(gtk.Window):
self._nb.append_page(canvas) self._nb.append_page(canvas)
canvas.show() canvas.show()
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): def set_zoom_level(self, level):
if level == sugar.ZOOM_HOME: if level == sugar.ZOOM_HOME:
self._nb.set_current_page(0) self._nb.set_current_page(0)

View File

@ -0,0 +1,102 @@
import hippo
import math
from sugar.graphics.canvasicon import CanvasIcon
class ActivitiesDonut(hippo.CanvasBox, hippo.CanvasItem):
__gtype_name__ = 'SugarActivitiesDonut'
def __init__(self, shell, **kwargs):
hippo.CanvasBox.__init__(self, **kwargs)
self._activities = {}
shell.connect('activity_opened', self.__activity_opened_cb)
shell.connect('activity_closed', self.__activity_closed_cb)
def __activity_opened_cb(self, model, activity):
self._add_activity(activity)
def __activity_closed_cb(self, model, activity):
self._remove_activity(activity)
def _remove_activity(self, activity):
icon = self._activities[activity.get_id()]
self.remove(icon)
del self._activities[activity.get_id()]
def _add_activity(self, activity):
icon_name = activity.get_icon_name()
icon_color = activity.get_icon_color()
icon = CanvasIcon(icon_name=icon_name, color=icon_color, size=75)
icon.connect('activated', self.__activity_icon_clicked_cb, activity)
self.append(icon, hippo.PACK_FIXED)
self._activities[activity.get_id()] = icon
self.emit_paint_needed(0, 0, -1, -1)
def __activity_icon_clicked_cb(self, item, activity):
activity.present()
def _get_angles(self, index):
angle = 2 * math.pi / 8
return [index * angle, (index + 1) * angle]
def _get_radius(self):
[width, height] = self.get_allocation()
return min(width, height) / 2
def _get_inner_radius(self):
return self._get_radius() * 0.5
def do_paint_below_children(self, cr, damaged_box):
[width, height] = self.get_allocation()
cr.translate(width / 2, height / 2)
radius = self._get_radius()
cr.set_source_rgb(0xf1 / 255.0, 0xf1 / 255.0, 0xf1 / 255.0)
cr.arc(0, 0, radius, 0, 2 * math.pi)
cr.fill()
angle_end = 0
for i in range(0, len(self._activities)):
[angle_start, angle_end] = self._get_angles(i)
cr.new_path()
cr.move_to(0, 0)
cr.line_to(radius * math.cos(angle_start),
radius * math.sin(angle_start))
cr.arc(0, 0, radius, angle_start, angle_end)
cr.line_to(0, 0)
cr.set_source_rgb(0xe2 / 255.0, 0xe2 / 255.0, 0xe2 / 255.0)
cr.set_line_width(4)
cr.stroke_preserve()
cr.set_source_rgb(1, 1, 1)
cr.fill()
cr.set_source_rgb(0xe2 / 255.0, 0xe2 / 255.0, 0xe2 / 255.0)
cr.arc(0, 0, self._get_inner_radius(), 0, 2 * math.pi)
cr.fill()
def do_allocate(self, width, height):
hippo.CanvasBox.do_allocate(self, width, height)
radius = (self._get_inner_radius() + self._get_radius()) / 2
i = 0
for icon in self._activities.values():
[angle_start, angle_end] = self._get_angles(i)
angle = angle_start + (angle_end - angle_start) / 2
[icon_width, icon_height] = icon.get_allocation()
x = int(radius * math.cos(angle)) - icon_width / 2
y = int(radius * math.sin(angle)) - icon_height / 2
self.move(icon, x + width / 2, y + height / 2)
i += 1