From 7990bc0d3126408e2728e3f41745e1332182de5b Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Thu, 17 Aug 2006 10:32:59 +0200 Subject: [PATCH] Get activity bar and activity creation to work --- shell/HomeWindow.py | 32 ++++++++++++++++--- sugar/canvas/IconItem.py | 69 +++++++++++++++++++--------------------- sugar/util.py | 14 ++++++++ 3 files changed, 74 insertions(+), 41 deletions(-) diff --git a/shell/HomeWindow.py b/shell/HomeWindow.py index d44add60..1449d0df 100644 --- a/shell/HomeWindow.py +++ b/shell/HomeWindow.py @@ -3,17 +3,27 @@ import goocanvas from sugar.canvas.IconItem import IconItem +class ActivityItem(IconItem): + def __init__(self, activity): + IconItem.__init__(self, activity.get_icon(), 'white', 30) + self._activity = activity + + def get_activity_id(self): + return self._activity.get_id() + class ActivityBar(goocanvas.Group): - def __init__(self, registry): + def __init__(self, shell): goocanvas.Group.__init__(self) + self._shell = shell + + registry = shell.get_registry() for activity in registry.list_activities(): if activity.get_show_launcher(): self.add_activity(activity) def add_activity(self, activity): - item = IconItem(icon_name=activity.get_icon(), - color='white', width=42, height=42) + item = ActivityItem(activity) self.add_child(item) class Background(goocanvas.Group): @@ -43,7 +53,8 @@ class Model(goocanvas.CanvasModelSimple): background = Background() root.add_child(background) - activity_bar = ActivityBar(shell.get_registry()) + activity_bar = ActivityBar(shell) + activity_bar.translate(50, 860) root.add_child(activity_bar) class HomeWindow(gtk.Window): @@ -55,14 +66,27 @@ class HomeWindow(gtk.Window): self.connect('realize', self.__realize_cb) canvas = goocanvas.CanvasView() + canvas_model = Model(shell) canvas.set_bounds(0, 0, 1200, 900) canvas.set_scale(float(800) / float(1200)) canvas.set_size_request(800, 600) + + canvas.connect("item_view_created", self.__item_view_created_cb) + self.add(canvas) canvas.show() canvas.set_model(canvas_model) + def __item_view_created_cb(self, view, item_view, item): + if isinstance(item, ActivityItem): + item_view.connect("button_press_event", + self.__activity_button_press_cb, + item.get_activity_id()) + + def __activity_button_press_cb(self, view, target, event, activity_id): + self._shell.start_activity(activity_id) + def __realize_cb(self, window): self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DESKTOP) diff --git a/sugar/canvas/IconItem.py b/sugar/canvas/IconItem.py index 66ec1934..7be096d1 100644 --- a/sugar/canvas/IconItem.py +++ b/sugar/canvas/IconItem.py @@ -3,52 +3,47 @@ import re import gobject import gtk import goocanvas -import rsvg -class IconView(goocanvas.ItemViewSimple): - def __init__(self, handle, canvas_view, parent_view): - goocanvas.SimpleItemView.__init__(self, canvas_view, parent_view) - self._handle = handle +from sugar.util import GObjectSingletonMeta - def do_paint(self, cr, bounds, scale): - self._handle.render_cairo(cr) - return self.bounds +class IconCache(gobject.GObject): + __metaclass__ = GObjectSingletonMeta -class IconItem(goocanvas.Image): - __gproperties__ = { - 'icon-name': (str, None, None, None, gobject.PARAM_READWRITE), - 'color': (str, None, None, None, gobject.PARAM_READWRITE) - } + def __init__(self): + gobject.GObject.__init__(self) + self._icons = {} - def __init__(self, **kwargs): - goocanvas.Image.__init__(self, **kwargs) - - def do_set_property(self, pspec, value): - if pspec.name == 'icon-name': - self._icon_name = value - elif pspec.name == 'color': - self._color = value - else: - raise AttributeError, 'unknown property %s' % pspec.name - - def do_get_property(self, pspec): - if pspec.name == 'icon-name': - return self._icon_name - if pspec.name == 'color': - return self._color - - def create_view(self, canvas_view, parent_view): - print 'Create view' + def _create_icon(self, name, color, size): theme = gtk.icon_theme_get_default() - info = theme.lookup_icon(self._icon_name, 48, 0) + info = theme.lookup_icon(name, size, 0) icon_file = open(info.get_filename(), 'r') data = icon_file.read() icon_file.close() - if self._color != None: - style = '.icon-color {fill: %s;}' % self._color + if color != None: + style = '.icon-color {fill: %s;}' % color data = re.sub('\.icon-color \{.*\}', style, data) - handle = rsvg.Handle(data=data) + loader = gtk.gdk.pixbuf_loader_new_with_mime_type('image/svg-xml') + loader.set_size(size, size) + loader.write(data) + loader.close() + + return loader.get_pixbuf() - return IconView(handle, canvas_view, parent_view) + def get_icon(self, name, color, size): + key = (name, color, size) + if self._icons.has_key(key): + return self._icons[key] + else: + icon = self._create_icon(name, color, size) + self._icons[key] = icon + return icon + +class IconItem(goocanvas.Image): + def __init__(self, icon_name, color, size, **kwargs): + goocanvas.Image.__init__(self, **kwargs) + + icon_cache = IconCache() + pixbuf = icon_cache.get_icon(icon_name, color, size) + self.set_property('pixbuf', pixbuf) diff --git a/sugar/util.py b/sugar/util.py index bfddf32b..9cb7d55a 100644 --- a/sugar/util.py +++ b/sugar/util.py @@ -4,6 +4,20 @@ import random import binascii import string +import gobject + +class GObjectSingletonMeta(gobject.GObjectMeta): + """GObject Singleton Metaclass""" + + def __init__(klass, name, bases, dict): + gobject.GObjectMeta.__init__(klass, name, bases, dict) + klass.__instance = None + + def __call__(klass, *args, **kwargs): + if klass.__instance is None: + klass.__instance = gobject.GObjectMeta.__call__(klass, *args, **kwargs) + return klass.__instance + def _stringify_sha(sha_hash): """Convert binary sha1 hash data into printable characters.""" print_sha = ""