Preferences/Shortcuts.py

changeset 6625
a67fee7bc09c
parent 6048
82ad8ec9548c
child 6630
bddd12f27a4c
equal deleted inserted replaced
6624:9718755def80 6625:a67fee7bc09c
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:
178 __saveShortcut(act, "Window", prefClass)
179
180 for category, ref in e5App().getPluginObjects():
181 if hasattr(ref, "getActions"):
182 actions = ref.getActions()
183 for act in actions:
184 __saveShortcut(act, category, prefClass)
185
186 try:
187 category = e5App().getObject("DummyHelpViewer")\
188 .getActionsCategory()
189 for act in e5App().getObject("DummyHelpViewer").getActions():
185 __saveShortcut(act, category, prefClass) 190 __saveShortcut(act, category, prefClass)
186 191 except KeyError:
187 try: 192 # no QtWebKit available
188 for act in e5App().getObject("DummyHelpViewer").getActions(): 193 pass
189 __saveShortcut(act, "HelpViewer", prefClass) 194
190 except KeyError: 195 else:
191 # no QtWebKit available 196 helpViewerCategory = helpViewer.getActionsCategory()
192 pass 197
193 198 # step 1: clear all previously saved shortcuts
194 199 prefClass.settings.beginGroup(
195 def exportShortcuts(fn): 200 "Shortcuts/{0}".format(helpViewerCategory)
201 )
202 prefClass.settings.remove("")
203 prefClass.settings.endGroup()
204
205 # step 2: save the shortcuts
206 for act in helpViewer.getActions():
207 __saveShortcut(act, helpViewerCategory, prefClass)
208
209
210 def exportShortcuts(fn, helpViewer=None):
196 """ 211 """
197 Module function to export the keyboard shortcuts for the defined QActions. 212 Module function to export the keyboard shortcuts for the defined QActions.
198 213
199 @param fn filename of the export file (string) 214 @param fn filename of the export file (string)
215 @param helpViewer reference to the help window object
200 """ 216 """
201 # let the plugin manager create on demand plugin objects 217 # let the plugin manager create on demand plugin objects
202 pm = e5App().getObject("PluginManager") 218 pm = e5App().getObject("PluginManager")
203 pm.initOnDemandPlugins() 219 pm.initOnDemandPlugins()
204 220
205 f = QFile(fn) 221 f = QFile(fn)
206 if f.open(QIODevice.WriteOnly): 222 if f.open(QIODevice.WriteOnly):
207 from E5XML.ShortcutsWriter import ShortcutsWriter 223 from E5XML.ShortcutsWriter import ShortcutsWriter
208 ShortcutsWriter(f).writeXML() 224 ShortcutsWriter(f).writeXML(helpViewer=helpViewer)
209 f.close() 225 f.close()
210 else: 226 else:
211 E5MessageBox.critical( 227 E5MessageBox.critical(
212 None, 228 None,
213 QCoreApplication.translate( 229 QCoreApplication.translate(
217 "<p>The keyboard shortcuts could not be written to file" 233 "<p>The keyboard shortcuts could not be written to file"
218 " <b>{0}</b>.</p>") 234 " <b>{0}</b>.</p>")
219 .format(fn)) 235 .format(fn))
220 236
221 237
222 def importShortcuts(fn): 238 def importShortcuts(fn, helpViewer=None):
223 """ 239 """
224 Module function to import the keyboard shortcuts for the defined E5Actions. 240 Module function to import the keyboard shortcuts for the defined E5Actions.
225 241
226 @param fn filename of the import file (string) 242 @param fn filename of the import file (string)
243 @param helpViewer reference to the help window object
227 """ 244 """
228 # let the plugin manager create on demand plugin objects 245 # let the plugin manager create on demand plugin objects
229 pm = e5App().getObject("PluginManager") 246 pm = e5App().getObject("PluginManager")
230 pm.initOnDemandPlugins() 247 pm.initOnDemandPlugins()
231 248
235 reader = ShortcutsReader(f) 252 reader = ShortcutsReader(f)
236 reader.readXML() 253 reader.readXML()
237 f.close() 254 f.close()
238 if not reader.hasError(): 255 if not reader.hasError():
239 shortcuts = reader.getShortcuts() 256 shortcuts = reader.getShortcuts()
240 setActions(shortcuts) 257 setActions(shortcuts, helpViewer=helpViewer)
241 saveShortcuts() 258 saveShortcuts()
242 syncPreferences() 259 syncPreferences()
243 else: 260 else:
244 E5MessageBox.critical( 261 E5MessageBox.critical(
245 None, 262 None,
269 removeEmpty=True) 286 removeEmpty=True)
270 except KeyError: 287 except KeyError:
271 pass 288 pass
272 289
273 290
274 def setActions(shortcuts): 291 def setActions(shortcuts, helpViewer=None):
275 """ 292 """
276 Module function to set actions based on new format shortcuts file. 293 Module function to set actions based on new format shortcuts file.
277 294
278 @param shortcuts dictionary containing the accelerator information 295 @param shortcuts dictionary containing the accelerator information
279 read from a XML file 296 read from a XML file
280 """ 297 @param helpViewer reference to the help window object
281 if "Project" in shortcuts: 298 """
282 __setAction(e5App().getObject("Project").getActions(), 299 if helpViewer is None:
283 shortcuts["Project"]) 300 if "Project" in shortcuts:
284 301 __setAction(
285 if "General" in shortcuts: 302 e5App().getObject("Project").getActions(),
286 __setAction(e5App().getObject("UserInterface").getActions('ui'), 303 shortcuts["Project"])
287 shortcuts["General"]) 304
288 305 if "General" in shortcuts:
289 if "Wizards" in shortcuts: 306 __setAction(
290 __setAction(e5App().getObject("UserInterface").getActions('wizards'), 307 e5App().getObject("UserInterface").getActions('ui'),
291 shortcuts["Wizards"]) 308 shortcuts["General"])
292 309
293 if "Debug" in shortcuts: 310 if "Wizards" in shortcuts:
294 __setAction(e5App().getObject("DebugUI").getActions(), 311 __setAction(
295 shortcuts["Debug"]) 312 e5App().getObject("UserInterface").getActions('wizards'),
296 313 shortcuts["Wizards"])
297 if "Edit" in shortcuts: 314
298 __setAction(e5App().getObject("ViewManager").getActions('edit'), 315 if "Debug" in shortcuts:
299 shortcuts["Edit"]) 316 __setAction(
300 317 e5App().getObject("DebugUI").getActions(),
301 if "File" in shortcuts: 318 shortcuts["Debug"])
302 __setAction(e5App().getObject("ViewManager").getActions('file'), 319
303 shortcuts["File"]) 320 if "Edit" in shortcuts:
304 321 __setAction(
305 if "Search" in shortcuts: 322 e5App().getObject("ViewManager").getActions('edit'),
306 __setAction(e5App().getObject("ViewManager").getActions('search'), 323 shortcuts["Edit"])
307 shortcuts["Search"]) 324
308 325 if "File" in shortcuts:
309 if "View" in shortcuts: 326 __setAction(
310 __setAction(e5App().getObject("ViewManager").getActions('view'), 327 e5App().getObject("ViewManager").getActions('file'),
311 shortcuts["View"]) 328 shortcuts["File"])
312 329
313 if "Macro" in shortcuts: 330 if "Search" in shortcuts:
314 __setAction(e5App().getObject("ViewManager").getActions('macro'), 331 __setAction(
315 shortcuts["Macro"]) 332 e5App().getObject("ViewManager").getActions('search'),
316 333 shortcuts["Search"])
317 if "Bookmarks" in shortcuts: 334
318 __setAction(e5App().getObject("ViewManager").getActions('bookmark'), 335 if "View" in shortcuts:
319 shortcuts["Bookmarks"]) 336 __setAction(
320 337 e5App().getObject("ViewManager").getActions('view'),
321 if "Spelling" in shortcuts: 338 shortcuts["View"])
322 __setAction(e5App().getObject("ViewManager").getActions('spelling'), 339
323 shortcuts["Spelling"]) 340 if "Macro" in shortcuts:
324 341 __setAction(
325 if "Window" in shortcuts: 342 e5App().getObject("ViewManager").getActions('macro'),
326 actions = e5App().getObject("ViewManager").getActions('window') 343 shortcuts["Macro"])
327 if actions: 344
328 __setAction(actions, shortcuts["Window"]) 345 if "Bookmarks" in shortcuts:
329 346 __setAction(
330 for category, ref in e5App().getPluginObjects(): 347 e5App().getObject("ViewManager").getActions('bookmark'),
331 if category in shortcuts and hasattr(ref, "getActions"): 348 shortcuts["Bookmarks"])
332 actions = ref.getActions() 349
333 __setAction(actions, shortcuts[category]) 350 if "Spelling" in shortcuts:
334 351 __setAction(
335 try: 352 e5App().getObject("ViewManager").getActions('spelling'),
336 if "HelpViewer" in shortcuts: 353 shortcuts["Spelling"])
337 __setAction(e5App().getObject("DummyHelpViewer").getActions(), 354
338 shortcuts["HelpViewer"]) 355 if "Window" in shortcuts:
339 except KeyError: 356 actions = e5App().getObject("ViewManager").getActions('window')
340 # no QtWebKit available 357 if actions:
341 pass 358 __setAction(actions, shortcuts["Window"])
359
360 for category, ref in e5App().getPluginObjects():
361 if category in shortcuts and hasattr(ref, "getActions"):
362 actions = ref.getActions()
363 __setAction(actions, shortcuts[category])
364
365 try:
366 if "HelpViewer" in shortcuts:
367 __setAction(e5App().getObject("DummyHelpViewer").getActions(),
368 shortcuts["HelpViewer"])
369 except KeyError:
370 # no QtWebKit available
371 pass
372
373 else:
374 category = helpViewer.getActionsCategory()
375 if category in shortcuts:
376 __setAction(helpViewer.getActions(), shortcuts[category])

eric ide

mercurial