Imported Upstream version 3.2.2
[debian/gnuradio] / grc / gui / ParamsDialog.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 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,
49                         title='Properties: %s'%block.get_name(),
50                         buttons=(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE),
51                 )
52                 self.block = block
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_box = gtk.VBox()
64                 self._error_messages_text_display = TextDisplay()
65                 self._error_box.pack_start(gtk.Label(), False, False, 7) #spacing
66                 self._error_box.pack_start(get_title_label('Error Messages'), False)
67                 self._error_box.pack_start(self._error_messages_text_display, False)
68                 #Docs for the block
69                 self._docs_box = err_box = gtk.VBox()
70                 self._docs_text_display = TextDisplay()
71                 self._docs_box.pack_start(gtk.Label(), False, False, 7) #spacing
72                 self._docs_box.pack_start(get_title_label('Documentation'), False)
73                 self._docs_box.pack_start(self._docs_text_display, False)
74                 #Add all the parameters
75                 for param in self.block.get_params():
76                         vbox.pack_start(param.get_input_object(self._handle_changed), False)
77                 #Add the error and docs box
78                 vbox.pack_start(self._error_box, False)
79                 vbox.pack_start(self._docs_box, False)
80                 #connect and show
81                 self.connect('key_press_event', self._handle_key_press)
82                 self.show_all()
83                 #initial update
84                 for param in self.block.get_params(): param.update()
85                 self._update()
86
87         def _update(self):
88                 """
89                 Update the error messages box.
90                 Hide the box if there are no errors.
91                 Update the documentation block.
92                 Hide the box if there are no docs.
93                 """
94                 self.block.validate()
95                 #update the errors box
96                 if self.block.is_valid(): self._error_box.hide()
97                 else: self._error_box.show()
98                 messages = '\n\n'.join(self.block.get_error_messages())
99                 self._error_messages_text_display.set_text(messages)
100                 #update the docs box
101                 if self.block.get_doc(): self._docs_box.show()
102                 else: self._docs_box.hide()
103                 self._docs_text_display.set_text(self.block.get_doc())
104
105         def _handle_key_press(self, widget, event):
106                 """
107                 Handle key presses from the keyboard.
108                 Call the ok response when enter is pressed.
109                 @return false to forward the keypress
110                 """
111                 keyname = gtk.gdk.keyval_name(event.keyval)
112                 if keyname == 'Return': self.response(gtk.RESPONSE_OK)
113                 return False #forward the keypress
114
115         def _handle_changed(self, param):
116                 """
117                 A change occured, update any dependent parameters:
118                 The enum inside the variable type may have changed and,
119                 the variable param will need an external update.
120                 @param param the graphical parameter that initiated the callback
121                 """
122                 #update dependent params
123                 if param.is_enum():
124                         for other_param in param.get_parent().get_params():
125                                 if param.get_key() is not other_param.get_key() and (
126                                 param.get_key() in other_param._type or \
127                                 param.get_key() in other_param._hide): other_param.update()
128                 #update
129                 self._update()
130                 return True
131
132         def run(self):
133                 """
134                 Call run().
135                 @return true if a change occured.
136                 """
137                 original_data = list()
138                 for param in self.block.get_params():
139                         original_data.append(param.get_value())
140                 gtk.Dialog.run(self)
141                 self.destroy()
142                 new_data = list()
143                 for param in self.block.get_params():
144                         new_data.append(param.get_value())
145                 return original_data != new_data