22126bd0bb79a452db3b403d0ab1aa61be437675
[debian/gnuradio] / gr-wxgui / src / python / plotter / channel_plotter.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 from plotter_base import grid_plotter_base
24 from OpenGL.GL import *
25 from gnuradio.wxgui import common
26 import numpy
27 import gltext
28 import math
29
30 LEGEND_TEXT_FONT_SIZE = 8
31 LEGEND_BOX_PADDING = 3
32 PADDING = 35, 15, 40, 60 #top, right, bottom, left
33 #constants for the waveform storage
34 SAMPLES_KEY = 'samples'
35 COLOR_SPEC_KEY = 'color_spec'
36 MARKERY_KEY = 'marker'
37
38 ##################################################
39 # Channel Plotter for X Y Waveforms
40 ##################################################
41 class channel_plotter(grid_plotter_base):
42
43         def __init__(self, parent):
44                 """!
45                 Create a new channel plotter.
46                 """
47                 #init
48                 grid_plotter_base.__init__(self, parent, PADDING)
49                 self._channels = dict()
50                 self.enable_legend(False)
51
52         def _gl_init(self):
53                 """!
54                 Run gl initialization tasks.
55                 """
56                 glEnableClientState(GL_VERTEX_ARRAY)
57                 self._grid_compiled_list_id = glGenLists(1)
58
59         def enable_legend(self, enable=None):
60                 """!
61                 Enable/disable the legend.
62                 @param enable true to enable
63                 @return the enable state when None
64                 """
65                 if enable is None: return self._enable_legend
66                 self.lock()
67                 self._enable_legend = enable
68                 self.changed(True)
69                 self.unlock()
70
71         def draw(self):
72                 """!
73                 Draw the grid and waveforms.
74                 """
75                 self.lock()
76                 self.clear()
77                 #store the grid drawing operations
78                 if self.changed():
79                         glNewList(self._grid_compiled_list_id, GL_COMPILE)
80                         self._draw_grid()
81                         self._draw_legend()
82                         glEndList()
83                         self.changed(False)
84                 #draw the grid
85                 glCallList(self._grid_compiled_list_id)
86                 #use scissor to prevent drawing outside grid
87                 glEnable(GL_SCISSOR_TEST)
88                 glScissor(
89                         self.padding_left+1,
90                         self.padding_bottom+1,
91                         self.width-self.padding_left-self.padding_right-1,
92                         self.height-self.padding_top-self.padding_bottom-1,
93                 )
94                 #draw the waveforms
95                 self._draw_waveforms()
96                 glDisable(GL_SCISSOR_TEST)
97                 self._draw_point_label()
98                 #swap buffer into display
99                 self.SwapBuffers()
100                 self.unlock()
101
102         def _draw_waveforms(self):
103                 """!
104                 Draw the waveforms for each channel.
105                 Scale the waveform data to the grid using gl matrix operations.
106                 """
107                 for channel in reversed(sorted(self._channels.keys())):
108                         samples = self._channels[channel][SAMPLES_KEY]
109                         num_samps = len(samples)
110                         #use opengl to scale the waveform
111                         glPushMatrix()
112                         glTranslatef(self.padding_left, self.padding_top, 0)
113                         glScalef(
114                                 (self.width-self.padding_left-self.padding_right),
115                                 (self.height-self.padding_top-self.padding_bottom),
116                                 1,
117                         )
118                         glTranslatef(0, 1, 0)
119                         if isinstance(samples, tuple):
120                                 x_scale, x_trans = 1.0/(self.x_max-self.x_min), -self.x_min
121                                 points = zip(*samples)
122                         else:
123                                 x_scale, x_trans = 1.0/(num_samps-1), 0
124                                 points = zip(numpy.arange(0, num_samps), samples)
125                         glScalef(x_scale, -1.0/(self.y_max-self.y_min), 1)
126                         glTranslatef(x_trans, -self.y_min, 0)
127                         #draw the points/lines
128                         glColor3f(*self._channels[channel][COLOR_SPEC_KEY])
129                         marker = self._channels[channel][MARKERY_KEY]
130                         if marker: glPointSize(marker)
131                         glVertexPointer(2, GL_FLOAT, 0, points)
132                         glDrawArrays(marker is None and GL_LINE_STRIP or GL_POINTS, 0, len(points))
133                         glPopMatrix()
134
135         def _populate_point_label(self, x_val, y_val):
136                 """!
137                 Get the text the will populate the point label.
138                 Give X and Y values for the current point.
139                 Give values for the channel at the X coordinate.
140                 @param x_val the current x value
141                 @param y_val the current y value
142                 @return a string with newlines
143                 """
144                 #create text
145                 label_str = '%s: %s %s\n%s: %s %s'%(
146                         self.x_label,
147                         common.label_format(x_val),
148                         self.x_units, self.y_label,
149                         common.label_format(y_val),
150                         self.y_units,
151                 )
152                 for channel in sorted(self._channels.keys()):
153                         samples = self._channels[channel][SAMPLES_KEY]
154                         num_samps = len(samples)
155                         if not num_samps: continue
156                         if isinstance(samples, tuple): continue
157                         #linear interpolation
158                         x_index = (num_samps-1)*(x_val/self.x_scalar-self.x_min)/(self.x_max-self.x_min)
159                         x_index_low = int(math.floor(x_index))
160                         x_index_high = int(math.ceil(x_index))
161                         y_value = (samples[x_index_high] - samples[x_index_low])*(x_index - x_index_low) + samples[x_index_low]
162                         label_str += '\n%s: %s %s'%(channel, common.label_format(y_value), self.y_units)
163                 return label_str
164
165         def _draw_legend(self):
166                 """!
167                 Draw the legend in the upper right corner.
168                 For each channel, draw a rectangle out of the channel color,
169                 and overlay the channel text on top of the rectangle.
170                 """
171                 if not self.enable_legend(): return
172                 x_off = self.width - self.padding_right - LEGEND_BOX_PADDING
173                 for i, channel in enumerate(reversed(sorted(self._channels.keys()))):
174                         samples = self._channels[channel][SAMPLES_KEY]
175                         if not len(samples): continue
176                         color_spec = self._channels[channel][COLOR_SPEC_KEY]
177                         txt = gltext.Text(channel, font_size=LEGEND_TEXT_FONT_SIZE)
178                         w, h = txt.get_size()
179                         #draw rect + text
180                         glColor3f(*color_spec)
181                         self._draw_rect(
182                                 x_off - w - LEGEND_BOX_PADDING,
183                                 self.padding_top/2 - h/2 - LEGEND_BOX_PADDING,
184                                 w+2*LEGEND_BOX_PADDING,
185                                 h+2*LEGEND_BOX_PADDING,
186                         )
187                         txt.draw_text(wx.Point(x_off - w, self.padding_top/2 - h/2))
188                         x_off -= w + 4*LEGEND_BOX_PADDING
189
190         def set_waveform(self, channel, samples, color_spec, marker=None):
191                 """!
192                 Set the waveform for a given channel.
193                 @param channel the channel key
194                 @param samples the waveform samples
195                 @param color_spec the 3-tuple for line color
196                 @param marker None for line
197                 """
198                 self.lock()
199                 if channel not in self._channels.keys(): self.changed(True)
200                 self._channels[channel] = {
201                         SAMPLES_KEY: samples,
202                         COLOR_SPEC_KEY: color_spec,
203                         MARKERY_KEY: marker,
204                 }
205                 self.unlock()
206
207 if __name__ == '__main__':
208         app = wx.PySimpleApp()
209         frame = wx.Frame(None, -1, 'Demo', wx.DefaultPosition)
210         vbox = wx.BoxSizer(wx.VERTICAL)
211
212         plotter = channel_plotter(frame)
213         plotter.set_x_grid(-1, 1, .2)
214         plotter.set_y_grid(-1, 1, .4)
215         vbox.Add(plotter, 1, wx.EXPAND)
216
217         plotter = channel_plotter(frame)
218         plotter.set_x_grid(-1, 1, .2)
219         plotter.set_y_grid(-1, 1, .4)
220         vbox.Add(plotter, 1, wx.EXPAND)
221
222         frame.SetSizerAndFit(vbox)
223         frame.SetSize(wx.Size(800, 600))
224         frame.Show()
225         app.MainLoop()