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