]> git.gag.com Git - debian/gnuradio/commitdiff
switched hotkey handling to gtk accelerators
authorjblum <jblum@221aa14e-8319-0410-a670-987f0aec2ac5>
Wed, 26 Nov 2008 23:51:43 +0000 (23:51 +0000)
committerjblum <jblum@221aa14e-8319-0410-a670-987f0aec2ac5>
Wed, 26 Nov 2008 23:51:43 +0000 (23:51 +0000)
git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@10075 221aa14e-8319-0410-a670-987f0aec2ac5

grc/src/gui/ActionHandler.py
grc/src/gui/Actions.py
grc/src/gui/Bars.py
grc/src/gui/Dialogs.py
grc/src/gui/MainWindow.py

index 66b652d25ed5dd18dec914c0c6932ad1f1114903..cb31b3756e32bdc923eeb241eda39db82e26d709 100644 (file)
@@ -32,7 +32,7 @@ from .. utils import ParseXML
 import random
 from .. platforms.gui.Platform import Platform
 from MainWindow import MainWindow
-from Dialogs import PreferencesDialog, AboutDialog, HotKeysDialog
+from Dialogs import PreferencesDialog, AboutDialog
 from FileDialogs import OpenFlowGraphFileDialog, SaveFlowGraphFileDialog, SaveImageFileDialog
 
 gobject.threads_init()
@@ -56,7 +56,7 @@ class ActionHandler:
                #setup icon using icon theme
                try: gtk.window_set_default_icon(gtk.IconTheme().load_icon('gnuradio-grc', 256, 0))
                except: pass
-               for action in Actions.ACTIONS_LIST: action.connect('activate', self._handle_actions)
+               for action in Actions.get_all_actions(): action.connect('activate', self._handle_actions)
                #setup the main window
                self.main_window = MainWindow(self.handle_states, platform)
                self.main_window.connect('delete_event', self._quit)
@@ -77,72 +77,26 @@ class ActionHandler:
                """
                Handle key presses from the keyboard.
                Translate key combos into actions.
-               Key combinations that do not include special keys, such as ctrl or Fcn*,
-               Also, require that the flow graph has mouse focus when choosing to handle keys.
-               @return true if the flow graph is in active use
+               @return false to let the accelerators handle the key action
                """
-               keyname = gtk.gdk.keyval_name(event.keyval)
-               ctrl = event.state & gtk.gdk.CONTROL_MASK
-               alt = event.state & gtk.gdk.MOD1_MASK
-               shift = event.state & gtk.gdk.SHIFT_MASK
-               #################### save/open/new/close ###############################
-               if ctrl and keyname == 's':
-                       self.handle_states(Actions.FLOW_GRAPH_SAVE)
-               elif ctrl and keyname == 'o':
-                       self.handle_states(Actions.FLOW_GRAPH_OPEN)
-               elif ctrl and keyname == 'n':
-                       self.handle_states(Actions.FLOW_GRAPH_NEW)
-               elif ctrl and keyname == 'q':
-                       self.handle_states(Actions.FLOW_GRAPH_CLOSE)
-               #################### Cut/Copy/Paste ###############################
-               elif self.get_focus_flag() and ctrl and keyname == 'x': #mouse focus
-                       self.handle_states(Actions.BLOCK_CUT)
-               elif self.get_focus_flag() and ctrl and keyname == 'c': #mouse focus
-                       self.handle_states(Actions.BLOCK_COPY)
-               elif self.get_focus_flag() and ctrl and keyname == 'v': #mouse focus
-                       self.handle_states(Actions.BLOCK_PASTE)
-               #################### Undo/Redo ###############################
-               elif ctrl and keyname == 'z':
-                       self.handle_states(Actions.FLOW_GRAPH_UNDO)
-               elif ctrl and keyname == 'y':
-                       self.handle_states(Actions.FLOW_GRAPH_REDO)
-               #################### Delete ###############################
-               elif self.get_focus_flag() and keyname == 'Delete':     #mouse focus
-                       self.handle_states(Actions.ELEMENT_DELETE)
-               #################### Params     ###############################
-               elif self.get_focus_flag() and keyname == 'Return':     #mouse focus
-                       self.handle_states(Actions.BLOCK_PARAM_MODIFY)
-               #################### Rotate ###############################
-               elif self.get_focus_flag() and keyname == 'Right': #mouse focus
-                       self.handle_states(Actions.BLOCK_ROTATE_RIGHT)
-               elif self.get_focus_flag() and keyname == 'Left': #mouse focus
-                       self.handle_states(Actions.BLOCK_ROTATE_LEFT)
-               #################### Enable/Disable ###############################
-               elif self.get_focus_flag() and keyname == 'e': #mouse focus
-                       self.handle_states(Actions.BLOCK_ENABLE)
-               elif self.get_focus_flag() and keyname == 'd': #mouse focus
-                       self.handle_states(Actions.BLOCK_DISABLE)
-               #################### Data Type ###############################
-               elif self.get_focus_flag() and keyname == 'Down': #mouse focus
-                       self.handle_states(Actions.BLOCK_INC_TYPE)
-               elif self.get_focus_flag() and keyname == 'Up': #mouse focus
-                       self.handle_states(Actions.BLOCK_DEC_TYPE)
-               #################### Port Controllers ###############################
-               elif self.get_focus_flag() and keyname in ('equal','plus', 'KP_Add'): #mouse focus
-                       self.handle_states(Actions.PORT_CONTROLLER_INC)
-               elif self.get_focus_flag() and keyname in ('minus', 'KP_Subtract'): #mouse focus
-                       self.handle_states(Actions.PORT_CONTROLLER_DEC)
-               #################### Gen/Exec/Stop/Print ###############################
-               elif keyname == 'F5':
-                       self.handle_states(Actions.FLOW_GRAPH_GEN)
-               elif keyname == 'F6':
-                       self.handle_states(Actions.FLOW_GRAPH_EXEC)
-               elif keyname == 'F7':
-                       self.handle_states(Actions.FLOW_GRAPH_KILL)
-               elif keyname == 'Print':
-                       self.handle_states(Actions.FLOW_GRAPH_SCREEN_CAPTURE)
-               #propagate this if the fg is not in focus or nothing is selected
-               return self.get_focus_flag() and self.get_flow_graph().is_selected()
+               if self.get_focus_flag() and self.get_flow_graph().is_selected():
+                       try:
+                               self.handle_states({
+                                       'Left': Actions.BLOCK_ROTATE_LEFT,
+                                       'Right': Actions.BLOCK_ROTATE_RIGHT,
+                                       'Up': Actions.BLOCK_DEC_TYPE,
+                                       'Down': Actions.BLOCK_INC_TYPE,
+                                       'equal': Actions.PORT_CONTROLLER_INC,
+                                       'plus': Actions.PORT_CONTROLLER_INC,
+                                       'KP_Add': Actions.PORT_CONTROLLER_INC,
+                                       'minus': Actions.PORT_CONTROLLER_DEC,
+                                       'KP_Subtract': Actions.PORT_CONTROLLER_DEC,
+                               }[gtk.gdk.keyval_name(event.keyval)])
+                               return True
+                       #focus + selection: always return false for accelerator to handle
+                       except: return False
+               #no focus + selection: only allow accelerator to handle when a mod is used
+               return not event.state
 
        def _quit(self, window, event):
                """
@@ -177,14 +131,13 @@ class ActionHandler:
                # Initalize/Quit
                ##################################################
                if state == Actions.APPLICATION_INITIALIZE:
-                       for action in Actions.ACTIONS_LIST: action.set_sensitive(False) #set all actions disabled
+                       for action in Actions.get_all_actions(): action.set_sensitive(False) #set all actions disabled
                        # enable a select few actions
                        for action in (
                                Actions.APPLICATION_QUIT, Actions.FLOW_GRAPH_NEW,
                                Actions.FLOW_GRAPH_OPEN, Actions.FLOW_GRAPH_SAVE_AS,
                                Actions.FLOW_GRAPH_CLOSE, Actions.ABOUT_WINDOW_DISPLAY,
-                               Actions.HOTKEYS_WINDOW_DISPLAY, Actions.PREFS_WINDOW_DISPLAY,
-                               Actions.FLOW_GRAPH_SCREEN_CAPTURE,
+                               Actions.PREFS_WINDOW_DISPLAY, Actions.FLOW_GRAPH_SCREEN_CAPTURE,
                        ): Actions.get_action_from_name(action).set_sensitive(True)
                        if not self.init_file_paths and Preferences.restore_files():
                                self.init_file_paths = Preferences.files_open()
@@ -288,8 +241,6 @@ class ActionHandler:
                        self.get_flow_graph().update()
                elif state == Actions.ABOUT_WINDOW_DISPLAY:
                        AboutDialog()
-               elif state == Actions.HOTKEYS_WINDOW_DISPLAY:
-                       HotKeysDialog()
                ##################################################
                # Param Modifications
                ##################################################
index ddd2573a82c89a204e5e5d3f12afcf04389223c5..818995a32090f2f6c675a2cff68e616877e47665 100644 (file)
@@ -22,7 +22,7 @@ pygtk.require('2.0')
 import gtk
 
 ######################################################################################################
-# States
+# Action Names
 ######################################################################################################
 APPLICATION_INITIALIZE = 'app init'
 APPLICATION_QUIT = 'app quit'
@@ -56,13 +56,39 @@ FLOW_GRAPH_EXEC = 'flow graph exec'
 FLOW_GRAPH_KILL = 'flow graph kill'
 FLOW_GRAPH_SCREEN_CAPTURE = 'flow graph screen capture'
 ABOUT_WINDOW_DISPLAY = 'about window display'
-HOTKEYS_WINDOW_DISPLAY = 'hotkeys window display'
 PREFS_WINDOW_DISPLAY = 'prefs window display'
 
+######################################################################################################
+# Action Key Map
+######################################################################################################
+_actions_key_map = {
+       #action name: (key name, mask)
+       FLOW_GRAPH_NEW: ('n', gtk.gdk.CONTROL_MASK),
+       FLOW_GRAPH_OPEN: ('o', gtk.gdk.CONTROL_MASK),
+       FLOW_GRAPH_SAVE: ('s', gtk.gdk.CONTROL_MASK),
+       FLOW_GRAPH_CLOSE: ('w', gtk.gdk.CONTROL_MASK),
+       APPLICATION_QUIT: ('q', gtk.gdk.CONTROL_MASK),
+       FLOW_GRAPH_UNDO: ('z', gtk.gdk.CONTROL_MASK),
+       FLOW_GRAPH_REDO: ('y', gtk.gdk.CONTROL_MASK),
+       ELEMENT_DELETE: ('Delete', 0),
+       BLOCK_ROTATE_LEFT: ('Left', 0),
+       BLOCK_ROTATE_RIGHT: ('Right', 0),
+       BLOCK_PARAM_MODIFY: ('Return', 0),
+       BLOCK_ENABLE: ('e', 0),
+       BLOCK_DISABLE: ('d', 0),
+       BLOCK_CUT: ('x', gtk.gdk.CONTROL_MASK),
+       BLOCK_COPY: ('c', gtk.gdk.CONTROL_MASK),
+       BLOCK_PASTE: ('v', gtk.gdk.CONTROL_MASK),
+       FLOW_GRAPH_GEN: ('F5', 0),
+       FLOW_GRAPH_EXEC: ('F6', 0),
+       FLOW_GRAPH_KILL: ('F7', 0),
+       FLOW_GRAPH_SCREEN_CAPTURE: ('Print', 0),
+}
+
 ######################################################################################################
 # Actions
 ######################################################################################################
-ACTIONS_LIST = (
+_actions_list = (
        gtk.Action(FLOW_GRAPH_NEW, '_New', 'Create a new flow graph', 'gtk-new'),
        gtk.Action(FLOW_GRAPH_OPEN, '_Open', 'Open an existing flow graph', 'gtk-open'),
        gtk.Action(FLOW_GRAPH_SAVE, '_Save', 'Save the current flow graph', 'gtk-save'),
@@ -82,15 +108,14 @@ ACTIONS_LIST = (
        gtk.Action(BLOCK_PASTE, '_Paste', 'Paste', 'gtk-paste'),
        gtk.Action(PREFS_WINDOW_DISPLAY, '_Preferences', 'Configure Preferences', 'gtk-preferences'),
        gtk.Action(ABOUT_WINDOW_DISPLAY, '_About', 'About this program', 'gtk-about'),
-       gtk.Action(HOTKEYS_WINDOW_DISPLAY, '_HotKeys', 'Hot Keys', 'gtk-info'),
        gtk.Action(FLOW_GRAPH_GEN, '_Generate', 'Generate the flow graph', 'gtk-convert'),
        gtk.Action(FLOW_GRAPH_EXEC, '_Execute', 'Execute the flow graph', 'gtk-execute'),
        gtk.Action(FLOW_GRAPH_KILL, '_Kill', 'Kill the flow graph', 'gtk-stop'),
        gtk.Action(FLOW_GRAPH_SCREEN_CAPTURE, 'S_creen Capture', 'Create a screen capture of the flow graph', 'gtk-print'),
 )
+def get_all_actions(): return _actions_list
 
-ACTIONS_DICT = dict((action.get_name(), action) for action in ACTIONS_LIST)
-
+_actions_dict = dict((action.get_name(), action) for action in _actions_list)
 def get_action_from_name(action_name):
        """
        Retrieve the action from the action list.
@@ -99,5 +124,16 @@ def get_action_from_name(action_name):
        @throw KeyError bad action name
        @return a gtk action object
        """
-       if ACTIONS_DICT.has_key(action_name): return ACTIONS_DICT[action_name]
+       if _actions_dict.has_key(action_name): return _actions_dict[action_name]
        raise KeyError('Action Name: "%s" does not exist'%action_name)
+
+_accel_group = gtk.AccelGroup()
+def get_accel_group(): return _accel_group
+
+#load the actions key map
+#set the accelerator group, and accelerator path
+#register the key and mod with the accelerator path
+for action_name, (key_name, mod) in _actions_key_map.iteritems():
+       get_action_from_name(action_name).set_accel_group(get_accel_group())
+       get_action_from_name(action_name).set_accel_path('<main>/'+action_name)
+       gtk.accel_map_add_entry('<main>/'+action_name, gtk.gdk.keyval_from_name(key_name),mod)
index 915e5abec061c0295b540fde20fe7aa9dd0200f4..1416d4dd54b64f0ae7fc79088211d525755de169 100644 (file)
@@ -91,7 +91,6 @@ MENU_BAR_LIST = (
        ]),
        (gtk.Action('Help', '_Help', None, None), [
                Actions.ABOUT_WINDOW_DISPLAY,
-               Actions.HOTKEYS_WINDOW_DISPLAY,
        ]),
 )
 
index 5f278e343875bafe67117cecc8f7cd925dc39527..995fe46285853236a88e53af2a5ed866d9f8ca0e 100644 (file)
@@ -103,43 +103,3 @@ Achilleas Anastasopoulos -> trellis support
 -----""")
                self.run()
                self.destroy()
-
-class HotKeysDialog(gtk.Dialog):
-       """Display each action with the associated hotkey."""
-
-       def __init__(self):
-               """HotKeysDialog constructor."""
-               gtk.Dialog.__init__(self, buttons=('gtk-close', gtk.RESPONSE_CLOSE))
-               self.set_title('Hot Keys')
-               markup = ''
-               for action, hotkey in (
-                       ('New Flow Graph', 'Ctrl + n'),
-                       ('Open Flow Graph', 'Ctrl + o'),
-                       ('Save Flow Graph', 'Ctrl + s'),
-                       ('Close Flow Graph', 'Ctrl + q'),
-                       ('Cut Block', 'Ctrl + x'),
-                       ('Copy Block', 'Ctrl + c'),
-                       ('Paste Block', 'Ctrl + v'),
-                       ('Undo Change', 'Ctrl + z'),
-                       ('Redo Change', 'Ctrl + y'),
-                       ('Delete Element', 'Delete'),
-                       ('Modify Parameters', 'Enter'),
-                       ('Rotate Block', 'Right'),
-                       ('Rotate Block', 'Left'),
-                       ('Enable Block', 'e'),
-                       ('Disable Block', 'd'),
-                       ('Modify Data Type', 'Up'),
-                       ('Modify Data Type', 'Down'),
-                       ('Add a Port', '+'),
-                       ('Remove a Port', '-'),
-                       ('Flow Graph Generate', 'F5'),
-                       ('Flow Graph Execute', 'F6'),
-                       ('Flow Graph Kill', 'F7'),
-                       ('Screen Shot', 'PrintScreen'),
-               ): markup = '%s\n<b>%s:</b>%s'%(markup, action, hotkey.rjust(25-len(action), ' '))
-               label = gtk.Label()
-               label.set_markup('<tt>%s</tt>\n'%markup) #append newline
-               self.vbox.pack_start(label, False)
-               self.show_all()
-               self.run()
-               self.destroy()
index ce1eb274edf5b80108637400fa3231a2af351dfb..4c572c446f73471c4261f2a4b522b63defdeed3c 100644 (file)
@@ -22,7 +22,7 @@ from Constants import \
        NEW_FLOGRAPH_TITLE, REPORTS_WINDOW_HEIGHT
 from Actions import \
        APPLICATION_QUIT, FLOW_GRAPH_KILL, \
-       FLOW_GRAPH_SAVE
+       FLOW_GRAPH_SAVE, get_accel_group
 import pygtk
 pygtk.require('2.0')
 import gtk
@@ -55,6 +55,7 @@ class MainWindow(gtk.Window):
                hbox = gtk.HBox()
                self.add(vbox)
                #create the menu bar and toolbar
+               self.add_accel_group(get_accel_group())
                vbox.pack_start(Bars.MenuBar(), False)
                vbox.pack_start(Bars.Toolbar(), False)
                #setup scrolled window