diff options
author | Sebastian Koslowski <koslowski@kit.edu> | 2015-09-25 12:15:45 +0200 |
---|---|---|
committer | Sebastian Koslowski <koslowski@kit.edu> | 2015-10-01 21:51:09 +0200 |
commit | 6970a408e3f4a60d503149cabc0197c76cd16985 (patch) | |
tree | 1c9b948da5fb3694f644c57e629528a3e3f1accf /grc/gui/Bars.py | |
parent | 40ffa464f7d70839599f59a51aef06873518ac17 (diff) |
grc: add File->New submenu with generate modes preset, Bar.py refactoring
Diffstat (limited to 'grc/gui/Bars.py')
-rw-r--r-- | grc/gui/Bars.py | 112 |
1 files changed, 71 insertions, 41 deletions
diff --git a/grc/gui/Bars.py b/grc/gui/Bars.py index f1f90e4af2..4b5fd2ed04 100644 --- a/grc/gui/Bars.py +++ b/grc/gui/Bars.py @@ -17,12 +17,14 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ -import Actions import pygtk pygtk.require('2.0') import gtk -##The list of actions for the toolbar. +from . import Actions + + +# The list of actions for the toolbar. TOOLBAR_LIST = ( Actions.FLOW_GRAPH_NEW, Actions.FLOW_GRAPH_OPEN, @@ -57,11 +59,10 @@ TOOLBAR_LIST = ( Actions.OPEN_HIER, ) -##The list of actions and categories for the menu bar. - +# The list of actions and categories for the menu bar. MENU_BAR_LIST = ( (gtk.Action('File', '_File', None, None), [ - Actions.FLOW_GRAPH_NEW, + 'flow_graph_new', Actions.FLOW_GRAPH_OPEN, None, Actions.FLOW_GRAPH_SAVE, @@ -129,7 +130,7 @@ MENU_BAR_LIST = ( ]), ) - +# The list of actions for the context menu. CONTEXT_MENU_LIST = [ Actions.BLOCK_CUT, Actions.BLOCK_COPY, @@ -164,17 +165,46 @@ class Toolbar(gtk.Toolbar): gtk.Toolbar.__init__(self) self.set_style(gtk.TOOLBAR_ICONS) for action in TOOLBAR_LIST: - if action: #add a tool item - self.add(action.create_tool_item()) - #this reset of the tooltip property is required (after creating the tool item) for the tooltip to show + if action: # add a tool item + item = action.create_tool_item() + # this reset of the tooltip property is required + # (after creating the tool item) for the tooltip to show action.set_property('tooltip', action.get_property('tooltip')) - else: self.add(gtk.SeparatorToolItem()) + else: + item = gtk.SeparatorToolItem() + self.add(item) + + +class MenuHelperMixin(object): + """Mixin class to help build menus from the above action lists""" + + def _fill_menu(self, actions, menu=None): + """Create a menu from list of actions""" + menu = menu or gtk.Menu() + for item in actions: + if isinstance(item, tuple): + menu_item = self._make_sub_menu(*item) + elif isinstance(item, str): + menu_item = getattr(self, 'create_' + item)() + elif item is None: + menu_item = gtk.SeparatorMenuItem() + else: + menu_item = item.create_menu_item() + menu.append(menu_item) + menu.show_all() + return menu + def _make_sub_menu(self, main, actions): + """Create a submenu from a main action and a list of actions""" + main = main.create_menu_item() + main.set_submenu(self._fill_menu(actions)) + return main -class MenuBar(gtk.MenuBar): + +class MenuBar(gtk.MenuBar, MenuHelperMixin): """The gtk menu bar with actions added from the menu bar list.""" - def __init__(self): + def __init__(self, generate_modes, action_handler_callback): """ Parse the list of submenus from the menubar list. For each submenu, get a list of action names. @@ -182,37 +212,37 @@ class MenuBar(gtk.MenuBar): Add the submenu to the menu bar. """ gtk.MenuBar.__init__(self) + self.generate_modes = generate_modes + self.action_handler_callback = action_handler_callback for main_action, actions in MENU_BAR_LIST: - #create the main menu item - main_menu_item = main_action.create_menu_item() - self.append(main_menu_item) - #create the menu - main_menu = gtk.Menu() - main_menu_item.set_submenu(main_menu) - for action in actions: - main_menu.append(action.create_menu_item() if action else - gtk.SeparatorMenuItem()) - main_menu.show_all() #this show all is required for the separators to show - - -class ContextMenu(gtk.Menu): + self.append(self._make_sub_menu(main_action, actions)) + + def create_flow_graph_new(self): + """Sub menu to create flow-graph with pre-set generate mode""" + + def callback_adaptor(item, key): + """Sets original FLOW_GRAPH_NEW action as source""" + self.action_handler_callback(Actions.FLOW_GRAPH_NEW, key) + + sub_menu = gtk.Menu() + for key, name, default in self.generate_modes: + if default: + item = Actions.FLOW_GRAPH_NEW.create_menu_item() + item.set_label(name) + else: + item = gtk.MenuItem(name) + item.connect('activate', callback_adaptor, key) + sub_menu.append(item) + sub_menu.show_all() + main = gtk.ImageMenuItem(gtk.STOCK_NEW) + main.set_label(Actions.FLOW_GRAPH_NEW.get_label()) + main.set_submenu(sub_menu) + return main + + +class ContextMenu(gtk.Menu, MenuHelperMixin): """The gtk menu with actions added from the context menu list.""" def __init__(self): gtk.Menu.__init__(self) - for action in CONTEXT_MENU_LIST: - if isinstance(action, tuple): - action, sub_menu_action_list = action - item = action.create_menu_item() - self.append(item) - sub_menu = gtk.Menu() - item.set_submenu(sub_menu) - for action in sub_menu_action_list: - sub_menu.append(action.create_menu_item() if action else - gtk.SeparatorMenuItem()) - sub_menu.show_all() - - else: - self.append(action.create_menu_item() if action else - gtk.SeparatorMenuItem()) - self.show_all() + self._fill_menu(CONTEXT_MENU_LIST, self) |