Merged r10463:10658 from jblum/gui_guts into trunk. Trunk passes distcheck.
[debian/gnuradio] / gr-wxgui / src / python / plotter / grid_plotter_base.py
1 #
2 # Copyright 2009 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 import GL
25 import common
26 from plotter_base import plotter_base
27 import gltext
28 import math
29
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, .5)
40 POINT_LABEL_PADDING = 3
41 GRID_LINE_DASH_LEN = 4
42
43 ##################################################
44 # Grid Plotter Base Class
45 ##################################################
46 class grid_plotter_base(plotter_base):
47
48         def __init__(self, parent, min_padding=(0, 0, 0, 0)):
49                 plotter_base.__init__(self, parent)
50                 #setup grid cache
51                 self._grid_cache = self.new_gl_cache(self._draw_grid, 25)
52                 self.enable_grid_lines(True)
53                 #setup padding
54                 self.padding_top_min, self.padding_right_min, self.padding_bottom_min, self.padding_left_min = min_padding
55                 #store title and unit strings
56                 self.set_title('Title')
57                 self.set_x_label('X Label')
58                 self.set_y_label('Y Label')
59                 #init the grid to some value
60                 self.set_x_grid(-1, 1, 1)
61                 self.set_y_grid(-1, 1, 1)
62                 #setup point label cache
63                 self._point_label_cache = self.new_gl_cache(self._draw_point_label, 75)
64                 self.enable_point_label(False)
65                 self.set_point_label_coordinate(None)
66                 common.point_label_thread(self)
67                 #init grid plotter
68                 self.register_init(self._init_grid_plotter)
69
70         def _init_grid_plotter(self):
71                 """
72                 Run gl initialization tasks.
73                 """
74                 GL.glEnableClientState(GL.GL_VERTEX_ARRAY)
75
76         def set_point_label_coordinate(self, coor):
77                 """
78                 Set the point label coordinate.
79                 @param coor the coordinate x, y tuple or None 
80                 """
81                 self.lock()
82                 self._point_label_coordinate = coor
83                 self._point_label_cache.changed(True)
84                 self.update()
85                 self.unlock()
86
87         def enable_point_label(self, enable=None):
88                 """
89                 Enable/disable the point label.
90                 @param enable true to enable
91                 @return the enable state when None
92                 """
93                 if enable is None: return self._enable_point_label
94                 self.lock()
95                 self._enable_point_label = enable
96                 self._point_label_cache.changed(True)
97                 self.unlock()
98
99         def set_title(self, title):
100                 """
101                 Set the title.
102                 @param title the title string
103                 """
104                 self.lock()
105                 self.title = title
106                 self._grid_cache.changed(True)
107                 self.unlock()
108
109         def set_x_label(self, x_label, x_units=''):
110                 """
111                 Set the x label and units.
112                 @param x_label the x label string
113                 @param x_units the x units string
114                 """
115                 self.lock()
116                 self.x_label = x_label
117                 self.x_units = x_units
118                 self._grid_cache.changed(True)
119                 self.unlock()
120
121         def set_y_label(self, y_label, y_units=''):
122                 """
123                 Set the y label and units.
124                 @param y_label the y label string
125                 @param y_units the y units string
126                 """
127                 self.lock()
128                 self.y_label = y_label
129                 self.y_units = y_units
130                 self._grid_cache.changed(True)
131                 self.unlock()
132
133         def set_x_grid(self, minimum, maximum, step, scale=False):
134                 """
135                 Set the x grid parameters.
136                 @param minimum the left-most value
137                 @param maximum the right-most value
138                 @param step the grid spacing
139                 @param scale true to scale the x grid
140                 """
141                 self.lock()
142                 self.x_min = float(minimum)
143                 self.x_max = float(maximum)
144                 self.x_step = float(step)
145                 if scale:
146                         coeff, exp, prefix = common.get_si_components(max(abs(self.x_min), abs(self.x_max)))
147                         self.x_scalar = 10**(-exp)
148                         self.x_prefix = prefix
149                 else:
150                         self.x_scalar = 1.0
151                         self.x_prefix = ''
152                 for cache in self._gl_caches: cache.changed(True)
153                 self.unlock()
154
155         def set_y_grid(self, minimum, maximum, step, scale=False):
156                 """
157                 Set the y grid parameters.
158                 @param minimum the bottom-most value
159                 @param maximum the top-most value
160                 @param step the grid spacing
161                 @param scale true to scale the y grid
162                 """
163                 self.lock()
164                 self.y_min = float(minimum)
165                 self.y_max = float(maximum)
166                 self.y_step = float(step)
167                 if scale:
168                         coeff, exp, prefix = common.get_si_components(max(abs(self.y_min), abs(self.y_max)))
169                         self.y_scalar = 10**(-exp)
170                         self.y_prefix = prefix
171                 else:
172                         self.y_scalar = 1.0
173                         self.y_prefix = ''
174                 for cache in self._gl_caches: cache.changed(True)
175                 self.unlock()
176
177         def _draw_grid(self):
178                 """
179                 Create the x, y, tick, and title labels.
180                 Resize the padding for the labels.
181                 Draw the border, grid, title, and labels.
182                 """
183                 ##################################################
184                 # Create GL text labels
185                 ##################################################
186                 #create x tick labels
187                 x_tick_labels = [(tick, self._get_tick_label(tick, self.x_units))
188                         for tick in self._get_ticks(self.x_min, self.x_max, self.x_step, self.x_scalar)]
189                 #create x tick labels
190                 y_tick_labels = [(tick, self._get_tick_label(tick, self.y_units))
191                         for tick in self._get_ticks(self.y_min, self.y_max, self.y_step, self.y_scalar)]
192                 #create x label
193                 x_label_str = self.x_units and "%s (%s%s)"%(self.x_label, self.x_prefix, self.x_units) or self.x_label
194                 x_label = gltext.Text(x_label_str, bold=True, font_size=UNITS_TEXT_FONT_SIZE, centered=True)
195                 #create y label
196                 y_label_str = self.y_units and "%s (%s%s)"%(self.y_label, self.y_prefix, self.y_units) or self.y_label
197                 y_label = gltext.Text(y_label_str, bold=True, font_size=UNITS_TEXT_FONT_SIZE, centered=True)
198                 #create title
199                 title_label = gltext.Text(self.title, bold=True, font_size=TITLE_TEXT_FONT_SIZE, centered=True)
200                 ##################################################
201                 # Resize the padding
202                 ##################################################
203                 self.padding_top = max(2*TITLE_LABEL_PADDING + title_label.get_size()[1], self.padding_top_min)
204                 self.padding_right = max(2*TICK_LABEL_PADDING, self.padding_right_min)
205                 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)
206                 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)
207                 ##################################################
208                 # Draw Grid X
209                 ##################################################
210                 for tick, label in x_tick_labels:
211                         scaled_tick = (self.width-self.padding_left-self.padding_right)*\
212                                 (tick/self.x_scalar-self.x_min)/(self.x_max-self.x_min) + self.padding_left
213                         self._draw_grid_line(
214                                 (scaled_tick, self.padding_top),
215                                 (scaled_tick, self.height-self.padding_bottom),
216                         )
217                         w, h = label.get_size()
218                         label.draw_text(wx.Point(scaled_tick-w/2, self.height-self.padding_bottom+TICK_LABEL_PADDING))
219                 ##################################################
220                 # Draw Grid Y
221                 ##################################################
222                 for tick, label in y_tick_labels:
223                         scaled_tick = (self.height-self.padding_top-self.padding_bottom)*\
224                                 (1 - (tick/self.y_scalar-self.y_min)/(self.y_max-self.y_min)) + self.padding_top
225                         self._draw_grid_line(
226                                 (self.padding_left, scaled_tick),
227                                 (self.width-self.padding_right, scaled_tick),
228                         )
229                         w, h = label.get_size()
230                         label.draw_text(wx.Point(self.padding_left-w-TICK_LABEL_PADDING, scaled_tick-h/2))
231                 ##################################################
232                 # Draw Border
233                 ##################################################
234                 GL.glColor3f(*GRID_BORDER_COLOR_SPEC)
235                 self._draw_rect(
236                         self.padding_left,
237                         self.padding_top,
238                         self.width - self.padding_right - self.padding_left,
239                         self.height - self.padding_top - self.padding_bottom,
240                         fill=False,
241                 )
242                 ##################################################
243                 # Draw Labels
244                 ##################################################
245                 #draw title label
246                 title_label.draw_text(wx.Point(self.width/2.0, TITLE_LABEL_PADDING + title_label.get_size()[1]/2))
247                 #draw x labels
248                 x_label.draw_text(wx.Point(
249                                 (self.width-self.padding_left-self.padding_right)/2.0 + self.padding_left,
250                                 self.height-(AXIS_LABEL_PADDING + x_label.get_size()[1]/2),
251                                 )
252                 )
253                 #draw y labels
254                 y_label.draw_text(wx.Point(
255                                 AXIS_LABEL_PADDING + y_label.get_size()[1]/2,
256                                 (self.height-self.padding_top-self.padding_bottom)/2.0 + self.padding_top,
257                         ), rotation=90,
258                 )
259
260         def _get_tick_label(self, tick, unit):
261                 """
262                 Format the tick value and create a gl text.
263                 @param tick the floating point tick value
264                 @param unit the axis unit
265                 @return the tick label text
266                 """
267                 if unit: tick_str = common.sci_format(tick)
268                 else: tick_str = common.eng_format(tick)
269                 return gltext.Text(tick_str, font_size=TICK_TEXT_FONT_SIZE)
270
271         def _get_ticks(self, min, max, step, scalar):
272                 """
273                 Determine the positions for the ticks.
274                 @param min the lower bound
275                 @param max the upper bound
276                 @param step the grid spacing
277                 @param scalar the grid scaling
278                 @return a list of tick positions between min and max
279                 """
280                 #cast to float
281                 min = float(min)
282                 max = float(max)
283                 step = float(step)
284                 #check for valid numbers
285                 try:
286                         assert step > 0
287                         assert max > min
288                         assert max - min > step
289                 except AssertionError: return [-1, 1]
290                 #determine the start and stop value
291                 start = int(math.ceil(min/step))
292                 stop = int(math.floor(max/step))
293                 return [i*step*scalar for i in range(start, stop+1)]
294
295         def enable_grid_lines(self, enable=None):
296                 """
297                 Enable/disable the grid lines.
298                 @param enable true to enable
299                 @return the enable state when None
300                 """
301                 if enable is None: return self._enable_grid_lines
302                 self.lock()
303                 self._enable_grid_lines = enable
304                 self._grid_cache.changed(True)
305                 self.unlock()
306
307         def _draw_grid_line(self, coor1, coor2):
308                 """
309                 Draw a dashed line from coor1 to coor2.
310                 @param corr1 a tuple of x, y
311                 @param corr2 a tuple of x, y
312                 """
313                 if not self.enable_grid_lines(): return
314                 length = math.sqrt((coor1[0] - coor2[0])**2 + (coor1[1] - coor2[1])**2)
315                 num_points = int(length/GRID_LINE_DASH_LEN)
316                 #calculate points array
317                 points = [(
318                         coor1[0] + i*(coor2[0]-coor1[0])/(num_points - 1),
319                         coor1[1] + i*(coor2[1]-coor1[1])/(num_points - 1)
320                 ) for i in range(num_points)]
321                 #set color and draw
322                 GL.glColor3f(*GRID_LINE_COLOR_SPEC)
323                 GL.glVertexPointerf(points)
324                 GL.glDrawArrays(GL.GL_LINES, 0, len(points))
325
326         def _draw_rect(self, x, y, width, height, fill=True):
327                 """
328                 Draw a rectangle on the x, y plane.
329                 X and Y are the top-left corner.
330                 @param x the left position of the rectangle
331                 @param y the top position of the rectangle
332                 @param width the width of the rectangle
333                 @param height the height of the rectangle
334                 @param fill true to color inside of rectangle
335                 """
336                 GL.glBegin(fill and GL.GL_QUADS or GL.GL_LINE_LOOP)
337                 GL.glVertex2f(x, y)
338                 GL.glVertex2f(x+width, y)
339                 GL.glVertex2f(x+width, y+height)
340                 GL.glVertex2f(x, y+height)
341                 GL.glEnd()
342
343         def _draw_point_label(self):
344                 """
345                 Draw the point label for the last mouse motion coordinate.
346                 The mouse coordinate must be an X, Y tuple.
347                 The label will be drawn at the X, Y coordinate.
348                 The values of the X, Y coordinate will be scaled to the current X, Y bounds.
349                 """
350                 if not self.enable_point_label(): return
351                 if not self._point_label_coordinate: return
352                 x, y = self._point_label_coordinate
353                 if x < self.padding_left or x > self.width-self.padding_right: return
354                 if y < self.padding_top or y > self.height-self.padding_bottom: return
355                 #scale to window bounds
356                 x_win_scalar = float(x - self.padding_left)/(self.width-self.padding_left-self.padding_right)
357                 y_win_scalar = float((self.height - y) - self.padding_bottom)/(self.height-self.padding_top-self.padding_bottom)
358                 #scale to grid bounds
359                 x_val = x_win_scalar*(self.x_max-self.x_min) + self.x_min
360                 y_val = y_win_scalar*(self.y_max-self.y_min) + self.y_min
361                 #create text
362                 label_str = self._populate_point_label(x_val, y_val)
363                 if not label_str: return
364                 txt = gltext.Text(label_str, font_size=POINT_LABEL_FONT_SIZE)
365                 w, h = txt.get_size()
366                 #draw rect + text
367                 GL.glColor3f(*POINT_LABEL_COLOR_SPEC)
368                 if x > self.width/2: x -= w+2*POINT_LABEL_PADDING
369                 self._draw_rect(x, y-h-2*POINT_LABEL_PADDING, w+2*POINT_LABEL_PADDING, h+2*POINT_LABEL_PADDING)
370                 txt.draw_text(wx.Point(x+POINT_LABEL_PADDING, y-h-POINT_LABEL_PADDING))