Preferences/Shortcuts.py

branch
maintenance
changeset 6646
51eefa621de4
parent 6645
ad476851d7e0
equal deleted inserted replaced
6603:77189681b787 6646:51eefa621de4
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 2
3 # Copyright (c) 2004 - 2018 Detlev Offenbach <detlev@die-offenbachs.de> 3 # Copyright (c) 2004 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 # 4 #
5 5
6 """ 6 """
7 Module implementing functions dealing with keyboard shortcuts. 7 Module implementing functions dealing with keyboard shortcuts.
8 """ 8 """
35 "Shortcuts/{0}/{1}/AltAccel".format(category, act.objectName())) 35 "Shortcuts/{0}/{1}/AltAccel".format(category, act.objectName()))
36 if accel is not None: 36 if accel is not None:
37 act.setAlternateShortcut(QKeySequence(accel), removeEmpty=True) 37 act.setAlternateShortcut(QKeySequence(accel), removeEmpty=True)
38 38
39 39
40 def readShortcuts(prefClass=Prefs, helpViewer=None, pluginName=None, 40 def readShortcuts(prefClass=Prefs, helpViewer=None, pluginName=None):
41 helpViewerCategory=""):
42 """ 41 """
43 Module function to read the keyboard shortcuts for the defined QActions. 42 Module function to read the keyboard shortcuts for the defined QActions.
44 43
45 @keyparam prefClass preferences class used as the storage area 44 @keyparam prefClass preferences class used as the storage area
46 @keyparam helpViewer reference to the help window object 45 @keyparam helpViewer reference to the help window object
47 @keyparam pluginName name of the plugin for which to load shortcuts 46 @keyparam pluginName name of the plugin for which to load shortcuts
48 (string) 47 (string)
49 @keyparam helpViewerCategory name of the help viewer category (string)
50 """ 48 """
51 if helpViewer is None and pluginName is None: 49 if helpViewer is None and pluginName is None:
52 for act in e5App().getObject("Project").getActions(): 50 for act in e5App().getObject("Project").getActions():
53 __readShortcut(act, "Project", prefClass) 51 __readShortcut(act, "Project", prefClass)
54 52
92 actions = ref.getActions() 90 actions = ref.getActions()
93 for act in actions: 91 for act in actions:
94 __readShortcut(act, category, prefClass) 92 __readShortcut(act, category, prefClass)
95 93
96 if helpViewer is not None: 94 if helpViewer is not None:
97 if not helpViewerCategory: 95 helpViewerCategory = helpViewer.getActionsCategory()
98 helpViewerCategory = "HelpViewer"
99 for act in helpViewer.getActions(): 96 for act in helpViewer.getActions():
100 __readShortcut(act, helpViewerCategory, prefClass) 97 __readShortcut(act, helpViewerCategory, prefClass)
101 98
102 if pluginName is not None: 99 if pluginName is not None:
103 try: 100 try:
126 prefClass.settings.setValue( 123 prefClass.settings.setValue(
127 "Shortcuts/{0}/{1}/AltAccel".format(category, act.objectName()), 124 "Shortcuts/{0}/{1}/AltAccel".format(category, act.objectName()),
128 act.alternateShortcut().toString()) 125 act.alternateShortcut().toString())
129 126
130 127
131 def saveShortcuts(prefClass=Prefs): 128 def saveShortcuts(prefClass=Prefs, helpViewer=None):
132 """ 129 """
133 Module function to write the keyboard shortcuts for the defined QActions. 130 Module function to write the keyboard shortcuts for the defined QActions.
134 131
135 @param prefClass preferences class used as the storage area 132 @param prefClass preferences class used as the storage area
136 """ 133 @keyparam helpViewer reference to the help window object
137 # step 1: clear all previously saved shortcuts 134 """
138 prefClass.settings.beginGroup("Shortcuts") 135 if helpViewer is None:
139 prefClass.settings.remove("") 136 # step 1: clear all previously saved shortcuts
140 prefClass.settings.endGroup() 137 prefClass.settings.beginGroup("Shortcuts")
141 138 prefClass.settings.remove("")
142 # step 2: save the various shortcuts 139 prefClass.settings.endGroup()
143 for act in e5App().getObject("Project").getActions(): 140
144 __saveShortcut(act, "Project", prefClass) 141 # step 2: save the various shortcuts
145 142 for act in e5App().getObject("Project").getActions():
146 for act in e5App().getObject("UserInterface").getActions('ui'): 143 __saveShortcut(act, "Project", prefClass)
147 __saveShortcut(act, "General", prefClass) 144
148 145 for act in e5App().getObject("UserInterface").getActions('ui'):
149 for act in e5App().getObject("UserInterface").getActions('wizards'): 146 __saveShortcut(act, "General", prefClass)
150 __saveShortcut(act, "Wizards", prefClass) 147
151 148 for act in e5App().getObject("UserInterface").getActions('wizards'):
152 for act in e5App().getObject("DebugUI").getActions(): 149 __saveShortcut(act, "Wizards", prefClass)
153 __saveShortcut(act, "Debug", prefClass) 150
154 151 for act in e5App().getObject("DebugUI").getActions():
155 for act in e5App().getObject("ViewManager").getActions('edit'): 152 __saveShortcut(act, "Debug", prefClass)
156 __saveShortcut(act, "Edit", prefClass) 153
157 154 for act in e5App().getObject("ViewManager").getActions('edit'):
158 for act in e5App().getObject("ViewManager").getActions('file'): 155 __saveShortcut(act, "Edit", prefClass)
159 __saveShortcut(act, "File", prefClass) 156
160 157 for act in e5App().getObject("ViewManager").getActions('file'):
161 for act in e5App().getObject("ViewManager").getActions('search'): 158 __saveShortcut(act, "File", prefClass)
162 __saveShortcut(act, "Search", prefClass) 159
163 160 for act in e5App().getObject("ViewManager").getActions('search'):
164 for act in e5App().getObject("ViewManager").getActions('view'): 161 __saveShortcut(act, "Search", prefClass)
165 __saveShortcut(act, "View", prefClass) 162
166 163 for act in e5App().getObject("ViewManager").getActions('view'):
167 for act in e5App().getObject("ViewManager").getActions('macro'): 164 __saveShortcut(act, "View", prefClass)
168 __saveShortcut(act, "Macro", prefClass) 165
169 166 for act in e5App().getObject("ViewManager").getActions('macro'):
170 for act in e5App().getObject("ViewManager").getActions('bookmark'): 167 __saveShortcut(act, "Macro", prefClass)
171 __saveShortcut(act, "Bookmarks", prefClass) 168
172 169 for act in e5App().getObject("ViewManager").getActions('bookmark'):
173 for act in e5App().getObject("ViewManager").getActions('spelling'): 170 __saveShortcut(act, "Bookmarks", prefClass)
174 __saveShortcut(act, "Spelling", prefClass) 171
175 172 for act in e5App().getObject("ViewManager").getActions('spelling'):
176 actions = e5App().getObject("ViewManager").getActions('window') 173 __saveShortcut(act, "Spelling", prefClass)
177 if actions: 174
178 for act in actions: 175 actions = e5App().getObject("ViewManager").getActions('window')
179 __saveShortcut(act, "Window", prefClass) 176 if actions:
180
181 for category, ref in e5App().getPluginObjects():
182 if hasattr(ref, "getActions"):
183 actions = ref.getActions()
184 for act in actions: 177 for act in actions:
185 __saveShortcut(act, category, prefClass) 178 __saveShortcut(act, "Window", prefClass)
186 179
187 try: 180 for category, ref in e5App().getPluginObjects():
188 for act in e5App().getObject("DummyHelpViewer").getActions(): 181 if hasattr(ref, "getActions"):
189 __saveShortcut(act, "HelpViewer", prefClass) 182 actions = ref.getActions()
190 except KeyError: 183 for act in actions:
191 # no QtWebKit available 184 __saveShortcut(act, category, prefClass)
192 pass 185
193 186 else:
194 187 helpViewerCategory = helpViewer.getActionsCategory()
195 def exportShortcuts(fn): 188
189 # step 1: clear all previously saved shortcuts
190 prefClass.settings.beginGroup(
191 "Shortcuts/{0}".format(helpViewerCategory)
192 )
193 prefClass.settings.remove("")
194 prefClass.settings.endGroup()
195
196 # step 2: save the shortcuts
197 for act in helpViewer.getActions():
198 __saveShortcut(act, helpViewerCategory, prefClass)
199
200
201 def exportShortcuts(fn, helpViewer=None):
196 """ 202 """
197 Module function to export the keyboard shortcuts for the defined QActions. 203 Module function to export the keyboard shortcuts for the defined QActions.
198 204
199 @param fn filename of the export file (string) 205 @param fn filename of the export file (string)
206 @param helpViewer reference to the help window object
200 """ 207 """
201 # let the plugin manager create on demand plugin objects 208 # let the plugin manager create on demand plugin objects
202 pm = e5App().getObject("PluginManager") 209 pm = e5App().getObject("PluginManager")
203 pm.initOnDemandPlugins() 210 pm.initOnDemandPlugins()
204 211
205 f = QFile(fn) 212 f = QFile(fn)
206 if f.open(QIODevice.WriteOnly): 213 if f.open(QIODevice.WriteOnly):
207 from E5XML.ShortcutsWriter import ShortcutsWriter 214 from E5XML.ShortcutsWriter import ShortcutsWriter
208 ShortcutsWriter(f).writeXML() 215 ShortcutsWriter(f).writeXML(helpViewer=helpViewer)
209 f.close() 216 f.close()
210 else: 217 else:
211 E5MessageBox.critical( 218 E5MessageBox.critical(
212 None, 219 None,
213 QCoreApplication.translate( 220 QCoreApplication.translate(
217 "<p>The keyboard shortcuts could not be written to file" 224 "<p>The keyboard shortcuts could not be written to file"
218 " <b>{0}</b>.</p>") 225 " <b>{0}</b>.</p>")
219 .format(fn)) 226 .format(fn))
220 227
221 228
222 def importShortcuts(fn): 229 def importShortcuts(fn, helpViewer=None):
223 """ 230 """
224 Module function to import the keyboard shortcuts for the defined E5Actions. 231 Module function to import the keyboard shortcuts for the defined E5Actions.
225 232
226 @param fn filename of the import file (string) 233 @param fn filename of the import file (string)
234 @param helpViewer reference to the help window object
227 """ 235 """
228 # let the plugin manager create on demand plugin objects 236 # let the plugin manager create on demand plugin objects
229 pm = e5App().getObject("PluginManager") 237 pm = e5App().getObject("PluginManager")
230 pm.initOnDemandPlugins() 238 pm.initOnDemandPlugins()
231 239
235 reader = ShortcutsReader(f) 243 reader = ShortcutsReader(f)
236 reader.readXML() 244 reader.readXML()
237 f.close() 245 f.close()
238 if not reader.hasError(): 246 if not reader.hasError():
239 shortcuts = reader.getShortcuts() 247 shortcuts = reader.getShortcuts()
240 setActions(shortcuts) 248 setActions(shortcuts, helpViewer=helpViewer)
241 saveShortcuts() 249 saveShortcuts()
242 syncPreferences() 250 syncPreferences()
243 else: 251 else:
244 E5MessageBox.critical( 252 E5MessageBox.critical(
245 None, 253 None,
269 removeEmpty=True) 277 removeEmpty=True)
270 except KeyError: 278 except KeyError:
271 pass 279 pass
272 280
273 281
274 def setActions(shortcuts): 282 def setActions(shortcuts, helpViewer=None):
275 """ 283 """
276 Module function to set actions based on new format shortcuts file. 284 Module function to set actions based on new format shortcuts file.
277 285
278 @param shortcuts dictionary containing the accelerator information 286 @param shortcuts dictionary containing the accelerator information
279 read from a XML file 287 read from a XML file
280 """ 288 @param helpViewer reference to the help window object
281 if "Project" in shortcuts: 289 """
282 __setAction(e5App().getObject("Project").getActions(), 290 if helpViewer is None:
283 shortcuts["Project"]) 291 if "Project" in shortcuts:
284 292 __setAction(
285 if "General" in shortcuts: 293 e5App().getObject("Project").getActions(),
286 __setAction(e5App().getObject("UserInterface").getActions('ui'), 294 shortcuts["Project"])
287 shortcuts["General"]) 295
288 296 if "General" in shortcuts:
289 if "Wizards" in shortcuts: 297 __setAction(
290 __setAction(e5App().getObject("UserInterface").getActions('wizards'), 298 e5App().getObject("UserInterface").getActions('ui'),
291 shortcuts["Wizards"]) 299 shortcuts["General"])
292 300
293 if "Debug" in shortcuts: 301 if "Wizards" in shortcuts:
294 __setAction(e5App().getObject("DebugUI").getActions(), 302 __setAction(
295 shortcuts["Debug"]) 303 e5App().getObject("UserInterface").getActions('wizards'),
296 304 shortcuts["Wizards"])
297 if "Edit" in shortcuts: 305
298 __setAction(e5App().getObject("ViewManager").getActions('edit'), 306 if "Debug" in shortcuts:
299 shortcuts["Edit"]) 307 __setAction(
300 308 e5App().getObject("DebugUI").getActions(),
301 if "File" in shortcuts: 309 shortcuts["Debug"])
302 __setAction(e5App().getObject("ViewManager").getActions('file'), 310
303 shortcuts["File"]) 311 if "Edit" in shortcuts:
304 312 __setAction(
305 if "Search" in shortcuts: 313 e5App().getObject("ViewManager").getActions('edit'),
306 __setAction(e5App().getObject("ViewManager").getActions('search'), 314 shortcuts["Edit"])
307 shortcuts["Search"]) 315
308 316 if "File" in shortcuts:
309 if "View" in shortcuts: 317 __setAction(
310 __setAction(e5App().getObject("ViewManager").getActions('view'), 318 e5App().getObject("ViewManager").getActions('file'),
311 shortcuts["View"]) 319 shortcuts["File"])
312 320
313 if "Macro" in shortcuts: 321 if "Search" in shortcuts:
314 __setAction(e5App().getObject("ViewManager").getActions('macro'), 322 __setAction(
315 shortcuts["Macro"]) 323 e5App().getObject("ViewManager").getActions('search'),
316 324 shortcuts["Search"])
317 if "Bookmarks" in shortcuts: 325
318 __setAction(e5App().getObject("ViewManager").getActions('bookmark'), 326 if "View" in shortcuts:
319 shortcuts["Bookmarks"]) 327 __setAction(
320 328 e5App().getObject("ViewManager").getActions('view'),
321 if "Spelling" in shortcuts: 329 shortcuts["View"])
322 __setAction(e5App().getObject("ViewManager").getActions('spelling'), 330
323 shortcuts["Spelling"]) 331 if "Macro" in shortcuts:
324 332 __setAction(
325 if "Window" in shortcuts: 333 e5App().getObject("ViewManager").getActions('macro'),
326 actions = e5App().getObject("ViewManager").getActions('window') 334 shortcuts["Macro"])
327 if actions: 335
328 __setAction(actions, shortcuts["Window"]) 336 if "Bookmarks" in shortcuts:
329 337 __setAction(
330 for category, ref in e5App().getPluginObjects(): 338 e5App().getObject("ViewManager").getActions('bookmark'),
331 if category in shortcuts and hasattr(ref, "getActions"): 339 shortcuts["Bookmarks"])
332 actions = ref.getActions() 340
333 __setAction(actions, shortcuts[category]) 341 if "Spelling" in shortcuts:
334 342 __setAction(
335 try: 343 e5App().getObject("ViewManager").getActions('spelling'),
336 if "HelpViewer" in shortcuts: 344 shortcuts["Spelling"])
337 __setAction(e5App().getObject("DummyHelpViewer").getActions(), 345
338 shortcuts["HelpViewer"]) 346 if "Window" in shortcuts:
339 except KeyError: 347 actions = e5App().getObject("ViewManager").getActions('window')
340 # no QtWebKit available 348 if actions:
341 pass 349 __setAction(actions, shortcuts["Window"])
350
351 for category, ref in e5App().getPluginObjects():
352 if category in shortcuts and hasattr(ref, "getActions"):
353 actions = ref.getActions()
354 __setAction(actions, shortcuts[category])
355
356 else:
357 category = helpViewer.getActionsCategory()
358 if category in shortcuts:
359 __setAction(helpViewer.getActions(), shortcuts[category])

eric ide

mercurial