eric6/Preferences/ShortcutsFile.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 8019
5fb467ac4233
child 8218
7c09585bd960
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

8019
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a class representing the shortcuts JSON file.
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import json
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import time
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import typing
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 from PyQt5.QtCore import QObject
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 from E5Gui import E5MessageBox
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 from E5Gui.E5OverrideCursor import E5OverridenCursor
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 from E5Gui.E5Application import e5App
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 import Preferences
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 HelpViewer = typing.TypeVar("WebBrowserWindow")
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 class ShortcutsFile(QObject):
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 Class representing the shortcuts JSON file.
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 """
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 def __init__(self, parent: QObject = None):
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 """
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 Constructor
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 @param parent reference to the parent object (defaults to None)
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 @type QObject (optional)
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 """
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 super(ShortcutsFile, self).__init__(parent)
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 def __addActionsToDict(self, category: str, actions: list,
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 actionsDict: dict):
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 """
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 Private method to add a list of actions to the actions dictionary.
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 @param category category of the actions
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 @type str
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 @param actions list of actions
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 @type list of QAction
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 @param actionsDict reference to the actions dictionary to be modified
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 @type dict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 """
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 if actions:
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 if category not in actionsDict:
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 actionsDict[category] = {}
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 for act in actions:
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 if act.objectName():
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 # shortcuts are only exported, if their objectName is set
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 actionsDict[category][act.objectName()] = (
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 act.shortcut().toString(),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 act.alternateShortcut().toString()
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 def writeFile(self, filename: str, helpViewer: HelpViewer = None) -> bool:
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 """
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 Public method to write the shortcuts data to a shortcuts JSON file.
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 @param filename name of the shortcuts file
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 @type str
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 @param helpViewer reference to the help window object
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 @type WebBrowserWindow
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 @return flag indicating a successful write
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 @rtype bool
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 """
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 actionsDict = {}
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 # step 1: collect all the shortcuts
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 if helpViewer is None:
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 "Project",
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 e5App().getObject("Project").getActions(),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 "General",
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 e5App().getObject("UserInterface").getActions('ui'),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 "Wizards",
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 e5App().getObject("UserInterface").getActions('wizards'),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 "Debug",
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 e5App().getObject("DebugUI").getActions(),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 "Edit",
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 e5App().getObject("ViewManager").getActions('edit'),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 "File",
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 e5App().getObject("ViewManager").getActions('file'),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 "Search",
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 e5App().getObject("ViewManager").getActions('search'),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 "View",
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 e5App().getObject("ViewManager").getActions('view'),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 "Macro",
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 e5App().getObject("ViewManager").getActions('macro'),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 "Bookmarks",
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 e5App().getObject("ViewManager").getActions('bookmark'),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 "Spelling",
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 e5App().getObject("ViewManager").getActions('spelling'),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 "Window",
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 e5App().getObject("ViewManager").getActions('window'),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 for category, ref in e5App().getPluginObjects():
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 if hasattr(ref, "getActions"):
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 category, ref.getActions(), actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 else:
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 self.__addActionsToDict(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 helpViewer.getActionsCategory(),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 helpViewer.getActions(),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 # step 2: assemble the data structure to be written
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 shortcutsDict = {}
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 # step 2.0: header
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 shortcutsDict["header"] = {
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 "comment": "eric keyboard shortcuts file",
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 "saved": time.strftime('%Y-%m-%d, %H:%M:%S'),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 "author": Preferences.getUser("Email"),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 }
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 # step 2.1: keyboard shortcuts
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 shortcutsDict["shortcuts"] = actionsDict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 try:
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 jsonString = json.dumps(shortcutsDict, indent=2)
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 with open(filename, "w") as f:
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164 f.write(jsonString)
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 except (TypeError, EnvironmentError) as err:
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 with E5OverridenCursor():
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 E5MessageBox.critical(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 None,
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 self.tr("Export Keyboard Shortcuts"),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 self.tr(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 "<p>The keyboard shortcuts file <b>{0}</b> could not"
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 " be written.</p><p>Reason: {1}</p>"
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 ).format(filename, str(err))
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 return False
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 return True
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 def readFile(self, filename: str) -> bool:
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 """
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 Public method to read the shortcuts data from a shortcuts JSON file.
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 @param filename name of the shortcuts file
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 @type str
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 @return Dictionary of dictionaries of shortcuts. The keys of the
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 dictionary are the shortcuts categories, the values are
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 dictionaries. These dictionaries have the shortcut name as their
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 key and a tuple of accelerators as their value.
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 @rtype dict
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 """
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 try:
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 with open(filename, "r") as f:
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 jsonString = f.read()
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 shortcutsDict = json.loads(jsonString)
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 except (EnvironmentError, json.JSONDecodeError) as err:
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 E5MessageBox.critical(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 None,
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 self.tr("Import Keyboard Shortcuts"),
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 self.tr(
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 "<p>The keyboard shortcuts file <b>{0}</b> could not be"
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 " read.</p><p>Reason: {1}</p>"
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 ).format(filename, str(err))
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 )
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 return {}
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205
5fb467ac4233 Implemented the JSON based keyboard shortcuts files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 return shortcutsDict["shortcuts"]

eric ide

mercurial