7#ifndef __KIS_DOM_UTILS_H
8#define __KIS_DOM_UTILS_H
19#include <klocalizedstring.h>
21#include "kritaglobal_export.h"
36 return QString::number(
value);
43 stream.setString(&str, QIODevice::WriteOnly);
44 stream.setRealNumberPrecision(FLT_DIG);
53 stream.setString(&str, QIODevice::WriteOnly);
54 stream.setRealNumberPrecision(15);
59 inline int toInt(
const QString &str,
bool *ok=
nullptr) {
60 bool ok_locale =
false;
63 QLocale c(QLocale::German);
65 value = str.toInt(&ok_locale);
67 value = c.toInt(str, &ok_locale);
70 if (!ok_locale && ok ==
nullptr) {
82 inline double toDouble(
const QString &str,
bool *ok=
nullptr) {
83 bool ok_locale =
false;
86 QLocale c(QLocale::German);
98 value = str.toDouble(&ok_locale);
100 value = c.toDouble(str, &ok_locale);
103 if (!ok_locale && ok ==
nullptr) {
104 warnKrita <<
"WARNING: KisDomUtils::toDouble failed:" <<
ppVar(str);
119 QString customColor = QString::number(color.red()).append(
",")
120 .append(QString::number(color.green())).append(
",")
121 .append(QString::number(color.blue())).append(
",")
122 .append(QString::number(color.alpha()));
129 QStringList colorComponents = colorString.split(
',');
130 return QColor(colorComponents[0].
toInt(), colorComponents[1].
toInt(), colorComponents[2].
toInt(), colorComponents[3].
toInt());
143void KRITAGLOBAL_EXPORT
saveValue(QDomElement *parent,
const QString &tag,
const QRect &rc);
144void KRITAGLOBAL_EXPORT
saveValue(QDomElement *parent,
const QString &tag,
const QRectF &rc);
145void KRITAGLOBAL_EXPORT
saveValue(QDomElement *parent,
const QString &tag,
const QSize &size);
146void KRITAGLOBAL_EXPORT
saveValue(QDomElement *parent,
const QString &tag,
const QPoint &pt);
147void KRITAGLOBAL_EXPORT
saveValue(QDomElement *parent,
const QString &tag,
const QPointF &pt);
148void KRITAGLOBAL_EXPORT
saveValue(QDomElement *parent,
const QString &tag,
const QVector3D &pt);
149void KRITAGLOBAL_EXPORT
saveValue(QDomElement *parent,
const QString &tag,
const QTransform &t);
150void KRITAGLOBAL_EXPORT
saveValue(QDomElement *parent,
const QString &tag,
const QColor &t);
162 QDomDocument doc = parent->ownerDocument();
163 QDomElement e = doc.createElement(tag);
164 parent->appendChild(e);
166 e.setAttribute(
"type",
"value");
178template <
template <
class...>
class Container,
typename T,
typename ...Args>
180saveValue(QDomElement *parent,
const QString &tag,
const Container<T, Args...> &array)
182 QDomDocument doc = parent->ownerDocument();
183 QDomElement e = doc.createElement(tag);
184 parent->appendChild(e);
186 e.setAttribute(
"type",
"array");
189 Q_FOREACH (
const T &
v, array) {
190 saveValue(&e, QString(
"item_%1").arg(i++),
v);
200bool KRITAGLOBAL_EXPORT
findOnlyElement(
const QDomElement &parent,
const QString &tag, QDomElement *el,
QStringList *errorMessages = 0);
211bool KRITAGLOBAL_EXPORT
loadValue(
const QDomElement &e,
float *
v);
212bool KRITAGLOBAL_EXPORT
loadValue(
const QDomElement &e,
double *
v);
213bool KRITAGLOBAL_EXPORT
loadValue(
const QDomElement &e, QSize *size);
214bool KRITAGLOBAL_EXPORT
loadValue(
const QDomElement &e, QRect *rc);
215bool KRITAGLOBAL_EXPORT
loadValue(
const QDomElement &e, QRectF *rc);
216bool KRITAGLOBAL_EXPORT
loadValue(
const QDomElement &e, QPoint *pt);
217bool KRITAGLOBAL_EXPORT
loadValue(
const QDomElement &e, QPointF *pt);
218bool KRITAGLOBAL_EXPORT
loadValue(
const QDomElement &e, QVector3D *pt);
219bool KRITAGLOBAL_EXPORT
loadValue(
const QDomElement &e, QTransform *t);
220bool KRITAGLOBAL_EXPORT
loadValue(
const QDomElement &e, QString *
value);
221bool KRITAGLOBAL_EXPORT
loadValue(
const QDomElement &e, QColor *
value);
225 bool KRITAGLOBAL_EXPORT
checkType(
const QDomElement &e,
const QString &expectedType);
238 typename std::enable_if<std::is_arithmetic<T>::value,
bool>::type
243 QVariant
v(e.attribute(
"value",
"no-value"));
252template <
typename T,
typename E>
253 typename std::enable_if<std::is_empty<E>::value,
bool>::type
267template <
template <
class ...>
class Container,
typename T,
typename E,
typename ...Args>
269loadValue(
const QDomElement &e, Container<T, Args...> *array,
const E &env = std::tuple<>())
273 QDomElement child = e.firstChildElement();
274 while (!child.isNull()) {
277 array->push_back(
value);
278 child = child.nextSiblingElement();
283template <
template <
class ...>
class Container,
typename T,
typename E,
typename F,
typename ...Args>
285loadValue(
const QDomElement &e, Container<T, Args...> *array,
const E &env1,
const F &env2)
289 QDomElement child = e.firstChildElement();
290 while (!child.isNull()) {
293 array->push_back(
value);
294 child = child.nextSiblingElement();
299template <
typename T,
typename E = std::tuple<>>
300 bool loadValue(
const QDomElement &parent,
const QString &tag, T *
value,
const E &env = E())
308template <
typename T,
typename E,
typename F>
309 bool loadValue(
const QDomElement &parent,
const QString &tag, T *
value,
const E &env1,
const F &env2)
320 const QString &attribute,
321 const QString &
value);
323KRITAGLOBAL_EXPORT
bool removeElements(QDomElement &parent,
const QString &tag);
float value(const T *src, size_t ch)
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)
QString qColorToQString(QColor color)
bool findOnlyElement(const QDomElement &parent, const QString &tag, QDomElement *el, QStringList *errorMessages)
QColor qStringToQColor(QString colorString)
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)
void setUtf8OnStream(QTextStream &stream)