standardized the Element inheritance __init__ usage in gui
[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 class InputParam(gtk.HBox):
27         """The base class for an input parameter inside the input parameters dialog."""
28
29         def __init__(self, param, _handle_changed):
30                 gtk.HBox.__init__(self)
31                 self.param = param
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)
37                 self.tp = None
38         def set_color(self, color): pass
39
40 class EntryParam(InputParam):
41         """Provide an entry box for strings and numbers."""
42
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
50                 #tool tip
51                 self.tp = gtk.Tooltips()
52                 self.tp.set_tip(self.entry, '')
53                 self.tp.enable()
54         def set_color(self, color): self.entry.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
55
56 class EnumParam(InputParam):
57         """Provide an entry box for Enum types with a drop down menu."""
58
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()]
67
68 class EnumEntryParam(InputParam):
69         """Provide an entry box and drop down menu for Raw Enum types."""
70
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()))
76                 except:
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)
82         def get_text(self):
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'))
90
91 PARAM_MARKUP_TMPL="""\
92 #set $foreground = $param.is_valid() and 'black' or 'red'
93 <span foreground="$foreground" font_desc="Sans 7.5"><b>$encode($param.get_name()): </b>$encode(repr($param))</span>"""
94
95 PARAM_LABEL_MARKUP_TMPL="""\
96 #set $foreground = $param.is_valid() and 'black' or 'red'
97 #set $underline = $has_cb and 'low' or 'none'
98 <span underline="$underline" foreground="$foreground" font_desc="Sans 9">$encode($param.get_name())</span>"""
99
100 TIP_MARKUP_TMPL="""\
101 Key: $param.get_key()
102 Type: $param.get_type()
103 #if $param.is_valid()
104 Value: $param.get_evaluated()
105 #elif len($param.get_error_messages()) == 1
106 Error: $(param.get_error_messages()[0])
107 #else
108 Error:
109         #for $error_msg in $param.get_error_messages()
110  * $error_msg
111         #end for
112 #end if"""
113
114 class Param(Element):
115         """The graphical parameter."""
116
117         def __init__(self): Element.__init__(self)
118
119         def get_input_class(self):
120                 """
121                 Get the graphical gtk class to represent this parameter.
122                 An enum requires and combo parameter.
123                 A non-enum with options gets a combined entry/combo parameter.
124                 All others get a standard entry parameter.
125                 @return gtk input class
126                 """
127                 if self.is_enum(): return EnumParam
128                 if self.get_options(): return EnumEntryParam
129                 return EntryParam
130
131         def update(self):
132                 """
133                 Called when an external change occurs.
134                 Update the graphical input by calling the change handler.
135                 """
136                 if hasattr(self, '_input'): self._handle_changed()
137
138         def get_input_object(self, callback=None):
139                 """
140                 Get the graphical gtk object to represent this parameter.
141                 Create the input object with this data type and the handle changed method.
142                 @param callback a function of one argument(this param) to be called from the change handler
143                 @return gtk input object
144                 """
145                 self._callback = callback
146                 self._input = self.get_input_class()(self, self._handle_changed)
147                 if not self._callback: self.update()
148                 return self._input
149
150         def _handle_changed(self, widget=None):
151                 """
152                 When the input changes, write the inputs to the data type.
153                 Finish by calling the exteral callback.
154                 """
155                 self.set_value(self._input.get_text())
156                 self.validate()
157                 #is param is involved in a callback? #FIXME: messy
158                 has_cb = \
159                         hasattr(self.get_parent(), 'get_callbacks') and \
160                         filter(lambda c: self.get_key() in c, self.get_parent()._callbacks)
161                 self._input.set_markup(Utils.parse_template(PARAM_LABEL_MARKUP_TMPL, param=self, has_cb=has_cb))
162                 #hide/show
163                 if self.get_hide() == 'all': self._input.hide_all()
164                 else: self._input.show_all()
165                 #set the color
166                 self._input.set_color(self.get_color())
167                 #set the tooltip
168                 if self._input.tp: self._input.tp.set_tip(
169                         self._input.entry,
170                         Utils.parse_template(TIP_MARKUP_TMPL, param=self).strip(),
171                 )
172                 #execute the external callback
173                 if self._callback: self._callback(self)
174
175         def get_layout(self):
176                 """
177                 Create a layout based on the current markup.
178                 @return the pango layout
179                 """
180                 layout = gtk.DrawingArea().create_pango_layout('')
181                 layout.set_markup(Utils.parse_template(PARAM_MARKUP_TMPL, param=self))
182                 return layout