A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
svgitem.py
Go to the documentation of this file.
1from gi.repository import GObject, GooCanvas
2import rsvg
3#import cairo
4import os.path
5
6
7
8class SvgItem(GooCanvas.ItemSimple):
9
35
36
37 __gproperties__ = {
38 'x': (float, # property type
39 'X', # property nick name
40 'The x coordinate of a SVG image', # property description
41 -10e6, # property minimum value
42 10e6, # property maximum value
43 0, # property default value
44 GObject.PARAM_READWRITE), # property flags
45
46 'y': (float,
47 'Y',
48 'The y coordinate of a SVG image',
49 -10e6,
50 10e6,
51 0,
52 GObject.PARAM_READWRITE),
53
54 'width': (float,
55 'Width',
56 'The width of the SVG Image',
57 0,
58 10e6,
59 0,
60 GObject.PARAM_READWRITE),
61
62 'height': (float,
63 'Height',
64 'The width of the SVG Image',
65 0,
66 10e6,
67 0,
68 GObject.PARAM_READWRITE),
69 }
70
71 def __init__(self, x, y, rsvg_handle, **kwargs):
72 """!
73 Initializer
74 @param self this object
75 @param x The x coordinate of a SVG image
76 @param y The y coordinate of a SVG image
77 @param rsvg_handle SVG handle
78 @param kwargs key-value arguments
79 """
80 super(SvgItem, self).__init__(**kwargs)
81 assert isinstance(rsvg_handle, rsvg.Handle)
82 self.x = x
83 self.y = y
84 self.sx = 1.0
85 self.sy = 1.0
86 self.handle = rsvg_handle
87 self.width = self.handle.props.width
88 self.height = self.handle.props.height
89 self.custom_width = None
90 self.custom_height = None
91
92 def do_set_property(self, pspec, value):
93 """!
94 Set Property
95 @param self this object
96 @param pspec property name
97 @param value property value
98 @return exception if unknown property
99 """
100 if pspec.name == 'x':
101 self.x = value
102
103 # make sure we update the display
104 self.changed(True)
105
106 elif pspec.name == 'y':
107 self.y = value
108
109 # make sure we update the display
110 self.changed(True)
111
112 elif pspec.name == 'width':
113 self.custom_width = value
114 self._size_changed()
115
116 # make sure we update the display
117 self.changed(True)
118
119 elif pspec.name == 'height':
120 self.custom_height = value
121 self._size_changed()
122
123 # make sure we update the display
124 self.changed(True)
125
126 else:
127 raise AttributeError('unknown property %s' % pspec.name)
128
129 def _size_changed(self):
130 """!
131 Size Changed function
132 @param self this object
133 @return exception if unknown property
134 """
135 if self.custom_width is None and self.custom_height is None:
136 self.width = self.handle.props.width
137 self.height = self.handle.props.height
138 self.sx = 1.0
139 self.sy = 1.0
140 elif self.custom_width is not None and self.custom_height is None:
141 self.width = self.custom_width
142 self.sx = self.custom_width / self.handle.props.width
143 self.sy = self.sx
144 self.height = self.handle.props.height*self.sy
145 elif self.custom_width is None and self.custom_height is not None:
146 self.height = self.custom_height
147 self.sy = self.custom_height / self.handle.props.height
148 self.sx = self.sy
149 self.width = self.handle.props.width*self.sx
150 else:
151 self.width = self.custom_width
152 self.height = self.custom_height
153 self.sx = self.custom_width / self.handle.props.width
154 self.sy = self.custom_height / self.handle.props.height
155
156 def do_get_property(self, pspec):
157 """!
158 Get Property
159 @param self this object
160 @param pspec property name
161 @return property value or exception if unknown property
162 """
163 if pspec.name == 'x':
164 return self.x
165
166 elif pspec.name == 'y':
167 return self.y
168
169 elif pspec.name == 'width':
170 self.width = self.handle.props.width
171 self.height = self.handle.props.height
172
173 return self.width
174
175 elif pspec.name == 'height':
176 return self.height
177
178 else:
179 raise AttributeError('unknown property %s' % pspec.name)
180
181 def do_simple_paint(self, cr, bounds):
182 """!
183 Simple Paint function
184 @param self this object
185 @param cr rendered
186 @param bounds bounds
187 @return none
188 """
189 cr.translate(self.x, self.y)
190 cr.scale(self.sx, self.sy)
191 self.handle.render_cairo(cr)
192
193 def do_simple_update(self, cr):
194 """!
195 Simple Update function
196 @param self this object
197 @param cr rendered
198 @return none
199 """
200 self.bounds_x1 = float(self.x)
201 self.bounds_y1 = float(self.y)
202 self.bounds_x2 = float(self.x + self.width)
203 self.bounds_y2 = float(self.y + self.height)
204
205 def do_simple_is_item_at(self, x, y, cr, is_pointer_event):
206 """!
207 Simple Is Item At function
208 @param self this object
209 @param x the X position
210 @param y the Y position
211 @param cr rendered
212 @param is_pointer_event is the event a pointer event
213 @return true if at or false if not
214 """
215 if ((x < self.x) or (x > self.x + self.width)) or ((y < self.y) or (y > self.y + self.height)):
216 return False
217 else:
218 return True
219
220
221_rsvg_cache = dict()
222
223def rsvg_handle_factory(base_file_name):
224 try:
225 return _rsvg_cache[base_file_name]
226 except KeyError:
227 full_path = os.path.join(os.path.dirname(__file__), 'resource', base_file_name)
228 rsvg_handle = rsvg.Handle(full_path)
229 _rsvg_cache[base_file_name] = rsvg_handle
230 return rsvg_handle
231
SvgItem class.
Definition: svgitem.py:8
def do_simple_update(self, cr)
Simple Update function.
Definition: svgitem.py:193
custom_height
custom height
Definition: svgitem.py:90
def do_simple_is_item_at(self, x, y, cr, is_pointer_event)
Simple Is Item At function.
Definition: svgitem.py:205
def _size_changed(self)
Size Changed function.
Definition: svgitem.py:129
def do_get_property(self, pspec)
Get Property.
Definition: svgitem.py:156
def do_simple_paint(self, cr, bounds)
Simple Paint function.
Definition: svgitem.py:181
def __init__(self, x, y, rsvg_handle, **kwargs)
Initializer.
Definition: svgitem.py:71
def do_set_property(self, pspec, value)
Set Property.
Definition: svgitem.py:92
custom_width
custom width
Definition: svgitem.py:89
def rsvg_handle_factory(base_file_name)
Definition: svgitem.py:223