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._semaphore = threading.Semaphore(1)
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._semaphore.acquire(True)
68 def unlock(self): self._semaphore.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)
247 glBegin(GL_LINE_LOOP)
248 glVertex3f(self.padding_left, self.padding_top, 0)
249 glVertex3f(self.width - self.padding_right, self.padding_top, 0)
250 glVertex3f(self.width - self.padding_right, self.height - self.padding_bottom, 0)
251 glVertex3f(self.padding_left, self.height - self.padding_bottom, 0)
253 ##################################################
255 ##################################################
256 for tick in self._get_ticks(self.x_min, self.x_max, self.x_step, self.x_scalar):
257 scaled_tick = (self.width-self.padding_left-self.padding_right)*\
258 (tick/self.x_scalar-self.x_min)/(self.x_max-self.x_min) + self.padding_left
259 glColor3f(*GRID_LINE_COLOR_SPEC)
261 (scaled_tick, self.padding_top, 0),
262 (scaled_tick, self.height-self.padding_bottom, 0),
264 txt = self._get_tick_label(tick)
265 w, h = txt.get_size()
266 txt.draw_text(wx.Point(scaled_tick-w/2, self.height-self.padding_bottom+TICK_LABEL_PADDING))
267 ##################################################
269 ##################################################
270 for tick in self._get_ticks(self.y_min, self.y_max, self.y_step, self.y_scalar):
271 scaled_tick = (self.height-self.padding_top-self.padding_bottom)*\
272 (1 - (tick/self.y_scalar-self.y_min)/(self.y_max-self.y_min)) + self.padding_top
273 glColor3f(*GRID_LINE_COLOR_SPEC)
275 (self.padding_left, scaled_tick, 0),
276 (self.width-self.padding_right, scaled_tick, 0),
278 txt = self._get_tick_label(tick)
279 w, h = txt.get_size()
280 txt.draw_text(wx.Point(self.padding_left-w-TICK_LABEL_PADDING, scaled_tick-h/2))
281 ##################################################
283 ##################################################
285 txt = gltext.Text(self.title, bold=True, font_size=TITLE_TEXT_FONT_SIZE, centered=True)
286 txt.draw_text(wx.Point(self.width/2.0, .5*self.padding_top))
287 ##################################################
289 ##################################################
291 x_label_str = self.x_units and "%s (%s)"%(self.x_label, self.x_units) or self.x_label
292 txt = gltext.Text(x_label_str, bold=True, font_size=UNITS_TEXT_FONT_SIZE, centered=True)
293 txt.draw_text(wx.Point(
294 (self.width-self.padding_left-self.padding_right)/2.0 + self.padding_left,
295 self.height-.25*self.padding_bottom,
299 y_label_str = self.y_units and "%s (%s)"%(self.y_label, self.y_units) or self.y_label
300 txt = gltext.Text(y_label_str, bold=True, font_size=UNITS_TEXT_FONT_SIZE, centered=True)
301 txt.draw_text(wx.Point(
302 .25*self.padding_left,
303 (self.height-self.padding_top-self.padding_bottom)/2.0 + self.padding_top,
307 def _get_tick_label(self, tick):
309 Format the tick value and create a gl text.
310 @param tick the floating point tick value
311 @return the tick label text
313 tick_str = common.label_format(tick)
314 return gltext.Text(tick_str, font_size=TICK_TEXT_FONT_SIZE)
316 def _get_ticks(self, min, max, step, scalar):
318 Determine the positions for the ticks.
319 @param min the lower bound
320 @param max the upper bound
321 @param step the grid spacing
322 @param scalar the grid scaling
323 @return a list of tick positions between min and max
329 #check for valid numbers
332 assert max - min > step
333 #determine the start and stop value
334 start = int(math.ceil(min/step))
335 stop = int(math.floor(max/step))
336 return [i*step*scalar for i in range(start, stop+1)]
338 def _draw_line(self, coor1, coor2):
340 Draw a line from coor1 to coor2.
341 @param corr1 a tuple of x, y, z
342 @param corr2 a tuple of x, y, z
349 def _draw_rect(self, x, y, width, height, fill=True):
351 Draw a rectangle on the x, y plane.
352 X and Y are the top-left corner.
353 @param x the left position of the rectangle
354 @param y the top position of the rectangle
355 @param width the width of the rectangle
356 @param height the height of the rectangle
357 @param fill true to color inside of rectangle
359 glBegin(fill and GL_QUADS or GL_LINE_LOOP)
361 glVertex2f(x+width, y)
362 glVertex2f(x+width, y+height)
363 glVertex2f(x, y+height)
366 def _draw_point_label(self):
368 Draw the point label for the last mouse motion coordinate.
369 The mouse coordinate must be an X, Y tuple.
370 The label will be drawn at the X, Y coordinate.
371 The values of the X, Y coordinate will be scaled to the current X, Y bounds.
373 if not self.enable_point_label(): return
374 if not self._mouse_coordinate: return
375 x, y = self._mouse_coordinate
376 if x < self.padding_left or x > self.width-self.padding_right: return
377 if y < self.padding_top or y > self.height-self.padding_bottom: return
378 #scale to window bounds
379 x_win_scalar = float(x - self.padding_left)/(self.width-self.padding_left-self.padding_right)
380 y_win_scalar = float((self.height - y) - self.padding_bottom)/(self.height-self.padding_top-self.padding_bottom)
381 #scale to grid bounds
382 x_val = self.x_scalar*(x_win_scalar*(self.x_max-self.x_min) + self.x_min)
383 y_val = self.y_scalar*(y_win_scalar*(self.y_max-self.y_min) + self.y_min)
385 label_str = self._populate_point_label(x_val, y_val)
386 txt = gltext.Text(label_str, font_size=POINT_LABEL_FONT_SIZE)
387 w, h = txt.get_size()
389 glColor3f(*POINT_LABEL_COLOR_SPEC)
390 if x > self.width/2: x -= w+2*POINT_LABEL_PADDING
391 self._draw_rect(x, y-h-2*POINT_LABEL_PADDING, w+2*POINT_LABEL_PADDING, h+2*POINT_LABEL_PADDING)
392 txt.draw_text(wx.Point(x+POINT_LABEL_PADDING, y-h-POINT_LABEL_PADDING))