Imported Upstream version 3.2.2
[debian/gnuradio] / gr-wxgui / src / python / forms / converters.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 from gnuradio import eng_notation
23 import math
24
25 class abstract_converter(object):
26         def external_to_internal(self, v):
27                 """
28                 Convert from user specified value to value acceptable to underlying primitive.
29                 The underlying primitive usually expects strings.
30                 """
31                 raise NotImplementedError
32         def internal_to_external(self, s):
33                 """
34                 Convert from underlying primitive value to user specified value.
35                 The underlying primitive usually expects strings.
36                 """
37                 raise NotImplementedError
38         def help(self):
39                 return "Any string is acceptable"
40
41 class identity_converter(abstract_converter):
42         def external_to_internal(self,v):
43                 return v
44         def internal_to_external(self, s):
45                 return s
46
47 ########################################################################
48 # Commonly used converters
49 ########################################################################
50 class chooser_converter(abstract_converter):
51         """
52         Convert between a set of possible choices and an index.
53         Used in the chooser base and all sub-classes.
54         """
55         def __init__(self, choices):
56                 #choices must be a list because tuple does not have .index() in python2.5
57                 self._choices = list(choices)
58         def external_to_internal(self, choice):
59                 return self._choices.index(choice)
60         def internal_to_external(self, index):
61                 return self._choices[index]
62         def help(self):
63                 return 'Enter a possible value in choices: "%s"'%str(self._choices)
64
65 class bool_converter(abstract_converter):
66         """
67         The internal representation is boolean.
68         The external representation is specified.
69         Used in the check box form.
70         """
71         def __init__(self, true, false):
72                 self._true = true
73                 self._false = false
74         def external_to_internal(self, v):
75                 return bool(v)
76         def internal_to_external(self, v):
77                 if v: return self._true
78                 else: return self._false
79         def help(self):
80                 return "Value must be cast-able to type bool."
81
82 class eval_converter(abstract_converter):
83         """
84         A catchall converter when int and float are not enough.
85         Evaluate the internal representation with python's eval().
86         Possible uses, set a complex number, constellation points.
87         Used in text box.
88         """
89         def __init__(self, formatter=lambda x: '%s'%(x)):
90                 self._formatter = formatter
91         def external_to_internal(self, v):
92                 return self._formatter(v)
93         def internal_to_external(self, s):
94                 return eval(s)
95         def help(self):
96                 return "Value must be evaluatable by python's eval."
97
98 class str_converter(abstract_converter):
99         def __init__(self, formatter=lambda x: '%s'%(x)):
100                 self._formatter = formatter
101         def external_to_internal(self, v):
102                 return self._formatter(v)
103         def internal_to_external(self, s):
104                 return str(s)
105
106 class int_converter(abstract_converter):
107         def __init__(self, formatter=lambda x: '%d'%round(x)):
108                 self._formatter = formatter
109         def external_to_internal(self, v):
110                 return self._formatter(v)
111         def internal_to_external(self, s):
112                 return int(s, 0)
113         def help(self):
114                 return "Enter an integer.  Leading 0x indicates hex"
115
116 class float_converter(abstract_converter):
117         def __init__(self, formatter=eng_notation.num_to_str):
118                 self._formatter = formatter
119         def external_to_internal(self, v):
120                 return self._formatter(v)
121         def internal_to_external(self, s):
122                 return eng_notation.str_to_num(s)
123         def help(self):
124                 return "Enter a float with optional scale suffix.  E.g., 100.1M"
125
126 class slider_converter(abstract_converter):
127         """
128         Scale values to and from the slider.
129         """
130         def __init__(self, minimum, maximum, num_steps, cast):
131                 assert minimum < maximum
132                 assert num_steps > 0
133                 self._offset = minimum
134                 self._scaler = float(maximum - minimum)/num_steps
135                 self._cast = cast
136         def external_to_internal(self, v):
137                 #slider's internal representation is an integer
138                 return int(round((v - self._offset)/self._scaler))
139         def internal_to_external(self, v):
140                 return self._cast(v*self._scaler + self._offset)
141         def help(self):
142                 return "Value should be within slider range"
143
144 class log_slider_converter(slider_converter):
145         def __init__(self, min_exp, max_exp, num_steps, base):
146                 assert min_exp < max_exp
147                 assert num_steps > 0
148                 self._base = base
149                 slider_converter.__init__(self, minimum=min_exp, maximum=max_exp, num_steps=num_steps, cast=float)
150         def external_to_internal(self, v):
151                 return slider_converter.external_to_internal(self, math.log(v, self._base))
152         def internal_to_external(self, v):
153                 return self._base**slider_converter.internal_to_external(self, v)