eric7/Preferences/Shortcuts.py

branch
eric7
changeset 8356
68ec9c3d4de5
parent 8351
7d13e08ddb3f
child 8358
144a6b854f70
equal deleted inserted replaced
8355:8a7677a63c8d 8356:68ec9c3d4de5
10 import contextlib 10 import contextlib
11 11
12 from PyQt6.QtCore import QFile, QIODevice, QCoreApplication 12 from PyQt6.QtCore import QFile, QIODevice, QCoreApplication
13 from PyQt6.QtGui import QKeySequence 13 from PyQt6.QtGui import QKeySequence
14 14
15 from E5Gui.E5Application import e5App 15 from E5Gui.EricApplication import ericApp
16 from E5Gui import E5MessageBox 16 from E5Gui import EricMessageBox
17 17
18 from Preferences import Prefs, syncPreferences 18 from Preferences import Prefs, syncPreferences
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 (E5Action) 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(
45 @param helpViewer reference to the help window object 45 @param helpViewer reference to the help window object
46 @param pluginName name of the plugin for which to load shortcuts 46 @param pluginName name of the plugin for which to load shortcuts
47 (string) 47 (string)
48 """ 48 """
49 if helpViewer is None and pluginName is None: 49 if helpViewer is None and pluginName is None:
50 for act in e5App().getObject("Project").getActions(): 50 for act in ericApp().getObject("Project").getActions():
51 __readShortcut(act, "Project", prefClass) 51 __readShortcut(act, "Project", prefClass)
52 52
53 for act in e5App().getObject("UserInterface").getActions('ui'): 53 for act in ericApp().getObject("UserInterface").getActions('ui'):
54 __readShortcut(act, "General", prefClass) 54 __readShortcut(act, "General", prefClass)
55 55
56 for act in e5App().getObject("UserInterface").getActions('wizards'): 56 for act in ericApp().getObject("UserInterface").getActions('wizards'):
57 __readShortcut(act, "Wizards", prefClass) 57 __readShortcut(act, "Wizards", prefClass)
58 58
59 for act in e5App().getObject("DebugUI").getActions(): 59 for act in ericApp().getObject("DebugUI").getActions():
60 __readShortcut(act, "Debug", prefClass) 60 __readShortcut(act, "Debug", prefClass)
61 61
62 for act in e5App().getObject("ViewManager").getActions('edit'): 62 for act in ericApp().getObject("ViewManager").getActions('edit'):
63 __readShortcut(act, "Edit", prefClass) 63 __readShortcut(act, "Edit", prefClass)
64 64
65 for act in e5App().getObject("ViewManager").getActions('file'): 65 for act in ericApp().getObject("ViewManager").getActions('file'):
66 __readShortcut(act, "File", prefClass) 66 __readShortcut(act, "File", prefClass)
67 67
68 for act in e5App().getObject("ViewManager").getActions('search'): 68 for act in ericApp().getObject("ViewManager").getActions('search'):
69 __readShortcut(act, "Search", prefClass) 69 __readShortcut(act, "Search", prefClass)
70 70
71 for act in e5App().getObject("ViewManager").getActions('view'): 71 for act in ericApp().getObject("ViewManager").getActions('view'):
72 __readShortcut(act, "View", prefClass) 72 __readShortcut(act, "View", prefClass)
73 73
74 for act in e5App().getObject("ViewManager").getActions('macro'): 74 for act in ericApp().getObject("ViewManager").getActions('macro'):
75 __readShortcut(act, "Macro", prefClass) 75 __readShortcut(act, "Macro", prefClass)
76 76
77 for act in e5App().getObject("ViewManager").getActions('bookmark'): 77 for act in ericApp().getObject("ViewManager").getActions('bookmark'):
78 __readShortcut(act, "Bookmarks", prefClass) 78 __readShortcut(act, "Bookmarks", prefClass)
79 79
80 for act in e5App().getObject("ViewManager").getActions('spelling'): 80 for act in ericApp().getObject("ViewManager").getActions('spelling'):
81 __readShortcut(act, "Spelling", prefClass) 81 __readShortcut(act, "Spelling", prefClass)
82 82
83 actions = e5App().getObject("ViewManager").getActions('window') 83 actions = ericApp().getObject("ViewManager").getActions('window')
84 if actions: 84 if actions:
85 for act in actions: 85 for act in actions:
86 __readShortcut(act, "Window", prefClass) 86 __readShortcut(act, "Window", prefClass)
87 87
88 for category, ref in e5App().getPluginObjects(): 88 for category, ref in ericApp().getPluginObjects():
89 if hasattr(ref, "getActions"): 89 if hasattr(ref, "getActions"):
90 actions = ref.getActions() 90 actions = ref.getActions()
91 for act in actions: 91 for act in actions:
92 __readShortcut(act, category, prefClass) 92 __readShortcut(act, category, prefClass)
93 93
96 for act in helpViewer.getActions(): 96 for act in helpViewer.getActions():
97 __readShortcut(act, helpViewerCategory, prefClass) 97 __readShortcut(act, helpViewerCategory, prefClass)
98 98
99 if pluginName is not None: 99 if pluginName is not None:
100 with contextlib.suppress(KeyError): 100 with contextlib.suppress(KeyError):
101 ref = e5App().getPluginObject(pluginName) 101 ref = ericApp().getPluginObject(pluginName)
102 if hasattr(ref, "getActions"): 102 if hasattr(ref, "getActions"):
103 actions = ref.getActions() 103 actions = ref.getActions()
104 for act in actions: 104 for act in actions:
105 __readShortcut(act, pluginName, prefClass) 105 __readShortcut(act, pluginName, prefClass)
106 106
107 107
108 def __saveShortcut(act, category, prefClass): 108 def __saveShortcut(act, category, prefClass):
109 """ 109 """
110 Private function to write a single keyboard shortcut to the settings. 110 Private function to write a single keyboard shortcut to the settings.
111 111
112 @param act reference to the action object (E5Action) 112 @param act reference to the action object (EricAction)
113 @param category category the action belongs to (string) 113 @param category category the action belongs to (string)
114 @param prefClass preferences class used as the storage area 114 @param prefClass preferences class used as the storage area
115 """ 115 """
116 if act.objectName(): 116 if act.objectName():
117 prefClass.settings.setValue( 117 prefClass.settings.setValue(
134 prefClass.settings.beginGroup("Shortcuts") 134 prefClass.settings.beginGroup("Shortcuts")
135 prefClass.settings.remove("") 135 prefClass.settings.remove("")
136 prefClass.settings.endGroup() 136 prefClass.settings.endGroup()
137 137
138 # step 2: save the various shortcuts 138 # step 2: save the various shortcuts
139 for act in e5App().getObject("Project").getActions(): 139 for act in ericApp().getObject("Project").getActions():
140 __saveShortcut(act, "Project", prefClass) 140 __saveShortcut(act, "Project", prefClass)
141 141
142 for act in e5App().getObject("UserInterface").getActions('ui'): 142 for act in ericApp().getObject("UserInterface").getActions('ui'):
143 __saveShortcut(act, "General", prefClass) 143 __saveShortcut(act, "General", prefClass)
144 144
145 for act in e5App().getObject("UserInterface").getActions('wizards'): 145 for act in ericApp().getObject("UserInterface").getActions('wizards'):
146 __saveShortcut(act, "Wizards", prefClass) 146 __saveShortcut(act, "Wizards", prefClass)
147 147
148 for act in e5App().getObject("DebugUI").getActions(): 148 for act in ericApp().getObject("DebugUI").getActions():
149 __saveShortcut(act, "Debug", prefClass) 149 __saveShortcut(act, "Debug", prefClass)
150 150
151 for act in e5App().getObject("ViewManager").getActions('edit'): 151 for act in ericApp().getObject("ViewManager").getActions('edit'):
152 __saveShortcut(act, "Edit", prefClass) 152 __saveShortcut(act, "Edit", prefClass)
153 153
154 for act in e5App().getObject("ViewManager").getActions('file'): 154 for act in ericApp().getObject("ViewManager").getActions('file'):
155 __saveShortcut(act, "File", prefClass) 155 __saveShortcut(act, "File", prefClass)
156 156
157 for act in e5App().getObject("ViewManager").getActions('search'): 157 for act in ericApp().getObject("ViewManager").getActions('search'):
158 __saveShortcut(act, "Search", prefClass) 158 __saveShortcut(act, "Search", prefClass)
159 159
160 for act in e5App().getObject("ViewManager").getActions('view'): 160 for act in ericApp().getObject("ViewManager").getActions('view'):
161 __saveShortcut(act, "View", prefClass) 161 __saveShortcut(act, "View", prefClass)
162 162
163 for act in e5App().getObject("ViewManager").getActions('macro'): 163 for act in ericApp().getObject("ViewManager").getActions('macro'):
164 __saveShortcut(act, "Macro", prefClass) 164 __saveShortcut(act, "Macro", prefClass)
165 165
166 for act in e5App().getObject("ViewManager").getActions('bookmark'): 166 for act in ericApp().getObject("ViewManager").getActions('bookmark'):
167 __saveShortcut(act, "Bookmarks", prefClass) 167 __saveShortcut(act, "Bookmarks", prefClass)
168 168
169 for act in e5App().getObject("ViewManager").getActions('spelling'): 169 for act in ericApp().getObject("ViewManager").getActions('spelling'):
170 __saveShortcut(act, "Spelling", prefClass) 170 __saveShortcut(act, "Spelling", prefClass)
171 171
172 actions = e5App().getObject("ViewManager").getActions('window') 172 actions = ericApp().getObject("ViewManager").getActions('window')
173 if actions: 173 if actions:
174 for act in actions: 174 for act in actions:
175 __saveShortcut(act, "Window", prefClass) 175 __saveShortcut(act, "Window", prefClass)
176 176
177 for category, ref in e5App().getPluginObjects(): 177 for category, ref in ericApp().getPluginObjects():
178 if hasattr(ref, "getActions"): 178 if hasattr(ref, "getActions"):
179 actions = ref.getActions() 179 actions = ref.getActions()
180 for act in actions: 180 for act in actions:
181 __saveShortcut(act, category, prefClass) 181 __saveShortcut(act, category, prefClass)
182 182
203 @type str 203 @type str
204 @param helpViewer reference to the help window object 204 @param helpViewer reference to the help window object
205 @type WebBrowserWindow 205 @type WebBrowserWindow
206 """ 206 """
207 # let the plugin manager create on demand plugin objects 207 # let the plugin manager create on demand plugin objects
208 pm = e5App().getObject("PluginManager") 208 pm = ericApp().getObject("PluginManager")
209 pm.initOnDemandPlugins() 209 pm.initOnDemandPlugins()
210 210
211 from .ShortcutsFile import ShortcutsFile 211 from .ShortcutsFile import ShortcutsFile
212 shortcutsFile = ShortcutsFile() 212 shortcutsFile = ShortcutsFile()
213 shortcutsFile.writeFile(fn, helpViewer) 213 shortcutsFile.writeFile(fn, helpViewer)
221 @type str 221 @type str
222 @param helpViewer reference to the help window object 222 @param helpViewer reference to the help window object
223 @type WebBrowserWindow 223 @type WebBrowserWindow
224 """ 224 """
225 # let the plugin manager create on demand plugin objects 225 # let the plugin manager create on demand plugin objects
226 pm = e5App().getObject("PluginManager") 226 pm = ericApp().getObject("PluginManager")
227 pm.initOnDemandPlugins() 227 pm.initOnDemandPlugins()
228 228
229 if fn.endswith(".ekj"): 229 if fn.endswith(".ekj"):
230 # new JSON based file 230 # new JSON based file
231 from .ShortcutsFile import ShortcutsFile 231 from .ShortcutsFile import ShortcutsFile
247 shortcuts = reader.getShortcuts() 247 shortcuts = reader.getShortcuts()
248 setActions(shortcuts, helpViewer=helpViewer) 248 setActions(shortcuts, helpViewer=helpViewer)
249 saveShortcuts() 249 saveShortcuts()
250 syncPreferences() 250 syncPreferences()
251 else: 251 else:
252 E5MessageBox.critical( 252 EricMessageBox.critical(
253 None, 253 None,
254 QCoreApplication.translate( 254 QCoreApplication.translate(
255 "Shortcuts", "Import Keyboard Shortcuts"), 255 "Shortcuts", "Import Keyboard Shortcuts"),
256 QCoreApplication.translate( 256 QCoreApplication.translate(
257 "Shortcuts", 257 "Shortcuts",
263 def __setAction(actions, shortcutsDict): 263 def __setAction(actions, shortcutsDict):
264 """ 264 """
265 Private function to set a single keyboard shortcut category shortcuts. 265 Private function to set a single keyboard shortcut category shortcuts.
266 266
267 @param actions list of actions to set 267 @param actions list of actions to set
268 @type list of E5Action 268 @type list of EricAction
269 @param shortcutsDict dictionary containing accelerator information for 269 @param shortcutsDict dictionary containing accelerator information for
270 one category 270 one category
271 @type dict 271 @type dict
272 """ 272 """
273 for act in actions: 273 for act in actions:
290 @type WebBrowserWindow 290 @type WebBrowserWindow
291 """ 291 """
292 if helpViewer is None: 292 if helpViewer is None:
293 if "Project" in shortcuts: 293 if "Project" in shortcuts:
294 __setAction( 294 __setAction(
295 e5App().getObject("Project").getActions(), 295 ericApp().getObject("Project").getActions(),
296 shortcuts["Project"]) 296 shortcuts["Project"])
297 297
298 if "General" in shortcuts: 298 if "General" in shortcuts:
299 __setAction( 299 __setAction(
300 e5App().getObject("UserInterface").getActions('ui'), 300 ericApp().getObject("UserInterface").getActions('ui'),
301 shortcuts["General"]) 301 shortcuts["General"])
302 302
303 if "Wizards" in shortcuts: 303 if "Wizards" in shortcuts:
304 __setAction( 304 __setAction(
305 e5App().getObject("UserInterface").getActions('wizards'), 305 ericApp().getObject("UserInterface").getActions('wizards'),
306 shortcuts["Wizards"]) 306 shortcuts["Wizards"])
307 307
308 if "Debug" in shortcuts: 308 if "Debug" in shortcuts:
309 __setAction( 309 __setAction(
310 e5App().getObject("DebugUI").getActions(), 310 ericApp().getObject("DebugUI").getActions(),
311 shortcuts["Debug"]) 311 shortcuts["Debug"])
312 312
313 if "Edit" in shortcuts: 313 if "Edit" in shortcuts:
314 __setAction( 314 __setAction(
315 e5App().getObject("ViewManager").getActions('edit'), 315 ericApp().getObject("ViewManager").getActions('edit'),
316 shortcuts["Edit"]) 316 shortcuts["Edit"])
317 317
318 if "File" in shortcuts: 318 if "File" in shortcuts:
319 __setAction( 319 __setAction(
320 e5App().getObject("ViewManager").getActions('file'), 320 ericApp().getObject("ViewManager").getActions('file'),
321 shortcuts["File"]) 321 shortcuts["File"])
322 322
323 if "Search" in shortcuts: 323 if "Search" in shortcuts:
324 __setAction( 324 __setAction(
325 e5App().getObject("ViewManager").getActions('search'), 325 ericApp().getObject("ViewManager").getActions('search'),
326 shortcuts["Search"]) 326 shortcuts["Search"])
327 327
328 if "View" in shortcuts: 328 if "View" in shortcuts:
329 __setAction( 329 __setAction(
330 e5App().getObject("ViewManager").getActions('view'), 330 ericApp().getObject("ViewManager").getActions('view'),
331 shortcuts["View"]) 331 shortcuts["View"])
332 332
333 if "Macro" in shortcuts: 333 if "Macro" in shortcuts:
334 __setAction( 334 __setAction(
335 e5App().getObject("ViewManager").getActions('macro'), 335 ericApp().getObject("ViewManager").getActions('macro'),
336 shortcuts["Macro"]) 336 shortcuts["Macro"])
337 337
338 if "Bookmarks" in shortcuts: 338 if "Bookmarks" in shortcuts:
339 __setAction( 339 __setAction(
340 e5App().getObject("ViewManager").getActions('bookmark'), 340 ericApp().getObject("ViewManager").getActions('bookmark'),
341 shortcuts["Bookmarks"]) 341 shortcuts["Bookmarks"])
342 342
343 if "Spelling" in shortcuts: 343 if "Spelling" in shortcuts:
344 __setAction( 344 __setAction(
345 e5App().getObject("ViewManager").getActions('spelling'), 345 ericApp().getObject("ViewManager").getActions('spelling'),
346 shortcuts["Spelling"]) 346 shortcuts["Spelling"])
347 347
348 if "Window" in shortcuts: 348 if "Window" in shortcuts:
349 actions = e5App().getObject("ViewManager").getActions('window') 349 actions = ericApp().getObject("ViewManager").getActions('window')
350 if actions: 350 if actions:
351 __setAction(actions, shortcuts["Window"]) 351 __setAction(actions, shortcuts["Window"])
352 352
353 for category, ref in e5App().getPluginObjects(): 353 for category, ref in ericApp().getPluginObjects():
354 if category in shortcuts and hasattr(ref, "getActions"): 354 if category in shortcuts and hasattr(ref, "getActions"):
355 actions = ref.getActions() 355 actions = ref.getActions()
356 __setAction(actions, shortcuts[category]) 356 __setAction(actions, shortcuts[category])
357 357
358 else: 358 else:

eric ide

mercurial