2 # Copyright 2008 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.
24 from OpenGL.GL import *
25 from gnuradio.wxgui import common
31 BACKGROUND_COLOR_SPEC = (1, 0.976, 1, 1) #creamy white
32 GRID_LINE_COLOR_SPEC = (0, 0, 0) #black
33 TICK_TEXT_FONT_SIZE = 9
34 TITLE_TEXT_FONT_SIZE = 13
35 UNITS_TEXT_FONT_SIZE = 9
36 TICK_LABEL_PADDING = 5
37 POINT_LABEL_FONT_SIZE = 8
38 POINT_LABEL_COLOR_SPEC = (1, 1, .5)
39 POINT_LABEL_PADDING = 3
41 ##################################################
42 # OpenGL WX Plotter Canvas
43 ##################################################
44 class _plotter_base(wx.glcanvas.GLCanvas):
46 Plotter base class for all plot types.
49 def __init__(self, parent):
51 Create a new plotter base.
52 Initialize the GLCanvas with double buffering.
53 Initialize various plotter flags.
54 Bind the paint and size events.
55 @param parent the parent widgit
57 self._global_lock = threading.Lock()
58 attribList = (wx.glcanvas.WX_GL_DOUBLEBUFFER, wx.glcanvas.WX_GL_RGBA)
59 wx.glcanvas.GLCanvas.__init__(self, parent, attribList=attribList)
61 self._gl_init_flag = False
62 self._resized_flag = True
64 self.Bind(wx.EVT_PAINT, self._on_paint)
65 self.Bind(wx.EVT_SIZE, self._on_size)
67 def lock(self): self._global_lock.acquire()
68 def unlock(self): self._global_lock.release()
70 def _on_size(self, event):
72 Flag the resize event.
73 The paint event will handle the actual resizing.
75 self._resized_flag = True
77 def _on_paint(self, event):
79 Respond to paint events, call update.
80 Initialize GL if this is the first paint event.
83 #check if gl was initialized
84 if not self._gl_init_flag:
85 glClearColor(*BACKGROUND_COLOR_SPEC)
87 self._gl_init_flag = True
88 #check for a change in window size
89 if self._resized_flag:
91 self.width, self.height = self.GetSize()
92 glMatrixMode(GL_PROJECTION)
94 glOrtho(0, self.width, self.height, 0, 1, 0)
95 glMatrixMode(GL_MODELVIEW)
97 glViewport(0, 0, self.width, self.height)
98 self._resized_flag = False
106 Record the timestamp.
108 wx.PostEvent(self, wx.PaintEvent())
109 self._update_ts = time.time()
111 def clear(self): glClear(GL_COLOR_BUFFER_BIT)
113 def changed(self, state=None):
115 Set the changed flag if state is not None.
116 Otherwise return the changed flag.
118 if state is not None: self._changed = state
119 else: return self._changed
121 ##################################################
122 # Grid Plotter Base Class
123 ##################################################
124 class grid_plotter_base(_plotter_base):
126 def __init__(self, parent, padding):
127 _plotter_base.__init__(self, parent)
128 self.padding_top, self.padding_right, self.padding_bottom, self.padding_left = padding
129 #store title and unit strings
130 self.set_title('Title')
131 self.set_x_label('X Label')
132 self.set_y_label('Y Label')
133 #init the grid to some value
134 self.set_x_grid(-1, 1, 1)
135 self.set_y_grid(-1, 1, 1)
137 self.enable_point_label(False)
138 self._mouse_coordinate = None
139 self.Bind(wx.EVT_MOTION, self._on_motion)
140 self.Bind(wx.EVT_LEAVE_WINDOW, self._on_leave_window)
142 def _on_motion(self, event):
144 Mouse motion, record the position X, Y.
147 self._mouse_coordinate = event.GetPosition()
148 #update based on last known update time
149 if time.time() - self._update_ts > 0.03: self.update()
152 def _on_leave_window(self, event):
154 Mouse leave window, set the position to None.
157 self._mouse_coordinate = None
161 def enable_point_label(self, enable=None):
163 Enable/disable the point label.
164 @param enable true to enable
165 @return the enable state when None
167 if enable is None: return self._enable_point_label
169 self._enable_point_label = enable
173 def set_title(self, title):
176 @param title the title string
183 def set_x_label(self, x_label, x_units=''):
185 Set the x label and units.
186 @param x_label the x label string
187 @param x_units the x units string
190 self.x_label = x_label
191 self.x_units = x_units
195 def set_y_label(self, y_label, y_units=''):
197 Set the y label and units.
198 @param y_label the y label string
199 @param y_units the y units string
202 self.y_label = y_label
203 self.y_units = y_units
207 def set_x_grid(self, x_min, x_max, x_step, x_scalar=1.0):
209 Set the x grid parameters.
210 @param x_min the left-most value
211 @param x_max the right-most value
212 @param x_step the grid spacing
213 @param x_scalar the scalar factor
216 self.x_min = float(x_min)
217 self.x_max = float(x_max)
218 self.x_step = float(x_step)
219 self.x_scalar = float(x_scalar)
223 def set_y_grid(self, y_min, y_max, y_step, y_scalar=1.0):
225 Set the y grid parameters.
226 @param y_min the bottom-most value
227 @param y_max the top-most value
228 @param y_step the grid spacing
229 @param y_scalar the scalar factor
232 self.y_min = float(y_min)
233 self.y_max = float(y_max)
234 self.y_step = float(y_step)
235 self.y_scalar = float(y_scalar)
239 def _draw_grid(self):
241 Draw the border, grid, title, and units.
243 ##################################################
245 ##################################################
246 glColor3f(*GRID_LINE_COLOR_SPEC)
250 self.width - self.padding_right - self.padding_left,
251 self.height - self.padding_top - self.padding_bottom,
254 ##################################################
256 ##################################################
257 for tick in self._get_ticks(self.x_min, self.x_max, self.x_step, self.x_scalar):
258 scaled_tick = (self.width-self.padding_left-self.padding_right)*\
259 (tick/self.x_scalar-self.x_min)/(self.x_max-self.x_min) + self.padding_left
260 glColor3f(*GRID_LINE_COLOR_SPEC)
262 (scaled_tick, self.padding_top, 0),
263 (scaled_tick, self.height-self.padding_bottom, 0),
265 txt = self._get_tick_label(tick)
266 w, h = txt.get_size()
267 txt.draw_text(wx.Point(scaled_tick-w/2, self.height-self.padding_bottom+TICK_LABEL_PADDING))
268 ##################################################
270 ##################################################
271 for tick in self._get_ticks(self.y_min, self.y_max, self.y_step, self.y_scalar):
272 scaled_tick = (self.height-self.padding_top-self.padding_bottom)*\
273 (1 - (tick/self.y_scalar-self.y_min)/(self.y_max-self.y_min)) + self.padding_top
274 glColor3f(*GRID_LINE_COLOR_SPEC)
276 (self.padding_left, scaled_tick, 0),
277 (self.width-self.padding_right, scaled_tick, 0),
279 txt = self._get_tick_label(tick)
280 w, h = txt.get_size()
281 txt.draw_text(wx.Point(self.padding_left-w-TICK_LABEL_PADDING, scaled_tick-h/2))
282 ##################################################
284 ##################################################
286 txt = gltext.Text(self.title, bold=True, font_size=TITLE_TEXT_FONT_SIZE, centered=True)
287 txt.draw_text(wx.Point(self.width/2.0, .5*self.padding_top))
288 ##################################################
290 ##################################################
292 x_label_str = self.x_units and "%s (%s)"%(self.x_label, self.x_units) or self.x_label
293 txt = gltext.Text(x_label_str, bold=True, font_size=UNITS_TEXT_FONT_SIZE, centered=True)
294 txt.draw_text(wx.Point(
295 (self.width-self.padding_left-self.padding_right)/2.0 + self.padding_left,
296 self.height-.25*self.padding_bottom,
300 y_label_str = self.y_units and "%s (%s)"%(self.y_label, self.y_units) or self.y_label
301 txt = gltext.Text(y_label_str, bold=True, font_size=UNITS_TEXT_FONT_SIZE, centered=True)
302 txt.draw_text(wx.Point(
303 .25*self.padding_left,
304 (self.height-self.padding_top-self.padding_bottom)/2.0 + self.padding_top,
308 def _get_tick_label(self, tick):
310 Format the tick value and create a gl text.
311 @param tick the floating point tick value
312 @return the tick label text
314 tick_str = common.label_format(tick)
315 return gltext.Text(tick_str, font_size=TICK_TEXT_FONT_SIZE)
317 def _get_ticks(self, min, max, step, scalar):
319 Determine the positions for the ticks.
320 @param min the lower bound
321 @param max the upper bound
322 @param step the grid spacing
323 @param scalar the grid scaling
324 @return a list of tick positions between min and max
330 #check for valid numbers
333 assert max - min > step
334 #determine the start and stop value
335 start = int(math.ceil(min/step))
336 stop = int(math.floor(max/step))
337 return [i*step*scalar for i in range(start, stop+1)]
339 def _draw_line(self, coor1, coor2):
341 Draw a line from coor1 to coor2.
342 @param corr1 a tuple of x, y, z
343 @param corr2 a tuple of x, y, z
350 def _draw_rect(self, x, y, width, height, fill=True):
352 Draw a rectangle on the x, y plane.
353 X and Y are the top-left corner.
354 @param x the left position of the rectangle
355 @param y the top position of the rectangle
356 @param width the width of the rectangle
357 @param height the height of the rectangle
358 @param fill true to color inside of rectangle
360 glBegin(fill and GL_QUADS or GL_LINE_LOOP)
362 glVertex2f(x+width, y)
363 glVertex2f(x+width, y+height)
364 glVertex2f(x, y+height)
367 def _draw_point_label(self):
369 Draw the point label for the last mouse motion coordinate.
370 The mouse coordinate must be an X, Y tuple.
371 The label will be drawn at the X, Y coordinate.
372 The values of the X, Y coordinate will be scaled to the current X, Y bounds.
374 if not self.enable_point_label(): return
375 if not self._mouse_coordinate: return
376 x, y = self._mouse_coordinate
377 if x < self.padding_left or x > self.width-self.padding_right: return
378 if y < self.padding_top or y > self.height-self.padding_bottom: return
379 #scale to window bounds
380 x_win_scalar = float(x - self.padding_left)/(self.width-self.padding_left-self.padding_right)
381 y_win_scalar = float((self.height - y) - self.padding_bottom)/(self.height-self.padding_top-self.padding_bottom)
382 #scale to grid bounds
383 x_val = self.x_scalar*(x_win_scalar*(self.x_max-self.x_min) + self.x_min)
384 y_val = self.y_scalar*(y_win_scalar*(self.y_max-self.y_min) + self.y_min)
386 label_str = self._populate_point_label(x_val, y_val)
387 txt = gltext.Text(label_str, font_size=POINT_LABEL_FONT_SIZE)
388 w, h = txt.get_size()
390 glColor3f(*POINT_LABEL_COLOR_SPEC)
391 if x > self.width/2: x -= w+2*POINT_LABEL_PADDING
392 self._draw_rect(x, y-h-2*POINT_LABEL_PADDING, w+2*POINT_LABEL_PADDING, h+2*POINT_LABEL_PADDING)
393 txt.draw_text(wx.Point(x+POINT_LABEL_PADDING, y-h-POINT_LABEL_PADDING))