Imported Upstream version 3.2.2
[debian/gnuradio] / gr-wxgui / src / python / plotter / bar_plotter.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 from grid_plotter_base import grid_plotter_base
24 from OpenGL import GL
25 import common
26 import numpy
27
28 LEGEND_TEXT_FONT_SIZE = 8
29 LEGEND_BOX_PADDING = 3
30 MIN_PADDING = 0, 0, 0, 70 #top, right, bottom, left
31 #constants for the waveform storage
32 SAMPLES_KEY = 'samples'
33 COLOR_SPEC_KEY = 'color_spec'
34 MARKERY_KEY = 'marker'
35 TRIG_OFF_KEY = 'trig_off'
36
37 ##################################################
38 # Bar Plotter for histogram waveforms
39 ##################################################
40 class bar_plotter(grid_plotter_base):
41
42         def __init__(self, parent):
43                 """
44                 Create a new bar plotter.
45                 """
46                 #init
47                 grid_plotter_base.__init__(self, parent, MIN_PADDING)
48                 self._bars = list()
49                 self._bar_width = .5
50                 self._color_spec = (0, 0, 0)
51                 #setup bar cache
52                 self._bar_cache = self.new_gl_cache(self._draw_bars)
53                 #setup bar plotter
54                 self.register_init(self._init_bar_plotter)
55
56         def _init_bar_plotter(self):
57                 """
58                 Run gl initialization tasks.
59                 """
60                 GL.glEnableClientState(GL.GL_VERTEX_ARRAY)
61
62         def _draw_bars(self):
63                 """
64                 Draw the vertical bars.
65                 """
66                 bars = self._bars
67                 num_bars = len(bars)
68                 if num_bars == 0: return
69                 #use scissor to prevent drawing outside grid
70                 GL.glEnable(GL.GL_SCISSOR_TEST)
71                 GL.glScissor(
72                         self.padding_left,
73                         self.padding_bottom+1,
74                         self.width-self.padding_left-self.padding_right-1,
75                         self.height-self.padding_top-self.padding_bottom-1,
76                 )
77                 #load the points
78                 points = list()
79                 width = self._bar_width/2
80                 for i, bar in enumerate(bars):
81                         points.extend([
82                                         (i-width, 0),
83                                         (i+width, 0),
84                                         (i+width, bar),
85                                         (i-width, bar),
86                                 ]
87                         )
88                 GL.glColor3f(*self._color_spec)
89                 #matrix transforms
90                 GL.glPushMatrix()
91                 GL.glTranslatef(self.padding_left, self.padding_top, 0)
92                 GL.glScalef(
93                         (self.width-self.padding_left-self.padding_right),
94                         (self.height-self.padding_top-self.padding_bottom),
95                         1,
96                 )
97                 GL.glTranslatef(0, 1, 0)
98                 GL.glScalef(1.0/(num_bars-1), -1.0/(self.y_max-self.y_min), 1)
99                 GL.glTranslatef(0, -self.y_min, 0)
100                 #draw the bars
101                 GL.glVertexPointerf(points)
102                 GL.glDrawArrays(GL.GL_QUADS, 0, len(points))
103                 GL.glPopMatrix()
104                 GL.glDisable(GL.GL_SCISSOR_TEST)
105
106         def _populate_point_label(self, x_val, y_val):
107                 """
108                 Get the text the will populate the point label.
109                 Give X and Y values for the current point.
110                 Give values for the channel at the X coordinate.
111                 @param x_val the current x value
112                 @param y_val the current y value
113                 @return a string with newlines
114                 """
115                 if len(self._bars) == 0: return ''
116                 scalar = float(len(self._bars)-1)/(self.x_max - self.x_min)
117                 #convert x val to bar #
118                 bar_index = scalar*(x_val - self.x_min)
119                 #if abs(bar_index - round(bar_index)) > self._bar_width/2: return ''
120                 bar_index = int(round(bar_index))
121                 bar_start = (bar_index - self._bar_width/2)/scalar + self.x_min
122                 bar_end = (bar_index + self._bar_width/2)/scalar + self.x_min
123                 bar_value = self._bars[bar_index]
124                 return '%s to %s\n%s: %s'%(
125                         common.eng_format(bar_start, self.x_units),
126                         common.eng_format(bar_end, self.x_units),
127                         self.y_label, common.eng_format(bar_value, self.y_units),
128                 )
129
130         def set_bars(self, bars, bar_width, color_spec):
131                 """
132                 Set the bars.
133                 @param bars a list of bars
134                 @param bar_width the fractional width of the bar, between 0 and 1
135                 @param color_spec the color tuple
136                 """
137                 self.lock()
138                 self._bars = bars
139                 self._bar_width = float(bar_width)
140                 self._color_spec = color_spec
141                 self._bar_cache.changed(True)
142                 self.unlock()
143
144