Replaced """! with """. Exclamation mark showed in doxygen docs.
[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                         if not num_samps: continue
111                         #use opengl to scale the waveform
112                         glPushMatrix()
113                         glTranslatef(self.padding_left, self.padding_top, 0)
114                         glScalef(
115                                 (self.width-self.padding_left-self.padding_right),
116                                 (self.height-self.padding_top-self.padding_bottom),
117                                 1,
118                         )
119                         glTranslatef(0, 1, 0)
120                         if isinstance(samples, tuple):
121                                 x_scale, x_trans = 1.0/(self.x_max-self.x_min), -self.x_min
122                                 points = zip(*samples)
123                         else:
124                                 x_scale, x_trans = 1.0/(num_samps-1), 0
125                                 points = zip(numpy.arange(0, num_samps), samples)
126                         glScalef(x_scale, -1.0/(self.y_max-self.y_min), 1)
127                         glTranslatef(x_trans, -self.y_min, 0)
128                         #draw the points/lines
129                         glColor3f(*self._channels[channel][COLOR_SPEC_KEY])
130                         marker = self._channels[channel][MARKERY_KEY]
131                         if marker: glPointSize(marker)
132                         glVertexPointerf(points)
133                         glDrawArrays(marker is None and GL_LINE_STRIP or GL_POINTS, 0, len(points))
134                         glPopMatrix()
135
136         def _populate_point_label(self, x_val, y_val):
137                 """
138                 Get the text the will populate the point label.
139                 Give X and Y values for the current point.
140                 Give values for the channel at the X coordinate.
141                 @param x_val the current x value
142                 @param y_val the current y value
143                 @return a string with newlines
144                 """
145                 #create text
146                 label_str = '%s: %s %s\n%s: %s %s'%(
147                         self.x_label,
148                         common.label_format(x_val),
149                         self.x_units, self.y_label,
150                         common.label_format(y_val),
151                         self.y_units,
152                 )
153                 for channel in sorted(self._channels.keys()):
154                         samples = self._channels[channel][SAMPLES_KEY]
155                         num_samps = len(samples)
156                         if not num_samps: continue
157                         if isinstance(samples, tuple): continue
158                         #linear interpolation
159                         x_index = (num_samps-1)*(x_val/self.x_scalar-self.x_min)/(self.x_max-self.x_min)
160                         x_index_low = int(math.floor(x_index))
161                         x_index_high = int(math.ceil(x_index))
162                         y_value = (samples[x_index_high] - samples[x_index_low])*(x_index - x_index_low) + samples[x_index_low]
163                         label_str += '\n%s: %s %s'%(channel, common.label_format(y_value), self.y_units)
164                 return label_str
165
166         def _draw_legend(self):
167                 """
168                 Draw the legend in the upper right corner.
169                 For each channel, draw a rectangle out of the channel color,
170                 and overlay the channel text on top of the rectangle.
171                 """
172                 if not self.enable_legend(): return
173                 x_off = self.width - self.padding_right - LEGEND_BOX_PADDING
174                 for i, channel in enumerate(reversed(sorted(self._channels.keys()))):
175                         samples = self._channels[channel][SAMPLES_KEY]
176                         if not len(samples): continue
177                         color_spec = self._channels[channel][COLOR_SPEC_KEY]
178                         txt = gltext.Text(channel, font_size=LEGEND_TEXT_FONT_SIZE)
179                         w, h = txt.get_size()
180                         #draw rect + text
181                         glColor3f(*color_spec)
182                         self._draw_rect(
183                                 x_off - w - LEGEND_BOX_PADDING,
184                                 self.padding_top/2 - h/2 - LEGEND_BOX_PADDING,
185                                 w+2*LEGEND_BOX_PADDING,
186                                 h+2*LEGEND_BOX_PADDING,
187                         )
188                         txt.draw_text(wx.Point(x_off - w, self.padding_top/2 - h/2))
189                         x_off -= w + 4*LEGEND_BOX_PADDING
190
191         def set_waveform(self, channel, samples, color_spec, marker=None):
192                 """
193                 Set the waveform for a given channel.
194                 @param channel the channel key
195                 @param samples the waveform samples
196                 @param color_spec the 3-tuple for line color
197                 @param marker None for line
198                 """
199                 self.lock()
200                 if channel not in self._channels.keys(): self.changed(True)
201                 self._channels[channel] = {
202                         SAMPLES_KEY: samples,
203                         COLOR_SPEC_KEY: color_spec,
204                         MARKERY_KEY: marker,
205                 }
206                 self.unlock()
207
208 if __name__ == '__main__':
209         app = wx.PySimpleApp()
210         frame = wx.Frame(None, -1, 'Demo', wx.DefaultPosition)
211         vbox = wx.BoxSizer(wx.VERTICAL)
212
213         plotter = channel_plotter(frame)
214         plotter.set_x_grid(-1, 1, .2)
215         plotter.set_y_grid(-1, 1, .4)
216         vbox.Add(plotter, 1, wx.EXPAND)
217
218         plotter = channel_plotter(frame)
219         plotter.set_x_grid(-1, 1, .2)
220         plotter.set_y_grid(-1, 1, .4)
221         vbox.Add(plotter, 1, wx.EXPAND)
222
223         frame.SetSizerAndFit(vbox)
224         frame.SetSize(wx.Size(800, 600))
225         frame.Show()
226         app.MainLoop()