Merged r9481:9518 on jblum/grc_reorganize into trunk. Reorganized grc source under...
[debian/gnuradio] / grc / src / gui / ParamsDialog.py
1 """
2 Copyright 2007 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 pygtk
21 pygtk.require('2.0')
22 import gtk
23
24 from Dialogs import TextDisplay
25 from Constants import MIN_DIALOG_WIDTH, MIN_DIALOG_HEIGHT
26
27 def get_title_label(title):
28         """
29         Get a title label for the params window.
30         The title will be bold, underlined, and left justified.
31         @param title the text of the title
32         @return a gtk object
33         """
34         label = gtk.Label()
35         label.set_markup('\n<b><span underline="low">%s</span>:</b>\n'%title)
36         hbox = gtk.HBox()
37         hbox.pack_start(label, False, False, padding=11)
38         return hbox
39
40 class ParamsDialog(gtk.Dialog):
41         """A dialog box to set block parameters."""
42
43         def __init__(self, block):
44                 """
45                 SignalBlockParamsDialog contructor.
46                 @param block the signal block
47                 """
48                 gtk.Dialog.__init__(self, buttons=('gtk-close', gtk.RESPONSE_CLOSE))
49                 self.block = block
50                 self.set_title('Properties: %s'%block.get_name())
51                 self.set_size_request(MIN_DIALOG_WIDTH, MIN_DIALOG_HEIGHT)
52                 vbox = gtk.VBox()
53                 #Add the title label
54                 vbox.pack_start(get_title_label('Parameters'), False)
55                 #Create the scrolled window to hold all the parameters
56                 scrolled_window = gtk.ScrolledWindow()
57                 scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
58                 scrolled_window.add_with_viewport(vbox)
59                 self.vbox.pack_start(scrolled_window, True)
60                 #Error Messages for the block
61                 self._error_messages_box = err_box = gtk.VBox()
62                 self._error_messages_text_display = TextDisplay('')
63                 err_box.pack_start(gtk.Label(''), False, False, 7) #spacing
64                 err_box.pack_start(get_title_label('Error Messages'), False)
65                 err_box.pack_start(self._error_messages_text_display, False)
66                 #Add all the parameters
67                 for param in self.block.get_params():
68                         vbox.pack_start(param.get_input_object(self._handle_changed), False)
69                 vbox.pack_start(err_box, False)
70                 #Done adding parameters
71                 if self.block.get_doc():
72                         vbox.pack_start(gtk.Label(''), False, False, 7) #spacing
73                         vbox.pack_start(get_title_label('Documentation'), False)
74                         #Create the text box to display notes about the block
75                         vbox.pack_start(TextDisplay(self.block.get_doc()), False)
76                 self.connect('key_press_event', self._handle_key_press)
77                 self.show_all()
78                 #initial update
79                 for param in self.block.get_params(): param.update()
80                 self._update_error_messages()
81
82         def _update_error_messages(self):
83                 """
84                 Update the error messages in the error messages box.
85                 Hide the box if there are no errors.
86                 """
87                 if self.block.is_valid(): self._error_messages_box.hide()
88                 else: self._error_messages_box.show()
89                 messages = '\n'.join(self.block.get_error_messages())
90                 self._error_messages_text_display.set_text(messages)
91
92         def _handle_key_press(self, widget, event):
93                 """
94                 Handle key presses from the keyboard.
95                 Call the ok response when enter is pressed.
96                 @return false to forward the keypress
97                 """
98                 keyname = gtk.gdk.keyval_name(event.keyval)
99                 if keyname == 'Return': self.response(gtk.RESPONSE_OK)
100                 return False #forward the keypress
101
102         def _handle_changed(self, param):
103                 """
104                 A change occured, update any dependent parameters:
105                 The enum inside the variable type may have changed and,
106                 the variable param will need an external update.
107                 @param param the graphical parameter that initiated the callback
108                 """
109                 self._update_error_messages()
110                 #update dependent params
111                 if param.is_enum():
112                         for other_param in param.get_parent().get_params():
113                                 if param.get_key() is not other_param.get_key() and (
114                                 param.get_key() in other_param._type or \
115                                 param.get_key() in other_param._hide): other_param.update()
116                 return True
117
118         def run(self):
119                 """
120                 Call run().
121                 @return true if a change occured.
122                 """
123                 original_data = list()
124                 for param in self.block.get_params():
125                         original_data.append(param.get_value())
126                 gtk.Dialog.run(self)
127                 self.destroy()
128                 new_data = list()
129                 for param in self.block.get_params():
130                         new_data.append(param.get_value())
131                 return original_data != new_data