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