A Discrete-Event Network Simulator
API
svgitem.py
Go to the documentation of this file.
1 from gi.repository import GObject, GooCanvas
2 import rsvg
3 #import cairo
4 import os.path
5 
6 
7 
8 class 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.xx = x
83  self.yy = y
84  self.sxsx = 1.0
85  self.sysy = 1.0
86  self.handlehandle = rsvg_handle
87  self.widthwidth = self.handlehandle.props.width
88  self.heightheight = self.handlehandle.props.height
89  self.custom_widthcustom_width = None
90  self.custom_heightcustom_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.xx = value
102 
103  # make sure we update the display
104  self.changed(True)
105 
106  elif pspec.name == 'y':
107  self.yy = value
108 
109  # make sure we update the display
110  self.changed(True)
111 
112  elif pspec.name == 'width':
113  self.custom_widthcustom_width = value
114  self._size_changed_size_changed()
115 
116  # make sure we update the display
117  self.changed(True)
118 
119  elif pspec.name == 'height':
120  self.custom_heightcustom_height = value
121  self._size_changed_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_widthcustom_width is None and self.custom_heightcustom_height is None:
136  self.widthwidth = self.handlehandle.props.width
137  self.heightheight = self.handlehandle.props.height
138  self.sxsx = 1.0
139  self.sysy = 1.0
140  elif self.custom_widthcustom_width is not None and self.custom_heightcustom_height is None:
141  self.widthwidth = self.custom_widthcustom_width
142  self.sxsx = self.custom_widthcustom_width / self.handlehandle.props.width
143  self.sysy = self.sxsx
144  self.heightheight = self.handlehandle.props.height*self.sysy
145  elif self.custom_widthcustom_width is None and self.custom_heightcustom_height is not None:
146  self.heightheight = self.custom_heightcustom_height
147  self.sysy = self.custom_heightcustom_height / self.handlehandle.props.height
148  self.sxsx = self.sysy
149  self.widthwidth = self.handlehandle.props.width*self.sxsx
150  else:
151  self.widthwidth = self.custom_widthcustom_width
152  self.heightheight = self.custom_heightcustom_height
153  self.sxsx = self.custom_widthcustom_width / self.handlehandle.props.width
154  self.sysy = self.custom_heightcustom_height / self.handlehandle.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.xx
165 
166  elif pspec.name == 'y':
167  return self.yy
168 
169  elif pspec.name == 'width':
170  self.widthwidth = self.handlehandle.props.width
171  self.heightheight = self.handlehandle.props.height
172 
173  return self.widthwidth
174 
175  elif pspec.name == 'height':
176  return self.heightheight
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.xx, self.yy)
190  cr.scale(self.sxsx, self.sysy)
191  self.handlehandle.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_x1bounds_x1 = float(self.xx)
201  self.bounds_y1bounds_y1 = float(self.yy)
202  self.bounds_x2bounds_x2 = float(self.xx + self.widthwidth)
203  self.bounds_y2bounds_y2 = float(self.yy + self.heightheight)
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.xx) or (x > self.xx + self.widthwidth)) or ((y < self.yy) or (y > self.yy + self.heightheight)):
216  return False
217  else:
218  return True
219 
220 
221 _rsvg_cache = dict()
222 
223 def 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