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