OLD | NEW |
(Empty) | |
| 1 import gobject |
| 2 import rsvg |
| 3 #import cairo |
| 4 import goocanvas |
| 5 import os.path |
| 6 |
| 7 |
| 8 class SvgItem(goocanvas.ItemSimple): |
| 9 # setup our custom properties |
| 10 __gproperties__ = { |
| 11 'x': (float, # property type |
| 12 'X', # property nick name |
| 13 'The x coordinate of a SVG image', # property description |
| 14 -10e6, # property minimum value |
| 15 10e6, # property maximum value |
| 16 0, # property default value |
| 17 gobject.PARAM_READWRITE), # property flags |
| 18 |
| 19 'y': (float, |
| 20 'Y', |
| 21 'The y coordinate of a SVG image', |
| 22 -10e6, |
| 23 10e6, |
| 24 0, |
| 25 gobject.PARAM_READWRITE), |
| 26 |
| 27 'width': (float, |
| 28 'Width', |
| 29 'The width of the SVG Image', |
| 30 0, |
| 31 10e6, |
| 32 0, |
| 33 gobject.PARAM_READWRITE), |
| 34 |
| 35 'height': (float, |
| 36 'Height', |
| 37 'The width of the SVG Image', |
| 38 0, |
| 39 10e6, |
| 40 0, |
| 41 gobject.PARAM_READWRITE), |
| 42 } |
| 43 ···· |
| 44 def __init__(self, x, y, rsvg_handle, **kwargs): |
| 45 super(SvgItem, self).__init__(**kwargs) |
| 46 assert isinstance(rsvg_handle, rsvg.Handle) |
| 47 self.x = x |
| 48 self.y = y |
| 49 self.sx = 1.0 |
| 50 self.sy = 1.0 |
| 51 self.handle = rsvg_handle |
| 52 self.width = self.handle.props.width |
| 53 self.height = self.handle.props.height |
| 54 self.custom_width = None |
| 55 self.custom_height = None |
| 56 |
| 57 def do_set_property(self, pspec, value): |
| 58 if pspec.name == 'x': |
| 59 self.x = value |
| 60 ············ |
| 61 # make sure we update the display |
| 62 self.changed(True) |
| 63 ········ |
| 64 elif pspec.name == 'y': |
| 65 self.y = value |
| 66 ············ |
| 67 # make sure we update the display |
| 68 self.changed(True) |
| 69 ········ |
| 70 elif pspec.name == 'width': |
| 71 self.custom_width = value |
| 72 self._size_changed() |
| 73 ············ |
| 74 # make sure we update the display |
| 75 self.changed(True) |
| 76 |
| 77 elif pspec.name == 'height': |
| 78 self.custom_height = value |
| 79 self._size_changed() |
| 80 ············ |
| 81 # make sure we update the display |
| 82 self.changed(True) |
| 83 ········ |
| 84 else: |
| 85 raise AttributeError, 'unknown property %s' % pspec.name |
| 86 |
| 87 def _size_changed(self): |
| 88 if self.custom_width is None and self.custom_height is None: |
| 89 self.width = self.handle.props.width |
| 90 self.height = self.handle.props.height |
| 91 self.sx = 1.0 |
| 92 self.sy = 1.0 |
| 93 elif self.custom_width is not None and self.custom_height is None: |
| 94 self.width = self.custom_width |
| 95 self.sx = self.custom_width / self.handle.props.width |
| 96 self.sy = self.sx |
| 97 self.height = self.handle.props.height*self.sy |
| 98 elif self.custom_width is None and self.custom_height is not None: |
| 99 self.height = self.custom_height |
| 100 self.sy = self.custom_height / self.handle.props.height |
| 101 self.sx = self.sy |
| 102 self.width = self.handle.props.width*self.sx |
| 103 else: |
| 104 self.width = self.custom_width |
| 105 self.height = self.custom_height |
| 106 self.sx = self.custom_width / self.handle.props.width |
| 107 self.sy = self.custom_height / self.handle.props.height |
| 108 |
| 109 def do_get_property(self, pspec): |
| 110 if pspec.name == 'x': |
| 111 return self.x |
| 112 |
| 113 elif pspec.name == 'y': |
| 114 return self.y |
| 115 |
| 116 elif pspec.name == 'width': |
| 117 self.width = self.handle.props.width |
| 118 self.height = self.handle.props.height |
| 119 |
| 120 return self.width |
| 121 |
| 122 elif pspec.name == 'height': |
| 123 return self.height |
| 124 |
| 125 else: |
| 126 raise AttributeError, 'unknown property %s' % pspec.name |
| 127 ···· |
| 128 def do_simple_paint(self, cr, bounds): |
| 129 cr.translate(self.x, self.y) |
| 130 cr.scale(self.sx, self.sy) |
| 131 self.handle.render_cairo(cr) |
| 132 |
| 133 def do_simple_update(self, cr): |
| 134 self.bounds_x1 = float(self.x) |
| 135 self.bounds_y1 = float(self.y) |
| 136 self.bounds_x2 = float(self.x + self.width) |
| 137 self.bounds_y2 = float(self.y + self.height) |
| 138 |
| 139 def do_simple_is_item_at(self, x, y, cr, is_pointer_event): |
| 140 if ((x < self.x) or (x > self.x + self.width)) or ((y < self.y) or (y >
self.y + self.height)): |
| 141 return False |
| 142 else:···· |
| 143 return True |
| 144 |
| 145 |
| 146 _rsvg_cache = dict() |
| 147 |
| 148 def rsvg_handle_factory(base_file_name): |
| 149 try: |
| 150 return _rsvg_cache[base_file_name] |
| 151 except KeyError: |
| 152 full_path = os.path.join(os.path.dirname(__file__), 'resource', base_fil
e_name) |
| 153 rsvg_handle = rsvg.Handle(full_path) |
| 154 _rsvg_cache[base_file_name] = rsvg_handle |
| 155 return rsvg_handle |
| 156 |
OLD | NEW |