Imported Upstream version 3.2.2
[debian/gnuradio] / grc / gui / Param.py
1 """
2 Copyright 2007, 2008, 2009 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 PARAM_MARKUP_TMPL="""\
27 #set $foreground = $param.is_valid() and 'black' or 'red'
28 <span foreground="$foreground" font_desc="Sans 7.5"><b>$encode($param.get_name()): </b>$encode(repr($param))</span>"""
29
30 PARAM_LABEL_MARKUP_TMPL="""\
31 #set $foreground = $param.is_valid() and 'black' or 'red'
32 #set $underline = $has_cb and 'low' or 'none'
33 <span underline="$underline" foreground="$foreground" font_desc="Sans 9">$encode($param.get_name())</span>"""
34
35 TIP_MARKUP_TMPL="""\
36 Key: $param.get_key()
37 Type: $param.get_type()
38 #if $param.is_valid()
39 Value: $param.get_evaluated()
40 #elif len($param.get_error_messages()) == 1
41 Error: $(param.get_error_messages()[0])
42 #else
43 Error:
44         #for $error_msg in $param.get_error_messages()
45  * $error_msg
46         #end for
47 #end if"""
48
49 class Param(Element):
50         """The graphical parameter."""
51
52         def update(self):
53                 """
54                 Called when an external change occurs.
55                 Update the graphical input by calling the change handler.
56                 """
57                 if hasattr(self, '_input'): self._handle_changed()
58
59         def get_input_object(self, callback=None):
60                 """
61                 Get the graphical gtk object to represent this parameter.
62                 Create the input object with this data type and the handle changed method.
63                 @param callback a function of one argument(this param) to be called from the change handler
64                 @return gtk input object
65                 """
66                 self._callback = callback
67                 self._input = self.get_input_class()(self, self._handle_changed)
68                 if not self._callback: self.update()
69                 return self._input
70
71         def _handle_changed(self, widget=None):
72                 """
73                 When the input changes, write the inputs to the data type.
74                 Finish by calling the exteral callback.
75                 """
76                 self.set_value(self._input.get_text())
77                 self.validate()
78                 #is param is involved in a callback? #FIXME: messy
79                 has_cb = \
80                         hasattr(self.get_parent(), 'get_callbacks') and \
81                         filter(lambda c: self.get_key() in c, self.get_parent()._callbacks)
82                 self._input.set_markup(Utils.parse_template(PARAM_LABEL_MARKUP_TMPL, param=self, has_cb=has_cb))
83                 #hide/show
84                 if self.get_hide() == 'all': self._input.hide_all()
85                 else: self._input.show_all()
86                 #set the color
87                 self._input.set_color(self.get_color())
88                 #set the tooltip
89                 if self._input.tp: self._input.tp.set_tip(
90                         self._input.entry,
91                         Utils.parse_template(TIP_MARKUP_TMPL, param=self).strip(),
92                 )
93                 #execute the external callback
94                 if self._callback: self._callback(self)
95
96         def get_layout(self):
97                 """
98                 Create a layout based on the current markup.
99                 @return the pango layout
100                 """
101                 layout = gtk.DrawingArea().create_pango_layout('')
102                 layout.set_markup(Utils.parse_template(PARAM_MARKUP_TMPL, param=self))
103                 return layout