eric6/Preferences/Shortcuts.py

branch
maintenance
changeset 8142
43248bafe9b2
parent 8043
0acf98cd089a
parent 8022
2da0139f4f91
child 8176
31965986ecd1
equal deleted inserted replaced
8044:874fdd14d3a2 8142:43248bafe9b2
198 198
199 def exportShortcuts(fn, helpViewer=None): 199 def exportShortcuts(fn, helpViewer=None):
200 """ 200 """
201 Module function to export the keyboard shortcuts for the defined QActions. 201 Module function to export the keyboard shortcuts for the defined QActions.
202 202
203 @param fn filename of the export file (string) 203 @param fn filename of the export file
204 @param helpViewer reference to the help window object 204 @type str
205 @param helpViewer reference to the help window object
206 @type WebBrowserWindow
205 """ 207 """
206 # let the plugin manager create on demand plugin objects 208 # let the plugin manager create on demand plugin objects
207 pm = e5App().getObject("PluginManager") 209 pm = e5App().getObject("PluginManager")
208 pm.initOnDemandPlugins() 210 pm.initOnDemandPlugins()
209 211
210 f = QFile(fn) 212 if fn.endswith(".ekj"):
211 if f.open(QIODevice.WriteOnly): 213 # new JSON based file
212 from E5XML.ShortcutsWriter import ShortcutsWriter 214 from .ShortcutsFile import ShortcutsFile
213 ShortcutsWriter(f).writeXML(helpViewer=helpViewer) 215 shortcutsFile = ShortcutsFile()
214 f.close() 216 shortcutsFile.writeFile(fn, helpViewer)
215 else: 217 else:
216 E5MessageBox.critical( 218 # old XML based file
217 None, 219 f = QFile(fn)
218 QCoreApplication.translate( 220 if f.open(QIODevice.WriteOnly):
219 "Shortcuts", "Export Keyboard Shortcuts"), 221 from E5XML.ShortcutsWriter import ShortcutsWriter
220 QCoreApplication.translate( 222 ShortcutsWriter(f).writeXML(helpViewer=helpViewer)
221 "Shortcuts", 223 f.close()
222 "<p>The keyboard shortcuts could not be written to file" 224 else:
223 " <b>{0}</b>.</p>") 225 E5MessageBox.critical(
224 .format(fn)) 226 None,
227 QCoreApplication.translate(
228 "Shortcuts", "Export Keyboard Shortcuts"),
229 QCoreApplication.translate(
230 "Shortcuts",
231 "<p>The keyboard shortcuts file <b>{0}</b> could not"
232 " be written.</p>")
233 .format(fn))
225 234
226 235
227 def importShortcuts(fn, helpViewer=None): 236 def importShortcuts(fn, helpViewer=None):
228 """ 237 """
229 Module function to import the keyboard shortcuts for the defined E5Actions. 238 Module function to import the keyboard shortcuts for the defined actions.
230 239
231 @param fn filename of the import file (string) 240 @param fn filename of the import file
232 @param helpViewer reference to the help window object 241 @type str
242 @param helpViewer reference to the help window object
243 @type WebBrowserWindow
233 """ 244 """
234 # let the plugin manager create on demand plugin objects 245 # let the plugin manager create on demand plugin objects
235 pm = e5App().getObject("PluginManager") 246 pm = e5App().getObject("PluginManager")
236 pm.initOnDemandPlugins() 247 pm.initOnDemandPlugins()
237 248
238 f = QFile(fn) 249 if fn.endswith(".ekj"):
239 if f.open(QIODevice.ReadOnly): 250 # new JSON based file
240 from E5XML.ShortcutsReader import ShortcutsReader 251 from .ShortcutsFile import ShortcutsFile
241 reader = ShortcutsReader(f) 252 shortcutsFile = ShortcutsFile()
242 reader.readXML() 253 shortcuts = shortcutsFile.readFile(fn)
243 f.close() 254 if shortcuts:
244 if not reader.hasError():
245 shortcuts = reader.getShortcuts()
246 setActions(shortcuts, helpViewer=helpViewer) 255 setActions(shortcuts, helpViewer=helpViewer)
247 saveShortcuts() 256 saveShortcuts()
248 syncPreferences() 257 syncPreferences()
249 else: 258 else:
250 E5MessageBox.critical( 259 # old XML based file
251 None, 260 f = QFile(fn)
252 QCoreApplication.translate( 261 if f.open(QIODevice.ReadOnly):
253 "Shortcuts", "Import Keyboard Shortcuts"), 262 from E5XML.ShortcutsReader import ShortcutsReader
254 QCoreApplication.translate( 263 reader = ShortcutsReader(f)
255 "Shortcuts", 264 reader.readXML()
256 "<p>The keyboard shortcuts could not be read from file" 265 f.close()
257 " <b>{0}</b>.</p>") 266 if not reader.hasError():
258 .format(fn)) 267 shortcuts = reader.getShortcuts()
259 return 268 setActions(shortcuts, helpViewer=helpViewer)
260 269 saveShortcuts()
261 270 syncPreferences()
262 def __setAction(actions, sdict): 271 else:
263 """ 272 E5MessageBox.critical(
264 Private function to write a single keyboard shortcut to the settings. 273 None,
265 274 QCoreApplication.translate(
266 @param actions list of actions to set (list of E5Action) 275 "Shortcuts", "Import Keyboard Shortcuts"),
267 @param sdict dictionary containg accelerator information for one category 276 QCoreApplication.translate(
277 "Shortcuts",
278 "<p>The keyboard shortcuts file <b>{0}</b> could not be"
279 " read.</p>")
280 .format(fn))
281
282
283 def __setAction(actions, shortcutsDict):
284 """
285 Private function to set a single keyboard shortcut category shortcuts.
286
287 @param actions list of actions to set
288 @type list of E5Action
289 @param shortcutsDict dictionary containing accelerator information for
290 one category
291 @type dict
268 """ 292 """
269 for act in actions: 293 for act in actions:
270 if act.objectName(): 294 if act.objectName():
271 try: 295 try:
272 accel, altAccel = sdict[act.objectName()] 296 accel, altAccel = shortcutsDict[act.objectName()]
273 act.setShortcut(QKeySequence(accel)) 297 act.setShortcut(QKeySequence(accel))
274 act.setAlternateShortcut(QKeySequence(altAccel), 298 act.setAlternateShortcut(QKeySequence(altAccel),
275 removeEmpty=True) 299 removeEmpty=True)
276 except KeyError: 300 except KeyError:
277 pass 301 pass
278 302
279 303
280 def setActions(shortcuts, helpViewer=None): 304 def setActions(shortcuts, helpViewer=None):
281 """ 305 """
282 Module function to set actions based on new format shortcuts file. 306 Module function to set actions based on the imported shortcuts file.
283 307
284 @param shortcuts dictionary containing the accelerator information 308 @param shortcuts dictionary containing the accelerator information
285 read from a XML file 309 read from a JSON or XML file
286 @param helpViewer reference to the help window object 310 @type dict
311 @param helpViewer reference to the help window object
312 @type WebBrowserWindow
287 """ 313 """
288 if helpViewer is None: 314 if helpViewer is None:
289 if "Project" in shortcuts: 315 if "Project" in shortcuts:
290 __setAction( 316 __setAction(
291 e5App().getObject("Project").getActions(), 317 e5App().getObject("Project").getActions(),

eric ide

mercurial