src/eric7/Preferences/ShortcutsFile.py

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

eric ide

mercurial