A Discrete-Event Network Simulator
API
hud.py
Go to the documentation of this file.
1 import math
2 from .base import PIXELS_PER_METER
3 from gi.repository import Pango
4 from gi.repository import Gtk
5 from gi.repository import GooCanvas
6 
7 
8 
9 class Axes(object):
10 
22  def __init__(self, viz):
23  """!
24  Initializer function
25 
26  @param self: this object
27  @param viz: visualization object
28  """
29  self.vizviz = viz
30  self.colorcolor = 0x8080C0FF
31  self.hlineshlines = GooCanvas.CanvasPath(parent=viz.canvas.get_root_item(), stroke_color_rgba=self.colorcolor)
32  self.hlineshlines.lower(None)
33  self.vlinesvlines = GooCanvas.CanvasPath(parent=viz.canvas.get_root_item(), stroke_color_rgba=self.colorcolor)
34  self.vlinesvlines.lower(None)
35  self.labelslabels = []
36  hadj = self.vizviz.get_hadjustment()
37  vadj = self.vizviz.get_vadjustment()
38  def update(adj):
39  if self.visiblevisible:
40  self.update_viewupdate_view()
41  hadj.connect("value-changed", update)
42  vadj.connect("value-changed", update)
43  hadj.connect("changed", update)
44  vadj.connect("changed", update)
45  self.visiblevisible = True
46  self.update_viewupdate_view()
47 
48  def set_visible(self, visible):
49  """!
50  Set visible function
51 
52  @param self: this object
53  @param visible: visible indicator
54  @return none
55  """
56  self.visiblevisible = visible
57  if self.visiblevisible:
58  self.hlineshlines.props.visibility = GooCanvas.CanvasItemVisibility.VISIBLE
59  self.vlinesvlines.props.visibility = GooCanvas.CanvasItemVisibility.VISIBLE
60  else:
61  self.hlineshlines.props.visibility = GooCanvas.CanvasItemVisibility.HIDDEN
62  self.vlinesvlines.props.visibility = GooCanvas.CanvasItemVisibility.HIDDEN
63  for label in self.labelslabels:
64  label.props.visibility = GooCanvas.CanvasItemVisibility.HIDDEN
65 
66  def _compute_divisions(self, xi, xf):
67  """!
68  Compute divisions function
69 
70  @param self: this object
71  @param xi: xi
72  @param xf: xf
73  @return x0 and div
74  """
75  assert xf > xi
76  dx = xf - xi
77  size = dx
78  ndiv = 5
79  text_width = dx/ndiv/2
80 
81  def rint(x):
82  """!
83  Compute divisions function
84 
85  @param x: x
86  @return x rounded up
87  """
88  return math.floor(x+0.5)
89 
90  dx_over_ndiv = dx / ndiv
91  for n in range(5): # iterate 5 times to find optimum division size
92  #/* div: length of each division */
93  tbe = math.log10(dx_over_ndiv)#; /* looking for approx. 'ndiv' divisions in a length 'dx' */
94  div = pow(10, rint(tbe))#; /* div: power of 10 closest to dx/ndiv */
95  if math.fabs(div/2 - dx_over_ndiv) < math.fabs(div - dx_over_ndiv): #/* test if div/2 is closer to dx/ndiv */
96  div /= 2
97  elif math.fabs(div*2 - dx_over_ndiv) < math.fabs(div - dx_over_ndiv):
98  div *= 2 # /* test if div*2 is closer to dx/ndiv */
99  x0 = div*math.ceil(xi / div) - div
100  if n > 1:
101  ndiv = rint(size / text_width)
102  return x0, div
103 
104 
105  def update_view(self):
106  """!
107  Update view function
108 
109  @param self: this object
110  @return none
111  """
112  if self.vizviz.zoom is None:
113  return
114 
115  unused_labels = self.labelslabels
116  self.labelslabels = []
117  for label in unused_labels:
118  label.set_property("visibility", GooCanvas.CanvasItemVisibility.HIDDEN)
119  def get_label():
120  """!
121  Get label function
122 
123  @param self: this object
124  @return label
125  """
126  try:
127  label = unused_labels.pop(0)
128  except IndexError:
129  label = GooCanvas.CanvasText(parent=self.vizviz.canvas.get_root_item(), stroke_color_rgba=self.colorcolor)
130  else:
131  label.set_property("visibility", GooCanvas.CanvasItemVisibility.VISIBLE)
132  label.lower(None)
133  self.labelslabels.append(label)
134  return label
135 
136  hadj = self.vizviz.get_hadjustment()
137  vadj = self.vizviz.get_vadjustment()
138  zoom = self.vizviz.zoom.get_value()
139  offset = 10/zoom
140 
141  x1, y1 = self.vizviz.canvas.convert_from_pixels(hadj.get_value(), vadj.get_value())
142  x2, y2 = self.vizviz.canvas.convert_from_pixels(hadj.get_value() + hadj.get_page_size(), vadj.get_value() + vadj.get_page_size())
143  line_width = 5.0/self.vizviz.zoom.get_value()
144 
145  # draw the horizontal axis
146  self.hlineshlines.set_property("line-width", line_width)
147  yc = y2 - line_width/2
148 
149  sim_x1 = x1/PIXELS_PER_METER
150  sim_x2 = x2/PIXELS_PER_METER
151  x0, xdiv = self._compute_divisions_compute_divisions(sim_x1, sim_x2)
152  path = ["M %r %r L %r %r" % (x1, yc, x2, yc)]
153  x = x0
154  while x < sim_x2:
155  path.append("M %r %r L %r %r" % (PIXELS_PER_METER*x, yc - offset, PIXELS_PER_METER*x, yc))
156  label = get_label()
157  label.set_properties(font=("Sans Serif %f" % int(12/zoom)),
158  text=("%G" % x),
159  fill_color_rgba=self.colorcolor,
160  alignment=Pango.Alignment.CENTER,
161  # anchor=Gtk.Widget.ANCHOR_S,
162  x=PIXELS_PER_METER*x,
163  y=(yc - offset))
164  x += xdiv
165  del x
166 
167  self.hlineshlines.set_property("data", " ".join(path))
168 
169  # draw the vertical axis
170  self.vlinesvlines.set_property("line-width", line_width)
171  xc = x1 + line_width/2
172 
173  sim_y1 = y1/PIXELS_PER_METER
174  sim_y2 = y2/PIXELS_PER_METER
175 
176 
177  y0, ydiv = self._compute_divisions_compute_divisions(sim_y1, sim_y2)
178  path = ["M %r %r L %r %r" % (xc, y1, xc, y2)]
179  y = y0
180  while y < sim_y2:
181  path.append("M %r %r L %r %r" % (xc, PIXELS_PER_METER*y, xc + offset, PIXELS_PER_METER*y))
182  label = get_label()
183  label.set_properties(font=("Sans Serif %f" % int(12/zoom)),
184  text=("%G" % y),
185  fill_color_rgba=self.colorcolor,
186  alignment=Pango.Alignment.LEFT,
187  # anchor=Gtk.ANCHOR_W,
188  x=xc + offset,
189  y=PIXELS_PER_METER*y)
190  y += ydiv
191 
192  self.vlinesvlines.set_property("data", " ".join(path))
193 
194 
195 
196  self.labelslabels.extend(unused_labels)
Axes class.
Definition: hud.py:9
def _compute_divisions(self, xi, xf)
Compute divisions function.
Definition: hud.py:66
hlines
horizontal lines
Definition: hud.py:31
viz
visualizer
Definition: hud.py:29
vlines
vertical lines
Definition: hud.py:33
def __init__(self, viz)
Initializer function.
Definition: hud.py:22
labels
list of labels
Definition: hud.py:35
def update_view(self)
Update view function.
Definition: hud.py:105
def set_visible(self, visible)
Set visible function.
Definition: hud.py:48
visible
visible
Definition: hud.py:45