|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2004 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the writer class for writing an XML shortcuts file. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import time |
|
13 |
|
14 from E5Gui.E5Application import e5App |
|
15 |
|
16 from .XMLStreamWriterBase import XMLStreamWriterBase |
|
17 from .Config import shortcutsFileFormatVersion |
|
18 |
|
19 import Preferences |
|
20 |
|
21 |
|
22 class ShortcutsWriter(XMLStreamWriterBase): |
|
23 """ |
|
24 Class implementing the writer class for writing an XML shortcuts file. |
|
25 """ |
|
26 def __init__(self, device): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param device reference to the I/O device to write to (QIODevice) |
|
31 """ |
|
32 XMLStreamWriterBase.__init__(self, device) |
|
33 |
|
34 self.email = Preferences.getUser("Email") |
|
35 |
|
36 def writeXML(self, helpViewer=None): |
|
37 """ |
|
38 Public method to write the XML to the file. |
|
39 |
|
40 @param helpViewer reference to the help window object |
|
41 """ |
|
42 XMLStreamWriterBase.writeXML(self) |
|
43 |
|
44 self.writeDTD('<!DOCTYPE Shortcuts SYSTEM "Shortcuts-{0}.dtd">'.format( |
|
45 shortcutsFileFormatVersion)) |
|
46 |
|
47 # add some generation comments |
|
48 self.writeComment(" Eric6 keyboard shortcuts ") |
|
49 self.writeComment( |
|
50 " Saved: {0}".format(time.strftime('%Y-%m-%d, %H:%M:%S'))) |
|
51 self.writeComment(" Author: {0} ".format(self.email)) |
|
52 |
|
53 # add the main tag |
|
54 self.writeStartElement("Shortcuts") |
|
55 self.writeAttribute("version", shortcutsFileFormatVersion) |
|
56 |
|
57 if helpViewer is None: |
|
58 self.__writeActions( |
|
59 "Project", |
|
60 e5App().getObject("Project").getActions()) |
|
61 self.__writeActions( |
|
62 "General", |
|
63 e5App().getObject("UserInterface").getActions('ui')) |
|
64 self.__writeActions( |
|
65 "Wizards", |
|
66 e5App().getObject("UserInterface").getActions('wizards')) |
|
67 self.__writeActions( |
|
68 "Debug", |
|
69 e5App().getObject("DebugUI").getActions()) |
|
70 self.__writeActions( |
|
71 "Edit", |
|
72 e5App().getObject("ViewManager").getActions('edit')) |
|
73 self.__writeActions( |
|
74 "File", |
|
75 e5App().getObject("ViewManager").getActions('file')) |
|
76 self.__writeActions( |
|
77 "Search", |
|
78 e5App().getObject("ViewManager").getActions('search')) |
|
79 self.__writeActions( |
|
80 "View", |
|
81 e5App().getObject("ViewManager").getActions('view')) |
|
82 self.__writeActions( |
|
83 "Macro", |
|
84 e5App().getObject("ViewManager").getActions('macro')) |
|
85 self.__writeActions( |
|
86 "Bookmarks", |
|
87 e5App().getObject("ViewManager").getActions('bookmark')) |
|
88 self.__writeActions( |
|
89 "Spelling", |
|
90 e5App().getObject("ViewManager").getActions('spelling')) |
|
91 self.__writeActions( |
|
92 "Window", |
|
93 e5App().getObject("ViewManager").getActions('window')) |
|
94 |
|
95 for category, ref in e5App().getPluginObjects(): |
|
96 if hasattr(ref, "getActions"): |
|
97 self.__writeActions(category, ref.getActions()) |
|
98 |
|
99 else: |
|
100 self.__writeActions( |
|
101 helpViewer.getActionsCategory(), |
|
102 helpViewer.getActions()) |
|
103 |
|
104 # add the main end tag |
|
105 self.writeEndElement() |
|
106 self.writeEndDocument() |
|
107 |
|
108 def __writeActions(self, category, actions): |
|
109 """ |
|
110 Private method to write the shortcuts for the given actions. |
|
111 |
|
112 @param category category the actions belong to (string) |
|
113 @param actions list of actions to write (E5Action) |
|
114 """ |
|
115 for act in actions: |
|
116 if act.objectName(): |
|
117 # shortcuts are only exported, if their objectName is set |
|
118 self.writeStartElement("Shortcut") |
|
119 self.writeAttribute("category", category) |
|
120 self.writeTextElement("Name", act.objectName()) |
|
121 self.writeTextElement("Accel", act.shortcut().toString()) |
|
122 self.writeTextElement( |
|
123 "AltAccel", act.alternateShortcut().toString()) |
|
124 self.writeEndElement() |