Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_dom_utils.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2014 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "kis_dom_utils.h"
8
9#include <QTransform>
10#include <QVector3D>
11
12#include "kis_debug.h"
13
14namespace KisDomUtils {
15
16void saveValue(QDomElement *parent, const QString &tag, const QSize &size)
17{
18 QDomDocument doc = parent->ownerDocument();
19 QDomElement e = doc.createElement(tag);
20 parent->appendChild(e);
21
22 e.setAttribute("type", "size");
23
24 e.setAttribute("w", toString(size.width()));
25 e.setAttribute("h", toString(size.height()));
26}
27
28void saveValue(QDomElement *parent, const QString &tag, const QRect &rc)
29{
30 QDomDocument doc = parent->ownerDocument();
31 QDomElement e = doc.createElement(tag);
32 parent->appendChild(e);
33
34 e.setAttribute("type", "rect");
35
36 e.setAttribute("x", toString(rc.x()));
37 e.setAttribute("y", toString(rc.y()));
38 e.setAttribute("w", toString(rc.width()));
39 e.setAttribute("h", toString(rc.height()));
40}
41
42void saveValue(QDomElement *parent, const QString &tag, const QRectF &rc)
43{
44 QDomDocument doc = parent->ownerDocument();
45 QDomElement e = doc.createElement(tag);
46 parent->appendChild(e);
47
48 e.setAttribute("type", "rectf");
49
50 e.setAttribute("x", toString(rc.x()));
51 e.setAttribute("y", toString(rc.y()));
52 e.setAttribute("w", toString(rc.width()));
53 e.setAttribute("h", toString(rc.height()));
54}
55
56void saveValue(QDomElement *parent, const QString &tag, const QPoint &pt)
57{
58 QDomDocument doc = parent->ownerDocument();
59 QDomElement e = doc.createElement(tag);
60 parent->appendChild(e);
61
62 e.setAttribute("type", "point");
63
64 e.setAttribute("x", toString(pt.x()));
65 e.setAttribute("y", toString(pt.y()));
66}
67
68void saveValue(QDomElement *parent, const QString &tag, const QPointF &pt)
69{
70 QDomDocument doc = parent->ownerDocument();
71 QDomElement e = doc.createElement(tag);
72 parent->appendChild(e);
73
74 e.setAttribute("type", "pointf");
75
76 e.setAttribute("x", toString(pt.x()));
77 e.setAttribute("y", toString(pt.y()));
78}
79
80void saveValue(QDomElement *parent, const QString &tag, const QVector3D &pt)
81{
82 QDomDocument doc = parent->ownerDocument();
83 QDomElement e = doc.createElement(tag);
84 parent->appendChild(e);
85
86 e.setAttribute("type", "vector3d");
87
88 e.setAttribute("x", toString(pt.x()));
89 e.setAttribute("y", toString(pt.y()));
90 e.setAttribute("z", toString(pt.z()));
91}
92
93void saveValue(QDomElement *parent, const QString &tag, const QTransform &t)
94{
95 QDomDocument doc = parent->ownerDocument();
96 QDomElement e = doc.createElement(tag);
97 parent->appendChild(e);
98
99 e.setAttribute("type", "transform");
100
101 e.setAttribute("m11", toString(t.m11()));
102 e.setAttribute("m12", toString(t.m12()));
103 e.setAttribute("m13", toString(t.m13()));
104
105 e.setAttribute("m21", toString(t.m21()));
106 e.setAttribute("m22", toString(t.m22()));
107 e.setAttribute("m23", toString(t.m23()));
108
109 e.setAttribute("m31", toString(t.m31()));
110 e.setAttribute("m32", toString(t.m32()));
111 e.setAttribute("m33", toString(t.m33()));
112}
113
114void saveValue(QDomElement *parent, const QString &tag, const QColor &c)
115{
116 QDomDocument doc = parent->ownerDocument();
117 QDomElement e = doc.createElement(tag);
118 parent->appendChild(e);
119
120 e.setAttribute("type", "qcolor");
121 e.setAttribute("value", c.name(QColor::HexArgb));
122}
123
124bool findOnlyElement(const QDomElement &parent, const QString &tag, QDomElement *el, QStringList *errorMessages)
125{
126 QDomNodeList list = parent.elementsByTagName(tag);
127 if (list.size() != 1 || !list.at(0).isElement()) {
128 QString msg = i18n("Could not find \"%1\" XML tag in \"%2\"", tag, parent.tagName());
129 if (errorMessages) {
130 *errorMessages << msg;
131 } else {
132 warnKrita << msg;
133 }
134
135 return false;
136 }
137
138 *el = list.at(0).toElement();
139 return true;
140}
141
142bool removeElements(QDomElement &parent, const QString &tag) {
143 QDomNodeList list = parent.elementsByTagName(tag);
144 KIS_SAFE_ASSERT_RECOVER_NOOP(list.size() <= 1);
145
146 for (int i = 0; i < list.size(); i++) {
147 parent.removeChild(list.at(i));
148 }
149
150 return list.size() > 0;
151}
152
153namespace Private {
154 bool checkType(const QDomElement &e, const QString &expectedType)
155 {
156 QString type = e.attribute("type", "unknown-type");
157 if (type != expectedType) {
158 warnKrita << i18n("Error: incorrect type (%2) for value %1. Expected %3", e.tagName(), type, expectedType);
159 return false;
160 }
161
162 return true;
163 }
164}
165
166bool loadValue(const QDomElement &e, float *v)
167{
168 if (!Private::checkType(e, "value")) return false;
169 *v = toDouble(e.attribute("value", "0"));
170 return true;
171}
172
173bool loadValue(const QDomElement &e, double *v)
174{
175 if (!Private::checkType(e, "value")) return false;
176 *v = toDouble(e.attribute("value", "0"));
177 return true;
178}
179
180bool loadValue(const QDomElement &e, QSize *size)
181{
182 if (!Private::checkType(e, "size")) return false;
183
184 size->setWidth(toInt(e.attribute("w", "0")));
185 size->setHeight(toInt(e.attribute("h", "0")));
186
187 return true;
188}
189
190bool loadValue(const QDomElement &e, QRect *rc)
191{
192 if (!Private::checkType(e, "rect")) return false;
193
194 rc->setX(toInt(e.attribute("x", "0")));
195 rc->setY(toInt(e.attribute("y", "0")));
196 rc->setWidth(toInt(e.attribute("w", "0")));
197 rc->setHeight(toInt(e.attribute("h", "0")));
198
199 return true;
200}
201
202bool loadValue(const QDomElement &e, QRectF *rc)
203{
204 if (!Private::checkType(e, "rectf")) return false;
205
206 rc->setX(toInt(e.attribute("x", "0")));
207 rc->setY(toInt(e.attribute("y", "0")));
208 rc->setWidth(toInt(e.attribute("w", "0")));
209 rc->setHeight(toInt(e.attribute("h", "0")));
210
211 return true;
212}
213
214bool loadValue(const QDomElement &e, QPoint *pt)
215{
216 if (!Private::checkType(e, "point")) return false;
217
218 pt->setX(toInt(e.attribute("x", "0")));
219 pt->setY(toInt(e.attribute("y", "0")));
220
221 return true;
222}
223
224bool loadValue(const QDomElement &e, QPointF *pt)
225{
226 if (!Private::checkType(e, "pointf")) return false;
227
228 pt->setX(toDouble(e.attribute("x", "0")));
229 pt->setY(toDouble(e.attribute("y", "0")));
230
231 return true;
232}
233
234bool loadValue(const QDomElement &e, QVector3D *pt)
235{
236 if (!Private::checkType(e, "vector3d")) return false;
237
238 pt->setX(toDouble(e.attribute("x", "0")));
239 pt->setY(toDouble(e.attribute("y", "0")));
240 pt->setZ(toDouble(e.attribute("z", "0")));
241
242 return true;
243}
244
245bool loadValue(const QDomElement &e, QTransform *t)
246{
247 if (!Private::checkType(e, "transform")) return false;
248
249 qreal m11 = toDouble(e.attribute("m11", "1.0"));
250 qreal m12 = toDouble(e.attribute("m12", "0.0"));
251 qreal m13 = toDouble(e.attribute("m13", "0.0"));
252
253 qreal m21 = toDouble(e.attribute("m21", "0.0"));
254 qreal m22 = toDouble(e.attribute("m22", "1.0"));
255 qreal m23 = toDouble(e.attribute("m23", "0.0"));
256
257 qreal m31 = toDouble(e.attribute("m31", "0.0"));
258 qreal m32 = toDouble(e.attribute("m32", "0.0"));
259 qreal m33 = toDouble(e.attribute("m33", "1.0"));
260
261 t->setMatrix(
262 m11, m12, m13,
263 m21, m22, m23,
264 m31, m32, m33);
265
266 return true;
267}
268
269bool loadValue(const QDomElement &e, QString *value)
270{
271 if (!Private::checkType(e, "value")) return false;
272 *value = e.attribute("value", "no-value");
273 return true;
274}
275
276bool loadValue(const QDomElement &e, QColor *value)
277{
278 if (!Private::checkType(e, "qcolor")) return false;
279 value->setNamedColor(e.attribute("value", "#FFFF0000"));
280 return true;
281}
282
283QDomElement findElementByAttribute(QDomNode parent,
284 const QString &tag,
285 const QString &attribute,
286 const QString &value)
287{
288 QDomElement e;
289 for (e = parent.firstChildElement(tag); !e.isNull(); e = e.nextSiblingElement(tag)) {
290 if (value == e.attribute(attribute, "<undefined>")) {
291 return e;
292 }
293 }
294
295 return QDomElement();
296}
297
298
299}
float value(const T *src, size_t ch)
qreal v
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
#define warnKrita
Definition kis_debug.h:87
bool checkType(const QDomElement &e, const QString &expectedType)
void saveValue(QDomElement *parent, const QString &tag, const QSize &size)
bool removeElements(QDomElement &parent, const QString &tag)
bool findOnlyElement(const QDomElement &parent, const QString &tag, QDomElement *el, QStringList *errorMessages)
double toDouble(const QString &str, bool *ok=nullptr)
int toInt(const QString &str, bool *ok=nullptr)
QDomElement findElementByAttribute(QDomNode parent, const QString &tag, const QString &attribute, const QString &value)
bool loadValue(const QDomElement &e, float *v)
QString toString(const QString &value)