3c5e99e9ebe10e68f94defd2e322a56c4a1334d8
[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         def set_color(self, color): pass
39
40         def update(self):
41                 """
42                 Set the markup, color, and tooltip.
43                 """
44                 #set the markup
45                 has_cb = \
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))
49                 #set the color
50                 self.set_color(self.param.get_color())
51                 #set the tooltip
52                 if self.tp: self.tp.set_tip(
53                         self.entry,
54                         Utils.parse_template(TIP_MARKUP_TMPL, param=self.param).strip(),
55                 )
56
57         def _handle_changed(self, *args):
58                 """
59                 Handle a gui change by setting the new param value,
60                 calling the callback (if applicable), and updating.
61                 """
62                 #set the new value
63                 self.param.set_value(self.get_text())
64                 #call the callback
65                 if self._callback: self._callback()
66                 #self.update() #dont update here, parent will update
67
68 class EntryParam(InputParam):
69         """Provide an entry box for strings and numbers."""
70
71         def __init__(self, *args, **kwargs):
72                 InputParam.__init__(self, *args, **kwargs)
73                 self.entry = input = gtk.Entry()
74                 input.set_text(self.param.get_value())
75                 input.connect('changed', self._handle_changed)
76                 self.pack_start(input, True)
77                 self.get_text = input.get_text
78                 #tool tip
79                 self.tp = gtk.Tooltips()
80                 self.tp.set_tip(self.entry, '')
81                 self.tp.enable()
82         def set_color(self, color): self.entry.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
83
84 class EnumParam(InputParam):
85         """Provide an entry box for Enum types with a drop down menu."""
86
87         def __init__(self, *args, **kwargs):
88                 InputParam.__init__(self, *args, **kwargs)
89                 self._input = gtk.combo_box_new_text()
90                 for option in self.param.get_options(): self._input.append_text(option.get_name())
91                 self._input.set_active(self.param.get_option_keys().index(self.param.get_value()))
92                 self._input.connect('changed', self._handle_changed)
93                 self.pack_start(self._input, False)
94         def get_text(self): return self.param.get_option_keys()[self._input.get_active()]
95
96 class EnumEntryParam(InputParam):
97         """Provide an entry box and drop down menu for Raw Enum types."""
98
99         def __init__(self, *args, **kwargs):
100                 InputParam.__init__(self, *args, **kwargs)
101                 self._input = gtk.combo_box_entry_new_text()
102                 for option in self.param.get_options(): self._input.append_text(option.get_name())
103                 try: self._input.set_active(self.param.get_option_keys().index(self.param.get_value()))
104                 except:
105                         self._input.set_active(-1)
106                         self._input.get_child().set_text(self.param.get_value())
107                 self._input.connect('changed', self._handle_changed)
108                 self._input.get_child().connect('changed', self._handle_changed)
109                 self.pack_start(self._input, False)
110         def get_text(self):
111                 if self._input.get_active() == -1: return self._input.get_child().get_text()
112                 return self.param.get_option_keys()[self._input.get_active()]
113         def set_color(self, color):
114                 if self._input.get_active() == -1: #custom entry, use color
115                         self._input.get_child().modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse(color))
116                 else: #from enum, make white background
117                         self._input.get_child().modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse('#ffffff'))
118
119 PARAM_MARKUP_TMPL="""\
120 #set $foreground = $param.is_valid() and 'black' or 'red'
121 <span foreground="$foreground" font_desc="Sans 7.5"><b>$encode($param.get_name()): </b>$encode(repr($param))</span>"""
122
123 PARAM_LABEL_MARKUP_TMPL="""\
124 #set $foreground = $param.is_valid() and 'black' or 'red'
125 #set $underline = $has_cb and 'low' or 'none'
126 <span underline="$underline" foreground="$foreground" font_desc="Sans 9">$encode($param.get_name())</span>"""
127
128 TIP_MARKUP_TMPL="""\
129 Key: $param.get_key()
130 Type: $param.get_type()
131 #if $param.is_valid()
132 Value: $param.get_evaluated()
133 #elif len($param.get_error_messages()) == 1
134 Error: $(param.get_error_messages()[0])
135 #else
136 Error:
137         #for $error_msg in $param.get_error_messages()
138  * $error_msg
139         #end for
140 #end if"""
141
142 class Param(Element):
143         """The graphical parameter."""
144
145         def __init__(self): Element.__init__(self)
146
147         def get_input_class(self):
148                 """
149                 Get the graphical gtk class to represent this parameter.
150                 An enum requires and combo parameter.
151                 A non-enum with options gets a combined entry/combo parameter.
152                 All others get a standard entry parameter.
153                 @return gtk input class
154                 """
155                 if self.is_enum(): return EnumParam
156                 if self.get_options(): return EnumEntryParam
157                 return EntryParam
158
159         def update(self):
160                 """
161                 Called when an external change occurs.
162                 Update the graphical input by calling the change handler.
163                 """
164                 if hasattr(self, '_input'): self._handle_changed()
165
166         def get_input_object(self, callback=None):
167                 """
168                 Get the graphical gtk object to represent this parameter.
169                 @param callback a function to be called from the input object. 
170                 @return gtk input object
171                 """
172                 return self.get_input_class()(self, callback=callback)
173
174         def get_layout(self):
175                 """
176                 Create a layout based on the current markup.
177                 @return the pango layout
178                 """
179                 layout = gtk.DrawingArea().create_pango_layout('')
180                 layout.set_markup(Utils.parse_template(PARAM_MARKUP_TMPL, param=self))
181                 return layout