plotter: require double buffering
[debian/gnuradio] / gr-wxgui / src / python / plotter / plotter_base.py
1 #
2 # Copyright 2008 Free Software Foundation, Inc.
3 #
4 # This file is part of GNU Radio
5 #
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)
9 # any later version.
10 #
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.
15 #
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.
20 #
21
22 import wx
23 import wx.glcanvas
24 from OpenGL.GL import *
25 from gnuradio.wxgui import common
26 import threading
27 import gltext
28 import math
29 import time
30
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
40
41 ##################################################
42 # OpenGL WX Plotter Canvas
43 ##################################################
44 class _plotter_base(wx.glcanvas.GLCanvas):
45         """!
46         Plotter base class for all plot types.
47         """
48
49         def __init__(self, parent):
50                 """!
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
56                 """
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)
60                 self.changed(False)
61                 self._gl_init_flag = False
62                 self._resized_flag = True
63                 self._update_ts = 0
64                 self.Bind(wx.EVT_PAINT, self._on_paint)
65                 self.Bind(wx.EVT_SIZE, self._on_size)
66
67         def lock(self): self._semaphore.acquire(True)
68         def unlock(self): self._semaphore.release()
69
70         def _on_size(self, event):
71                 """!
72                 Flag the resize event.
73                 The paint event will handle the actual resizing.
74                 """
75                 self._resized_flag = True
76
77         def _on_paint(self, event):
78                 """!
79                 Respond to paint events, call update.
80                 Initialize GL if this is the first paint event.
81                 """
82                 self.SetCurrent()
83                 #check if gl was initialized
84                 if not self._gl_init_flag:
85                         glClearColor(*BACKGROUND_COLOR_SPEC)
86                         self._gl_init()
87                         self._gl_init_flag = True
88                 #check for a change in window size
89                 if self._resized_flag:
90                         self.lock()
91                         self.width, self.height = self.GetSize()
92                         glMatrixMode(GL_PROJECTION)
93                         glLoadIdentity()
94                         glOrtho(0, self.width, self.height, 0, 1, 0)
95                         glMatrixMode(GL_MODELVIEW)
96                         glLoadIdentity()
97                         glViewport(0, 0, self.width, self.height)
98                         self._resized_flag = False
99                         self.changed(True)
100                         self.unlock()
101                 self.draw()
102
103         def update(self):
104                 """!
105                 Force a paint event.
106                 Record the timestamp.
107                 """
108                 wx.PostEvent(self, wx.PaintEvent())
109                 self._update_ts = time.time()
110
111         def clear(self): glClear(GL_COLOR_BUFFER_BIT)
112
113         def changed(self, state=None):
114                 """!
115                 Set the changed flag if state is not None.
116                 Otherwise return the changed flag.
117                 """
118                 if state is not None: self._changed = state
119                 else: return self._changed
120
121 ##################################################
122 # Grid Plotter Base Class
123 ##################################################
124 class grid_plotter_base(_plotter_base):
125
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)
136                 #setup point label
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)
141
142         def _on_motion(self, event):
143                 """!
144                 Mouse motion, record the position X, Y.
145                 """
146                 self.lock()
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()
150                 self.unlock()
151
152         def _on_leave_window(self, event):
153                 """!
154                 Mouse leave window, set the position to None.
155                 """
156                 self.lock()
157                 self._mouse_coordinate = None
158                 self.update()
159                 self.unlock()
160
161         def enable_point_label(self, enable=None):
162                 """!
163                 Enable/disable the point label.
164                 @param enable true to enable
165                 @return the enable state when None
166                 """
167                 if enable is None: return self._enable_point_label
168                 self.lock()
169                 self._enable_point_label = enable
170                 self.changed(True)
171                 self.unlock()
172
173         def set_title(self, title):
174                 """!
175                 Set the title.
176                 @param title the title string
177                 """
178                 self.lock()
179                 self.title = title
180                 self.changed(True)
181                 self.unlock()
182
183         def set_x_label(self, x_label, x_units=''):
184                 """!
185                 Set the x label and units.
186                 @param x_label the x label string
187                 @param x_units the x units string
188                 """
189                 self.lock()
190                 self.x_label = x_label
191                 self.x_units = x_units
192                 self.changed(True)
193                 self.unlock()
194
195         def set_y_label(self, y_label, y_units=''):
196                 """!
197                 Set the y label and units.
198                 @param y_label the y label string
199                 @param y_units the y units string
200                 """
201                 self.lock()
202                 self.y_label = y_label
203                 self.y_units = y_units
204                 self.changed(True)
205                 self.unlock()
206
207         def set_x_grid(self, x_min, x_max, x_step, x_scalar=1.0):
208                 """!
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
214                 """
215                 self.lock()
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)
220                 self.changed(True)
221                 self.unlock()
222
223         def set_y_grid(self, y_min, y_max, y_step, y_scalar=1.0):
224                 """!
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
230                 """
231                 self.lock()
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)
236                 self.changed(True)
237                 self.unlock()
238
239         def _draw_grid(self):
240                 """!
241                 Draw the border, grid, title, and units.
242                 """
243                 ##################################################
244                 # Draw Border
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)
252                 glEnd()
253                 ##################################################
254                 # Draw Grid X
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)
260                         self._draw_line(
261                                 (scaled_tick, self.padding_top, 0),
262                                 (scaled_tick, self.height-self.padding_bottom, 0),
263                         )
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                 ##################################################
268                 # Draw Grid Y
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)
274                         self._draw_line(
275                                 (self.padding_left, scaled_tick, 0),
276                                 (self.width-self.padding_right, scaled_tick, 0),
277                         )
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                 ##################################################
282                 # Draw Title
283                 ##################################################
284                 #draw x units
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                 ##################################################
288                 # Draw Labels
289                 ##################################################
290                 #draw x labels
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,
296                                 )
297                 )
298                 #draw y labels
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,
304                         ), rotation=90,
305                 )
306
307         def _get_tick_label(self, tick):
308                 """!
309                 Format the tick value and create a gl text.
310                 @param tick the floating point tick value
311                 @return the tick label text
312                 """
313                 tick_str = common.label_format(tick)
314                 return gltext.Text(tick_str, font_size=TICK_TEXT_FONT_SIZE)
315
316         def _get_ticks(self, min, max, step, scalar):
317                 """!
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
324                 """
325                 #cast to float
326                 min = float(min)
327                 max = float(max)
328                 step = float(step)
329                 #check for valid numbers
330                 assert step > 0
331                 assert max > min
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)]
337
338         def _draw_line(self, coor1, coor2):
339                 """!
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
343                 """
344                 glBegin(GL_LINES)
345                 glVertex3f(*coor1)
346                 glVertex3f(*coor2)
347                 glEnd()
348
349         def _draw_rect(self, x, y, width, height, fill=True):
350                 """!
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
358                 """
359                 glBegin(fill and GL_QUADS or GL_LINE_LOOP)
360                 glVertex2f(x, y)
361                 glVertex2f(x+width, y)
362                 glVertex2f(x+width, y+height)
363                 glVertex2f(x, y+height)
364                 glEnd()
365
366         def _draw_point_label(self):
367                 """!
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.
372                 """
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)
384                 #create text
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()
388                 #draw rect + text
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))