fixed issue where entry boxes lost focus (mishandling of hide changing)
[debian/gnuradio] / grc / gui / PropsDialog.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 PropsDialog(gtk.Dialog):
41         """
42         A dialog to set block parameters, view errors, and view documentation.
43         """
44
45         def __init__(self, block):
46                 """
47                 Properties dialog contructor.
48                 @param block a block instance
49                 """
50                 self._hash = 0
51                 LABEL_SPACING = 7
52                 gtk.Dialog.__init__(self,
53                         title='Properties: %s'%block.get_name(),
54                         buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT),
55                 )
56                 self._block = block
57                 self.set_size_request(MIN_DIALOG_WIDTH, MIN_DIALOG_HEIGHT)
58                 vbox = gtk.VBox()
59                 #Create the scrolled window to hold all the parameters
60                 scrolled_window = gtk.ScrolledWindow()
61                 scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
62                 scrolled_window.add_with_viewport(vbox)
63                 self.vbox.pack_start(scrolled_window, True)
64                 #Params box for block parameters
65                 self._params_box = gtk.VBox()
66                 self._params_box.pack_start(get_title_label('Parameters'), False)
67                 self._input_object_params = list()
68                 #Error Messages for the block
69                 self._error_box = gtk.VBox()
70                 self._error_messages_text_display = TextDisplay()
71                 self._error_box.pack_start(gtk.Label(), False, False, LABEL_SPACING)
72                 self._error_box.pack_start(get_title_label('Error Messages'), False)
73                 self._error_box.pack_start(self._error_messages_text_display, False)
74                 #Docs for the block
75                 self._docs_box = err_box = gtk.VBox()
76                 self._docs_text_display = TextDisplay()
77                 self._docs_box.pack_start(gtk.Label(), False, False, LABEL_SPACING)
78                 self._docs_box.pack_start(get_title_label('Documentation'), False)
79                 self._docs_box.pack_start(self._docs_text_display, False)
80                 #Add the boxes
81                 vbox.pack_start(self._params_box, False)
82                 vbox.pack_start(self._error_box, False)
83                 vbox.pack_start(self._docs_box, False)
84                 #connect events
85                 self.connect('key-press-event', self._handle_key_press)
86                 self.connect('show', self._update_gui)
87                 #show all (performs initial gui update)
88                 self.show_all()
89
90         def _params_changed(self):
91                 """
92                 Have the params in this dialog changed?
93                 Ex: Added, removed, type change, hide change...
94                 To the props dialog, the hide setting of 'none' and 'part' are identical.
95                 Therfore, the props dialog only cares if the hide setting is/not 'all'.
96                 Make a hash that uniquely represents the params state.
97                 @return true if changed
98                 """
99                 old_hash = self._hash
100                 self._hash = 0
101                 for param in self._block.get_params():
102                         self._hash ^= hash(param)
103                         self._hash ^= hash(param.get_type())
104                         self._hash ^= hash(param.get_hide() == 'all')
105                 return self._hash != old_hash
106
107         def _handle_changed(self, *args):
108                 """
109                 A change occured within a param:
110                 Rewrite/validate the block and update the gui.
111                 """
112                 #update for the block
113                 self._block.rewrite()
114                 self._block.validate()
115                 self._update_gui()
116
117         def _update_gui(self, *args):
118                 """
119                 Repopulate the parameters box (if changed).
120                 Update all the input parameters.
121                 Update the error messages box.
122                 Hide the box if there are no errors.
123                 Update the documentation block.
124                 Hide the box if there are no docs.
125                 """
126                 #update the params box
127                 if self._params_changed():
128                         #hide params box before changing
129                         self._params_box.hide_all()
130                         #empty the params box
131                         for io_param in list(self._input_object_params):
132                                 self._params_box.remove(io_param)
133                                 self._input_object_params.remove(io_param)
134                                 io_param.destroy()
135                         #repopulate the params box
136                         for param in self._block.get_params():
137                                 if param.get_hide() == 'all': continue
138                                 io_param = param.get_input(self._handle_changed)
139                                 self._input_object_params.append(io_param)
140                                 self._params_box.pack_start(io_param, False)
141                         #show params box with new params
142                         self._params_box.show_all()
143                 #update the errors box
144                 if self._block.is_valid(): self._error_box.hide()
145                 else: self._error_box.show()
146                 messages = '\n\n'.join(self._block.get_error_messages())
147                 self._error_messages_text_display.set_text(messages)
148                 #update the docs box
149                 if self._block.get_doc(): self._docs_box.show()
150                 else: self._docs_box.hide()
151                 self._docs_text_display.set_text(self._block.get_doc())
152
153         def _handle_key_press(self, widget, event):
154                 """
155                 Handle key presses from the keyboard.
156                 Call the ok response when enter is pressed.
157                 @return false to forward the keypress
158                 """
159                 if event.keyval == gtk.keysyms.Return:
160                         self.response(gtk.RESPONSE_ACCEPT)
161                         return True #handled here
162                 return False #forward the keypress
163
164         def run(self):
165                 """
166                 Run the dialog and get its response.
167                 @return true if the response was accept
168                 """
169                 response = gtk.Dialog.run(self)
170                 self.destroy()
171                 return response == gtk.RESPONSE_ACCEPT