-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandfile.cpp
More file actions
137 lines (113 loc) · 3.85 KB
/
Copy pathcommandfile.cpp
File metadata and controls
137 lines (113 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "commandfile.h"
#include <QKeySequence>
#include <QFile>
#include <QXmlStreamAttributes>
#include <QXmlStreamWriter>
#include <QXmlStreamReader>
#include <QMap>
#include <QDateTime>
struct Context // XML parsing context with strings.
{
Context();
const QString mappingElement;
const QString shortCutElement;
const QString idAttribute;
const QString keyElement;
const QString valueAttribute;
};
Context::Context() :
mappingElement(QLatin1String("mapping")),
shortCutElement(QLatin1String("shortcut")),
idAttribute(QLatin1String("id")),
keyElement(QLatin1String("key")),
valueAttribute(QLatin1String("value"))
{
}
/*!
\class CommandsFile
\brief The CommandsFile class provides a collection of import and export commands.
\inheaderfile commandsfile.h
*/
/*!
...
*/
CommandsFile::CommandsFile(const QString &filename)
: m_filename(filename)
{
}
/*!
...
*/
QMap<QString, QKeySequence> CommandsFile::importCommands() const
{
QMap<QString, QKeySequence> result;
QFile file(m_filename);
if (!file.open(QIODevice::ReadOnly|QIODevice::Text))
return result;
Context ctx;
QXmlStreamReader r(&file);
QString currentId;
while (!r.atEnd()) {
switch (r.readNext()) {
case QXmlStreamReader::StartElement: {
const QStringRef name = r.name();
if (name == ctx.shortCutElement) {
if (!currentId.isEmpty()) // shortcut element without key element == empty shortcut
result.insert(currentId, QKeySequence());
currentId = r.attributes().value(ctx.idAttribute).toString();
} else if (name == ctx.keyElement) {
const QXmlStreamAttributes attributes = r.attributes();
if (attributes.hasAttribute(ctx.valueAttribute)) {
const QString keyString = attributes.value(ctx.valueAttribute).toString();
result.insert(currentId, QKeySequence(keyString));
} else {
result.insert(currentId, QKeySequence());
}
currentId.clear();
} // if key element
} // case QXmlStreamReader::StartElement
default:
break;
} // switch
} // while !atEnd
file.close();
return result;
}
/*!
...
*/
bool CommandsFile::exportCommands(const QList<ShortcutItem *> &items, bool resetToDefault)
{
QFile file(m_filename);
if (!file.open(QIODevice::WriteOnly|QIODevice::Text))
return false;
const Context ctx;
QXmlStreamWriter w(&file);
w.setAutoFormatting(true);
w.setAutoFormattingIndent(1); // Historical, used to be QDom.
w.writeStartDocument();
w.writeDTD(QLatin1String("<!DOCTYPE KeyboardMappingScheme>"));
w.writeComment(QString::fromLatin1(" Written by %1, %2. ").
arg("Shortcut Setting",
QDateTime::currentDateTime().toString(Qt::ISODate)));
w.writeStartElement(ctx.mappingElement);
foreach (const ShortcutItem *item, items) {
const QString id = item->m_name;
if (item->m_key.isEmpty()) {
w.writeEmptyElement(ctx.shortCutElement);
w.writeAttribute(ctx.idAttribute, id);
} else {
w.writeStartElement(ctx.shortCutElement);
w.writeAttribute(ctx.idAttribute, id);
w.writeEmptyElement(ctx.keyElement);
if(resetToDefault)
w.writeAttribute(ctx.valueAttribute, item->m_default_key.toString());
else
w.writeAttribute(ctx.valueAttribute, item->m_key.toString());
w.writeEndElement(); // Shortcut
}
}
w.writeEndElement();
w.writeEndDocument();
return true;
}