File indexing completed on 2024-05-12 15:56:57
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Dmitry Kazakov <dimula73@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "kis_dom_utils.h" 0008 0009 #include <QTransform> 0010 0011 #include "kis_debug.h" 0012 0013 namespace KisDomUtils { 0014 0015 void saveValue(QDomElement *parent, const QString &tag, const QSize &size) 0016 { 0017 QDomDocument doc = parent->ownerDocument(); 0018 QDomElement e = doc.createElement(tag); 0019 parent->appendChild(e); 0020 0021 e.setAttribute("type", "size"); 0022 0023 e.setAttribute("w", toString(size.width())); 0024 e.setAttribute("h", toString(size.height())); 0025 } 0026 0027 void saveValue(QDomElement *parent, const QString &tag, const QRect &rc) 0028 { 0029 QDomDocument doc = parent->ownerDocument(); 0030 QDomElement e = doc.createElement(tag); 0031 parent->appendChild(e); 0032 0033 e.setAttribute("type", "rect"); 0034 0035 e.setAttribute("x", toString(rc.x())); 0036 e.setAttribute("y", toString(rc.y())); 0037 e.setAttribute("w", toString(rc.width())); 0038 e.setAttribute("h", toString(rc.height())); 0039 } 0040 0041 void saveValue(QDomElement *parent, const QString &tag, const QRectF &rc) 0042 { 0043 QDomDocument doc = parent->ownerDocument(); 0044 QDomElement e = doc.createElement(tag); 0045 parent->appendChild(e); 0046 0047 e.setAttribute("type", "rectf"); 0048 0049 e.setAttribute("x", toString(rc.x())); 0050 e.setAttribute("y", toString(rc.y())); 0051 e.setAttribute("w", toString(rc.width())); 0052 e.setAttribute("h", toString(rc.height())); 0053 } 0054 0055 void saveValue(QDomElement *parent, const QString &tag, const QPoint &pt) 0056 { 0057 QDomDocument doc = parent->ownerDocument(); 0058 QDomElement e = doc.createElement(tag); 0059 parent->appendChild(e); 0060 0061 e.setAttribute("type", "point"); 0062 0063 e.setAttribute("x", toString(pt.x())); 0064 e.setAttribute("y", toString(pt.y())); 0065 } 0066 0067 void saveValue(QDomElement *parent, const QString &tag, const QPointF &pt) 0068 { 0069 QDomDocument doc = parent->ownerDocument(); 0070 QDomElement e = doc.createElement(tag); 0071 parent->appendChild(e); 0072 0073 e.setAttribute("type", "pointf"); 0074 0075 e.setAttribute("x", toString(pt.x())); 0076 e.setAttribute("y", toString(pt.y())); 0077 } 0078 0079 void saveValue(QDomElement *parent, const QString &tag, const QVector3D &pt) 0080 { 0081 QDomDocument doc = parent->ownerDocument(); 0082 QDomElement e = doc.createElement(tag); 0083 parent->appendChild(e); 0084 0085 e.setAttribute("type", "vector3d"); 0086 0087 e.setAttribute("x", toString(pt.x())); 0088 e.setAttribute("y", toString(pt.y())); 0089 e.setAttribute("z", toString(pt.z())); 0090 } 0091 0092 void saveValue(QDomElement *parent, const QString &tag, const QTransform &t) 0093 { 0094 QDomDocument doc = parent->ownerDocument(); 0095 QDomElement e = doc.createElement(tag); 0096 parent->appendChild(e); 0097 0098 e.setAttribute("type", "transform"); 0099 0100 e.setAttribute("m11", toString(t.m11())); 0101 e.setAttribute("m12", toString(t.m12())); 0102 e.setAttribute("m13", toString(t.m13())); 0103 0104 e.setAttribute("m21", toString(t.m21())); 0105 e.setAttribute("m22", toString(t.m22())); 0106 e.setAttribute("m23", toString(t.m23())); 0107 0108 e.setAttribute("m31", toString(t.m31())); 0109 e.setAttribute("m32", toString(t.m32())); 0110 e.setAttribute("m33", toString(t.m33())); 0111 } 0112 0113 void saveValue(QDomElement *parent, const QString &tag, const QColor &c) 0114 { 0115 QDomDocument doc = parent->ownerDocument(); 0116 QDomElement e = doc.createElement(tag); 0117 parent->appendChild(e); 0118 0119 e.setAttribute("type", "qcolor"); 0120 e.setAttribute("value", c.name(QColor::HexArgb)); 0121 } 0122 0123 bool findOnlyElement(const QDomElement &parent, const QString &tag, QDomElement *el, QStringList *errorMessages) 0124 { 0125 QDomNodeList list = parent.elementsByTagName(tag); 0126 if (list.size() != 1 || !list.at(0).isElement()) { 0127 QString msg = i18n("Could not find \"%1\" XML tag in \"%2\"", tag, parent.tagName()); 0128 if (errorMessages) { 0129 *errorMessages << msg; 0130 } else { 0131 warnKrita << msg; 0132 } 0133 0134 return false; 0135 } 0136 0137 *el = list.at(0).toElement(); 0138 return true; 0139 } 0140 0141 bool removeElements(QDomElement &parent, const QString &tag) { 0142 QDomNodeList list = parent.elementsByTagName(tag); 0143 KIS_SAFE_ASSERT_RECOVER_NOOP(list.size() <= 1); 0144 0145 for (int i = 0; i < list.size(); i++) { 0146 parent.removeChild(list.at(i)); 0147 } 0148 0149 return list.size() > 0; 0150 } 0151 0152 namespace Private { 0153 bool checkType(const QDomElement &e, const QString &expectedType) 0154 { 0155 QString type = e.attribute("type", "unknown-type"); 0156 if (type != expectedType) { 0157 warnKrita << i18n("Error: incorrect type (%2) for value %1. Expected %3", e.tagName(), type, expectedType); 0158 return false; 0159 } 0160 0161 return true; 0162 } 0163 } 0164 0165 bool loadValue(const QDomElement &e, float *v) 0166 { 0167 if (!Private::checkType(e, "value")) return false; 0168 *v = toDouble(e.attribute("value", "0")); 0169 return true; 0170 } 0171 0172 bool loadValue(const QDomElement &e, double *v) 0173 { 0174 if (!Private::checkType(e, "value")) return false; 0175 *v = toDouble(e.attribute("value", "0")); 0176 return true; 0177 } 0178 0179 bool loadValue(const QDomElement &e, QSize *size) 0180 { 0181 if (!Private::checkType(e, "size")) return false; 0182 0183 size->setWidth(toInt(e.attribute("w", "0"))); 0184 size->setHeight(toInt(e.attribute("h", "0"))); 0185 0186 return true; 0187 } 0188 0189 bool loadValue(const QDomElement &e, QRect *rc) 0190 { 0191 if (!Private::checkType(e, "rect")) return false; 0192 0193 rc->setX(toInt(e.attribute("x", "0"))); 0194 rc->setY(toInt(e.attribute("y", "0"))); 0195 rc->setWidth(toInt(e.attribute("w", "0"))); 0196 rc->setHeight(toInt(e.attribute("h", "0"))); 0197 0198 return true; 0199 } 0200 0201 bool loadValue(const QDomElement &e, QRectF *rc) 0202 { 0203 if (!Private::checkType(e, "rectf")) return false; 0204 0205 rc->setX(toInt(e.attribute("x", "0"))); 0206 rc->setY(toInt(e.attribute("y", "0"))); 0207 rc->setWidth(toInt(e.attribute("w", "0"))); 0208 rc->setHeight(toInt(e.attribute("h", "0"))); 0209 0210 return true; 0211 } 0212 0213 bool loadValue(const QDomElement &e, QPoint *pt) 0214 { 0215 if (!Private::checkType(e, "point")) return false; 0216 0217 pt->setX(toInt(e.attribute("x", "0"))); 0218 pt->setY(toInt(e.attribute("y", "0"))); 0219 0220 return true; 0221 } 0222 0223 bool loadValue(const QDomElement &e, QPointF *pt) 0224 { 0225 if (!Private::checkType(e, "pointf")) return false; 0226 0227 pt->setX(toDouble(e.attribute("x", "0"))); 0228 pt->setY(toDouble(e.attribute("y", "0"))); 0229 0230 return true; 0231 } 0232 0233 bool loadValue(const QDomElement &e, QVector3D *pt) 0234 { 0235 if (!Private::checkType(e, "vector3d")) return false; 0236 0237 pt->setX(toDouble(e.attribute("x", "0"))); 0238 pt->setY(toDouble(e.attribute("y", "0"))); 0239 pt->setZ(toDouble(e.attribute("z", "0"))); 0240 0241 return true; 0242 } 0243 0244 bool loadValue(const QDomElement &e, QTransform *t) 0245 { 0246 if (!Private::checkType(e, "transform")) return false; 0247 0248 qreal m11 = toDouble(e.attribute("m11", "1.0")); 0249 qreal m12 = toDouble(e.attribute("m12", "0.0")); 0250 qreal m13 = toDouble(e.attribute("m13", "0.0")); 0251 0252 qreal m21 = toDouble(e.attribute("m21", "0.0")); 0253 qreal m22 = toDouble(e.attribute("m22", "1.0")); 0254 qreal m23 = toDouble(e.attribute("m23", "0.0")); 0255 0256 qreal m31 = toDouble(e.attribute("m31", "0.0")); 0257 qreal m32 = toDouble(e.attribute("m32", "0.0")); 0258 qreal m33 = toDouble(e.attribute("m33", "1.0")); 0259 0260 t->setMatrix( 0261 m11, m12, m13, 0262 m21, m22, m23, 0263 m31, m32, m33); 0264 0265 return true; 0266 } 0267 0268 bool loadValue(const QDomElement &e, QString *value) 0269 { 0270 if (!Private::checkType(e, "value")) return false; 0271 *value = e.attribute("value", "no-value"); 0272 return true; 0273 } 0274 0275 bool loadValue(const QDomElement &e, QColor *value) 0276 { 0277 if (!Private::checkType(e, "qcolor")) return false; 0278 value->setNamedColor(e.attribute("value", "#FFFF0000")); 0279 return true; 0280 } 0281 0282 QDomElement findElementByAttibute(QDomNode parent, 0283 const QString &tag, 0284 const QString &attribute, 0285 const QString &key) 0286 { 0287 QDomNode node = parent.firstChild(); 0288 0289 while (!node.isNull()) { 0290 QDomElement el = node.toElement(); 0291 0292 if (!el.isNull() && el.tagName() == tag) { 0293 QString value = el.attribute(attribute, "<undefined>"); 0294 if (value == key) return el; 0295 } 0296 0297 node = node.nextSibling(); 0298 } 0299 0300 return QDomElement(); 0301 } 0302 0303 0304 }