replaced semaphore with primitive lock, adjusted drawing bounds
[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._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)
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._global_lock.acquire()
68         def unlock(self): self._global_lock.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                 self._draw_rect(
248                         self.padding_left,
249                         self.padding_top,
250                         self.width - self.padding_right - self.padding_left,
251                         self.height - self.padding_top - self.padding_bottom,
252                         fill=False,
253                 )
254                 ##################################################
255                 # Draw Grid X
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)
261                         self._draw_line(
262                                 (scaled_tick, self.padding_top, 0),
263                                 (scaled_tick, self.height-self.padding_bottom, 0),
264                         )
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                 ##################################################
269                 # Draw Grid Y
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)
275                         self._draw_line(
276                                 (self.padding_left, scaled_tick, 0),
277                                 (self.width-self.padding_right, scaled_tick, 0),
278                         )
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                 ##################################################
283                 # Draw Title
284                 ##################################################
285                 #draw x units
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                 ##################################################
289                 # Draw Labels
290                 ##################################################
291                 #draw x labels
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,
297                                 )
298                 )
299                 #draw y labels
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,
305                         ), rotation=90,
306                 )
307
308         def _get_tick_label(self, tick):
309                 """!
310                 Format the tick value and create a gl text.
311                 @param tick the floating point tick value
312                 @return the tick label text
313                 """
314                 tick_str = common.label_format(tick)
315                 return gltext.Text(tick_str, font_size=TICK_TEXT_FONT_SIZE)
316
317         def _get_ticks(self, min, max, step, scalar):
318                 """!
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
325                 """
326                 #cast to float
327                 min = float(min)
328                 max = float(max)
329                 step = float(step)
330                 #check for valid numbers
331                 assert step > 0
332                 assert max > min
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)]
338
339         def _draw_line(self, coor1, coor2):
340                 """!
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
344                 """
345                 glBegin(GL_LINES)
346                 glVertex3f(*coor1)
347                 glVertex3f(*coor2)
348                 glEnd()
349
350         def _draw_rect(self, x, y, width, height, fill=True):
351                 """!
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
359                 """
360                 glBegin(fill and GL_QUADS or GL_LINE_LOOP)
361                 glVertex2f(x, y)
362                 glVertex2f(x+width, y)
363                 glVertex2f(x+width, y+height)
364                 glVertex2f(x, y+height)
365                 glEnd()
366
367         def _draw_point_label(self):
368                 """!
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.
373                 """
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)
385                 #create text
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()
389                 #draw rect + text
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))