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