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 
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  """
76  super(SvgItem, self).__init__(**kwargs)
77  assert isinstance(rsvg_handle, rsvg.Handle)
78  self.x = x
79  self.y = y
80  self.sx = 1.0
81  self.sy = 1.0
82  self.handle = rsvg_handle
83  self.width = self.handle.props.width
84  self.height = self.handle.props.height
85  self.custom_width = None
86  self.custom_height = None
87 
88  def do_set_property(self, pspec, value):
89  """!
90  Set Property
91  @param self this object
92  @param pspec property name
93  @param value property value
94  @return exception if unknown property
95  """
96  if pspec.name == 'x':
97  self.x = value
98 
99  # make sure we update the display
100  self.changed(True)
101 
102  elif pspec.name == 'y':
103  self.y = value
104 
105  # make sure we update the display
106  self.changed(True)
107 
108  elif pspec.name == 'width':
109  self.custom_width = value
110  self._size_changed()
111 
112  # make sure we update the display
113  self.changed(True)
114 
115  elif pspec.name == 'height':
116  self.custom_height = value
117  self._size_changed()
118 
119  # make sure we update the display
120  self.changed(True)
121 
122  else:
123  raise AttributeError('unknown property %s' % pspec.name)
124 
125  def _size_changed(self):
126  """!
127  Size Changed function
128  @param self this object
129  @return exception if unknown property
130  """
131  if self.custom_width is None and self.custom_height is None:
132  self.width = self.handle.props.width
133  self.height = self.handle.props.height
134  self.sx = 1.0
135  self.sy = 1.0
136  elif self.custom_width is not None and self.custom_height is None:
137  self.width = self.custom_width
138  self.sx = self.custom_width / self.handle.props.width
139  self.sy = self.sx
140  self.height = self.handle.props.height*self.sy
141  elif self.custom_width is None and self.custom_height is not None:
142  self.height = self.custom_height
143  self.sy = self.custom_height / self.handle.props.height
144  self.sx = self.sy
145  self.width = self.handle.props.width*self.sx
146  else:
147  self.width = self.custom_width
148  self.height = self.custom_height
149  self.sx = self.custom_width / self.handle.props.width
150  self.sy = self.custom_height / self.handle.props.height
151 
152  def do_get_property(self, pspec):
153  """!
154  Get Property
155  @param self this object
156  @param pspec property name
157  @return property value or exception if unknown property
158  """
159  if pspec.name == 'x':
160  return self.x
161 
162  elif pspec.name == 'y':
163  return self.y
164 
165  elif pspec.name == 'width':
166  self.width = self.handle.props.width
167  self.height = self.handle.props.height
168 
169  return self.width
170 
171  elif pspec.name == 'height':
172  return self.height
173 
174  else:
175  raise AttributeError('unknown property %s' % pspec.name)
176 
177  def do_simple_paint(self, cr, bounds):
178  """!
179  Simple Paint function
180  @param self this object
181  @param cr rendered
182  @param bounds bounds
183  @return none
184  """
185  cr.translate(self.x, self.y)
186  cr.scale(self.sx, self.sy)
187  self.handle.render_cairo(cr)
188 
189  def do_simple_update(self, cr):
190  """!
191  Simple Update function
192  @param self this object
193  @param cr rendered
194  @return none
195  """
196  self.bounds_x1 = float(self.x)
197  self.bounds_y1 = float(self.y)
198  self.bounds_x2 = float(self.x + self.width)
199  self.bounds_y2 = float(self.y + self.height)
200 
201  def do_simple_is_item_at(self, x, y, cr, is_pointer_event):
202  """!
203  Simple Is Item At function
204  @param self this object
205  @param x the X position
206  @param y the Y position
207  @param cr rendered
208  @param is_pointer_event is the event a pointer event
209  @return true if at or false if not
210  """
211  if ((x < self.x) or (x > self.x + self.width)) or ((y < self.y) or (y > self.y + self.height)):
212  return False
213  else:
214  return True
215 
216 
217 _rsvg_cache = dict()
218 
219 def rsvg_handle_factory(base_file_name):
220  try:
221  return _rsvg_cache[base_file_name]
222  except KeyError:
223  full_path = os.path.join(os.path.dirname(__file__), 'resource', base_file_name)
224  rsvg_handle = rsvg.Handle(full_path)
225  _rsvg_cache[base_file_name] = rsvg_handle
226  return rsvg_handle
227 
def rsvg_handle_factory(base_file_name)
Definition: svgitem.py:219
def do_get_property(self, pspec)
Get Property.
Definition: svgitem.py:152
custom_width
custom width
Definition: svgitem.py:85
def do_simple_update(self, cr)
Simple Update function.
Definition: svgitem.py:189
def do_simple_paint(self, cr, bounds)
Simple Paint function.
Definition: svgitem.py:177
def do_simple_is_item_at(self, x, y, cr, is_pointer_event)
Simple Is Item At function.
Definition: svgitem.py:201
custom_height
custom height
Definition: svgitem.py:86
def do_set_property(self, pspec, value)
Set Property.
Definition: svgitem.py:88
def __init__(self, x, y, rsvg_handle, kwargs)
Definition: svgitem.py:71
def _size_changed(self)
Size Changed function.
Definition: svgitem.py:125
SvgItem class.
Definition: svgitem.py:8