--- a/eric6/Preferences/Shortcuts.py Mon Feb 01 10:38:43 2021 +0100 +++ b/eric6/Preferences/Shortcuts.py Tue Mar 02 17:12:08 2021 +0100 @@ -200,76 +200,100 @@ """ Module function to export the keyboard shortcuts for the defined QActions. - @param fn filename of the export file (string) + @param fn filename of the export file + @type str @param helpViewer reference to the help window object + @type WebBrowserWindow """ # let the plugin manager create on demand plugin objects pm = e5App().getObject("PluginManager") pm.initOnDemandPlugins() - f = QFile(fn) - if f.open(QIODevice.WriteOnly): - from E5XML.ShortcutsWriter import ShortcutsWriter - ShortcutsWriter(f).writeXML(helpViewer=helpViewer) - f.close() + if fn.endswith(".ekj"): + # new JSON based file + from .ShortcutsFile import ShortcutsFile + shortcutsFile = ShortcutsFile() + shortcutsFile.writeFile(fn, helpViewer) else: - E5MessageBox.critical( - None, - QCoreApplication.translate( - "Shortcuts", "Export Keyboard Shortcuts"), - QCoreApplication.translate( - "Shortcuts", - "<p>The keyboard shortcuts could not be written to file" - " <b>{0}</b>.</p>") - .format(fn)) + # old XML based file + f = QFile(fn) + if f.open(QIODevice.WriteOnly): + from E5XML.ShortcutsWriter import ShortcutsWriter + ShortcutsWriter(f).writeXML(helpViewer=helpViewer) + f.close() + else: + E5MessageBox.critical( + None, + QCoreApplication.translate( + "Shortcuts", "Export Keyboard Shortcuts"), + QCoreApplication.translate( + "Shortcuts", + "<p>The keyboard shortcuts file <b>{0}</b> could not" + " be written.</p>") + .format(fn)) def importShortcuts(fn, helpViewer=None): """ - Module function to import the keyboard shortcuts for the defined E5Actions. + Module function to import the keyboard shortcuts for the defined actions. - @param fn filename of the import file (string) + @param fn filename of the import file + @type str @param helpViewer reference to the help window object + @type WebBrowserWindow """ # let the plugin manager create on demand plugin objects pm = e5App().getObject("PluginManager") pm.initOnDemandPlugins() - f = QFile(fn) - if f.open(QIODevice.ReadOnly): - from E5XML.ShortcutsReader import ShortcutsReader - reader = ShortcutsReader(f) - reader.readXML() - f.close() - if not reader.hasError(): - shortcuts = reader.getShortcuts() + if fn.endswith(".ekj"): + # new JSON based file + from .ShortcutsFile import ShortcutsFile + shortcutsFile = ShortcutsFile() + shortcuts = shortcutsFile.readFile(fn) + if shortcuts: setActions(shortcuts, helpViewer=helpViewer) saveShortcuts() syncPreferences() else: - E5MessageBox.critical( - None, - QCoreApplication.translate( - "Shortcuts", "Import Keyboard Shortcuts"), - QCoreApplication.translate( - "Shortcuts", - "<p>The keyboard shortcuts could not be read from file" - " <b>{0}</b>.</p>") - .format(fn)) - return + # old XML based file + f = QFile(fn) + if f.open(QIODevice.ReadOnly): + from E5XML.ShortcutsReader import ShortcutsReader + reader = ShortcutsReader(f) + reader.readXML() + f.close() + if not reader.hasError(): + shortcuts = reader.getShortcuts() + setActions(shortcuts, helpViewer=helpViewer) + saveShortcuts() + syncPreferences() + else: + E5MessageBox.critical( + None, + QCoreApplication.translate( + "Shortcuts", "Import Keyboard Shortcuts"), + QCoreApplication.translate( + "Shortcuts", + "<p>The keyboard shortcuts file <b>{0}</b> could not be" + " read.</p>") + .format(fn)) -def __setAction(actions, sdict): +def __setAction(actions, shortcutsDict): """ - Private function to write a single keyboard shortcut to the settings. + Private function to set a single keyboard shortcut category shortcuts. - @param actions list of actions to set (list of E5Action) - @param sdict dictionary containg accelerator information for one category + @param actions list of actions to set + @type list of E5Action + @param shortcutsDict dictionary containing accelerator information for + one category + @type dict """ for act in actions: if act.objectName(): try: - accel, altAccel = sdict[act.objectName()] + accel, altAccel = shortcutsDict[act.objectName()] act.setShortcut(QKeySequence(accel)) act.setAlternateShortcut(QKeySequence(altAccel), removeEmpty=True) @@ -279,11 +303,13 @@ def setActions(shortcuts, helpViewer=None): """ - Module function to set actions based on new format shortcuts file. + Module function to set actions based on the imported shortcuts file. @param shortcuts dictionary containing the accelerator information - read from a XML file + read from a JSON or XML file + @type dict @param helpViewer reference to the help window object + @type WebBrowserWindow """ if helpViewer is None: if "Project" in shortcuts: