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