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
|
#!/usr/bin/env python
#
# Copyright 2005 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
from gnuradio import eng_notation
# ----------------------------------------------------------------
# Wrappers for certain widgets
# ----------------------------------------------------------------
def button_with_callback(parent, label, callback):
new_id = wx.NewId()
btn = wx.Button(parent, new_id, label)
wx.EVT_BUTTON(parent, new_id, lambda evt: callback())
return btn
# ----------------------------------------------------------------
# Format converters
# ----------------------------------------------------------------
class abstract_converter(object):
def value_to_prim(self, v):
"""
Convert from user specified value to value acceptable to underlying primitive.
The underlying primitive usually expects strings.
"""
raise NotImplementedError
def prim_to_value(self, s):
"""
Convert from underlying primitive value to user specified value.
The underlying primitive usually expects strings.
"""
raise NotImplementedError
def help(self):
return "Any string is acceptable"
class identity_converter(abstract_converter):
def value_to_prim(self,v):
return v
def prim_to_value(self, s):
return s
class int_converter(abstract_converter):
def value_to_prim(self, v):
return str(v)
def prim_to_value(self, s):
return int(s, 0)
def help(self):
return "Enter an integer. Leading 0x indicates hex"
class float_converter(abstract_converter):
def value_to_prim(self, v):
return eng_notation.num_to_str(v)
def prim_to_value(self, s):
return eng_notation.str_to_num(s)
def help(self):
return "Enter a float with optional scale suffix. E.g., 100.1M"
# ----------------------------------------------------------------
# Various types of data entry fields
# ----------------------------------------------------------------
class field(object):
"""
A field in a form.
"""
def __init__(self, converter, value):
self.converter = converter
if value is not None:
self.set_value(value)
def set_value(self, v):
self._set_prim_value(self.converter.value_to_prim(v))
def get_value(self):
return self.converter.prim_to_value(self._get_prim_value())
def get_value_with_check(self):
"""
Returns (value, error_msg), where error_msg is not None if there was problem
"""
try:
return (self.get_value(), None)
except:
return (None, self._error_msg())
def _set_prim_value(self, v):
raise NotImplementedError
def _get_prim_value(self):
raise NotImplementedError
def _pair_with_label(self, widget, parent=None, sizer=None, label=None, weight=1):
self.label = label
if label is None:
sizer.Add (widget, weight, wx.EXPAND)
return widget
elif 0:
hbox = wx.BoxSizer(wx.HORIZONTAL)
label_widget = wx.StaticText(parent, -1, label + ': ')
hbox.Add(label_widget, 0, wx.EXPAND)
hbox.Add(widget, 1, wx.EXPAND)
sizer.Add(hbox, weight, wx.EXPAND)
return widget
else:
label_widget = wx.StaticText(parent, -1, label + ': ')
sizer.Add(label_widget, 0, wx.EXPAND)
sizer.Add(widget, weight, wx.EXPAND)
return widget
def _error_msg(self):
prefix = ''
if self.label:
prefix = self.label + ': '
return "%s%s is invalid. %s" % (prefix, self._get_prim_value(),
self.converter.help())
# static (display-only) text fields
class static_text_field(field):
def __init__(self, parent=None, sizer=None, label=None, value=None,
converter=identity_converter(), weight=0):
self.f = self._pair_with_label(wx.StaticText(parent, -1, ""),
parent=parent, sizer=sizer, label=label, weight=weight)
field.__init__(self, converter, value)
def _get_prim_value(self):
return self.f.GetLabel()
def _set_prim_value(self, v):
self.f.SetLabel(v)
class static_int_field(static_text_field):
def __init__(self, parent=None, sizer=None, label=None, value=None, weight=0):
static_text_field.__init__(self, parent, sizer, label, value, int_converter(), weight)
class static_float_field(static_text_field):
def __init__(self, parent=None, sizer=None, label=None, value=None, weight=0):
static_text_field.__init__(self, parent, sizer, label, value, float_converter(), weight)
# editable text fields
class text_field(field):
def __init__(self, parent=None, sizer=None, label=None, value=None,
converter=identity_converter(), callback=None, weight=1):
style = 0
if callback:
style = wx.TE_PROCESS_ENTER
new_id = wx.NewId()
w = wx.TextCtrl(parent, new_id, "", style=style)
self.f = self._pair_with_label(w, parent=parent, sizer=sizer, label=label, weight=weight)
if callback:
wx.EVT_TEXT_ENTER(w, new_id, lambda evt: callback())
field.__init__(self, converter, value)
def _get_prim_value(self):
return self.f.GetValue()
def _set_prim_value(self, v):
self.f.SetValue(v)
class int_field(text_field):
def __init__(self, parent=None, sizer=None, label=None, value=None,
callback=None, weight=1):
text_field.__init__(self, parent, sizer, label, value, int_converter(), callback, weight)
class float_field(text_field):
def __init__(self, parent=None, sizer=None, label=None, value=None,
callback=None, weight=1):
text_field.__init__(self, parent, sizer, label, value, float_converter(), callback, weight)
# other fields
class slider_field(field):
def __init__(self, parent=None, sizer=None, label=None, value=None,
converter=identity_converter(), callback=None, min=0, max=100, weight=1):
new_id = wx.NewId()
w = wx.Slider(parent, new_id, (max+min)/2, min, max,
size=wx.Size(250, -1), style=wx.SL_HORIZONTAL | wx.SL_LABELS)
self.f = self._pair_with_label(w, parent=parent, sizer=sizer, label=label, weight=weight)
if callback:
wx.EVT_COMMAND_SCROLL(w, new_id, lambda evt: callback(evt.GetInt()))
field.__init__(self, converter, value)
def _get_prim_value(self):
return self.f.GetValue()
def _set_prim_value(self, v):
self.f.SetValue(int(v))
class quantized_slider_field(field):
def __init__(self, parent=None, sizer=None, label=None, value=None,
converter=identity_converter(), callback=None, range=None, weight=1):
if not isinstance(range, (tuple, list)) or len(range) != 3:
raise ValueError, range
self.min = range[0]
self.max = range[1]
self.step_size = float(range[2])
nsteps = int((self.max-self.min)/self.step_size)
new_id = wx.NewId()
w = wx.Slider(parent, new_id, 0, 0, nsteps,
size=wx.Size(250, -1), style=wx.SL_HORIZONTAL)
self.f = self._pair_with_label(w, parent=parent, sizer=sizer, label=label, weight=weight)
if callback:
wx.EVT_COMMAND_SCROLL(w, new_id,
lambda evt: callback(self._map_out(evt.GetInt())))
field.__init__(self, converter, value)
def _get_prim_value(self):
return self._map_out(self.f.GetValue())
def _set_prim_value(self, v):
self.f.SetValue(self._map_in(v))
def _map_in(self, x):
return int((x-self.min) / self.step_size)
def _map_out(self, x):
return x * self.step_size + self.min
class checkbox_field(field):
def __init__(self, parent=None, sizer=None, label=None, value=None,
converter=identity_converter(), callback=None, weight=1):
new_id = wx.NewId()
w = wx.CheckBox(parent, new_id, label, style=wx.CHK_2STATE)
self.f = self._pair_with_label(w, parent=parent, sizer=sizer, label=None, weight=weight)
if callback:
wx.EVT_CHECKBOX(w, new_id, lambda evt: callback(evt.GetInt()))
field.__init__(self, converter, value)
def _get_prim_value(self):
return self.f.GetValue()
def _set_prim_value(self, v):
self.f.SetValue(int(v))
class radiobox_field(field):
def __init__(self, parent=None, sizer=None, label="", value=None,
converter=identity_converter(), callback=None, weight=1,
choices=None, major_dimension=1, specify_rows=False):
new_id = wx.NewId()
if specify_rows:
style=wx.RA_SPECIFY_ROWS | wx.RA_HORIZONTAL
else:
style=wx.RA_SPECIFY_COLS | wx.RA_HORIZONTAL
w = wx.RadioBox(parent, new_id, label="", style=style, majorDimension=major_dimension,
choices=choices)
self.f = self._pair_with_label(w, parent=parent, sizer=sizer, label=label, weight=weight)
if callback:
wx.EVT_RADIOBOX(w, new_id, lambda evt: callback(evt.GetString()))
field.__init__(self, converter, value)
def _get_prim_value(self):
return self.f.GetStringSelection()
def _set_prim_value(self, v):
self.f.SetStringSelection(str(v))
# ----------------------------------------------------------------
# the form class
# ----------------------------------------------------------------
class form(dict):
def __init__(self):
dict.__init__(self)
def check_input_for_errors(self):
"""
Returns list of error messages if there's trouble,
else empty list.
"""
vals = [f.get_value_with_check() for f in self.values()]
return [t[1] for t in vals if t[1] is not None]
def get_key_vals(self):
d = {}
for (key, f) in self.items():
d[key] = f.get_value()
return d
def _nop(*args): pass
def check_input_and_call(self, callback, status_handler=_nop):
"""
Return a function that checks the form for errors, and then if it's OK,
invokes the user specified callback, passing it the form key/value dictionary.
status_handler is called with a string indicating results.
"""
def doit_callback(*ignore):
errors = self.check_input_for_errors()
if errors:
status_handler(errors[0])
#print '\n'.join(tuple(errors))
else:
kv = self.get_key_vals()
if callback(kv):
status_handler("OK")
else:
status_handler("Failed")
return doit_callback
# ----------------------------------------------------------------
# Stand-alone example code
# ----------------------------------------------------------------
import sys
from gnuradio.wxgui import stdgui2
class demo_app_flow_graph (stdgui2.std_top_block):
def __init__(self, frame, panel, vbox, argv):
stdgui2.std_top_block.__init__ (self, frame, panel, vbox, argv)
self.frame = frame
self.panel = panel
def _print_kv(kv):
print "kv =", kv
return True
self.form = form()
self.form['static1'] = \
static_text_field(parent=panel, sizer=vbox,
label="Static Text",
value="The Static Value")
self.form['text1'] = \
text_field(parent=panel, sizer=vbox,
label="TextCtrl",
value="The Editable Value")
self.form['int1'] = \
int_field(parent=panel, sizer=vbox,
label="Int Field",
value=1234)
self.form['float1'] = \
float_field(parent=panel, sizer=vbox,
label="Float Field",
value=3.14159)
self.doit = button_with_callback(
panel, "Do It!",
self.form.check_input_and_call(_print_kv, self._set_status_msg))
vbox.Add(self.doit, 0, wx.CENTER)
def _set_status_msg(self, msg):
self.frame.GetStatusBar().SetStatusText(msg, 0)
def main ():
app = stdgui2.stdapp(demo_app_flow_graph, "wxgui form demo", nstatus=1)
app.MainLoop ()
if __name__ == '__main__':
main ()
|