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