1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
#
# Copyright 2008 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#
import wx
import wx.glcanvas
from OpenGL.GL import *
from gnuradio.wxgui import common
import threading
import gltext
import math
import time
BACKGROUND_COLOR_SPEC = (1, 0.976, 1, 1) #creamy white
GRID_LINE_COLOR_SPEC = (0, 0, 0) #black
TICK_TEXT_FONT_SIZE = 9
TITLE_TEXT_FONT_SIZE = 13
UNITS_TEXT_FONT_SIZE = 9
TICK_LABEL_PADDING = 5
POINT_LABEL_FONT_SIZE = 8
POINT_LABEL_COLOR_SPEC = (1, 1, .5)
POINT_LABEL_PADDING = 3
##################################################
# OpenGL WX Plotter Canvas
##################################################
class _plotter_base(wx.glcanvas.GLCanvas):
"""
Plotter base class for all plot types.
"""
def __init__(self, parent):
"""
Create a new plotter base.
Initialize the GLCanvas with double buffering.
Initialize various plotter flags.
Bind the paint and size events.
@param parent the parent widgit
"""
self._global_lock = threading.Lock()
attribList = (wx.glcanvas.WX_GL_DOUBLEBUFFER, wx.glcanvas.WX_GL_RGBA)
wx.glcanvas.GLCanvas.__init__(self, parent, attribList=attribList)
self.changed(False)
self._gl_init_flag = False
self._resized_flag = True
self._update_ts = 0
self.Bind(wx.EVT_PAINT, self._on_paint)
self.Bind(wx.EVT_SIZE, self._on_size)
def lock(self): self._global_lock.acquire()
def unlock(self): self._global_lock.release()
def _on_size(self, event):
"""
Flag the resize event.
The paint event will handle the actual resizing.
"""
self._resized_flag = True
def _on_paint(self, event):
"""
Respond to paint events, call update.
Initialize GL if this is the first paint event.
"""
self.SetCurrent()
#check if gl was initialized
if not self._gl_init_flag:
glClearColor(*BACKGROUND_COLOR_SPEC)
self._gl_init()
self._gl_init_flag = True
#check for a change in window size
if self._resized_flag:
self.lock()
self.width, self.height = self.GetSize()
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0, self.width, self.height, 0, 1, 0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glViewport(0, 0, self.width, self.height)
self._resized_flag = False
self.changed(True)
self.unlock()
self.draw()
def update(self):
"""
Force a paint event.
Record the timestamp.
"""
wx.PostEvent(self, wx.PaintEvent())
self._update_ts = time.time()
def clear(self): glClear(GL_COLOR_BUFFER_BIT)
def changed(self, state=None):
"""
Set the changed flag if state is not None.
Otherwise return the changed flag.
"""
if state is not None: self._changed = state
else: return self._changed
##################################################
# Grid Plotter Base Class
##################################################
class grid_plotter_base(_plotter_base):
def __init__(self, parent, padding):
_plotter_base.__init__(self, parent)
self.padding_top, self.padding_right, self.padding_bottom, self.padding_left = padding
#store title and unit strings
self.set_title('Title')
self.set_x_label('X Label')
self.set_y_label('Y Label')
#init the grid to some value
self.set_x_grid(-1, 1, 1)
self.set_y_grid(-1, 1, 1)
#setup point label
self.enable_point_label(False)
self._mouse_coordinate = None
self.Bind(wx.EVT_MOTION, self._on_motion)
self.Bind(wx.EVT_LEAVE_WINDOW, self._on_leave_window)
def _on_motion(self, event):
"""
Mouse motion, record the position X, Y.
"""
self.lock()
self._mouse_coordinate = event.GetPosition()
#update based on last known update time
if time.time() - self._update_ts > 0.03: self.update()
self.unlock()
def _on_leave_window(self, event):
"""
Mouse leave window, set the position to None.
"""
self.lock()
self._mouse_coordinate = None
self.update()
self.unlock()
def enable_point_label(self, enable=None):
"""
Enable/disable the point label.
@param enable true to enable
@return the enable state when None
"""
if enable is None: return self._enable_point_label
self.lock()
self._enable_point_label = enable
self.changed(True)
self.unlock()
def set_title(self, title):
"""
Set the title.
@param title the title string
"""
self.lock()
self.title = title
self.changed(True)
self.unlock()
def set_x_label(self, x_label, x_units=''):
"""
Set the x label and units.
@param x_label the x label string
@param x_units the x units string
"""
self.lock()
self.x_label = x_label
self.x_units = x_units
self.changed(True)
self.unlock()
def set_y_label(self, y_label, y_units=''):
"""
Set the y label and units.
@param y_label the y label string
@param y_units the y units string
"""
self.lock()
self.y_label = y_label
self.y_units = y_units
self.changed(True)
self.unlock()
def set_x_grid(self, x_min, x_max, x_step, x_scalar=1.0):
"""
Set the x grid parameters.
@param x_min the left-most value
@param x_max the right-most value
@param x_step the grid spacing
@param x_scalar the scalar factor
"""
self.lock()
self.x_min = float(x_min)
self.x_max = float(x_max)
self.x_step = float(x_step)
self.x_scalar = float(x_scalar)
self.changed(True)
self.unlock()
def set_y_grid(self, y_min, y_max, y_step, y_scalar=1.0):
"""
Set the y grid parameters.
@param y_min the bottom-most value
@param y_max the top-most value
@param y_step the grid spacing
@param y_scalar the scalar factor
"""
self.lock()
self.y_min = float(y_min)
self.y_max = float(y_max)
self.y_step = float(y_step)
self.y_scalar = float(y_scalar)
self.changed(True)
self.unlock()
def _draw_grid(self):
"""
Draw the border, grid, title, and units.
"""
##################################################
# Draw Border
##################################################
glColor3f(*GRID_LINE_COLOR_SPEC)
self._draw_rect(
self.padding_left,
self.padding_top,
self.width - self.padding_right - self.padding_left,
self.height - self.padding_top - self.padding_bottom,
fill=False,
)
##################################################
# Draw Grid X
##################################################
for tick in self._get_ticks(self.x_min, self.x_max, self.x_step, self.x_scalar):
scaled_tick = (self.width-self.padding_left-self.padding_right)*\
(tick/self.x_scalar-self.x_min)/(self.x_max-self.x_min) + self.padding_left
glColor3f(*GRID_LINE_COLOR_SPEC)
self._draw_line(
(scaled_tick, self.padding_top, 0),
(scaled_tick, self.height-self.padding_bottom, 0),
)
txt = self._get_tick_label(tick)
w, h = txt.get_size()
txt.draw_text(wx.Point(scaled_tick-w/2, self.height-self.padding_bottom+TICK_LABEL_PADDING))
##################################################
# Draw Grid Y
##################################################
for tick in self._get_ticks(self.y_min, self.y_max, self.y_step, self.y_scalar):
scaled_tick = (self.height-self.padding_top-self.padding_bottom)*\
(1 - (tick/self.y_scalar-self.y_min)/(self.y_max-self.y_min)) + self.padding_top
glColor3f(*GRID_LINE_COLOR_SPEC)
self._draw_line(
(self.padding_left, scaled_tick, 0),
(self.width-self.padding_right, scaled_tick, 0),
)
txt = self._get_tick_label(tick)
w, h = txt.get_size()
txt.draw_text(wx.Point(self.padding_left-w-TICK_LABEL_PADDING, scaled_tick-h/2))
##################################################
# Draw Title
##################################################
#draw x units
txt = gltext.Text(self.title, bold=True, font_size=TITLE_TEXT_FONT_SIZE, centered=True)
txt.draw_text(wx.Point(self.width/2.0, .5*self.padding_top))
##################################################
# Draw Labels
##################################################
#draw x labels
x_label_str = self.x_units and "%s (%s)"%(self.x_label, self.x_units) or self.x_label
txt = gltext.Text(x_label_str, bold=True, font_size=UNITS_TEXT_FONT_SIZE, centered=True)
txt.draw_text(wx.Point(
(self.width-self.padding_left-self.padding_right)/2.0 + self.padding_left,
self.height-.25*self.padding_bottom,
)
)
#draw y labels
y_label_str = self.y_units and "%s (%s)"%(self.y_label, self.y_units) or self.y_label
txt = gltext.Text(y_label_str, bold=True, font_size=UNITS_TEXT_FONT_SIZE, centered=True)
txt.draw_text(wx.Point(
.25*self.padding_left,
(self.height-self.padding_top-self.padding_bottom)/2.0 + self.padding_top,
), rotation=90,
)
def _get_tick_label(self, tick):
"""
Format the tick value and create a gl text.
@param tick the floating point tick value
@return the tick label text
"""
tick_str = common.label_format(tick)
return gltext.Text(tick_str, font_size=TICK_TEXT_FONT_SIZE)
def _get_ticks(self, min, max, step, scalar):
"""
Determine the positions for the ticks.
@param min the lower bound
@param max the upper bound
@param step the grid spacing
@param scalar the grid scaling
@return a list of tick positions between min and max
"""
#cast to float
min = float(min)
max = float(max)
step = float(step)
#check for valid numbers
assert step > 0
assert max > min
assert max - min > step
#determine the start and stop value
start = int(math.ceil(min/step))
stop = int(math.floor(max/step))
return [i*step*scalar for i in range(start, stop+1)]
def _draw_line(self, coor1, coor2):
"""
Draw a line from coor1 to coor2.
@param corr1 a tuple of x, y, z
@param corr2 a tuple of x, y, z
"""
glBegin(GL_LINES)
glVertex3f(*coor1)
glVertex3f(*coor2)
glEnd()
def _draw_rect(self, x, y, width, height, fill=True):
"""
Draw a rectangle on the x, y plane.
X and Y are the top-left corner.
@param x the left position of the rectangle
@param y the top position of the rectangle
@param width the width of the rectangle
@param height the height of the rectangle
@param fill true to color inside of rectangle
"""
glBegin(fill and GL_QUADS or GL_LINE_LOOP)
glVertex2f(x, y)
glVertex2f(x+width, y)
glVertex2f(x+width, y+height)
glVertex2f(x, y+height)
glEnd()
def _draw_point_label(self):
"""
Draw the point label for the last mouse motion coordinate.
The mouse coordinate must be an X, Y tuple.
The label will be drawn at the X, Y coordinate.
The values of the X, Y coordinate will be scaled to the current X, Y bounds.
"""
if not self.enable_point_label(): return
if not self._mouse_coordinate: return
x, y = self._mouse_coordinate
if x < self.padding_left or x > self.width-self.padding_right: return
if y < self.padding_top or y > self.height-self.padding_bottom: return
#scale to window bounds
x_win_scalar = float(x - self.padding_left)/(self.width-self.padding_left-self.padding_right)
y_win_scalar = float((self.height - y) - self.padding_bottom)/(self.height-self.padding_top-self.padding_bottom)
#scale to grid bounds
x_val = self.x_scalar*(x_win_scalar*(self.x_max-self.x_min) + self.x_min)
y_val = self.y_scalar*(y_win_scalar*(self.y_max-self.y_min) + self.y_min)
#create text
label_str = self._populate_point_label(x_val, y_val)
txt = gltext.Text(label_str, font_size=POINT_LABEL_FONT_SIZE)
w, h = txt.get_size()
#draw rect + text
glColor3f(*POINT_LABEL_COLOR_SPEC)
if x > self.width/2: x -= w+2*POINT_LABEL_PADDING
self._draw_rect(x, y-h-2*POINT_LABEL_PADDING, w+2*POINT_LABEL_PADDING, h+2*POINT_LABEL_PADDING)
txt.draw_text(wx.Point(x+POINT_LABEL_PADDING, y-h-POINT_LABEL_PADDING))
|