-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionaryitem.cpp
More file actions
302 lines (264 loc) · 6.64 KB
/
dictionaryitem.cpp
File metadata and controls
302 lines (264 loc) · 6.64 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "dictionaryitem.h"
#include <QDomElement>
#include <QCoreApplication>
DictionaryItem::DictionaryItem() :
tagName("dict"), engName(QString()), rusName(QString()), mEnumValue(0)
{
}
DictionaryItem::DictionaryItem(ItemType type, DictionaryItem *parent) : parentItem(parent)
{
tagName = tagNameForType(type);
}
DictionaryItem::DictionaryItem(const QDomElement &domElement, DictionaryItem *parent)
: parentItem(parent)
{
tagName = domElement.tagName();
rusName = domElement.attribute("name_rus", QString());
engName = domElement.attribute("name_eng", QString());
mEnumValue = domElement.attribute("value", QString("0")).toUInt();
QDomNodeList childNodes = domElement.childNodes();
for (int i = 0; i < childNodes.count(); i++)
{
QDomNode childDomNode = childNodes.at(i);
QDomElement childDomElement = childDomNode.toElement();
DictionaryItem *childItem = new DictionaryItem(childDomElement, this);
childItems.append(childItem);
}
}
DictionaryItem::DictionaryItem(DictionaryItem *item, DictionaryItem *parent) : parentItem(parent)
{
tagName = item->tagName;
engName = item->engName;
rusName = item->rusName;
mEnumValue = item->mEnumValue;
foreach (DictionaryItem *childItem, item->childItems)
{
childItems << new DictionaryItem(childItem, this);
}
}
DictionaryItem::~DictionaryItem()
{
qDeleteAll(childItems);
}
QString DictionaryItem::englishName() const
{
return engName;
}
QString DictionaryItem::russiaName() const
{
return rusName;
}
DictionaryItem::ItemType DictionaryItem::type() const
{
return typeForTagName(tagName);
}
quint32 DictionaryItem::enumValue() const
{
return mEnumValue;
}
DictionaryItem::ItemType DictionaryItem::typeForTagName(const QString &tagName)
{
if (tagName == "enum")
{
return EnumType;
}
if (tagName == "string")
{
return StringType;
}
if (tagName == "arg")
{
return ArgType;
}
if (tagName == "context")
{
return ContextType;
}
return Invalid;
}
QByteArray DictionaryItem::tagNameForType(DictionaryItem::ItemType type)
{
switch (type)
{
case DictionaryItem::ContextType: return "context";
case DictionaryItem::StringType: return "string";
case DictionaryItem::EnumType: return "enum";
case DictionaryItem::ArgType: return "arg";
default: return QByteArray();
}
}
QString DictionaryItem::ruTagNameForType(DictionaryItem::ItemType type)
{
switch (type)
{
case DictionaryItem::ContextType: return QCoreApplication::translate("DictionaryItem", "context");
case DictionaryItem::StringType: return QCoreApplication::translate("DictionaryItem", "string");
case DictionaryItem::EnumType: return QCoreApplication::translate("DictionaryItem", "enum");
case DictionaryItem::ArgType: return QCoreApplication::translate("DictionaryItem", "arg");
default: return QString();
}
}
void DictionaryItem::setEnglishName(const QString &name)
{
engName = name;
}
void DictionaryItem::setRussiaName(const QString &name)
{
rusName = name;
}
void DictionaryItem::setEnumValue(const quint32 &enumValue)
{
mEnumValue = enumValue;
}
void DictionaryItem::setParent(DictionaryItem *parent)
{
parentItem = parent;
if (!parent)
{
return;
}
changeType(this, parent);
parent->childItems.insert(parent->childCount(), this);
}
void DictionaryItem::changeType(DictionaryItem *childItem, DictionaryItem *parentItem)
{
if (parentItem->type() == EnumType)
{
childItem->setType(ArgType);
}
if (parentItem->type() == ContextType && childItem->type() == ArgType)
{
childItem->setType(StringType);
}
}
void DictionaryItem::setType(DictionaryItem::ItemType type)
{
tagName = tagNameForType(type);
}
DictionaryItem *DictionaryItem::childAt(const int &i) const
{
if ((childItems.isEmpty()) || (childItems.count() <= i))
{
return 0;
}
return childItems.at(i);
}
DictionaryItem *DictionaryItem::takeChild(const int &i)
{
if ((childItems.isEmpty()) || (childItems.count() <= i))
{
return 0;
}
DictionaryItem *childItem = childItems.takeAt(i);
childItem->setParent(0);
return childItem;
}
int DictionaryItem::rowOfChild(DictionaryItem *child) const
{
return childItems.indexOf(child);
}
int DictionaryItem::childCount() const
{
return childItems.count();
}
void DictionaryItem::insertChild(const int &i, DictionaryItem *childItem)
{
if (!childItem)
{
return;
}
childItem->parentItem = this;
changeType(childItem, this);
childItems.insert(i, childItem);
}
void DictionaryItem::moveChild(const int &from, const int &to)
{
childItems.move(from, to);
}
DictionaryItem *DictionaryItem::parent() const
{
return parentItem;
}
void DictionaryItem::setElementAttributes(QDomElement &domElement) const
{
if (!engName.isEmpty())
{
domElement.setAttribute("name_eng", engName);
}
if (!rusName.isEmpty())
{
domElement.setAttribute("name_rus", rusName);
}
if (typeForTagName(tagName) == ArgType)
{
domElement.setAttribute("value", mEnumValue);
}
}
QPixmap DictionaryItem::pixmap()
{
switch (type())
{
case ContextType: return QPixmap(":images/context");
case ArgType: return QPixmap(":images/argument");
case EnumType: return QPixmap(":images/enum");
case StringType: return QPixmap(":images/string");
default: return QPixmap();
}
}
QDomElement DictionaryItem::toDomElement(QDomDocument &domDocument) const
{
QDomElement domElement = domDocument.createElement(tagName);
setElementAttributes(domElement);
for (int i = 0; i < childItems.count(); i++)
{
const DictionaryItem *childItem = childItems.at(i);
QDomElement childElement = childItem->toDomElement(domDocument);
domElement.appendChild(childElement);
}
return domElement;
}
QDomDocument DictionaryItem::toDomDocument() const
{
QDomDocument domDocument("dictionary");
QDomElement domElement = toDomElement(domDocument);
setElementAttributes(domElement);
domDocument.appendChild(domElement);
return domDocument;
}
QDataStream &operator <<(QDataStream &in, DictionaryItem *item)
{
in << static_cast<quint8>(item->type());
in << item->englishName();
in << item->russiaName();
in << item->enumValue();
in << static_cast<quint32>(item->childCount());
foreach (DictionaryItem *childItem, item->childItems)
{
in << childItem;
}
return in;
}
QDataStream &operator >>(QDataStream &out, DictionaryItem *item)
{
quint8 type = DictionaryItem::Invalid;
QString engName;
QString rusName;
quint32 enumValue = 0;
quint32 childCount = 0;
out >> type;
out >> engName;
out >> rusName;
out >> enumValue;
item->engName = engName;
item->rusName = rusName;
item->mEnumValue = enumValue;
item->tagName = DictionaryItem::tagNameForType(static_cast<DictionaryItem::ItemType>(type));
out >> childCount;
for (quint32 i = 0; i < childCount; i++)
{
DictionaryItem *childItem = new DictionaryItem;
out >> childItem;
item->insertChild(i, childItem);
}
return out;
}