Removing PowerLine since we weren't using it and don't have plans to.
[debian/gnuradio] / grc / gui / Param.py
1 """
2 Copyright 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
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.
9
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.
14
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
18 """
19
20 import Utils
21 from Element import Element
22 import pygtk
23 pygtk.require('2.0')
24 import gtk
25
26 class InputParam(gtk.HBox):
27         """The base class for an input parameter inside the input parameters dialog."""
28
29         def __init__(self, param, callback=None):
30                 gtk.HBox.__init__(self)
31                 self.param = param
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)
37                 self.tp = None
38                 #connect events
39                 self.connect('show', self._update_gui)
40         def set_color(self, color): pass
41         def set_tooltip_text(self, text): pass
42
43         def _update_gui(self, *args):
44                 """
45                 Set the markup, color, tooltip, show/hide.
46                 """
47                 #set the markup
48                 has_cb = \
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))
52                 #set the color
53                 self.set_color(self.param.get_color())
54                 #set the tooltip
55                 self.set_tooltip_text(
56                         Utils.parse_template(TIP_MARKUP_TMPL, param=self.param).strip(),
57                 )
58                 #show/hide
59                 if self.param.get_hide() == 'all': self.hide_all()
60                 else: self.show_all()
61
62         def _handle_changed(self, *args):
63                 """
64                 Handle a gui change by setting the new param value,
65                 calling the callback (if applicable), and updating.
66                 """
67                 #set the new value
68                 self.param.set_value(self.get_text())
69                 #call the callback
70                 if self._callback: self._callback(*args)
71                 else: self.param.validate()
72                 #gui update
73                 self._update_gui()
74
75 class EntryParam(InputParam):
76         """Provide an entry box for strings and numbers."""
77
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)
87
88 class EnumParam(InputParam):
89         """Provide an entry box for Enum types with a drop down menu."""
90
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)
100
101 class EnumEntryParam(InputParam):
102         """Provide an entry box and drop down menu for Raw Enum types."""
103
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()))
109                 except:
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)
115         def get_text(self):
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'))
127
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>"""
131
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>"""
136
137 TIP_MARKUP_TMPL="""\
138 ########################################
139 #def truncate(string)
140         #set $max_len = 100
141         #set $string = str($string)
142         #if len($string) > $max_len
143 $('%s...%s'%($string[:$max_len/2], $string[-$max_len/2:]))#slurp
144         #else
145 $string#slurp
146         #end if
147 #end def
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])
155 #else
156 Error:
157         #for $error_msg in $param.get_error_messages()
158  * $error_msg
159         #end for
160 #end if"""
161
162 class Param(Element):
163         """The graphical parameter."""
164
165         def __init__(self): Element.__init__(self)
166
167         def get_input(self, *args, **kwargs):
168                 """
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
174                 """
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)
178
179         def get_markup(self):
180                 """
181                 Get the markup for this param.
182                 @return a pango markup string
183                 """
184                 return Utils.parse_template(PARAM_MARKUP_TMPL, param=self)