Merging r11186:11273 from grc branch.
[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                 self._choices = choices
57         def external_to_internal(self, choice):
58                 return self._choices.index(choice)
59         def internal_to_external(self, index):
60                 return self._choices[index]
61         def help(self):
62                 return 'Enter a possible value in choices: "%s"'%str(self._choices)
63
64 class bool_converter(abstract_converter):
65         """
66         The internal representation is boolean.
67         The external representation is specified.
68         Used in the check box form.
69         """
70         def __init__(self, true, false):
71                 self._true = true
72                 self._false = false
73         def external_to_internal(self, v):
74                 return bool(v)
75         def internal_to_external(self, v):
76                 if v: return self._true
77                 else: return self._false
78         def help(self):
79                 return "Value must be cast-able to type bool."
80
81 class eval_converter(abstract_converter):
82         """
83         A catchall converter when int and float are not enough.
84         Evaluate the internal representation with python's eval().
85         Possible uses, set a complex number, constellation points.
86         Used in text box.
87         """
88         def __init__(self, formatter=lambda x: '%s'%(x)):
89                 self._formatter = formatter
90         def external_to_internal(self, v):
91                 return self._formatter(v)
92         def internal_to_external(self, s):
93                 return eval(s)
94         def help(self):
95                 return "Value must be evaluatable by python's eval."
96
97 class str_converter(abstract_converter):
98         def __init__(self, formatter=lambda x: '%s'%(x)):
99                 self._formatter = formatter
100         def external_to_internal(self, v):
101                 return self._formatter(v)
102         def internal_to_external(self, s):
103                 return str(s)
104
105 class int_converter(abstract_converter):
106         def __init__(self, formatter=lambda x: '%d'%round(x)):
107                 self._formatter = formatter
108         def external_to_internal(self, v):
109                 return self._formatter(v)
110         def internal_to_external(self, s):
111                 return int(s, 0)
112         def help(self):
113                 return "Enter an integer.  Leading 0x indicates hex"
114
115 class float_converter(abstract_converter):
116         def __init__(self, formatter=eng_notation.num_to_str):
117                 self._formatter = formatter
118         def external_to_internal(self, v):
119                 return self._formatter(v)
120         def internal_to_external(self, s):
121                 return eng_notation.str_to_num(s)
122         def help(self):
123                 return "Enter a float with optional scale suffix.  E.g., 100.1M"
124
125 class slider_converter(abstract_converter):
126         """
127         Scale values to and from the slider.
128         """
129         def __init__(self, minimum, maximum, num_steps, cast):
130                 assert minimum < maximum
131                 assert num_steps > 0
132                 self._offset = minimum
133                 self._scaler = float(maximum - minimum)/num_steps
134                 self._cast = cast
135         def external_to_internal(self, v):
136                 #slider's internal representation is an integer
137                 return int(round((v - self._offset)/self._scaler))
138         def internal_to_external(self, v):
139                 return self._cast(v*self._scaler + self._offset)
140         def help(self):
141                 return "Value should be within slider range"
142
143 class log_slider_converter(slider_converter):
144         def __init__(self, min_exp, max_exp, num_steps, base):
145                 assert min_exp < max_exp
146                 assert num_steps > 0
147                 self._base = base
148                 slider_converter.__init__(self, minimum=min_exp, maximum=max_exp, num_steps=num_steps, cast=float)
149         def external_to_internal(self, v):
150                 return slider_converter.external_to_internal(self, math.log(v, self._base))
151         def internal_to_external(self, v):
152                 return self._base**slider_converter.internal_to_external(self, v)