Merged r9481:9518 on jblum/grc_reorganize into trunk. Reorganized grc source under...
[debian/gnuradio] / grc / src / gui / BlockTreeWindow.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 from Constants import BLOCK_SELECTION_WINDOW_WIDTH
21 import pygtk
22 pygtk.require('2.0')
23 import gtk
24 import gobject
25
26 NAME_INDEX = 0
27 KEY_INDEX = 1
28
29 class BlockTreeWindow(gtk.VBox):
30         """The block selection panel."""
31
32         def __init__(self, platform, get_flow_graph):
33                 """
34                 BlockTreeWindow constructor.
35                 Create a tree view of the possible blocks in the platform.
36                 The tree view nodes will be category names, the leaves will be block names.
37                 A mouse double click or button press action will trigger the add block event.
38                 @param platform the particular platform will all block prototypes
39                 @param get_flow_graph get the selected flow graph
40                 """
41                 gtk.VBox.__init__(self)
42                 self.platform = platform
43                 self.get_flow_graph = get_flow_graph
44                 #make the tree model for holding blocks
45                 self.treestore = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_STRING)
46                 self.treeview = gtk.TreeView(self.treestore)
47                 self.treeview.set_enable_search(False) #disable pop up search box
48                 self.treeview.add_events(gtk.gdk.BUTTON_PRESS_MASK)
49                 self.treeview.connect('button_press_event', self._handle_mouse_button_press)
50                 selection = self.treeview.get_selection()
51                 selection.set_mode('single')
52                 selection.connect('changed', self._handle_selection_change)
53                 renderer = gtk.CellRendererText()
54                 column = gtk.TreeViewColumn('Blocks', renderer, text=NAME_INDEX)
55                 self.treeview.append_column(column)
56                 #make the scrolled window to hold the tree view
57                 scrolled_window = gtk.ScrolledWindow()
58                 scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
59                 scrolled_window.add_with_viewport(self.treeview)
60                 scrolled_window.set_size_request(BLOCK_SELECTION_WINDOW_WIDTH, -1)
61                 self.pack_start(scrolled_window)
62                 #add button
63                 self.add_button = gtk.Button(None, 'gtk-add')
64                 self.add_button.connect('clicked', self._handle_add_button)
65                 self.pack_start(self.add_button, False)
66                 #map categories to iters
67                 self.categories = dict()
68                 self.categories[tuple()] = None
69                 #add blocks and categories
70                 self.platform.load_block_tree(self)
71                 #initialize
72                 self._update_add_button()
73
74         ############################################################
75         ## Block Tree Methods
76         ############################################################
77         def add_block(self, category, block=None):
78                 """
79                 Add a block with category to this selection window.
80                 Add only the category when block is None.
81                 @param category the category string
82                 @param block the block object or None
83                 """
84                 #rectify category
85                 category = filter(lambda x: x, category.split('/'))
86                 #add category and all sub categories
87                 for i in range(len(category)):
88                         sub_category = tuple(category[:i+1])
89                         if sub_category not in self.categories.keys():
90                                 iter = self.treestore.insert_before(self.categories[tuple(category[:i])], None)
91                                 self.treestore.set_value(iter, NAME_INDEX, '[ %s ]'%category[i])
92                                 self.treestore.set_value(iter, KEY_INDEX, '')
93                                 self.categories[sub_category] = iter
94                 #add block
95                 if block is None: return
96                 iter = self.treestore.insert_before(self.categories[tuple(category)], None)
97                 self.treestore.set_value(iter, NAME_INDEX, block.get_name())
98                 self.treestore.set_value(iter, KEY_INDEX, block.get_key())
99
100         ############################################################
101         ## Helper Methods
102         ############################################################
103         def _get_selected_block_key(self):
104                 """
105                 Get the currently selected block key.
106                 @return the key of the selected block or a empty string
107                 """
108                 selection = self.treeview.get_selection()
109                 treestore, iter = selection.get_selected()
110                 return iter and treestore.get_value(iter, KEY_INDEX) or ''
111
112         def _update_add_button(self):
113                 """
114                 Update the add button's sensitivity.
115                 The button should be active only if a block is selected.
116                 """
117                 key = self._get_selected_block_key()
118                 self.add_button.set_sensitive(bool(key))
119
120         def _add_selected_block(self):
121                 """
122                 Add the selected block with the given key to the flow graph.
123                 """
124                 key = self._get_selected_block_key()
125                 if key: self.get_flow_graph().add_new_block(key)
126
127         ############################################################
128         ## Event Handlers
129         ############################################################
130         def _handle_mouse_button_press(self, widget, event):
131                 """
132                 Handle the mouse button press.
133                 If a left double click is detected, call add selected block.
134                 """
135                 if event.button == 1 and event.type == gtk.gdk._2BUTTON_PRESS:
136                         self._add_selected_block()
137
138         def _handle_selection_change(self, selection):
139                 """
140                 Handle a selection change in the tree view.
141                 If a selection changes, set the add button sensitive.
142                 """
143                 self._update_add_button()
144
145         def _handle_add_button(self, widget):
146                 """
147                 Handle the add button clicked signal.
148                 Call add selected block.
149                 """
150                 self._add_selected_block()