use show signal to perform initial gui update
[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, 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
42         def _update_gui(self, *args):
43                 """
44                 Set the markup, color, tooltip, show/hide.
45                 """
46                 #set the markup
47                 has_cb = \
48                         hasattr(self.param.get_parent(), 'get_callbacks') and \
49                         filter(lambda c: self.param.get_key() in c, self.param.get_parent()._callbacks)
50                 self.set_markup(Utils.parse_template(PARAM_LABEL_MARKUP_TMPL, param=self.param, has_cb=has_cb))
51                 #set the color
52                 self.set_color(self.param.get_color())
53                 #set the tooltip
54                 if self.tp: self.tp.set_tip(
55                         self.entry,
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.entry = input = gtk.Entry()
81                 input.set_text(self.param.get_value())
82                 input.connect('changed', self._handle_changed)
83                 self.pack_start(input, True)
84                 self.get_text = input.get_text
85                 #tool tip
86                 self.tp = gtk.Tooltips()
87                 self.tp.set_tip(self.entry, '')
88                 self.tp.enable()
89         def set_color(self, color): self.entry.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
90
91 class EnumParam(InputParam):
92         """Provide an entry box for Enum types with a drop down menu."""
93
94         def __init__(self, *args, **kwargs):
95                 InputParam.__init__(self, *args, **kwargs)
96                 self._input = gtk.combo_box_new_text()
97                 for option in self.param.get_options(): self._input.append_text(option.get_name())
98                 self._input.set_active(self.param.get_option_keys().index(self.param.get_value()))
99                 self._input.connect('changed', self._handle_changed)
100                 self.pack_start(self._input, False)
101         def get_text(self): return self.param.get_option_keys()[self._input.get_active()]
102
103 class EnumEntryParam(InputParam):
104         """Provide an entry box and drop down menu for Raw Enum types."""
105
106         def __init__(self, *args, **kwargs):
107                 InputParam.__init__(self, *args, **kwargs)
108                 self._input = gtk.combo_box_entry_new_text()
109                 for option in self.param.get_options(): self._input.append_text(option.get_name())
110                 try: self._input.set_active(self.param.get_option_keys().index(self.param.get_value()))
111                 except:
112                         self._input.set_active(-1)
113                         self._input.get_child().set_text(self.param.get_value())
114                 self._input.connect('changed', self._handle_changed)
115                 self._input.get_child().connect('changed', self._handle_changed)
116                 self.pack_start(self._input, False)
117         def get_text(self):
118                 if self._input.get_active() == -1: return self._input.get_child().get_text()
119                 return self.param.get_option_keys()[self._input.get_active()]
120         def set_color(self, color):
121                 if self._input.get_active() == -1: #custom entry, use color
122                         self._input.get_child().modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
123                 else: #from enum, make white background
124                         self._input.get_child().modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse('#ffffff'))
125
126 PARAM_MARKUP_TMPL="""\
127 #set $foreground = $param.is_valid() and 'black' or 'red'
128 <span foreground="$foreground" font_desc="Sans 7.5"><b>$encode($param.get_name()): </b>$encode(repr($param))</span>"""
129
130 PARAM_LABEL_MARKUP_TMPL="""\
131 #set $foreground = $param.is_valid() and 'black' or 'red'
132 #set $underline = $has_cb and 'low' or 'none'
133 <span underline="$underline" foreground="$foreground" font_desc="Sans 9">$encode($param.get_name())</span>"""
134
135 TIP_MARKUP_TMPL="""\
136 Key: $param.get_key()
137 Type: $param.get_type()
138 #if $param.is_valid()
139 Value: $param.get_evaluated()
140 #elif len($param.get_error_messages()) == 1
141 Error: $(param.get_error_messages()[0])
142 #else
143 Error:
144         #for $error_msg in $param.get_error_messages()
145  * $error_msg
146         #end for
147 #end if"""
148
149 class Param(Element):
150         """The graphical parameter."""
151
152         def __init__(self): Element.__init__(self)
153
154         def get_input(self, *args, **kwargs):
155                 """
156                 Get the graphical gtk class to represent this parameter.
157                 An enum requires and combo parameter.
158                 A non-enum with options gets a combined entry/combo parameter.
159                 All others get a standard entry parameter.
160                 @return gtk input class
161                 """
162                 if self.is_enum(): return EnumParam(self, *args, **kwargs)
163                 if self.get_options(): return EnumEntryParam(self, *args, **kwargs)
164                 return EntryParam(self, *args, **kwargs)
165
166         def get_layout(self):
167                 """
168                 Create a layout based on the current markup.
169                 @return the pango layout
170                 """
171                 layout = gtk.DrawingArea().create_pango_layout('')
172                 layout.set_markup(Utils.parse_template(PARAM_MARKUP_TMPL, param=self))
173                 return layout