9 |
9 |
10 import time |
10 import time |
11 |
11 |
12 from E5Gui.E5Application import e5App |
12 from E5Gui.E5Application import e5App |
13 |
13 |
14 from .XMLWriterBase import XMLWriterBase |
14 from .XMLStreamWriterBase import XMLStreamWriterBase |
15 from .Config import shortcutsFileFormatVersion |
15 from .Config import shortcutsFileFormatVersion |
16 |
16 |
17 import Preferences |
17 import Preferences |
18 |
18 |
19 class ShortcutsWriter(XMLWriterBase): |
19 class ShortcutsWriter(XMLStreamWriterBase): |
20 """ |
20 """ |
21 Class implementing the writer class for writing an XML shortcuts file. |
21 Class implementing the writer class for writing an XML shortcuts file. |
22 """ |
22 """ |
23 def __init__(self, file): |
23 def __init__(self, device): |
24 """ |
24 """ |
25 Constructor |
25 Constructor |
26 |
26 |
27 @param file open file (like) object for writing |
27 @param device reference to the I/O device to write to (QIODevice) |
28 """ |
28 """ |
29 XMLWriterBase.__init__(self, file) |
29 XMLStreamWriterBase.__init__(self, device) |
30 |
30 |
31 self.email = Preferences.getUser("Email") |
31 self.email = Preferences.getUser("Email") |
32 |
32 |
33 def writeXML(self): |
33 def writeXML(self): |
34 """ |
34 """ |
35 Public method to write the XML to the file. |
35 Public method to write the XML to the file. |
36 """ |
36 """ |
37 XMLWriterBase.writeXML(self) |
37 XMLStreamWriterBase.writeXML(self) |
38 |
38 |
39 self._write('<!DOCTYPE Shortcuts SYSTEM "Shortcuts-{0}.dtd">'.format( |
39 self.writeDTD('<!DOCTYPE Shortcuts SYSTEM "Shortcuts-{0}.dtd">'.format( |
40 shortcutsFileFormatVersion)) |
40 shortcutsFileFormatVersion)) |
41 |
41 |
42 # add some generation comments |
42 # add some generation comments |
43 self._write("<!-- Eric5 keyboard shortcuts -->") |
43 self.writeComment(" Eric5 keyboard shortcuts ") |
44 self._write("<!-- Saved: {0} -->".format(time.strftime('%Y-%m-%d, %H:%M:%S'))) |
44 self.writeComment(" Saved: {0}".format(time.strftime('%Y-%m-%d, %H:%M:%S'))) |
45 self._write("<!-- Author: {0} -->".format(self.escape("{0}".format(self.email)))) |
45 self.writeComment(" Author: {0} ".format(self.email)) |
46 |
46 |
47 # add the main tag |
47 # add the main tag |
48 self._write('<Shortcuts version="{0}">'.format(shortcutsFileFormatVersion)) |
48 self.writeStartElement("Shortcuts") |
|
49 self.writeAttribute("version", shortcutsFileFormatVersion) |
49 |
50 |
50 for act in e5App().getObject("Project").getActions(): |
51 self.__writeActions("Project", |
51 self._write(' <Shortcut category="Project">') |
52 e5App().getObject("Project").getActions()) |
52 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
53 self.__writeActions("General", |
53 self._write(' <Accel>{0}</Accel>'.format( |
54 e5App().getObject("UserInterface").getActions('ui')) |
54 self.escape("{0}".format(act.shortcut().toString())))) |
55 self.__writeActions("Wizards", |
55 self._write(' <AltAccel>{0}</AltAccel>' \ |
56 e5App().getObject("UserInterface").getActions('wizards')) |
56 .format(self.escape("{0}".format(act.alternateShortcut().toString())))) |
57 self.__writeActions("Debug", |
57 self._write(' </Shortcut>') |
58 e5App().getObject("DebugUI").getActions()) |
58 |
59 self.__writeActions("Edit", |
59 for act in e5App().getObject("UserInterface").getActions('ui'): |
60 e5App().getObject("ViewManager").getActions('edit')) |
60 self._write(' <Shortcut category="General">') |
61 self.__writeActions("File", |
61 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
62 e5App().getObject("ViewManager").getActions('file')) |
62 self._write(' <Accel>{0}</Accel>'.format( |
63 self.__writeActions("Search", |
63 self.escape("{0}".format(act.shortcut().toString())))) |
64 e5App().getObject("ViewManager").getActions('search')) |
64 self._write(' <AltAccel>{0}</AltAccel>'.format( |
65 self.__writeActions("View", |
65 self.escape("{0}".format(act.alternateShortcut().toString())))) |
66 e5App().getObject("ViewManager").getActions('view')) |
66 self._write(' </Shortcut>') |
67 self.__writeActions("Macro", |
67 |
68 e5App().getObject("ViewManager").getActions('macro')) |
68 for act in e5App().getObject("UserInterface").getActions('wizards'): |
69 self.__writeActions("Bookmarks", |
69 self._write(' <Shortcut category="Wizards">') |
70 e5App().getObject("ViewManager").getActions('bookmark')) |
70 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
71 self.__writeActions("Spelling", |
71 self._write(' <Accel>{0}</Accel>'.format( |
72 e5App().getObject("ViewManager").getActions('spelling')) |
72 self.escape("{0}".format(act.shortcut().toString())))) |
73 self.__writeActions("Window", |
73 self._write(' <AltAccel>{0}</AltAccel>'.format( |
74 e5App().getObject("ViewManager").getActions('window')) |
74 self.escape("{0}".format(act.alternateShortcut().toString())))) |
|
75 self._write(' </Shortcut>') |
|
76 |
|
77 for act in e5App().getObject("DebugUI").getActions(): |
|
78 self._write(' <Shortcut category="Debug">') |
|
79 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
|
80 self._write(' <Accel>{0}</Accel>'.format( |
|
81 self.escape("{0}".format(act.shortcut().toString())))) |
|
82 self._write(' <AltAccel>{0}</AltAccel>'.format( |
|
83 self.escape("{0}".format(act.alternateShortcut().toString())))) |
|
84 self._write(' </Shortcut>') |
|
85 |
|
86 for act in e5App().getObject("ViewManager").getActions('edit'): |
|
87 self._write(' <Shortcut category="Edit">') |
|
88 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
|
89 self._write(' <Accel>{0}</Accel>'.format( |
|
90 self.escape("{0}".format(act.shortcut().toString())))) |
|
91 self._write(' <AltAccel>{0}</AltAccel>'.format( |
|
92 self.escape("{0}".format(act.alternateShortcut().toString())))) |
|
93 self._write(' </Shortcut>') |
|
94 |
|
95 for act in e5App().getObject("ViewManager").getActions('file'): |
|
96 self._write(' <Shortcut category="File">') |
|
97 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
|
98 self._write(' <Accel>{0}</Accel>'.format( |
|
99 self.escape("{0}".format(act.shortcut().toString())))) |
|
100 self._write(' <AltAccel>{0}</AltAccel>'.format( |
|
101 self.escape("{0}".format(act.alternateShortcut().toString())))) |
|
102 self._write(' </Shortcut>') |
|
103 |
|
104 for act in e5App().getObject("ViewManager").getActions('search'): |
|
105 self._write(' <Shortcut category="Search">') |
|
106 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
|
107 self._write(' <Accel>{0}</Accel>'.format( |
|
108 self.escape("{0}".format(act.shortcut().toString())))) |
|
109 self._write(' <AltAccel>{0}</AltAccel>'.format( |
|
110 self.escape("{0}".format(act.alternateShortcut().toString())))) |
|
111 self._write(' </Shortcut>') |
|
112 |
|
113 for act in e5App().getObject("ViewManager").getActions('view'): |
|
114 self._write(' <Shortcut category="View">') |
|
115 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
|
116 self._write(' <Accel>{0}</Accel>'.format( |
|
117 self.escape("{0}".format(act.shortcut().toString())))) |
|
118 self._write(' <AltAccel>{0}</AltAccel>'.format( |
|
119 self.escape("{0}".format(act.alternateShortcut().toString())))) |
|
120 self._write(' </Shortcut>') |
|
121 |
|
122 for act in e5App().getObject("ViewManager").getActions('macro'): |
|
123 self._write(' <Shortcut category="Macro">') |
|
124 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
|
125 self._write(' <Accel>{0}</Accel>'.format( |
|
126 self.escape("{0}".format(act.shortcut().toString())))) |
|
127 self._write(' <AltAccel>{0}</AltAccel>'.format( |
|
128 self.escape("{0}".format(act.alternateShortcut().toString())))) |
|
129 self._write(' </Shortcut>') |
|
130 |
|
131 for act in e5App().getObject("ViewManager").getActions('bookmark'): |
|
132 self._write(' <Shortcut category="Bookmarks">') |
|
133 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
|
134 self._write(' <Accel>{0}</Accel>'.format( |
|
135 self.escape("{0}".format(act.shortcut().toString())))) |
|
136 self._write(' <AltAccel>{0}</AltAccel>'.format( |
|
137 self.escape("{0}".format(act.alternateShortcut().toString())))) |
|
138 self._write(' </Shortcut>') |
|
139 |
|
140 for act in e5App().getObject("ViewManager").getActions('spelling'): |
|
141 self._write(' <Shortcut category="Spelling">') |
|
142 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
|
143 self._write(' <Accel>{0}</Accel>'.format( |
|
144 self.escape("{0}".format(act.shortcut().toString())))) |
|
145 self._write(' <AltAccel>{0}</AltAccel>'.format( |
|
146 self.escape("{0}".format(act.alternateShortcut().toString())))) |
|
147 self._write(' </Shortcut>') |
|
148 |
|
149 actions = e5App().getObject("ViewManager").getActions('window') |
|
150 for act in actions: |
|
151 self._write(' <Shortcut category="Window">') |
|
152 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
|
153 self._write(' <Accel>{0}</Accel>'.format( |
|
154 self.escape("{0}".format(act.shortcut().toString())))) |
|
155 self._write(' <AltAccel>{0}</AltAccel>'.format( |
|
156 self.escape("{0}".format(act.alternateShortcut().toString())))) |
|
157 self._write(' </Shortcut>') |
|
158 |
75 |
159 for category, ref in e5App().getPluginObjects(): |
76 for category, ref in e5App().getPluginObjects(): |
160 if hasattr(ref, "getActions"): |
77 if hasattr(ref, "getActions"): |
161 actions = ref.getActions() |
78 self.__writeActions(category, ref.getActions()) |
162 for act in actions: |
|
163 if act.objectName(): |
|
164 # shortcuts are only exported, if their objectName is set |
|
165 self._write(' <Shortcut category="{0}">'.format(category)) |
|
166 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
|
167 self._write(' <Accel>{0}</Accel>'.format( |
|
168 self.escape("{0}".format(act.shortcut().toString())))) |
|
169 self._write(' <AltAccel>{0}</AltAccel>'\ |
|
170 .format(self.escape("{0}".format( |
|
171 act.alternateShortcut().toString())))) |
|
172 self._write(' </Shortcut>') |
|
173 |
79 |
174 for act in e5App().getObject("DummyHelpViewer").getActions(): |
80 self.__writeActions("HelpViewer", |
175 self._write(' <Shortcut category="HelpViewer">') |
81 e5App().getObject("DummyHelpViewer").getActions()) |
176 self._write(' <Name>{0}</Name>'.format(act.objectName())) |
|
177 self._write(' <Accel>{0}</Accel>'.format( |
|
178 self.escape("{0}".format(act.shortcut().toString())))) |
|
179 self._write(' <AltAccel>{0}</AltAccel>' \ |
|
180 .format(self.escape("{0}".format(act.alternateShortcut().toString())))) |
|
181 self._write(' </Shortcut>') |
|
182 |
82 |
183 # add the main end tag |
83 # add the main end tag |
184 self._write("</Shortcuts>", newline = False) |
84 self.writeEndElement() |
|
85 self.writeEndDocument() |
|
86 |
|
87 def __writeActions(self, category, actions): |
|
88 """ |
|
89 Private method to write the shortcuts for the given actions. |
|
90 |
|
91 @param category category the actions belong to (string) |
|
92 @param actions list of actions to write (E5Action) |
|
93 """ |
|
94 for act in actions: |
|
95 if act.objectName(): |
|
96 # shortcuts are only exported, if their objectName is set |
|
97 self.writeStartElement("Shortcut") |
|
98 self.writeAttribute("category", category) |
|
99 self.writeTextElement("Name", act.objectName()) |
|
100 self.writeTextElement("Accel", act.shortcut().toString()) |
|
101 self.writeTextElement("AltAccel", act.alternateShortcut().toString()) |
|
102 self.writeEndElement() |