Preferences/Shortcuts.py

changeset 6625
a67fee7bc09c
parent 6048
82ad8ec9548c
child 6630
bddd12f27a4c
--- a/Preferences/Shortcuts.py	Wed Dec 12 19:43:53 2018 +0100
+++ b/Preferences/Shortcuts.py	Wed Dec 12 19:52:24 2018 +0100
@@ -37,8 +37,7 @@
             act.setAlternateShortcut(QKeySequence(accel), removeEmpty=True)
 
 
-def readShortcuts(prefClass=Prefs, helpViewer=None, pluginName=None,
-                  helpViewerCategory=""):
+def readShortcuts(prefClass=Prefs, helpViewer=None, pluginName=None):
     """
     Module function to read the keyboard shortcuts for the defined QActions.
     
@@ -46,7 +45,6 @@
     @keyparam helpViewer reference to the help window object
     @keyparam pluginName name of the plugin for which to load shortcuts
         (string)
-    @keyparam helpViewerCategory name of the help viewer category (string)
     """
     if helpViewer is None and pluginName is None:
         for act in e5App().getObject("Project").getActions():
@@ -94,8 +92,7 @@
                     __readShortcut(act, category, prefClass)
     
     if helpViewer is not None:
-        if not helpViewerCategory:
-            helpViewerCategory = "HelpViewer"
+        helpViewerCategory = helpViewer.getActionsCategory()
         for act in helpViewer.getActions():
             __readShortcut(act, helpViewerCategory, prefClass)
     
@@ -128,75 +125,94 @@
             act.alternateShortcut().toString())
 
 
-def saveShortcuts(prefClass=Prefs):
+def saveShortcuts(prefClass=Prefs, helpViewer=None):
     """
     Module function to write the keyboard shortcuts for the defined QActions.
     
     @param prefClass preferences class used as the storage area
+    @keyparam helpViewer reference to the help window object
     """
-    # step 1: clear all previously saved shortcuts
-    prefClass.settings.beginGroup("Shortcuts")
-    prefClass.settings.remove("")
-    prefClass.settings.endGroup()
-    
-    # step 2: save the various shortcuts
-    for act in e5App().getObject("Project").getActions():
-        __saveShortcut(act, "Project", prefClass)
-    
-    for act in e5App().getObject("UserInterface").getActions('ui'):
-        __saveShortcut(act, "General", prefClass)
-    
-    for act in e5App().getObject("UserInterface").getActions('wizards'):
-        __saveShortcut(act, "Wizards", prefClass)
-    
-    for act in e5App().getObject("DebugUI").getActions():
-        __saveShortcut(act, "Debug", prefClass)
-    
-    for act in e5App().getObject("ViewManager").getActions('edit'):
-        __saveShortcut(act, "Edit", prefClass)
-    
-    for act in e5App().getObject("ViewManager").getActions('file'):
-        __saveShortcut(act, "File", prefClass)
-    
-    for act in e5App().getObject("ViewManager").getActions('search'):
-        __saveShortcut(act, "Search", prefClass)
+    if helpViewer is None:
+        # step 1: clear all previously saved shortcuts
+        prefClass.settings.beginGroup("Shortcuts")
+        prefClass.settings.remove("")
+        prefClass.settings.endGroup()
+        
+        # step 2: save the various shortcuts
+        for act in e5App().getObject("Project").getActions():
+            __saveShortcut(act, "Project", prefClass)
+        
+        for act in e5App().getObject("UserInterface").getActions('ui'):
+            __saveShortcut(act, "General", prefClass)
+        
+        for act in e5App().getObject("UserInterface").getActions('wizards'):
+            __saveShortcut(act, "Wizards", prefClass)
+        
+        for act in e5App().getObject("DebugUI").getActions():
+            __saveShortcut(act, "Debug", prefClass)
+        
+        for act in e5App().getObject("ViewManager").getActions('edit'):
+            __saveShortcut(act, "Edit", prefClass)
+        
+        for act in e5App().getObject("ViewManager").getActions('file'):
+            __saveShortcut(act, "File", prefClass)
+        
+        for act in e5App().getObject("ViewManager").getActions('search'):
+            __saveShortcut(act, "Search", prefClass)
+        
+        for act in e5App().getObject("ViewManager").getActions('view'):
+            __saveShortcut(act, "View", prefClass)
+        
+        for act in e5App().getObject("ViewManager").getActions('macro'):
+            __saveShortcut(act, "Macro", prefClass)
+        
+        for act in e5App().getObject("ViewManager").getActions('bookmark'):
+            __saveShortcut(act, "Bookmarks", prefClass)
+        
+        for act in e5App().getObject("ViewManager").getActions('spelling'):
+            __saveShortcut(act, "Spelling", prefClass)
+        
+        actions = e5App().getObject("ViewManager").getActions('window')
+        if actions:
+            for act in actions:
+                __saveShortcut(act, "Window", prefClass)
+        
+        for category, ref in e5App().getPluginObjects():
+            if hasattr(ref, "getActions"):
+                actions = ref.getActions()
+                for act in actions:
+                    __saveShortcut(act, category, prefClass)
+        
+        try:
+            category = e5App().getObject("DummyHelpViewer")\
+                .getActionsCategory()
+            for act in e5App().getObject("DummyHelpViewer").getActions():
+                __saveShortcut(act, category, prefClass)
+        except KeyError:
+            # no QtWebKit available
+            pass
     
-    for act in e5App().getObject("ViewManager").getActions('view'):
-        __saveShortcut(act, "View", prefClass)
-    
-    for act in e5App().getObject("ViewManager").getActions('macro'):
-        __saveShortcut(act, "Macro", prefClass)
-    
-    for act in e5App().getObject("ViewManager").getActions('bookmark'):
-        __saveShortcut(act, "Bookmarks", prefClass)
-    
-    for act in e5App().getObject("ViewManager").getActions('spelling'):
-        __saveShortcut(act, "Spelling", prefClass)
-    
-    actions = e5App().getObject("ViewManager").getActions('window')
-    if actions:
-        for act in actions:
-            __saveShortcut(act, "Window", prefClass)
-    
-    for category, ref in e5App().getPluginObjects():
-        if hasattr(ref, "getActions"):
-            actions = ref.getActions()
-            for act in actions:
-                __saveShortcut(act, category, prefClass)
-    
-    try:
-        for act in e5App().getObject("DummyHelpViewer").getActions():
-            __saveShortcut(act, "HelpViewer", prefClass)
-    except KeyError:
-        # no QtWebKit available
-        pass
+    else:
+        helpViewerCategory = helpViewer.getActionsCategory()
+        
+        # step 1: clear all previously saved shortcuts
+        prefClass.settings.beginGroup(
+            "Shortcuts/{0}".format(helpViewerCategory)
+        )
+        prefClass.settings.remove("")
+        prefClass.settings.endGroup()
+        
+        # step 2: save the shortcuts
+        for act in helpViewer.getActions():
+            __saveShortcut(act, helpViewerCategory, prefClass)
 
 
-def exportShortcuts(fn):
+def exportShortcuts(fn, helpViewer=None):
     """
     Module function to export the keyboard shortcuts for the defined QActions.
     
     @param fn filename of the export file (string)
+    @param helpViewer reference to the help window object
     """
     # let the plugin manager create on demand plugin objects
     pm = e5App().getObject("PluginManager")
@@ -205,7 +221,7 @@
     f = QFile(fn)
     if f.open(QIODevice.WriteOnly):
         from E5XML.ShortcutsWriter import ShortcutsWriter
-        ShortcutsWriter(f).writeXML()
+        ShortcutsWriter(f).writeXML(helpViewer=helpViewer)
         f.close()
     else:
         E5MessageBox.critical(
@@ -219,11 +235,12 @@
             .format(fn))
 
 
-def importShortcuts(fn):
+def importShortcuts(fn, helpViewer=None):
     """
     Module function to import the keyboard shortcuts for the defined E5Actions.
     
     @param fn filename of the import file (string)
+    @param helpViewer reference to the help window object
     """
     # let the plugin manager create on demand plugin objects
     pm = e5App().getObject("PluginManager")
@@ -237,7 +254,7 @@
         f.close()
         if not reader.hasError():
             shortcuts = reader.getShortcuts()
-            setActions(shortcuts)
+            setActions(shortcuts, helpViewer=helpViewer)
             saveShortcuts()
             syncPreferences()
     else:
@@ -271,71 +288,89 @@
                 pass
 
 
-def setActions(shortcuts):
+def setActions(shortcuts, helpViewer=None):
     """
     Module function to set actions based on new format shortcuts file.
     
     @param shortcuts dictionary containing the accelerator information
         read from a XML file
+    @param helpViewer reference to the help window object
     """
-    if "Project" in shortcuts:
-        __setAction(e5App().getObject("Project").getActions(),
-                    shortcuts["Project"])
-    
-    if "General" in shortcuts:
-        __setAction(e5App().getObject("UserInterface").getActions('ui'),
-                    shortcuts["General"])
-    
-    if "Wizards" in shortcuts:
-        __setAction(e5App().getObject("UserInterface").getActions('wizards'),
-                    shortcuts["Wizards"])
-    
-    if "Debug" in shortcuts:
-        __setAction(e5App().getObject("DebugUI").getActions(),
-                    shortcuts["Debug"])
-    
-    if "Edit" in shortcuts:
-        __setAction(e5App().getObject("ViewManager").getActions('edit'),
-                    shortcuts["Edit"])
-    
-    if "File" in shortcuts:
-        __setAction(e5App().getObject("ViewManager").getActions('file'),
-                    shortcuts["File"])
-    
-    if "Search" in shortcuts:
-        __setAction(e5App().getObject("ViewManager").getActions('search'),
-                    shortcuts["Search"])
+    if helpViewer is None:
+        if "Project" in shortcuts:
+            __setAction(
+                e5App().getObject("Project").getActions(),
+                shortcuts["Project"])
+        
+        if "General" in shortcuts:
+            __setAction(
+                e5App().getObject("UserInterface").getActions('ui'),
+                shortcuts["General"])
+        
+        if "Wizards" in shortcuts:
+            __setAction(
+                e5App().getObject("UserInterface").getActions('wizards'),
+                shortcuts["Wizards"])
+        
+        if "Debug" in shortcuts:
+            __setAction(
+                e5App().getObject("DebugUI").getActions(),
+                shortcuts["Debug"])
+        
+        if "Edit" in shortcuts:
+            __setAction(
+                e5App().getObject("ViewManager").getActions('edit'),
+                shortcuts["Edit"])
+        
+        if "File" in shortcuts:
+            __setAction(
+                e5App().getObject("ViewManager").getActions('file'),
+                shortcuts["File"])
+        
+        if "Search" in shortcuts:
+            __setAction(
+                e5App().getObject("ViewManager").getActions('search'),
+                shortcuts["Search"])
+        
+        if "View" in shortcuts:
+            __setAction(
+                e5App().getObject("ViewManager").getActions('view'),
+                shortcuts["View"])
+        
+        if "Macro" in shortcuts:
+            __setAction(
+                e5App().getObject("ViewManager").getActions('macro'),
+                shortcuts["Macro"])
+        
+        if "Bookmarks" in shortcuts:
+            __setAction(
+                e5App().getObject("ViewManager").getActions('bookmark'),
+                shortcuts["Bookmarks"])
+        
+        if "Spelling" in shortcuts:
+            __setAction(
+                e5App().getObject("ViewManager").getActions('spelling'),
+                shortcuts["Spelling"])
+        
+        if "Window" in shortcuts:
+            actions = e5App().getObject("ViewManager").getActions('window')
+            if actions:
+                __setAction(actions, shortcuts["Window"])
+        
+        for category, ref in e5App().getPluginObjects():
+            if category in shortcuts and hasattr(ref, "getActions"):
+                actions = ref.getActions()
+                __setAction(actions, shortcuts[category])
+        
+        try:
+            if "HelpViewer" in shortcuts:
+                __setAction(e5App().getObject("DummyHelpViewer").getActions(),
+                            shortcuts["HelpViewer"])
+        except KeyError:
+            # no QtWebKit available
+            pass
     
-    if "View" in shortcuts:
-        __setAction(e5App().getObject("ViewManager").getActions('view'),
-                    shortcuts["View"])
-    
-    if "Macro" in shortcuts:
-        __setAction(e5App().getObject("ViewManager").getActions('macro'),
-                    shortcuts["Macro"])
-    
-    if "Bookmarks" in shortcuts:
-        __setAction(e5App().getObject("ViewManager").getActions('bookmark'),
-                    shortcuts["Bookmarks"])
-    
-    if "Spelling" in shortcuts:
-        __setAction(e5App().getObject("ViewManager").getActions('spelling'),
-                    shortcuts["Spelling"])
-    
-    if "Window" in shortcuts:
-        actions = e5App().getObject("ViewManager").getActions('window')
-        if actions:
-            __setAction(actions, shortcuts["Window"])
-    
-    for category, ref in e5App().getPluginObjects():
-        if category in shortcuts and hasattr(ref, "getActions"):
-            actions = ref.getActions()
-            __setAction(actions, shortcuts[category])
-    
-    try:
-        if "HelpViewer" in shortcuts:
-            __setAction(e5App().getObject("DummyHelpViewer").getActions(),
-                        shortcuts["HelpViewer"])
-    except KeyError:
-        # no QtWebKit available
-        pass
+    else:
+        category = helpViewer.getActionsCategory()
+        if category in shortcuts:
+            __setAction(helpViewer.getActions(), shortcuts[category])

eric ide

mercurial