2 Copyright 2007, 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, 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)
38 def set_color(self, color): pass
42 Set the markup, color, tooltip, show/hide.
46 hasattr(self.param.get_parent(), 'get_callbacks') and \
47 filter(lambda c: self.param.get_key() in c, self.param.get_parent()._callbacks)
48 self.set_markup(Utils.parse_template(PARAM_LABEL_MARKUP_TMPL, param=self.param, has_cb=has_cb))
50 self.set_color(self.param.get_color())
52 if self.tp: self.tp.set_tip(
54 Utils.parse_template(TIP_MARKUP_TMPL, param=self.param).strip(),
57 if self.param.get_hide() == 'all': self.hide_all()
60 def _handle_changed(self, *args):
62 Handle a gui change by setting the new param value,
63 calling the callback (if applicable), and updating.
66 self.param.set_value(self.get_text())
68 if self._callback: self._callback()
69 #self.update() #dont update here, parent will update
71 class EntryParam(InputParam):
72 """Provide an entry box for strings and numbers."""
74 def __init__(self, *args, **kwargs):
75 InputParam.__init__(self, *args, **kwargs)
76 self.entry = input = gtk.Entry()
77 input.set_text(self.param.get_value())
78 input.connect('changed', self._handle_changed)
79 self.pack_start(input, True)
80 self.get_text = input.get_text
82 self.tp = gtk.Tooltips()
83 self.tp.set_tip(self.entry, '')
85 def set_color(self, color): self.entry.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
87 class EnumParam(InputParam):
88 """Provide an entry box for Enum types with a drop down menu."""
90 def __init__(self, *args, **kwargs):
91 InputParam.__init__(self, *args, **kwargs)
92 self._input = gtk.combo_box_new_text()
93 for option in self.param.get_options(): self._input.append_text(option.get_name())
94 self._input.set_active(self.param.get_option_keys().index(self.param.get_value()))
95 self._input.connect('changed', self._handle_changed)
96 self.pack_start(self._input, False)
97 def get_text(self): return self.param.get_option_keys()[self._input.get_active()]
99 class EnumEntryParam(InputParam):
100 """Provide an entry box and drop down menu for Raw Enum types."""
102 def __init__(self, *args, **kwargs):
103 InputParam.__init__(self, *args, **kwargs)
104 self._input = gtk.combo_box_entry_new_text()
105 for option in self.param.get_options(): self._input.append_text(option.get_name())
106 try: self._input.set_active(self.param.get_option_keys().index(self.param.get_value()))
108 self._input.set_active(-1)
109 self._input.get_child().set_text(self.param.get_value())
110 self._input.connect('changed', self._handle_changed)
111 self._input.get_child().connect('changed', self._handle_changed)
112 self.pack_start(self._input, False)
114 if self._input.get_active() == -1: return self._input.get_child().get_text()
115 return self.param.get_option_keys()[self._input.get_active()]
116 def set_color(self, color):
117 if self._input.get_active() == -1: #custom entry, use color
118 self._input.get_child().modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
119 else: #from enum, make white background
120 self._input.get_child().modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse('#ffffff'))
122 PARAM_MARKUP_TMPL="""\
123 #set $foreground = $param.is_valid() and 'black' or 'red'
124 <span foreground="$foreground" font_desc="Sans 7.5"><b>$encode($param.get_name()): </b>$encode(repr($param))</span>"""
126 PARAM_LABEL_MARKUP_TMPL="""\
127 #set $foreground = $param.is_valid() and 'black' or 'red'
128 #set $underline = $has_cb and 'low' or 'none'
129 <span underline="$underline" foreground="$foreground" font_desc="Sans 9">$encode($param.get_name())</span>"""
132 Key: $param.get_key()
133 Type: $param.get_type()
134 #if $param.is_valid()
135 Value: $param.get_evaluated()
136 #elif len($param.get_error_messages()) == 1
137 Error: $(param.get_error_messages()[0])
140 #for $error_msg in $param.get_error_messages()
145 class Param(Element):
146 """The graphical parameter."""
148 def __init__(self): Element.__init__(self)
150 def get_input(self, *args, **kwargs):
152 Get the graphical gtk class to represent this parameter.
153 An enum requires and combo parameter.
154 A non-enum with options gets a combined entry/combo parameter.
155 All others get a standard entry parameter.
156 @return gtk input class
158 if self.is_enum(): return EnumParam(*args, **kwargs)
159 if self.get_options(): return EnumEntryParam(*args, **kwargs)
160 return EntryParam(*args, **kwargs)
162 def get_layout(self):
164 Create a layout based on the current markup.
165 @return the pango layout
167 layout = gtk.DrawingArea().create_pango_layout('')
168 layout.set_markup(Utils.parse_template(PARAM_MARKUP_TMPL, param=self))