2 # Copyright 2009 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING. If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
26 from plotter_base import plotter_base
30 GRID_LINE_COLOR_SPEC = (.7, .7, .7) #gray
31 GRID_BORDER_COLOR_SPEC = (0, 0, 0) #black
32 TICK_TEXT_FONT_SIZE = 9
33 TITLE_TEXT_FONT_SIZE = 13
34 UNITS_TEXT_FONT_SIZE = 9
35 AXIS_LABEL_PADDING = 5
36 TICK_LABEL_PADDING = 5
37 TITLE_LABEL_PADDING = 7
38 POINT_LABEL_FONT_SIZE = 8
39 POINT_LABEL_COLOR_SPEC = (1, 1, 0.5, 0.75)
40 POINT_LABEL_PADDING = 3
41 POINT_LABEL_OFFSET = 10
42 GRID_LINE_DASH_LEN = 4
44 ##################################################
45 # Grid Plotter Base Class
46 ##################################################
47 class grid_plotter_base(plotter_base):
49 def __init__(self, parent, min_padding=(0, 0, 0, 0)):
50 plotter_base.__init__(self, parent)
52 self._grid_cache = self.new_gl_cache(self._draw_grid, 25)
53 self.enable_grid_lines(True)
55 self.padding_top_min, self.padding_right_min, self.padding_bottom_min, self.padding_left_min = min_padding
56 #store title and unit strings
57 self.set_title('Title')
58 self.set_x_label('X Label')
59 self.set_y_label('Y Label')
60 #init the grid to some value
61 self.set_x_grid(-1, 1, 1)
62 self.set_y_grid(-1, 1, 1)
63 #setup point label cache
64 self._point_label_cache = self.new_gl_cache(self._draw_point_label, 75)
65 self.enable_point_label(False)
66 self.enable_grid_aspect_ratio(False)
67 self.set_point_label_coordinate(None)
68 common.point_label_thread(self)
70 self.register_init(self._init_grid_plotter)
72 def _init_grid_plotter(self):
74 Run gl initialization tasks.
76 GL.glEnableClientState(GL.GL_VERTEX_ARRAY)
78 def set_point_label_coordinate(self, coor):
80 Set the point label coordinate.
81 @param coor the coordinate x, y tuple or None
84 self._point_label_coordinate = coor
85 self._point_label_cache.changed(True)
89 def enable_grid_aspect_ratio(self, enable=None):
91 Enable/disable the grid aspect ratio.
92 If enabled, enforce the aspect ratio on the padding:
93 horizontal_padding:vertical_padding == width:height
94 @param enable true to enable
95 @return the enable state when None
97 if enable is None: return self._enable_grid_aspect_ratio
99 self._enable_grid_aspect_ratio = enable
100 for cache in self._gl_caches: cache.changed(True)
103 def enable_point_label(self, enable=None):
105 Enable/disable the point label.
106 @param enable true to enable
107 @return the enable state when None
109 if enable is None: return self._enable_point_label
111 self._enable_point_label = enable
112 self._point_label_cache.changed(True)
115 def set_title(self, title):
118 @param title the title string
122 self._grid_cache.changed(True)
125 def set_x_label(self, x_label, x_units=''):
127 Set the x label and units.
128 @param x_label the x label string
129 @param x_units the x units string
132 self.x_label = x_label
133 self.x_units = x_units
134 self._grid_cache.changed(True)
137 def set_y_label(self, y_label, y_units=''):
139 Set the y label and units.
140 @param y_label the y label string
141 @param y_units the y units string
144 self.y_label = y_label
145 self.y_units = y_units
146 self._grid_cache.changed(True)
149 def set_x_grid(self, minimum, maximum, step, scale=False):
151 Set the x grid parameters.
152 @param minimum the left-most value
153 @param maximum the right-most value
154 @param step the grid spacing
155 @param scale true to scale the x grid
158 self.x_min = float(minimum)
159 self.x_max = float(maximum)
160 self.x_step = float(step)
162 coeff, exp, prefix = common.get_si_components(max(abs(self.x_min), abs(self.x_max)))
163 self.x_scalar = 10**(-exp)
164 self.x_prefix = prefix
168 for cache in self._gl_caches: cache.changed(True)
171 def set_y_grid(self, minimum, maximum, step, scale=False):
173 Set the y grid parameters.
174 @param minimum the bottom-most value
175 @param maximum the top-most value
176 @param step the grid spacing
177 @param scale true to scale the y grid
180 self.y_min = float(minimum)
181 self.y_max = float(maximum)
182 self.y_step = float(step)
184 coeff, exp, prefix = common.get_si_components(max(abs(self.y_min), abs(self.y_max)))
185 self.y_scalar = 10**(-exp)
186 self.y_prefix = prefix
190 for cache in self._gl_caches: cache.changed(True)
193 def _draw_grid(self):
195 Create the x, y, tick, and title labels.
196 Resize the padding for the labels.
197 Draw the border, grid, title, and labels.
199 ##################################################
200 # Create GL text labels
201 ##################################################
202 #create x tick labels
203 x_tick_labels = [(tick, self._get_tick_label(tick, self.x_units))
204 for tick in self._get_ticks(self.x_min, self.x_max, self.x_step, self.x_scalar)]
205 #create x tick labels
206 y_tick_labels = [(tick, self._get_tick_label(tick, self.y_units))
207 for tick in self._get_ticks(self.y_min, self.y_max, self.y_step, self.y_scalar)]
209 x_label_str = self.x_units and "%s (%s%s)"%(self.x_label, self.x_prefix, self.x_units) or self.x_label
210 x_label = gltext.Text(x_label_str, bold=True, font_size=UNITS_TEXT_FONT_SIZE, centered=True)
212 y_label_str = self.y_units and "%s (%s%s)"%(self.y_label, self.y_prefix, self.y_units) or self.y_label
213 y_label = gltext.Text(y_label_str, bold=True, font_size=UNITS_TEXT_FONT_SIZE, centered=True)
215 title_label = gltext.Text(self.title, bold=True, font_size=TITLE_TEXT_FONT_SIZE, centered=True)
216 ##################################################
218 ##################################################
219 self.padding_top = max(2*TITLE_LABEL_PADDING + title_label.get_size()[1], self.padding_top_min)
220 self.padding_right = max(2*TICK_LABEL_PADDING, self.padding_right_min)
221 self.padding_bottom = max(2*AXIS_LABEL_PADDING + TICK_LABEL_PADDING + x_label.get_size()[1] + max([label.get_size()[1] for tick, label in x_tick_labels]), self.padding_bottom_min)
222 self.padding_left = max(2*AXIS_LABEL_PADDING + TICK_LABEL_PADDING + y_label.get_size()[1] + max([label.get_size()[0] for tick, label in y_tick_labels]), self.padding_left_min)
223 #enforce padding aspect ratio if enabled
224 if self.enable_grid_aspect_ratio():
225 w_over_h_ratio = float(self.width)/float(self.height)
226 horizontal_padding = float(self.padding_right + self.padding_left)
227 veritical_padding = float(self.padding_top + self.padding_bottom)
228 if w_over_h_ratio > horizontal_padding/veritical_padding:
229 #increase the horizontal padding
230 new_padding = veritical_padding*w_over_h_ratio - horizontal_padding
231 #distribute the padding to left and right
232 self.padding_left += int(round(new_padding/2))
233 self.padding_right += int(round(new_padding/2))
235 #increase the vertical padding
236 new_padding = horizontal_padding/w_over_h_ratio - veritical_padding
237 #distribute the padding to top and bottom
238 self.padding_top += int(round(new_padding/2))
239 self.padding_bottom += int(round(new_padding/2))
240 ##################################################
242 ##################################################
243 for tick, label in x_tick_labels:
244 scaled_tick = (self.width-self.padding_left-self.padding_right)*\
245 (tick/self.x_scalar-self.x_min)/(self.x_max-self.x_min) + self.padding_left
246 self._draw_grid_line(
247 (scaled_tick, self.padding_top),
248 (scaled_tick, self.height-self.padding_bottom),
250 w, h = label.get_size()
251 label.draw_text(wx.Point(scaled_tick-w/2, self.height-self.padding_bottom+TICK_LABEL_PADDING))
252 ##################################################
254 ##################################################
255 for tick, label in y_tick_labels:
256 scaled_tick = (self.height-self.padding_top-self.padding_bottom)*\
257 (1 - (tick/self.y_scalar-self.y_min)/(self.y_max-self.y_min)) + self.padding_top
258 self._draw_grid_line(
259 (self.padding_left, scaled_tick),
260 (self.width-self.padding_right, scaled_tick),
262 w, h = label.get_size()
263 label.draw_text(wx.Point(self.padding_left-w-TICK_LABEL_PADDING, scaled_tick-h/2))
264 ##################################################
266 ##################################################
267 GL.glColor3f(*GRID_BORDER_COLOR_SPEC)
271 self.width - self.padding_right - self.padding_left,
272 self.height - self.padding_top - self.padding_bottom,
275 ##################################################
277 ##################################################
279 title_label.draw_text(wx.Point(self.width/2.0, TITLE_LABEL_PADDING + title_label.get_size()[1]/2))
281 x_label.draw_text(wx.Point(
282 (self.width-self.padding_left-self.padding_right)/2.0 + self.padding_left,
283 self.height-(AXIS_LABEL_PADDING + x_label.get_size()[1]/2),
287 y_label.draw_text(wx.Point(
288 AXIS_LABEL_PADDING + y_label.get_size()[1]/2,
289 (self.height-self.padding_top-self.padding_bottom)/2.0 + self.padding_top,
293 def _get_tick_label(self, tick, unit):
295 Format the tick value and create a gl text.
296 @param tick the floating point tick value
297 @param unit the axis unit
298 @return the tick label text
300 if unit: tick_str = common.sci_format(tick)
301 else: tick_str = common.eng_format(tick)
302 return gltext.Text(tick_str, font_size=TICK_TEXT_FONT_SIZE)
304 def _get_ticks(self, min, max, step, scalar):
306 Determine the positions for the ticks.
307 @param min the lower bound
308 @param max the upper bound
309 @param step the grid spacing
310 @param scalar the grid scaling
311 @return a list of tick positions between min and max
317 #check for valid numbers
321 assert max - min > step
322 except AssertionError: return [-1, 1]
323 #determine the start and stop value
324 start = int(math.ceil(min/step))
325 stop = int(math.floor(max/step))
326 return [i*step*scalar for i in range(start, stop+1)]
328 def enable_grid_lines(self, enable=None):
330 Enable/disable the grid lines.
331 @param enable true to enable
332 @return the enable state when None
334 if enable is None: return self._enable_grid_lines
336 self._enable_grid_lines = enable
337 self._grid_cache.changed(True)
340 def _draw_grid_line(self, coor1, coor2):
342 Draw a dashed line from coor1 to coor2.
343 @param corr1 a tuple of x, y
344 @param corr2 a tuple of x, y
346 if not self.enable_grid_lines(): return
347 length = math.sqrt((coor1[0] - coor2[0])**2 + (coor1[1] - coor2[1])**2)
348 num_points = int(length/GRID_LINE_DASH_LEN)
349 #calculate points array
351 coor1[0] + i*(coor2[0]-coor1[0])/(num_points - 1),
352 coor1[1] + i*(coor2[1]-coor1[1])/(num_points - 1)
353 ) for i in range(num_points)]
355 GL.glColor3f(*GRID_LINE_COLOR_SPEC)
356 GL.glVertexPointerf(points)
357 GL.glDrawArrays(GL.GL_LINES, 0, len(points))
359 def _draw_rect(self, x, y, width, height, fill=True):
361 Draw a rectangle on the x, y plane.
362 X and Y are the top-left corner.
363 @param x the left position of the rectangle
364 @param y the top position of the rectangle
365 @param width the width of the rectangle
366 @param height the height of the rectangle
367 @param fill true to color inside of rectangle
369 GL.glBegin(fill and GL.GL_QUADS or GL.GL_LINE_LOOP)
371 GL.glVertex2f(x+width, y)
372 GL.glVertex2f(x+width, y+height)
373 GL.glVertex2f(x, y+height)
376 def _draw_point_label(self):
378 Draw the point label for the last mouse motion coordinate.
379 The mouse coordinate must be an X, Y tuple.
380 The label will be drawn at the X, Y coordinate.
381 The values of the X, Y coordinate will be scaled to the current X, Y bounds.
383 if not self.enable_point_label(): return
384 if not self._point_label_coordinate: return
385 x, y = self._point_label_coordinate
386 if x < self.padding_left or x > self.width-self.padding_right: return
387 if y < self.padding_top or y > self.height-self.padding_bottom: return
388 #scale to window bounds
389 x_win_scalar = float(x - self.padding_left)/(self.width-self.padding_left-self.padding_right)
390 y_win_scalar = float((self.height - y) - self.padding_bottom)/(self.height-self.padding_top-self.padding_bottom)
391 #scale to grid bounds
392 x_val = x_win_scalar*(self.x_max-self.x_min) + self.x_min
393 y_val = y_win_scalar*(self.y_max-self.y_min) + self.y_min
395 label_str = self._populate_point_label(x_val, y_val)
396 if not label_str: return
397 txt = gltext.Text(label_str, font_size=POINT_LABEL_FONT_SIZE)
398 w, h = txt.get_size()
400 GL.glEnable(GL.GL_BLEND)
401 GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA)
403 GL.glColor4f(*POINT_LABEL_COLOR_SPEC)
404 if x > self.width/2: x -= w+2*POINT_LABEL_PADDING + POINT_LABEL_OFFSET
405 else: x += POINT_LABEL_OFFSET
406 self._draw_rect(x, y-h-2*POINT_LABEL_PADDING, w+2*POINT_LABEL_PADDING, h+2*POINT_LABEL_PADDING)
407 txt.draw_text(wx.Point(x+POINT_LABEL_PADDING, y-h-POINT_LABEL_PADDING))