2 Copyright 2008, 2009 Free Software Foundation, Inc.
3 This file is part of GNU Radio
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 from Element import Element
26 class InputParam(gtk.HBox):
27 """The base class for an input parameter inside the input parameters dialog."""
29 def __init__(self, param, _handle_changed):
30 gtk.HBox.__init__(self)
32 self._handle_changed = _handle_changed
33 self.label = gtk.Label('') #no label, markup is added by set_markup
34 self.label.set_size_request(150, -1)
35 self.pack_start(self.label, False)
36 self.set_markup = lambda m: self.label.set_markup(m)
38 def set_color(self, color): pass
40 class EntryParam(InputParam):
41 """Provide an entry box for strings and numbers."""
43 def __init__(self, *args, **kwargs):
44 InputParam.__init__(self, *args, **kwargs)
45 self.entry = input = gtk.Entry()
46 input.set_text(self.param.get_value())
47 input.connect('changed', self._handle_changed)
48 self.pack_start(input, True)
49 self.get_text = input.get_text
51 self.tp = gtk.Tooltips()
52 self.tp.set_tip(self.entry, '')
54 def set_color(self, color): self.entry.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
56 class EnumParam(InputParam):
57 """Provide an entry box for Enum types with a drop down menu."""
59 def __init__(self, *args, **kwargs):
60 InputParam.__init__(self, *args, **kwargs)
61 self._input = gtk.combo_box_new_text()
62 for option in self.param.get_options(): self._input.append_text(option.get_name())
63 self._input.set_active(self.param.get_option_keys().index(self.param.get_value()))
64 self._input.connect('changed', self._handle_changed)
65 self.pack_start(self._input, False)
66 def get_text(self): return self.param.get_option_keys()[self._input.get_active()]
68 class EnumEntryParam(InputParam):
69 """Provide an entry box and drop down menu for Raw Enum types."""
71 def __init__(self, *args, **kwargs):
72 InputParam.__init__(self, *args, **kwargs)
73 self._input = gtk.combo_box_entry_new_text()
74 for option in self.param.get_options(): self._input.append_text(option.get_name())
75 try: self._input.set_active(self.param.get_option_keys().index(self.param.get_value()))
77 self._input.set_active(-1)
78 self._input.get_child().set_text(self.param.get_value())
79 self._input.connect('changed', self._handle_changed)
80 self._input.get_child().connect('changed', self._handle_changed)
81 self.pack_start(self._input, False)
83 if self._input.get_active() == -1: return self._input.get_child().get_text()
84 return self.param.get_option_keys()[self._input.get_active()]
85 def set_color(self, color):
86 if self._input.get_active() == -1: #custom entry, use color
87 self._input.get_child().modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
88 else: #from enum, make white background
89 self._input.get_child().modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse('#ffffff'))
91 class Option(Element):
93 def __init__(self, param, n):
94 Element.__init__(self, param)
95 self._name = n.find('name')
96 self._key = n.find('key')
98 opts = n.findall('opt')
99 #test against opts when non enum
100 try: assert self.get_parent().is_enum() or not opts
101 except AssertionError: raise Exception, 'Options for non-enum types cannot have sub-options'
104 #separate the key:value
105 try: key, value = opt.split(':')
106 except: raise Exception, 'Error separating "%s" into key:value'%opt
107 #test against repeated keys
108 try: assert not self._opts.has_key(key)
109 except AssertionError: raise Exception, 'Key "%s" already exists in option'%key
111 self._opts[key] = value
113 def __str__(self): return 'Option %s(%s)'%(self.get_name(), self.get_key())
114 def get_name(self): return self._name
115 def get_key(self): return self._key
117 ##############################################
119 ##############################################
120 def get_opt_keys(self): return self._opts.keys()
121 def get_opt(self, key): return self._opts[key]
122 def get_opts(self): return self._opts.values()
124 class Param(Element):
126 ##possible param types
127 TYPES = ['enum', 'raw']
129 def __init__(self, block, n):
131 Make a new param from nested data.
132 @param block the parent element
133 @param n the nested odict
137 self._name = n.find('name')
138 self._key = n.find('key')
139 value = n.find('value') or ''
140 self._type = n.find('type')
141 self._hide = n.find('hide') or ''
143 Element.__init__(self, block)
144 #create the Option objects from the n data
145 self._options = odict()
146 for option in map(lambda o: Option(self, o), n.findall('option')):
147 key = option.get_key()
148 #test against repeated keys
149 try: assert key not in self.get_option_keys()
150 except AssertionError: raise Exception, 'Key "%s" already exists in options'%key
152 self._options[key] = option
153 #test the enum options
155 #test against options with identical keys
156 try: assert len(set(self.get_option_keys())) == len(self._options)
157 except AssertionError: raise Exception, 'Options keys "%s" are not unique.'%self.get_option_keys()
158 #test against inconsistent keys in options
159 opt_keys = self._options.values()[0].get_opt_keys()
160 for option in self._options.values():
161 try: assert set(opt_keys) == set(option.get_opt_keys())
162 except AssertionError: raise Exception, 'Opt keys "%s" are not identical across all options.'%opt_keys
163 #if a value is specified, it must be in the options keys
164 self._value = value or self.get_option_keys()[0]
165 try: assert self.get_value() in self.get_option_keys()
166 except AssertionError: raise Exception, 'The value "%s" is not in the possible values of "%s".'%(self.get_value(), self.get_option_keys())
167 else: self._value = value or ''
171 call test on all children
173 map(lambda c: c.test(), self.get_options())
178 The value must be evaluated and type must a possible type.
180 Element.validate(self)
181 try: assert self.get_type() in self.TYPES
182 except AssertionError: self.add_error_message('Type "%s" is not a possible type.'%self.get_type())
184 def get_evaluated(self): raise NotImplementedError
188 Convert the value to code.
189 @throw NotImplementedError
191 raise NotImplementedError
193 def get_color(self): return '#FFFFFF'
194 def __str__(self): return 'Param - %s(%s)'%(self.get_name(), self.get_key())
195 def is_param(self): return True
196 def get_name(self): return self._name
197 def get_key(self): return self._key
198 def get_hide(self): return self.get_parent().resolve_dependencies(self._hide)
202 if self.is_enum() and value not in self.get_option_keys():
203 value = self.get_option_keys()[0]
204 self.set_value(value)
207 def set_value(self, value):
209 self._value = str(value) #must be a string
211 def get_type(self): return self.get_parent().resolve_dependencies(self._type)
212 def is_enum(self): return self._type == 'enum'
216 Get the repr (nice string format) for this param.
217 Just return the value (special case enum).
218 Derived classes can handle complex formatting.
219 @return the string representation
221 if self.is_enum(): return self.get_option(self.get_value()).get_name()
222 return self.get_value()
224 def get_input_class(self):
226 Get the graphical gtk class to represent this parameter.
227 An enum requires and combo parameter.
228 A non-enum with options gets a combined entry/combo parameter.
229 All others get a standard entry parameter.
230 @return gtk input class
232 if self.is_enum(): return EnumParam
233 if self.get_options(): return EnumEntryParam
236 ##############################################
238 ##############################################
239 def get_option_keys(self): return self._options.keys()
240 def get_option(self, key): return self._options[key]
241 def get_options(self): return self._options.values()
243 ##############################################
245 ##############################################
246 def get_opt_keys(self): return self._options[self.get_value()].get_opt_keys()
247 def get_opt(self, key): return self._options[self.get_value()].get_opt(key)
248 def get_opts(self): return self._options[self.get_value()].get_opts()
250 ##############################################
251 ## Import/Export Methods
252 ##############################################
253 def export_data(self):
255 Export this param's key/value.
256 @return a nested data odict
259 n['key'] = self.get_key()
260 n['value'] = self.get_value()