eric6/Preferences/Shortcuts.py

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

eric ide

mercurial