|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a class for reading an XML shortcuts file. |
|
8 """ |
|
9 |
|
10 from .Config import shortcutsFileFormatVersion |
|
11 from .XMLStreamReaderBase import XMLStreamReaderBase |
|
12 |
|
13 |
|
14 class ShortcutsReader(XMLStreamReaderBase): |
|
15 """ |
|
16 Class for reading an XML shortcuts file. |
|
17 """ |
|
18 supportedVersions = ["3.6"] |
|
19 |
|
20 def __init__(self, device): |
|
21 """ |
|
22 Constructor |
|
23 |
|
24 @param device reference to the I/O device to read from (QIODevice) |
|
25 """ |
|
26 XMLStreamReaderBase.__init__(self, device) |
|
27 |
|
28 self.version = "" |
|
29 self.shortcuts = {} |
|
30 |
|
31 def readXML(self): |
|
32 """ |
|
33 Public method to read and parse the XML document. |
|
34 """ |
|
35 while not self.atEnd(): |
|
36 self.readNext() |
|
37 if self.isStartElement(): |
|
38 if self.name() == "Shortcuts": |
|
39 self.version = self.attribute( |
|
40 "version", shortcutsFileFormatVersion) |
|
41 if self.version not in self.supportedVersions: |
|
42 self.raiseUnsupportedFormatVersion(self.version) |
|
43 elif self.name() == "Shortcut": |
|
44 self.__readShortCut() |
|
45 else: |
|
46 self.raiseUnexpectedStartTag(self.name()) |
|
47 |
|
48 self.showErrorMessage() |
|
49 |
|
50 def __readShortCut(self): |
|
51 """ |
|
52 Private method to read the shortcut data. |
|
53 """ |
|
54 category = self.attribute("category") |
|
55 name = "" |
|
56 accel = "" |
|
57 altAccel = "" |
|
58 |
|
59 while not self.atEnd(): |
|
60 self.readNext() |
|
61 if self.isEndElement() and self.name() == "Shortcut": |
|
62 if category: |
|
63 if category not in self.shortcuts: |
|
64 self.shortcuts[category] = {} |
|
65 self.shortcuts[category][name] = (accel, altAccel) |
|
66 break |
|
67 |
|
68 if self.isStartElement(): |
|
69 if self.name() == "Name": |
|
70 name = self.readElementText() |
|
71 elif self.name() == "Accel": |
|
72 accel = self.readElementText() |
|
73 elif self.name() == "AltAccel": |
|
74 altAccel = self.readElementText() |
|
75 else: |
|
76 self.raiseUnexpectedStartTag(self.name()) |
|
77 |
|
78 def getShortcuts(self): |
|
79 """ |
|
80 Public method to retrieve the shortcuts. |
|
81 |
|
82 @return Dictionary of dictionaries of shortcuts. The keys of the |
|
83 dictionary are the categories, the values are dictionaries. |
|
84 These dictionaries have the shortcut name as their key and |
|
85 a tuple of accelerators as their value. |
|
86 """ |
|
87 return self.shortcuts |