File indexing completed on 2024-06-16 04:17:31

0001 /*
0002  *  SPDX-FileCopyrightText: 2022 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "KisSensorData.h"
0008 
0009 #include <KisDynamicSensorIds.h>
0010 
0011 #include <QDomDocument>
0012 #include <QDomElement>
0013 
0014 KisSensorData::KisSensorData(const KoID &sensorId)
0015     : id(sensorId),
0016       curve(DEFAULT_CURVE_STRING)
0017 {
0018 }
0019 
0020 KisSensorData::~KisSensorData()
0021 {
0022 }
0023 
0024 void KisSensorData::setBaseCurveRange(const QRectF &rect)
0025 {
0026     Q_UNUSED(rect);
0027     KIS_SAFE_ASSERT_RECOVER_NOOP(0 && "setBaseCurveRange is not implemented for standard Krita sensors");
0028 }
0029 
0030 QRectF KisSensorData::baseCurveRange() const
0031 {
0032     return QRectF(0.0,0.0,1.0,1.0);
0033 }
0034 
0035 void KisSensorData::write(QDomDocument& doc, QDomElement &e) const
0036 {
0037     e.setAttribute("id", id.id());
0038     if (curve != DEFAULT_CURVE_STRING) {
0039         QDomElement curve_elt = doc.createElement("curve");
0040         QDomText text = doc.createTextNode(curve);
0041         curve_elt.appendChild(text);
0042         e.appendChild(curve_elt);
0043     }
0044 }
0045 
0046 void KisSensorData::read(const QDomElement& e)
0047 {
0048     KIS_ASSERT(e.attribute("id", "") == id.id());
0049     QDomElement curve_elt = e.firstChildElement("curve");
0050     if (!curve_elt.isNull()) {
0051         curve = curve_elt.text();
0052     } else {
0053         curve = DEFAULT_CURVE_STRING;
0054     }
0055 }
0056 
0057 void KisSensorData::reset()
0058 {
0059     *this = KisSensorData(id);
0060 }
0061 
0062 KisSensorWithLengthData::KisSensorWithLengthData(const KoID &sensorId, const QLatin1String &lengthTag)
0063     : KisSensorData(sensorId)
0064     , m_lengthTag(lengthTag.isNull() ? QLatin1Literal("length") : lengthTag)
0065 {
0066     if (sensorId == FadeId) {
0067         isPeriodic = false;
0068         length = 1000;
0069     } else if (sensorId == DistanceId) {
0070         isPeriodic = false;
0071         length = 30;
0072     } else if (sensorId == TimeId) {
0073         isPeriodic = false;
0074         length = 30;
0075     } else {
0076         qFatal("This sensor type \"%s\" has no length associated!", sensorId.id().toLatin1().data());
0077     }
0078 }
0079 
0080 void KisSensorWithLengthData::write(QDomDocument &doc, QDomElement &e) const
0081 {
0082     KisSensorData::write(doc, e);
0083     e.setAttribute("periodic", isPeriodic);
0084     e.setAttribute(m_lengthTag, length);
0085 }
0086 
0087 void KisSensorWithLengthData::read(const QDomElement &e)
0088 {
0089     reset();
0090     KisSensorData::read(e);
0091 
0092     if (e.hasAttribute("periodic")) {
0093         isPeriodic = e.attribute("periodic").toInt();
0094     }
0095 
0096     if (e.hasAttribute(m_lengthTag)) {
0097         length = e.attribute(m_lengthTag).toInt();
0098     }
0099 }
0100 
0101 void KisSensorWithLengthData::reset()
0102 {
0103     *this = KisSensorWithLengthData(id, m_lengthTag);
0104 }
0105 
0106 KisDrawingAngleSensorData::KisDrawingAngleSensorData()
0107     : KisSensorData(DrawingAngleId)
0108 {
0109 }
0110 
0111 void KisDrawingAngleSensorData::write(QDomDocument &doc, QDomElement &e) const
0112 {
0113     KisSensorData::write(doc, e);
0114     e.setAttribute("fanCornersEnabled", fanCornersEnabled);
0115     e.setAttribute("fanCornersStep", fanCornersStep);
0116     e.setAttribute("angleOffset", angleOffset);
0117     e.setAttribute("lockedAngleMode", lockedAngleMode);
0118 }
0119 
0120 void KisDrawingAngleSensorData::read(const QDomElement &e)
0121 {
0122     reset();
0123     KisSensorData::read(e);
0124 
0125     if (e.hasAttribute("fanCornersEnabled")) {
0126         fanCornersEnabled = e.attribute("fanCornersEnabled").toInt();
0127     }
0128     if (e.hasAttribute("fanCornersStep")) {
0129         fanCornersStep = e.attribute("fanCornersStep").toInt();
0130     }
0131     if (e.hasAttribute("angleOffset")) {
0132         angleOffset = e.attribute("angleOffset").toInt();
0133     }
0134     if (e.hasAttribute("lockedAngleMode")) {
0135         lockedAngleMode = e.attribute("lockedAngleMode").toInt();
0136     }
0137 }
0138 
0139 void KisDrawingAngleSensorData::reset()
0140 {
0141     *this = KisDrawingAngleSensorData();
0142 }