commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 06/16: grc: add File->New submenu with gene


From: git
Subject: [Commit-gnuradio] [gnuradio] 06/16: grc: add File->New submenu with generate modes preset, Bar.py refactoring
Date: Sat, 3 Oct 2015 19:14:11 +0000 (UTC)

This is an automated email from the git hooks/post-receive script.

jcorgan pushed a commit to branch master
in repository gnuradio.

commit 6970a408e3f4a60d503149cabc0197c76cd16985
Author: Sebastian Koslowski <address@hidden>
Date:   Fri Sep 25 12:15:45 2015 +0200

    grc: add File->New submenu with generate modes preset, Bar.py refactoring
---
 grc/blocks/options.xml   |   2 +-
 grc/gui/ActionHandler.py |   7 ++-
 grc/gui/Bars.py          | 112 ++++++++++++++++++++++++++++++-----------------
 grc/gui/MainWindow.py    |  10 ++++-
 4 files changed, 85 insertions(+), 46 deletions(-)

diff --git a/grc/blocks/options.xml b/grc/blocks/options.xml
index dc02b83..756a4cd 100644
--- a/grc/blocks/options.xml
+++ b/grc/blocks/options.xml
@@ -77,7 +77,7 @@ else: self.stop(); self.wait()</callback>
                        <key>hb</key>
                </option>
                <option>
-                       <name>QT GUI Hier Block</name>
+                       <name>Hier Block (QT GUI)</name>
                        <key>hb_qt_gui</key>
                </option>
        </param>
diff --git a/grc/gui/ActionHandler.py b/grc/gui/ActionHandler.py
index e6ffe9c..faa59b6 100644
--- a/grc/gui/ActionHandler.py
+++ b/grc/gui/ActionHandler.py
@@ -60,7 +60,7 @@ class ActionHandler:
         for action in Actions.get_all_actions(): action.connect('activate', 
self._handle_action)
         #setup the main window
         self.platform = platform;
-        self.main_window = MainWindow(platform)
+        self.main_window = MainWindow(platform, self._handle_action)
         self.main_window.connect('delete-event', self._quit)
         self.main_window.connect('key-press-event', self._handle_key_press)
         self.get_page = self.main_window.get_page
@@ -107,7 +107,7 @@ class ActionHandler:
         Actions.APPLICATION_QUIT()
         return True
 
-    def _handle_action(self, action):
+    def _handle_action(self, action, *args):
         #print action
         ##################################################
         # Initialize/Quit
@@ -466,6 +466,9 @@ class ActionHandler:
         ##################################################
         elif action == Actions.FLOW_GRAPH_NEW:
             self.main_window.new_page()
+            if args:
+                
self.get_flow_graph()._options_block.get_param('generate_options').set_value(args[0])
+                self.get_flow_graph().update()
         elif action == Actions.FLOW_GRAPH_OPEN:
             file_paths = 
OpenFlowGraphFileDialog(self.get_page().get_file_path()).run()
             if file_paths: #open a new page for each file, show only the first
diff --git a/grc/gui/Bars.py b/grc/gui/Bars.py
index f1f90e4..4b5fd2e 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)
diff --git a/grc/gui/MainWindow.py b/grc/gui/MainWindow.py
index 03a4b53..f658a85 100644
--- a/grc/gui/MainWindow.py
+++ b/grc/gui/MainWindow.py
@@ -65,12 +65,17 @@ PAGE_TITLE_MARKUP_TMPL = """\
 class MainWindow(gtk.Window):
     """The topmost window with menus, the tool bar, and other major windows."""
 
-    def __init__(self, platform):
+    def __init__(self, platform, action_handler_callback):
         """
         MainWindow contructor
         Setup the menu, toolbar, flowgraph editor notebook, block selection 
window...
         """
         self._platform = platform
+        gen_opts = platform.get_block('options').get_param('generate_options')
+        generate_mode_default = gen_opts.get_value()
+        generate_modes = [
+            (o.get_key(), o.get_name(), o.get_key() == generate_mode_default)
+            for o in gen_opts.get_options()]
         #setup window
         gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
         vbox = gtk.VBox()
@@ -78,7 +83,8 @@ class MainWindow(gtk.Window):
         self.add(vbox)
         #create the menu bar and toolbar
         self.add_accel_group(Actions.get_accel_group())
-        vbox.pack_start(Bars.MenuBar(), False)
+        menu_bar = Bars.MenuBar(generate_modes, action_handler_callback)
+        vbox.pack_start(menu_bar, False)
         vbox.pack_start(Bars.Toolbar(), False)
         vbox.pack_start(self.hpaned)
         #create the notebook



reply via email to

[Prev in Thread] Current Thread [Next in Thread]