2006-08-16 19:55:13 +02:00
|
|
|
import re
|
|
|
|
|
2006-08-16 23:20:22 +02:00
|
|
|
import gobject
|
2006-08-16 19:55:13 +02:00
|
|
|
import gtk
|
|
|
|
import goocanvas
|
2006-08-31 22:29:31 +02:00
|
|
|
import rsvg
|
2006-09-11 13:13:17 +02:00
|
|
|
import cairo
|
2006-08-16 19:55:13 +02:00
|
|
|
|
2006-08-17 10:32:59 +02:00
|
|
|
from sugar.util import GObjectSingletonMeta
|
2006-08-23 21:03:17 +02:00
|
|
|
from sugar.canvas.IconColor import IconColor
|
2006-08-19 14:27:56 +02:00
|
|
|
|
2006-08-31 22:29:31 +02:00
|
|
|
class _IconCache:
|
2006-08-17 10:32:59 +02:00
|
|
|
def __init__(self):
|
|
|
|
self._icons = {}
|
2006-09-22 10:55:10 +02:00
|
|
|
self._theme = gtk.icon_theme_get_default()
|
2006-08-16 23:20:22 +02:00
|
|
|
|
2006-09-22 10:55:10 +02:00
|
|
|
def _read_icon(self, filename, color):
|
|
|
|
icon_file = open(filename, 'r')
|
2006-08-16 19:55:13 +02:00
|
|
|
|
2006-09-11 00:36:12 +02:00
|
|
|
if color == None:
|
2006-09-22 10:55:10 +02:00
|
|
|
return rsvg.Handle(file=filename)
|
2006-09-11 00:36:12 +02:00
|
|
|
else:
|
|
|
|
data = icon_file.read()
|
|
|
|
icon_file.close()
|
|
|
|
|
2006-08-19 14:27:56 +02:00
|
|
|
fill = color.get_fill_color()
|
|
|
|
stroke = color.get_stroke_color()
|
2006-09-22 14:32:07 +02:00
|
|
|
|
|
|
|
entity = '<!ENTITY fill_color "%s">' % fill
|
|
|
|
data = re.sub('<!ENTITY fill_color .*>', entity, data)
|
2006-08-19 14:27:56 +02:00
|
|
|
|
2006-09-22 14:32:07 +02:00
|
|
|
entity = '<!ENTITY stroke_color "%s">' % stroke
|
|
|
|
data = re.sub('<!ENTITY stroke_color .*>', entity, data)
|
2006-08-19 11:54:41 +02:00
|
|
|
|
2006-09-11 00:36:12 +02:00
|
|
|
return rsvg.Handle(data=data)
|
|
|
|
|
2006-09-22 10:55:10 +02:00
|
|
|
def get_handle(self, name, color, size):
|
2006-09-22 11:43:44 +02:00
|
|
|
info = self._theme.lookup_icon(name, int(size), 0)
|
2006-09-22 10:55:10 +02:00
|
|
|
|
2006-09-11 00:36:12 +02:00
|
|
|
if color:
|
2006-09-22 10:55:10 +02:00
|
|
|
key = (info.get_filename(), color.to_string())
|
2006-09-11 00:36:12 +02:00
|
|
|
else:
|
2006-09-22 10:55:10 +02:00
|
|
|
key = info.get_filename()
|
2006-08-17 10:32:59 +02:00
|
|
|
|
|
|
|
if self._icons.has_key(key):
|
2006-08-31 22:29:31 +02:00
|
|
|
icon = self._icons[key]
|
2006-08-17 10:32:59 +02:00
|
|
|
else:
|
2006-09-22 10:55:10 +02:00
|
|
|
icon = self._read_icon(info.get_filename(), color)
|
2006-08-17 10:32:59 +02:00
|
|
|
self._icons[key] = icon
|
2006-08-31 22:29:31 +02:00
|
|
|
return icon
|
|
|
|
|
|
|
|
class IconView(goocanvas.ItemViewSimple, goocanvas.ItemView):
|
|
|
|
__gtype_name__ = 'IconView'
|
|
|
|
|
2006-09-11 00:36:12 +02:00
|
|
|
_cache = _IconCache()
|
2006-08-31 22:29:31 +02:00
|
|
|
|
|
|
|
def __init__(self, canvas_view, parent_view, item):
|
|
|
|
goocanvas.ItemViewSimple.__init__(self)
|
2006-09-10 23:41:11 +02:00
|
|
|
|
2006-08-31 22:29:31 +02:00
|
|
|
self.parent_view = parent_view
|
|
|
|
self.canvas_view = canvas_view
|
|
|
|
self.item = item
|
|
|
|
|
2006-09-11 13:13:17 +02:00
|
|
|
self._buffer = None
|
2006-09-22 10:55:10 +02:00
|
|
|
self._buffer_size = 0.0
|
2006-09-11 13:13:17 +02:00
|
|
|
|
2006-09-10 23:41:11 +02:00
|
|
|
item.connect('changed', goocanvas.item_view_simple_item_changed, self)
|
|
|
|
|
2006-08-31 22:29:31 +02:00
|
|
|
def do_get_item_view_at(self, x, y, cr, is_pointer_event, parent_is_visible):
|
|
|
|
result = self
|
|
|
|
|
|
|
|
cr.save()
|
|
|
|
|
2006-09-01 18:46:56 +02:00
|
|
|
if self.item.transform != None:
|
|
|
|
cr.transform(self.item.transform)
|
|
|
|
if self.transform != None:
|
|
|
|
cr.transform(self.transform)
|
2006-08-31 22:29:31 +02:00
|
|
|
|
|
|
|
[user_x, user_y] = cr.device_to_user(x, y)
|
|
|
|
if user_x < self.item.x or \
|
|
|
|
user_x > self.item.x + self.item.size or \
|
|
|
|
user_y < self.item.y or \
|
|
|
|
user_y > self.item.y + self.item.size:
|
|
|
|
result = None
|
|
|
|
|
|
|
|
cr.restore()
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def do_update(self, entire_tree, cr):
|
|
|
|
if entire_tree or self.flags & goocanvas.ITEM_VIEW_NEED_UPDATE:
|
|
|
|
self.flags &= ~goocanvas.ITEM_VIEW_NEED_UPDATE
|
|
|
|
|
|
|
|
cr.save()
|
|
|
|
|
2006-09-01 18:46:56 +02:00
|
|
|
if self.item.transform != None:
|
|
|
|
cr.transform(self.item.transform)
|
|
|
|
if self.transform != None:
|
|
|
|
cr.transform(self.transform)
|
2006-08-31 22:29:31 +02:00
|
|
|
|
2006-09-10 23:48:23 +02:00
|
|
|
self.get_canvas_view().request_redraw(self.bounds)
|
|
|
|
|
2006-08-31 22:29:31 +02:00
|
|
|
bounds = goocanvas.Bounds()
|
|
|
|
bounds.x1 = self.item.x
|
|
|
|
bounds.y1 = self.item.y
|
|
|
|
bounds.x2 = self.item.x + self.item.size
|
|
|
|
bounds.y2 = self.item.y + self.item.size
|
|
|
|
self.item.user_bounds_to_device(cr, bounds)
|
|
|
|
self.bounds = bounds
|
2006-08-17 10:32:59 +02:00
|
|
|
|
2006-09-08 11:54:31 +02:00
|
|
|
self.get_canvas_view().request_redraw(self.bounds)
|
|
|
|
|
2006-08-31 22:29:31 +02:00
|
|
|
cr.restore()
|
|
|
|
|
|
|
|
return self.bounds
|
|
|
|
|
2006-09-22 10:55:10 +02:00
|
|
|
def _get_buffer(self, cr, handle, size):
|
|
|
|
if self._buffer and self._buffer_size != size:
|
2006-09-11 13:13:17 +02:00
|
|
|
del self._buffer
|
|
|
|
self._buffer = None
|
|
|
|
|
|
|
|
if self._buffer == None:
|
2006-09-22 11:43:44 +02:00
|
|
|
target = cr.get_target()
|
|
|
|
surface = target.create_similar(cairo.CONTENT_COLOR_ALPHA,
|
|
|
|
int(size) + 1, int(size) + 1)
|
2006-09-11 13:13:17 +02:00
|
|
|
|
2006-09-22 10:55:10 +02:00
|
|
|
dimensions = handle.get_dimension_data()
|
2006-09-25 03:53:39 +02:00
|
|
|
scale = float(size) / float(dimensions[0])
|
2006-09-22 10:55:10 +02:00
|
|
|
|
2006-09-11 13:13:17 +02:00
|
|
|
ctx = cairo.Context(surface)
|
|
|
|
ctx.scale(scale, scale)
|
|
|
|
handle.render_cairo(ctx)
|
|
|
|
del ctx
|
|
|
|
|
|
|
|
self._buffer = surface
|
|
|
|
self._buffer_scale = scale
|
|
|
|
|
|
|
|
return self._buffer
|
|
|
|
|
2006-08-31 22:29:31 +02:00
|
|
|
def do_paint(self, cr, bounds, scale):
|
2006-09-09 13:25:56 +02:00
|
|
|
icon_name = self.item.icon_name
|
|
|
|
if icon_name == None:
|
|
|
|
icon_name = 'stock-missing'
|
|
|
|
|
2006-09-22 10:55:10 +02:00
|
|
|
handle = IconView._cache.get_handle(
|
|
|
|
icon_name, self.item.color, self.item.size)
|
|
|
|
buf = self._get_buffer(cr, handle, self.item.size)
|
2006-08-31 22:29:31 +02:00
|
|
|
|
|
|
|
cr.save()
|
|
|
|
|
2006-09-01 18:46:56 +02:00
|
|
|
if self.item.transform != None:
|
|
|
|
cr.transform(self.item.transform)
|
|
|
|
if self.transform != None:
|
|
|
|
cr.transform(self.transform)
|
2006-08-31 22:29:31 +02:00
|
|
|
|
|
|
|
cr.translate(self.item.x, self.item.y)
|
2006-09-11 13:13:17 +02:00
|
|
|
cr.set_source_surface(buf, 0.0, 0.0)
|
|
|
|
cr.paint()
|
2006-08-31 22:29:31 +02:00
|
|
|
|
|
|
|
cr.restore()
|
|
|
|
|
|
|
|
return self.bounds
|
|
|
|
|
|
|
|
class IconItem(goocanvas.ItemSimple, goocanvas.Item):
|
2006-08-28 14:36:48 +02:00
|
|
|
__gsignals__ = {
|
|
|
|
'clicked': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([])),
|
2006-09-14 13:03:11 +02:00
|
|
|
'popup': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([int, int, int, int])),
|
|
|
|
'popdown': (gobject.SIGNAL_RUN_FIRST,
|
|
|
|
gobject.TYPE_NONE, ([])),
|
2006-08-28 14:36:48 +02:00
|
|
|
}
|
|
|
|
|
2006-08-28 12:44:46 +02:00
|
|
|
__gproperties__ = {
|
2006-08-31 22:29:31 +02:00
|
|
|
'x' : (float, None, None, -10e6, 10e6, 0,
|
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'y' : (float, None, None, -10e6, 10e6, 0,
|
|
|
|
gobject.PARAM_READWRITE),
|
2006-08-28 12:44:46 +02:00
|
|
|
'icon-name': (str, None, None, None,
|
|
|
|
gobject.PARAM_READWRITE),
|
|
|
|
'color' : (object, None, None,
|
|
|
|
gobject.PARAM_READWRITE),
|
2006-09-22 10:55:10 +02:00
|
|
|
'size' : (float, None, None,
|
2006-08-28 12:44:46 +02:00
|
|
|
0, 1024, 24,
|
|
|
|
gobject.PARAM_READWRITE)
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2006-08-31 22:29:31 +02:00
|
|
|
self.x = 0.0
|
|
|
|
self.y = 0.0
|
2006-09-07 15:11:51 +02:00
|
|
|
self.size = 24
|
2006-09-08 00:51:45 +02:00
|
|
|
self.color = None
|
2006-09-09 13:25:56 +02:00
|
|
|
self.icon_name = None
|
2006-08-28 14:36:48 +02:00
|
|
|
|
2006-08-31 22:41:41 +02:00
|
|
|
goocanvas.ItemSimple.__init__(self, **kwargs)
|
|
|
|
|
2006-08-28 14:36:48 +02:00
|
|
|
def do_set_property(self, pspec, value):
|
2006-09-10 23:41:11 +02:00
|
|
|
recompute_bounds = False
|
|
|
|
|
2006-08-28 14:36:48 +02:00
|
|
|
if pspec.name == 'icon-name':
|
2006-08-31 22:29:31 +02:00
|
|
|
self.icon_name = value
|
2006-08-28 14:36:48 +02:00
|
|
|
elif pspec.name == 'color':
|
2006-08-31 22:29:31 +02:00
|
|
|
self.color = value
|
|
|
|
elif pspec.name == 'size':
|
|
|
|
self.size = value
|
2006-09-10 23:41:11 +02:00
|
|
|
recompute_bounds = True
|
2006-08-31 22:29:31 +02:00
|
|
|
elif pspec.name == 'x':
|
|
|
|
self.x = value
|
2006-09-10 23:41:11 +02:00
|
|
|
recompute_bounds = True
|
2006-08-31 22:29:31 +02:00
|
|
|
elif pspec.name == 'y':
|
|
|
|
self.y = value
|
2006-09-10 23:41:11 +02:00
|
|
|
recompute_bounds = True
|
|
|
|
|
|
|
|
self.emit('changed', recompute_bounds)
|
2006-08-31 22:29:31 +02:00
|
|
|
|
|
|
|
def do_get_property(self, pspec):
|
|
|
|
if pspec.name == 'x':
|
|
|
|
return self.x
|
|
|
|
elif pspec.name == 'y':
|
|
|
|
return self.y
|
2006-08-28 14:36:48 +02:00
|
|
|
elif pspec.name == 'size':
|
2006-08-31 22:29:31 +02:00
|
|
|
return self.size
|
|
|
|
elif pspec.name == 'icon-name':
|
|
|
|
return self.icon_name
|
|
|
|
elif pspec.name == 'color':
|
|
|
|
return self.color
|
2006-08-28 14:36:48 +02:00
|
|
|
|
|
|
|
def do_create_view(self, canvas, parent_view):
|
2006-08-31 22:29:31 +02:00
|
|
|
view = IconView(canvas, parent_view, self)
|
2006-09-14 13:03:11 +02:00
|
|
|
view.connect('button-press-event', self._button_press_cb)
|
2006-08-28 14:36:48 +02:00
|
|
|
return view
|
|
|
|
|
2006-09-25 11:35:30 +02:00
|
|
|
def get_size_request(self):
|
|
|
|
return [self.props.size, self.props.size]
|
|
|
|
|
2006-09-14 13:03:11 +02:00
|
|
|
def _button_press_cb(self, view, target, event):
|
2006-08-28 14:36:48 +02:00
|
|
|
self.emit('clicked')
|