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