2 Copyright 2007, 2008, 2009, 2010 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, callback=None):
30 gtk.HBox.__init__(self)
32 self._callback = callback
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)
39 self.connect('show', self._update_gui)
40 def set_color(self, color): pass
41 def set_tooltip_text(self, text): pass
43 def _update_gui(self, *args):
45 Set the markup, color, tooltip, show/hide.
49 hasattr(self.param.get_parent(), 'get_callbacks') and \
50 filter(lambda c: self.param.get_key() in c, self.param.get_parent()._callbacks)
51 self.set_markup(Utils.parse_template(PARAM_LABEL_MARKUP_TMPL, param=self.param, has_cb=has_cb))
53 self.set_color(self.param.get_color())
55 self.set_tooltip_text(
56 Utils.parse_template(TIP_MARKUP_TMPL, param=self.param).strip(),
59 if self.param.get_hide() == 'all': self.hide_all()
62 def _handle_changed(self, *args):
64 Handle a gui change by setting the new param value,
65 calling the callback (if applicable), and updating.
68 self.param.set_value(self.get_text())
70 if self._callback: self._callback(*args)
71 else: self.param.validate()
75 class EntryParam(InputParam):
76 """Provide an entry box for strings and numbers."""
78 def __init__(self, *args, **kwargs):
79 InputParam.__init__(self, *args, **kwargs)
80 self._input = gtk.Entry()
81 self._input.set_text(self.param.get_value())
82 self._input.connect('changed', self._handle_changed)
83 self.pack_start(self._input, True)
84 def get_text(self): return self._input.get_text()
85 def set_color(self, color): self._input.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
86 def set_tooltip_text(self, text): self._input.set_tooltip_text(text)
88 class EnumParam(InputParam):
89 """Provide an entry box for Enum types with a drop down menu."""
91 def __init__(self, *args, **kwargs):
92 InputParam.__init__(self, *args, **kwargs)
93 self._input = gtk.combo_box_new_text()
94 for option in self.param.get_options(): self._input.append_text(option.get_name())
95 self._input.set_active(self.param.get_option_keys().index(self.param.get_value()))
96 self._input.connect('changed', self._handle_changed)
97 self.pack_start(self._input, False)
98 def get_text(self): return self.param.get_option_keys()[self._input.get_active()]
99 def set_tooltip_text(self, text): self._input.set_tooltip_text(text)
101 class EnumEntryParam(InputParam):
102 """Provide an entry box and drop down menu for Raw Enum types."""
104 def __init__(self, *args, **kwargs):
105 InputParam.__init__(self, *args, **kwargs)
106 self._input = gtk.combo_box_entry_new_text()
107 for option in self.param.get_options(): self._input.append_text(option.get_name())
108 try: self._input.set_active(self.param.get_option_keys().index(self.param.get_value()))
110 self._input.set_active(-1)
111 self._input.get_child().set_text(self.param.get_value())
112 self._input.connect('changed', self._handle_changed)
113 self._input.get_child().connect('changed', self._handle_changed)
114 self.pack_start(self._input, False)
116 if self._input.get_active() == -1: return self._input.get_child().get_text()
117 return self.param.get_option_keys()[self._input.get_active()]
118 def set_tooltip_text(self, text):
119 if self._input.get_active() == -1: #custom entry
120 self._input.get_child().set_tooltip_text(text)
121 else: self._input.set_tooltip_text(text)
122 def set_color(self, color):
123 if self._input.get_active() == -1: #custom entry, use color
124 self._input.get_child().modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
125 else: #from enum, make white background
126 self._input.get_child().modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse('#ffffff'))
128 PARAM_MARKUP_TMPL="""\
129 #set $foreground = $param.is_valid() and 'black' or 'red'
130 <span foreground="$foreground" font_desc="Sans 7.5"><b>$encode($param.get_name()): </b>$encode(repr($param))</span>"""
132 PARAM_LABEL_MARKUP_TMPL="""\
133 #set $foreground = $param.is_valid() and 'black' or 'red'
134 #set $underline = $has_cb and 'low' or 'none'
135 <span underline="$underline" foreground="$foreground" font_desc="Sans 9">$encode($param.get_name())</span>"""
138 ########################################
139 #def truncate(string)
141 #set $string = str($string)
142 #if len($string) > $max_len
143 $('%s...%s'%($string[:$max_len/2], $string[-$max_len/2:]))#slurp
148 ########################################
149 Key: $param.get_key()
150 Type: $param.get_type()
151 #if $param.is_valid()
152 Value: $truncate($param.get_evaluated())
153 #elif len($param.get_error_messages()) == 1
154 Error: $(param.get_error_messages()[0])
157 #for $error_msg in $param.get_error_messages()
162 class Param(Element):
163 """The graphical parameter."""
165 def __init__(self): Element.__init__(self)
167 def get_input(self, *args, **kwargs):
169 Get the graphical gtk class to represent this parameter.
170 An enum requires and combo parameter.
171 A non-enum with options gets a combined entry/combo parameter.
172 All others get a standard entry parameter.
173 @return gtk input class
175 if self.is_enum(): return EnumParam(self, *args, **kwargs)
176 if self.get_options(): return EnumEntryParam(self, *args, **kwargs)
177 return EntryParam(self, *args, **kwargs)
179 def get_markup(self):
181 Get the markup for this param.
182 @return a pango markup string
184 return Utils.parse_template(PARAM_MARKUP_TMPL, param=self)