Start rewriting our canvas stuff to cleanup and use hippo.
Out of the build.
This commit is contained in:
parent
be70008c07
commit
d05f1f13d3
5
sugar/graphics/Makefile.am
Normal file
5
sugar/graphics/Makefile.am
Normal file
@ -0,0 +1,5 @@
|
||||
sugardir = $(pythondir)/sugar/graphics
|
||||
sugar_PYTHON = \
|
||||
__init__.py \
|
||||
canvasicon.py \
|
||||
grid.py
|
0
sugar/graphics/__init__.py
Normal file
0
sugar/graphics/__init__.py
Normal file
134
sugar/graphics/canvasicon.py
Normal file
134
sugar/graphics/canvasicon.py
Normal file
@ -0,0 +1,134 @@
|
||||
import re
|
||||
|
||||
import gobject
|
||||
import gtk
|
||||
import hippo
|
||||
import rsvg
|
||||
import cairo
|
||||
|
||||
from sugar.canvas.IconColor import IconColor
|
||||
|
||||
class _IconCache:
|
||||
def __init__(self):
|
||||
self._icons = {}
|
||||
self._theme = gtk.icon_theme_get_default()
|
||||
|
||||
def _read_icon(self, filename, color):
|
||||
icon_file = open(filename, 'r')
|
||||
|
||||
if color == None:
|
||||
return rsvg.Handle(file=filename)
|
||||
else:
|
||||
data = icon_file.read()
|
||||
icon_file.close()
|
||||
|
||||
fill = color.get_fill_color()
|
||||
stroke = color.get_stroke_color()
|
||||
|
||||
entity = '<!ENTITY fill_color "%s">' % fill
|
||||
data = re.sub('<!ENTITY fill_color .*>', entity, data)
|
||||
|
||||
entity = '<!ENTITY stroke_color "%s">' % stroke
|
||||
data = re.sub('<!ENTITY stroke_color .*>', entity, data)
|
||||
|
||||
return rsvg.Handle(data=data)
|
||||
|
||||
def get_handle(self, name, color, size):
|
||||
info = self._theme.lookup_icon(name, int(size), 0)
|
||||
|
||||
if color:
|
||||
key = (info.get_filename(), color.to_string())
|
||||
else:
|
||||
key = info.get_filename()
|
||||
|
||||
if self._icons.has_key(key):
|
||||
icon = self._icons[key]
|
||||
else:
|
||||
icon = self._read_icon(info.get_filename(), color)
|
||||
self._icons[key] = icon
|
||||
return icon
|
||||
|
||||
class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
|
||||
__gtype_name__ = 'CanvasIcon'
|
||||
|
||||
__gproperties__ = {
|
||||
'icon-name': (str, None, None, None,
|
||||
gobject.PARAM_READWRITE),
|
||||
'color' : (object, None, None,
|
||||
gobject.PARAM_READWRITE),
|
||||
'size' : (int, None, None,
|
||||
0, 1024, 24,
|
||||
gobject.PARAM_READWRITE)
|
||||
}
|
||||
|
||||
_cache = _IconCache()
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self._size = 24
|
||||
self._color = None
|
||||
self._icon_name = None
|
||||
|
||||
hippo.CanvasBox.__init__(self, **kwargs)
|
||||
|
||||
self._buffer = None
|
||||
self._buffer_size = 0.0
|
||||
|
||||
def do_set_property(self, pspec, value):
|
||||
if pspec.name == 'icon-name':
|
||||
self._icon_name = value
|
||||
self.emit_paint_needed(0, 0, -1, -1)
|
||||
elif pspec.name == 'color':
|
||||
self._color = value
|
||||
self.emit_paint_needed(0, 0, -1, -1)
|
||||
elif pspec.name == 'size':
|
||||
self._size = value
|
||||
self.emit_request_changed()
|
||||
|
||||
def do_get_property(self, pspec):
|
||||
if pspec.name == 'size':
|
||||
return self._size
|
||||
elif pspec.name == 'icon-name':
|
||||
return self._icon_name
|
||||
elif pspec.name == 'color':
|
||||
return self._color
|
||||
|
||||
def _get_buffer(self, cr, handle, size):
|
||||
if self._buffer and self._buffer_size != size:
|
||||
del self._buffer
|
||||
self._buffer = None
|
||||
|
||||
if self._buffer == None:
|
||||
target = cr.get_target()
|
||||
surface = target.create_similar(cairo.CONTENT_COLOR_ALPHA,
|
||||
int(size) + 1, int(size) + 1)
|
||||
|
||||
dimensions = handle.get_dimension_data()
|
||||
scale = float(size) / float(dimensions[0])
|
||||
|
||||
ctx = cairo.Context(surface)
|
||||
ctx.scale(scale, scale)
|
||||
handle.render_cairo(ctx)
|
||||
del ctx
|
||||
|
||||
self._buffer = surface
|
||||
self._buffer_scale = scale
|
||||
|
||||
return self._buffer
|
||||
|
||||
def do_paint_below_children(self, cr, damaged_box):
|
||||
icon_name = self._icon_name
|
||||
if icon_name == None:
|
||||
icon_name = 'stock-missing'
|
||||
|
||||
handle = CanvasIcon._cache.get_handle(
|
||||
icon_name, self._color, self._size)
|
||||
buf = self._get_buffer(cr, handle, self._size)
|
||||
|
||||
cr.set_source_surface(buf, 0.0, 0.0)
|
||||
cr.paint()
|
||||
|
||||
def do_get_width_request(self):
|
||||
return self._size
|
||||
|
||||
def do_get_height_request(self, for_width):
|
||||
return self._size
|
15
sugar/graphics/grid.py
Normal file
15
sugar/graphics/grid.py
Normal file
@ -0,0 +1,15 @@
|
||||
import gtk
|
||||
|
||||
COLS = 16
|
||||
ROWS = 12
|
||||
|
||||
class Grid(object):
|
||||
def __init__(self):
|
||||
self._factor = gtk.gdk.screen_width() / COLS
|
||||
|
||||
def position(self, x, y):
|
||||
return [x * self._factor, y * self._factor]
|
||||
|
||||
def rectangle(self, x, y, width, height):
|
||||
return [x * self._factor, y * self._factor,
|
||||
width * self._factor, height * self._factor]
|
13
sugar/graphics/style.py
Normal file
13
sugar/graphics/style.py
Normal file
@ -0,0 +1,13 @@
|
||||
_styles = {}
|
||||
|
||||
def register_style(name, style):
|
||||
_styles[name] = style
|
||||
|
||||
def apply_style(name, item):
|
||||
if _styles.has_key(name):
|
||||
for name in _styles.keys():
|
||||
item.set_property(name, _styles[name]
|
||||
|
||||
def Style(dict):
|
||||
def set_property(self, name, value):
|
||||
self._properties[name] = value
|
Loading…
Reference in New Issue
Block a user