2 Copyright 2007 Free Software Foundation, Inc.
3 This file is part of GNU Radio
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.
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.
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
20 from Constants import BLOCK_SELECTION_WINDOW_WIDTH
29 class BlockTreeWindow(gtk.VBox):
30 """The block selection panel."""
32 def __init__(self, platform, get_flow_graph):
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
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)
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)
72 self._update_add_button()
74 ############################################################
76 ############################################################
77 def add_block(self, category, block=None):
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
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
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())
100 ############################################################
102 ############################################################
103 def _get_selected_block_key(self):
105 Get the currently selected block key.
106 @return the key of the selected block or a empty string
108 selection = self.treeview.get_selection()
109 treestore, iter = selection.get_selected()
110 return iter and treestore.get_value(iter, KEY_INDEX) or ''
112 def _update_add_button(self):
114 Update the add button's sensitivity.
115 The button should be active only if a block is selected.
117 key = self._get_selected_block_key()
118 self.add_button.set_sensitive(bool(key))
120 def _add_selected_block(self):
122 Add the selected block with the given key to the flow graph.
124 key = self._get_selected_block_key()
125 if key: self.get_flow_graph().add_new_block(key)
127 ############################################################
129 ############################################################
130 def _handle_mouse_button_press(self, widget, event):
132 Handle the mouse button press.
133 If a left double click is detected, call add selected block.
135 if event.button == 1 and event.type == gtk.gdk._2BUTTON_PRESS:
136 self._add_selected_block()
138 def _handle_selection_change(self, selection):
140 Handle a selection change in the tree view.
141 If a selection changes, set the add button sensitive.
143 self._update_add_button()
145 def _handle_add_button(self, widget):
147 Handle the add button clicked signal.
148 Call add selected block.
150 self._add_selected_block()