A Discrete-Event Network Simulator
API
svgitem.py
Go to the documentation of this file.
1 # import cairo
2 import os.path
3 
4 import rsvg
5 from gi.repository import GObject, GooCanvas
6 
7 
8 
9 class SvgItem(GooCanvas.ItemSimple):
10 
36 
37 
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.xx = x
89  self.yy = y
90  self.sxsx = 1.0
91  self.sysy = 1.0
92  self.handlehandle = rsvg_handle
93  self.widthwidth = self.handlehandle.props.width
94  self.heightheight = self.handlehandle.props.height
95  self.custom_widthcustom_width = None
96  self.custom_heightcustom_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.xx = value
108 
109  # make sure we update the display
110  self.changed(True)
111 
112  elif pspec.name == "y":
113  self.yy = value
114 
115  # make sure we update the display
116  self.changed(True)
117 
118  elif pspec.name == "width":
119  self.custom_widthcustom_width = value
120  self._size_changed_size_changed()
121 
122  # make sure we update the display
123  self.changed(True)
124 
125  elif pspec.name == "height":
126  self.custom_heightcustom_height = value
127  self._size_changed_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_widthcustom_width is None and self.custom_heightcustom_height is None:
142  self.widthwidth = self.handlehandle.props.width
143  self.heightheight = self.handlehandle.props.height
144  self.sxsx = 1.0
145  self.sysy = 1.0
146  elif self.custom_widthcustom_width is not None and self.custom_heightcustom_height is None:
147  self.widthwidth = self.custom_widthcustom_width
148  self.sxsx = self.custom_widthcustom_width / self.handlehandle.props.width
149  self.sysy = self.sxsx
150  self.heightheight = self.handlehandle.props.height * self.sysy
151  elif self.custom_widthcustom_width is None and self.custom_heightcustom_height is not None:
152  self.heightheight = self.custom_heightcustom_height
153  self.sysy = self.custom_heightcustom_height / self.handlehandle.props.height
154  self.sxsx = self.sysy
155  self.widthwidth = self.handlehandle.props.width * self.sxsx
156  else:
157  self.widthwidth = self.custom_widthcustom_width
158  self.heightheight = self.custom_heightcustom_height
159  self.sxsx = self.custom_widthcustom_width / self.handlehandle.props.width
160  self.sysy = self.custom_heightcustom_height / self.handlehandle.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.xx
171 
172  elif pspec.name == "y":
173  return self.yy
174 
175  elif pspec.name == "width":
176  self.widthwidth = self.handlehandle.props.width
177  self.heightheight = self.handlehandle.props.height
178 
179  return self.widthwidth
180 
181  elif pspec.name == "height":
182  return self.heightheight
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.xx, self.yy)
196  cr.scale(self.sxsx, self.sysy)
197  self.handlehandle.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_x1bounds_x1 = float(self.xx)
207  self.bounds_y1bounds_y1 = float(self.yy)
208  self.bounds_x2bounds_x2 = float(self.xx + self.widthwidth)
209  self.bounds_y2bounds_y2 = float(self.yy + self.heightheight)
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.xx) or (x > self.xx + self.widthwidth)) or (
222  (y < self.yy) or (y > self.yy + self.heightheight)
223  ):
224  return False
225  else:
226  return True
227 
228 
229 _rsvg_cache = dict()
230 
231 
232 def 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