Get activity bar and activity creation to work
This commit is contained in:
parent
dae3d2be2b
commit
7990bc0d31
@ -3,17 +3,27 @@ import goocanvas
|
|||||||
|
|
||||||
from sugar.canvas.IconItem import IconItem
|
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):
|
class ActivityBar(goocanvas.Group):
|
||||||
def __init__(self, registry):
|
def __init__(self, shell):
|
||||||
goocanvas.Group.__init__(self)
|
goocanvas.Group.__init__(self)
|
||||||
|
|
||||||
|
self._shell = shell
|
||||||
|
|
||||||
|
registry = shell.get_registry()
|
||||||
for activity in registry.list_activities():
|
for activity in registry.list_activities():
|
||||||
if activity.get_show_launcher():
|
if activity.get_show_launcher():
|
||||||
self.add_activity(activity)
|
self.add_activity(activity)
|
||||||
|
|
||||||
def add_activity(self, activity):
|
def add_activity(self, activity):
|
||||||
item = IconItem(icon_name=activity.get_icon(),
|
item = ActivityItem(activity)
|
||||||
color='white', width=42, height=42)
|
|
||||||
self.add_child(item)
|
self.add_child(item)
|
||||||
|
|
||||||
class Background(goocanvas.Group):
|
class Background(goocanvas.Group):
|
||||||
@ -43,7 +53,8 @@ class Model(goocanvas.CanvasModelSimple):
|
|||||||
background = Background()
|
background = Background()
|
||||||
root.add_child(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)
|
root.add_child(activity_bar)
|
||||||
|
|
||||||
class HomeWindow(gtk.Window):
|
class HomeWindow(gtk.Window):
|
||||||
@ -55,14 +66,27 @@ class HomeWindow(gtk.Window):
|
|||||||
self.connect('realize', self.__realize_cb)
|
self.connect('realize', self.__realize_cb)
|
||||||
|
|
||||||
canvas = goocanvas.CanvasView()
|
canvas = goocanvas.CanvasView()
|
||||||
|
|
||||||
canvas_model = Model(shell)
|
canvas_model = Model(shell)
|
||||||
canvas.set_bounds(0, 0, 1200, 900)
|
canvas.set_bounds(0, 0, 1200, 900)
|
||||||
canvas.set_scale(float(800) / float(1200))
|
canvas.set_scale(float(800) / float(1200))
|
||||||
canvas.set_size_request(800, 600)
|
canvas.set_size_request(800, 600)
|
||||||
|
|
||||||
|
canvas.connect("item_view_created", self.__item_view_created_cb)
|
||||||
|
|
||||||
self.add(canvas)
|
self.add(canvas)
|
||||||
canvas.show()
|
canvas.show()
|
||||||
|
|
||||||
canvas.set_model(canvas_model)
|
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):
|
def __realize_cb(self, window):
|
||||||
self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DESKTOP)
|
self.window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DESKTOP)
|
||||||
|
@ -3,52 +3,47 @@ import re
|
|||||||
import gobject
|
import gobject
|
||||||
import gtk
|
import gtk
|
||||||
import goocanvas
|
import goocanvas
|
||||||
import rsvg
|
|
||||||
|
|
||||||
class IconView(goocanvas.ItemViewSimple):
|
from sugar.util import GObjectSingletonMeta
|
||||||
def __init__(self, handle, canvas_view, parent_view):
|
|
||||||
goocanvas.SimpleItemView.__init__(self, canvas_view, parent_view)
|
|
||||||
self._handle = handle
|
|
||||||
|
|
||||||
def do_paint(self, cr, bounds, scale):
|
class IconCache(gobject.GObject):
|
||||||
self._handle.render_cairo(cr)
|
__metaclass__ = GObjectSingletonMeta
|
||||||
return self.bounds
|
|
||||||
|
|
||||||
class IconItem(goocanvas.Image):
|
def __init__(self):
|
||||||
__gproperties__ = {
|
gobject.GObject.__init__(self)
|
||||||
'icon-name': (str, None, None, None, gobject.PARAM_READWRITE),
|
self._icons = {}
|
||||||
'color': (str, None, None, None, gobject.PARAM_READWRITE)
|
|
||||||
}
|
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def _create_icon(self, name, color, size):
|
||||||
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'
|
|
||||||
theme = gtk.icon_theme_get_default()
|
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')
|
icon_file = open(info.get_filename(), 'r')
|
||||||
data = icon_file.read()
|
data = icon_file.read()
|
||||||
icon_file.close()
|
icon_file.close()
|
||||||
|
|
||||||
if self._color != None:
|
if color != None:
|
||||||
style = '.icon-color {fill: %s;}' % self._color
|
style = '.icon-color {fill: %s;}' % color
|
||||||
data = re.sub('\.icon-color \{.*\}', style, data)
|
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 IconView(handle, canvas_view, parent_view)
|
return loader.get_pixbuf()
|
||||||
|
|
||||||
|
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)
|
||||||
|
@ -4,6 +4,20 @@ import random
|
|||||||
import binascii
|
import binascii
|
||||||
import string
|
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):
|
def _stringify_sha(sha_hash):
|
||||||
"""Convert binary sha1 hash data into printable characters."""
|
"""Convert binary sha1 hash data into printable characters."""
|
||||||
print_sha = ""
|
print_sha = ""
|
||||||
|
Loading…
Reference in New Issue
Block a user