src/eric7/Preferences/Shortcuts.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
19 19
20 20
21 def __readShortcut(act, category, prefClass): 21 def __readShortcut(act, category, prefClass):
22 """ 22 """
23 Private function to read a single keyboard shortcut from the settings. 23 Private function to read a single keyboard shortcut from the settings.
24 24
25 @param act reference to the action object (EricAction) 25 @param act reference to the action object (EricAction)
26 @param category category the action belongs to (string) 26 @param category category the action belongs to (string)
27 @param prefClass preferences class used as the storage area 27 @param prefClass preferences class used as the storage area
28 """ 28 """
29 if act.objectName(): 29 if act.objectName():
30 accel = prefClass.settings.value( 30 accel = prefClass.settings.value(
31 "Shortcuts/{0}/{1}/Accel".format(category, act.objectName())) 31 "Shortcuts/{0}/{1}/Accel".format(category, act.objectName())
32 )
32 if accel is not None: 33 if accel is not None:
33 act.setShortcut(QKeySequence(accel)) 34 act.setShortcut(QKeySequence(accel))
34 accel = prefClass.settings.value( 35 accel = prefClass.settings.value(
35 "Shortcuts/{0}/{1}/AltAccel".format(category, act.objectName())) 36 "Shortcuts/{0}/{1}/AltAccel".format(category, act.objectName())
37 )
36 if accel is not None: 38 if accel is not None:
37 act.setAlternateShortcut(QKeySequence(accel), removeEmpty=True) 39 act.setAlternateShortcut(QKeySequence(accel), removeEmpty=True)
38 40
39 41
40 def readShortcuts(prefClass=Prefs, helpViewer=None, pluginName=None): 42 def readShortcuts(prefClass=Prefs, helpViewer=None, pluginName=None):
41 """ 43 """
42 Module function to read the keyboard shortcuts for the defined QActions. 44 Module function to read the keyboard shortcuts for the defined QActions.
43 45
44 @param prefClass preferences class used as the storage area 46 @param prefClass preferences class used as the storage area
45 @param helpViewer reference to the help window object 47 @param helpViewer reference to the help window object
46 @param pluginName name of the plugin for which to load shortcuts 48 @param pluginName name of the plugin for which to load shortcuts
47 (string) 49 (string)
48 """ 50 """
49 if helpViewer is None and pluginName is None: 51 if helpViewer is None and pluginName is None:
50 for act in ericApp().getObject("Project").getActions(): 52 for act in ericApp().getObject("Project").getActions():
51 __readShortcut(act, "Project", prefClass) 53 __readShortcut(act, "Project", prefClass)
52 54
53 for act in ericApp().getObject("UserInterface").getActions('ui'): 55 for act in ericApp().getObject("UserInterface").getActions("ui"):
54 __readShortcut(act, "General", prefClass) 56 __readShortcut(act, "General", prefClass)
55 57
56 for act in ericApp().getObject("UserInterface").getActions('wizards'): 58 for act in ericApp().getObject("UserInterface").getActions("wizards"):
57 __readShortcut(act, "Wizards", prefClass) 59 __readShortcut(act, "Wizards", prefClass)
58 60
59 for act in ericApp().getObject("DebugUI").getActions(): 61 for act in ericApp().getObject("DebugUI").getActions():
60 __readShortcut(act, "Debug", prefClass) 62 __readShortcut(act, "Debug", prefClass)
61 63
62 for act in ericApp().getObject("ViewManager").getActions('edit'): 64 for act in ericApp().getObject("ViewManager").getActions("edit"):
63 __readShortcut(act, "Edit", prefClass) 65 __readShortcut(act, "Edit", prefClass)
64 66
65 for act in ericApp().getObject("ViewManager").getActions('file'): 67 for act in ericApp().getObject("ViewManager").getActions("file"):
66 __readShortcut(act, "File", prefClass) 68 __readShortcut(act, "File", prefClass)
67 69
68 for act in ericApp().getObject("ViewManager").getActions('search'): 70 for act in ericApp().getObject("ViewManager").getActions("search"):
69 __readShortcut(act, "Search", prefClass) 71 __readShortcut(act, "Search", prefClass)
70 72
71 for act in ericApp().getObject("ViewManager").getActions('view'): 73 for act in ericApp().getObject("ViewManager").getActions("view"):
72 __readShortcut(act, "View", prefClass) 74 __readShortcut(act, "View", prefClass)
73 75
74 for act in ericApp().getObject("ViewManager").getActions('macro'): 76 for act in ericApp().getObject("ViewManager").getActions("macro"):
75 __readShortcut(act, "Macro", prefClass) 77 __readShortcut(act, "Macro", prefClass)
76 78
77 for act in ericApp().getObject("ViewManager").getActions('bookmark'): 79 for act in ericApp().getObject("ViewManager").getActions("bookmark"):
78 __readShortcut(act, "Bookmarks", prefClass) 80 __readShortcut(act, "Bookmarks", prefClass)
79 81
80 for act in ericApp().getObject("ViewManager").getActions('spelling'): 82 for act in ericApp().getObject("ViewManager").getActions("spelling"):
81 __readShortcut(act, "Spelling", prefClass) 83 __readShortcut(act, "Spelling", prefClass)
82 84
83 actions = ericApp().getObject("ViewManager").getActions('window') 85 actions = ericApp().getObject("ViewManager").getActions("window")
84 if actions: 86 if actions:
85 for act in actions: 87 for act in actions:
86 __readShortcut(act, "Window", prefClass) 88 __readShortcut(act, "Window", prefClass)
87 89
88 for category, ref in ericApp().getPluginObjects(): 90 for category, ref in ericApp().getPluginObjects():
89 if hasattr(ref, "getActions"): 91 if hasattr(ref, "getActions"):
90 actions = ref.getActions() 92 actions = ref.getActions()
91 for act in actions: 93 for act in actions:
92 __readShortcut(act, category, prefClass) 94 __readShortcut(act, category, prefClass)
93 95
94 if helpViewer is not None: 96 if helpViewer is not None:
95 helpViewerCategory = helpViewer.getActionsCategory() 97 helpViewerCategory = helpViewer.getActionsCategory()
96 for act in helpViewer.getActions(): 98 for act in helpViewer.getActions():
97 __readShortcut(act, helpViewerCategory, prefClass) 99 __readShortcut(act, helpViewerCategory, prefClass)
98 100
99 if pluginName is not None: 101 if pluginName is not None:
100 with contextlib.suppress(KeyError): 102 with contextlib.suppress(KeyError):
101 ref = ericApp().getPluginObject(pluginName) 103 ref = ericApp().getPluginObject(pluginName)
102 if hasattr(ref, "getActions"): 104 if hasattr(ref, "getActions"):
103 actions = ref.getActions() 105 actions = ref.getActions()
104 for act in actions: 106 for act in actions:
105 __readShortcut(act, pluginName, prefClass) 107 __readShortcut(act, pluginName, prefClass)
106 108
107 109
108 def __saveShortcut(act, category, prefClass): 110 def __saveShortcut(act, category, prefClass):
109 """ 111 """
110 Private function to write a single keyboard shortcut to the settings. 112 Private function to write a single keyboard shortcut to the settings.
111 113
112 @param act reference to the action object (EricAction) 114 @param act reference to the action object (EricAction)
113 @param category category the action belongs to (string) 115 @param category category the action belongs to (string)
114 @param prefClass preferences class used as the storage area 116 @param prefClass preferences class used as the storage area
115 """ 117 """
116 if act.objectName(): 118 if act.objectName():
117 prefClass.settings.setValue( 119 prefClass.settings.setValue(
118 "Shortcuts/{0}/{1}/Accel".format(category, act.objectName()), 120 "Shortcuts/{0}/{1}/Accel".format(category, act.objectName()),
119 act.shortcut().toString()) 121 act.shortcut().toString(),
122 )
120 prefClass.settings.setValue( 123 prefClass.settings.setValue(
121 "Shortcuts/{0}/{1}/AltAccel".format(category, act.objectName()), 124 "Shortcuts/{0}/{1}/AltAccel".format(category, act.objectName()),
122 act.alternateShortcut().toString()) 125 act.alternateShortcut().toString(),
126 )
123 127
124 128
125 def saveShortcuts(prefClass=Prefs, helpViewer=None): 129 def saveShortcuts(prefClass=Prefs, helpViewer=None):
126 """ 130 """
127 Module function to write the keyboard shortcuts for the defined QActions. 131 Module function to write the keyboard shortcuts for the defined QActions.
128 132
129 @param prefClass preferences class used as the storage area 133 @param prefClass preferences class used as the storage area
130 @param helpViewer reference to the help window object 134 @param helpViewer reference to the help window object
131 """ 135 """
132 if helpViewer is None: 136 if helpViewer is None:
133 # step 1: clear all previously saved shortcuts 137 # step 1: clear all previously saved shortcuts
134 prefClass.settings.beginGroup("Shortcuts") 138 prefClass.settings.beginGroup("Shortcuts")
135 prefClass.settings.remove("") 139 prefClass.settings.remove("")
136 prefClass.settings.endGroup() 140 prefClass.settings.endGroup()
137 141
138 # step 2: save the various shortcuts 142 # step 2: save the various shortcuts
139 for act in ericApp().getObject("Project").getActions(): 143 for act in ericApp().getObject("Project").getActions():
140 __saveShortcut(act, "Project", prefClass) 144 __saveShortcut(act, "Project", prefClass)
141 145
142 for act in ericApp().getObject("UserInterface").getActions('ui'): 146 for act in ericApp().getObject("UserInterface").getActions("ui"):
143 __saveShortcut(act, "General", prefClass) 147 __saveShortcut(act, "General", prefClass)
144 148
145 for act in ericApp().getObject("UserInterface").getActions('wizards'): 149 for act in ericApp().getObject("UserInterface").getActions("wizards"):
146 __saveShortcut(act, "Wizards", prefClass) 150 __saveShortcut(act, "Wizards", prefClass)
147 151
148 for act in ericApp().getObject("DebugUI").getActions(): 152 for act in ericApp().getObject("DebugUI").getActions():
149 __saveShortcut(act, "Debug", prefClass) 153 __saveShortcut(act, "Debug", prefClass)
150 154
151 for act in ericApp().getObject("ViewManager").getActions('edit'): 155 for act in ericApp().getObject("ViewManager").getActions("edit"):
152 __saveShortcut(act, "Edit", prefClass) 156 __saveShortcut(act, "Edit", prefClass)
153 157
154 for act in ericApp().getObject("ViewManager").getActions('file'): 158 for act in ericApp().getObject("ViewManager").getActions("file"):
155 __saveShortcut(act, "File", prefClass) 159 __saveShortcut(act, "File", prefClass)
156 160
157 for act in ericApp().getObject("ViewManager").getActions('search'): 161 for act in ericApp().getObject("ViewManager").getActions("search"):
158 __saveShortcut(act, "Search", prefClass) 162 __saveShortcut(act, "Search", prefClass)
159 163
160 for act in ericApp().getObject("ViewManager").getActions('view'): 164 for act in ericApp().getObject("ViewManager").getActions("view"):
161 __saveShortcut(act, "View", prefClass) 165 __saveShortcut(act, "View", prefClass)
162 166
163 for act in ericApp().getObject("ViewManager").getActions('macro'): 167 for act in ericApp().getObject("ViewManager").getActions("macro"):
164 __saveShortcut(act, "Macro", prefClass) 168 __saveShortcut(act, "Macro", prefClass)
165 169
166 for act in ericApp().getObject("ViewManager").getActions('bookmark'): 170 for act in ericApp().getObject("ViewManager").getActions("bookmark"):
167 __saveShortcut(act, "Bookmarks", prefClass) 171 __saveShortcut(act, "Bookmarks", prefClass)
168 172
169 for act in ericApp().getObject("ViewManager").getActions('spelling'): 173 for act in ericApp().getObject("ViewManager").getActions("spelling"):
170 __saveShortcut(act, "Spelling", prefClass) 174 __saveShortcut(act, "Spelling", prefClass)
171 175
172 actions = ericApp().getObject("ViewManager").getActions('window') 176 actions = ericApp().getObject("ViewManager").getActions("window")
173 if actions: 177 if actions:
174 for act in actions: 178 for act in actions:
175 __saveShortcut(act, "Window", prefClass) 179 __saveShortcut(act, "Window", prefClass)
176 180
177 for category, ref in ericApp().getPluginObjects(): 181 for category, ref in ericApp().getPluginObjects():
178 if hasattr(ref, "getActions"): 182 if hasattr(ref, "getActions"):
179 actions = ref.getActions() 183 actions = ref.getActions()
180 for act in actions: 184 for act in actions:
181 __saveShortcut(act, category, prefClass) 185 __saveShortcut(act, category, prefClass)
182 186
183 else: 187 else:
184 helpViewerCategory = helpViewer.getActionsCategory() 188 helpViewerCategory = helpViewer.getActionsCategory()
185 189
186 # step 1: clear all previously saved shortcuts 190 # step 1: clear all previously saved shortcuts
187 prefClass.settings.beginGroup( 191 prefClass.settings.beginGroup("Shortcuts/{0}".format(helpViewerCategory))
188 "Shortcuts/{0}".format(helpViewerCategory)
189 )
190 prefClass.settings.remove("") 192 prefClass.settings.remove("")
191 prefClass.settings.endGroup() 193 prefClass.settings.endGroup()
192 194
193 # step 2: save the shortcuts 195 # step 2: save the shortcuts
194 for act in helpViewer.getActions(): 196 for act in helpViewer.getActions():
195 __saveShortcut(act, helpViewerCategory, prefClass) 197 __saveShortcut(act, helpViewerCategory, prefClass)
196 198
197 199
198 def exportShortcuts(fn, helpViewer=None): 200 def exportShortcuts(fn, helpViewer=None):
199 """ 201 """
200 Module function to export the keyboard shortcuts for the defined QActions. 202 Module function to export the keyboard shortcuts for the defined QActions.
201 203
202 @param fn filename of the export file 204 @param fn filename of the export file
203 @type str 205 @type str
204 @param helpViewer reference to the help window object 206 @param helpViewer reference to the help window object
205 @type WebBrowserWindow 207 @type WebBrowserWindow
206 """ 208 """
207 # let the plugin manager create on demand plugin objects 209 # let the plugin manager create on demand plugin objects
208 pm = ericApp().getObject("PluginManager") 210 pm = ericApp().getObject("PluginManager")
209 pm.initOnDemandPlugins() 211 pm.initOnDemandPlugins()
210 212
211 from .ShortcutsFile import ShortcutsFile 213 from .ShortcutsFile import ShortcutsFile
214
212 shortcutsFile = ShortcutsFile() 215 shortcutsFile = ShortcutsFile()
213 shortcutsFile.writeFile(fn, helpViewer) 216 shortcutsFile.writeFile(fn, helpViewer)
214 217
215 218
216 def importShortcuts(fn, helpViewer=None): 219 def importShortcuts(fn, helpViewer=None):
217 """ 220 """
218 Module function to import the keyboard shortcuts for the defined actions. 221 Module function to import the keyboard shortcuts for the defined actions.
219 222
220 @param fn filename of the import file 223 @param fn filename of the import file
221 @type str 224 @type str
222 @param helpViewer reference to the help window object 225 @param helpViewer reference to the help window object
223 @type WebBrowserWindow 226 @type WebBrowserWindow
224 """ 227 """
225 # let the plugin manager create on demand plugin objects 228 # let the plugin manager create on demand plugin objects
226 pm = ericApp().getObject("PluginManager") 229 pm = ericApp().getObject("PluginManager")
227 pm.initOnDemandPlugins() 230 pm.initOnDemandPlugins()
228 231
229 if fn.endswith(".ekj"): 232 if fn.endswith(".ekj"):
230 # new JSON based file 233 # new JSON based file
231 from .ShortcutsFile import ShortcutsFile 234 from .ShortcutsFile import ShortcutsFile
235
232 shortcutsFile = ShortcutsFile() 236 shortcutsFile = ShortcutsFile()
233 shortcuts = shortcutsFile.readFile(fn) 237 shortcuts = shortcutsFile.readFile(fn)
234 if shortcuts: 238 if shortcuts:
235 setActions(shortcuts, helpViewer=helpViewer) 239 setActions(shortcuts, helpViewer=helpViewer)
236 saveShortcuts() 240 saveShortcuts()
238 else: 242 else:
239 # old XML based file 243 # old XML based file
240 f = QFile(fn) 244 f = QFile(fn)
241 if f.open(QIODevice.OpenModeFlag.ReadOnly): 245 if f.open(QIODevice.OpenModeFlag.ReadOnly):
242 from EricXML.ShortcutsReader import ShortcutsReader 246 from EricXML.ShortcutsReader import ShortcutsReader
247
243 reader = ShortcutsReader(f) 248 reader = ShortcutsReader(f)
244 reader.readXML() 249 reader.readXML()
245 f.close() 250 f.close()
246 if not reader.hasError(): 251 if not reader.hasError():
247 shortcuts = reader.getShortcuts() 252 shortcuts = reader.getShortcuts()
249 saveShortcuts() 254 saveShortcuts()
250 syncPreferences() 255 syncPreferences()
251 else: 256 else:
252 EricMessageBox.critical( 257 EricMessageBox.critical(
253 None, 258 None,
254 QCoreApplication.translate( 259 QCoreApplication.translate("Shortcuts", "Import Keyboard Shortcuts"),
255 "Shortcuts", "Import Keyboard Shortcuts"),
256 QCoreApplication.translate( 260 QCoreApplication.translate(
257 "Shortcuts", 261 "Shortcuts",
258 "<p>The keyboard shortcuts file <b>{0}</b> could not be" 262 "<p>The keyboard shortcuts file <b>{0}</b> could not be"
259 " read.</p>") 263 " read.</p>",
260 .format(fn)) 264 ).format(fn),
265 )
261 266
262 267
263 def __setAction(actions, shortcutsDict): 268 def __setAction(actions, shortcutsDict):
264 """ 269 """
265 Private function to set a single keyboard shortcut category shortcuts. 270 Private function to set a single keyboard shortcut category shortcuts.
266 271
267 @param actions list of actions to set 272 @param actions list of actions to set
268 @type list of EricAction 273 @type list of EricAction
269 @param shortcutsDict dictionary containing accelerator information for 274 @param shortcutsDict dictionary containing accelerator information for
270 one category 275 one category
271 @type dict 276 @type dict
273 for act in actions: 278 for act in actions:
274 if act.objectName(): 279 if act.objectName():
275 with contextlib.suppress(KeyError): 280 with contextlib.suppress(KeyError):
276 accel, altAccel = shortcutsDict[act.objectName()] 281 accel, altAccel = shortcutsDict[act.objectName()]
277 act.setShortcut(QKeySequence(accel)) 282 act.setShortcut(QKeySequence(accel))
278 act.setAlternateShortcut(QKeySequence(altAccel), 283 act.setAlternateShortcut(QKeySequence(altAccel), removeEmpty=True)
279 removeEmpty=True)
280 284
281 285
282 def setActions(shortcuts, helpViewer=None): 286 def setActions(shortcuts, helpViewer=None):
283 """ 287 """
284 Module function to set actions based on the imported shortcuts file. 288 Module function to set actions based on the imported shortcuts file.
285 289
286 @param shortcuts dictionary containing the accelerator information 290 @param shortcuts dictionary containing the accelerator information
287 read from a JSON or XML file 291 read from a JSON or XML file
288 @type dict 292 @type dict
289 @param helpViewer reference to the help window object 293 @param helpViewer reference to the help window object
290 @type WebBrowserWindow 294 @type WebBrowserWindow
291 """ 295 """
292 if helpViewer is None: 296 if helpViewer is None:
293 if "Project" in shortcuts: 297 if "Project" in shortcuts:
294 __setAction( 298 __setAction(
295 ericApp().getObject("Project").getActions(), 299 ericApp().getObject("Project").getActions(), shortcuts["Project"]
296 shortcuts["Project"]) 300 )
297 301
298 if "General" in shortcuts: 302 if "General" in shortcuts:
299 __setAction( 303 __setAction(
300 ericApp().getObject("UserInterface").getActions('ui'), 304 ericApp().getObject("UserInterface").getActions("ui"),
301 shortcuts["General"]) 305 shortcuts["General"],
302 306 )
307
303 if "Wizards" in shortcuts: 308 if "Wizards" in shortcuts:
304 __setAction( 309 __setAction(
305 ericApp().getObject("UserInterface").getActions('wizards'), 310 ericApp().getObject("UserInterface").getActions("wizards"),
306 shortcuts["Wizards"]) 311 shortcuts["Wizards"],
307 312 )
313
308 if "Debug" in shortcuts: 314 if "Debug" in shortcuts:
309 __setAction( 315 __setAction(ericApp().getObject("DebugUI").getActions(), shortcuts["Debug"])
310 ericApp().getObject("DebugUI").getActions(), 316
311 shortcuts["Debug"])
312
313 if "Edit" in shortcuts: 317 if "Edit" in shortcuts:
314 __setAction( 318 __setAction(
315 ericApp().getObject("ViewManager").getActions('edit'), 319 ericApp().getObject("ViewManager").getActions("edit"), shortcuts["Edit"]
316 shortcuts["Edit"]) 320 )
317 321
318 if "File" in shortcuts: 322 if "File" in shortcuts:
319 __setAction( 323 __setAction(
320 ericApp().getObject("ViewManager").getActions('file'), 324 ericApp().getObject("ViewManager").getActions("file"), shortcuts["File"]
321 shortcuts["File"]) 325 )
322 326
323 if "Search" in shortcuts: 327 if "Search" in shortcuts:
324 __setAction( 328 __setAction(
325 ericApp().getObject("ViewManager").getActions('search'), 329 ericApp().getObject("ViewManager").getActions("search"),
326 shortcuts["Search"]) 330 shortcuts["Search"],
327 331 )
332
328 if "View" in shortcuts: 333 if "View" in shortcuts:
329 __setAction( 334 __setAction(
330 ericApp().getObject("ViewManager").getActions('view'), 335 ericApp().getObject("ViewManager").getActions("view"), shortcuts["View"]
331 shortcuts["View"]) 336 )
332 337
333 if "Macro" in shortcuts: 338 if "Macro" in shortcuts:
334 __setAction( 339 __setAction(
335 ericApp().getObject("ViewManager").getActions('macro'), 340 ericApp().getObject("ViewManager").getActions("macro"),
336 shortcuts["Macro"]) 341 shortcuts["Macro"],
337 342 )
343
338 if "Bookmarks" in shortcuts: 344 if "Bookmarks" in shortcuts:
339 __setAction( 345 __setAction(
340 ericApp().getObject("ViewManager").getActions('bookmark'), 346 ericApp().getObject("ViewManager").getActions("bookmark"),
341 shortcuts["Bookmarks"]) 347 shortcuts["Bookmarks"],
342 348 )
349
343 if "Spelling" in shortcuts: 350 if "Spelling" in shortcuts:
344 __setAction( 351 __setAction(
345 ericApp().getObject("ViewManager").getActions('spelling'), 352 ericApp().getObject("ViewManager").getActions("spelling"),
346 shortcuts["Spelling"]) 353 shortcuts["Spelling"],
347 354 )
355
348 if "Window" in shortcuts: 356 if "Window" in shortcuts:
349 actions = ericApp().getObject("ViewManager").getActions('window') 357 actions = ericApp().getObject("ViewManager").getActions("window")
350 if actions: 358 if actions:
351 __setAction(actions, shortcuts["Window"]) 359 __setAction(actions, shortcuts["Window"])
352 360
353 for category, ref in ericApp().getPluginObjects(): 361 for category, ref in ericApp().getPluginObjects():
354 if category in shortcuts and hasattr(ref, "getActions"): 362 if category in shortcuts and hasattr(ref, "getActions"):
355 actions = ref.getActions() 363 actions = ref.getActions()
356 __setAction(actions, shortcuts[category]) 364 __setAction(actions, shortcuts[category])
357 365
358 else: 366 else:
359 category = helpViewer.getActionsCategory() 367 category = helpViewer.getActionsCategory()
360 if category in shortcuts: 368 if category in shortcuts:
361 __setAction(helpViewer.getActions(), shortcuts[category]) 369 __setAction(helpViewer.getActions(), shortcuts[category])

eric ide

mercurial