File indexing completed on 2024-05-12 15:58:38

0001 /*
0002  *  SPDX-FileCopyrightText: 2006 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #ifndef _KIS_PROPERTIES_CONFIGURATION_H_
0007 #define _KIS_PROPERTIES_CONFIGURATION_H_
0008 
0009 #include <QString>
0010 #include <QMap>
0011 #include <QVariant>
0012 #include <kis_debug.h>
0013 #include <kis_cubic_curve.h>
0014 #include <KoColor.h>
0015 
0016 class QDomElement;
0017 class QDomDocument;
0018 
0019 #include "kis_serializable_configuration.h"
0020 #include "kritaimage_export.h"
0021 #include "kis_types.h"
0022 
0023 
0024 /**
0025  * KisPropertiesConfiguration is a map-based properties class that can
0026  * be serialized and deserialized.
0027  *
0028  * It differs from the base class KisSerializableConfiguration in that
0029  * it provides a number of convenience methods to get at the data and
0030  */
0031 class KRITAIMAGE_EXPORT KisPropertiesConfiguration : public KisSerializableConfiguration
0032 {
0033 
0034 public:
0035 
0036     /**
0037      * Create a new properties  config.
0038      */
0039     KisPropertiesConfiguration();
0040     ~KisPropertiesConfiguration() override;
0041 
0042     /**
0043      * Deep copy the properties \p rhs
0044      */
0045     KisPropertiesConfiguration(const KisPropertiesConfiguration& rhs);
0046 
0047     /**
0048      * Deep copy the properties \p rhs
0049      */
0050     KisPropertiesConfiguration& operator=(const KisPropertiesConfiguration& rhs);
0051 
0052 public:
0053 
0054 
0055     /**
0056      * Fill the properties  configuration object from the XML encoded representation in s.
0057      * This function use the "Legacy" style XML of the 1.x .kra file format.
0058      * @param xml the string that will be parsed as xml
0059      * @param clear if true, the properties map will be emptied.
0060      * @return true is the xml document could be parsed
0061      */
0062     bool fromXML(const QString& xml, bool clear = true) override;
0063 
0064     /**
0065      * Fill the properties  configuration object from the XML encoded representation in s.
0066      * This function use the "Legacy" style XML  of the 1.x .kra file format.
0067      *
0068      * Note: the existing properties will not be cleared
0069      */
0070     void fromXML(const QDomElement&) override;
0071 
0072     /**
0073      * Create a serialized version of this properties  config
0074      * This function use the "Legacy" style XML  of the 1.x .kra file format.
0075      */
0076     void toXML(QDomDocument&, QDomElement&) const override;
0077 
0078     /**
0079      * Create a serialized version of this properties  config
0080      * This function use the "Legacy" style XML  of the 1.x .kra file format.
0081      */
0082     QString toXML() const override;
0083 
0084     /**
0085      * @return true if the map contains a property with the specified name
0086      */
0087     virtual bool hasProperty(const QString& name) const;
0088 
0089     /**
0090      * Set the property with name to value.
0091      */
0092     virtual void setProperty(const QString & name, const QVariant & value);
0093 
0094     /**
0095      * Set value to the value associated with property name
0096      *
0097      * XXX: API alert: a setter that is prefixed with get?
0098      *
0099      * @return false if the specified property did not exist.
0100      */
0101     virtual bool getProperty(const QString & name, QVariant & value) const;
0102 
0103     virtual QVariant getProperty(const QString & name) const;
0104 
0105     template <typename T>
0106         T getPropertyLazy(const QString & name, const T &defaultValue) const {
0107         QVariant value = getProperty(name);
0108         return value.isValid() ? value.value<T>() : defaultValue;
0109     }
0110 
0111     QString getPropertyLazy(const QString & name, const char *defaultValue) const {
0112         return getPropertyLazy(name, QString(defaultValue));
0113     }
0114 
0115     int getInt(const QString & name, int def = 0) const;
0116 
0117     double getDouble(const QString & name, double def = 0.0) const;
0118 
0119     float getFloat(const QString& name, float def = 0.0) const;
0120 
0121     bool getBool(const QString & name, bool def = false) const;
0122 
0123     QString getString(const QString & name, const QString & def = QString()) const;
0124 
0125     KisCubicCurve getCubicCurve(const QString & name, const KisCubicCurve & curve = KisCubicCurve()) const;
0126 
0127     /**
0128      * @brief getColor fetch the given property as a KoColor.
0129      *
0130      * The color can be stored as
0131      * <ul>
0132      * <li>A KoColor
0133      * <li>A QColor
0134      * <li>A string that can be parsed as an XML color definition
0135      * <li>A string that QColor can convert to a color (see https://doc.qt.io/qt-5/qcolor.html#setNamedColor)
0136      * <li>An integer that QColor can convert to a color
0137      * </ul>
0138      *
0139      * @param name the name of the property
0140      * @param color the default value to be returned if the @param name does not exist.
0141      * @return returns the named property as a KoColor if the value can be converted to a color,
0142      * otherwise a empty KoColor is returned.
0143      */
0144     KoColor getColor(const QString& name, const KoColor& color = KoColor()) const;
0145 
0146     QMap<QString, QVariant> getProperties() const;
0147 
0148     /// Clear the map of properties
0149     void clearProperties();
0150 
0151     /// Marks a property that should not be saved by toXML
0152     void setPropertyNotSaved(const QString & name);
0153 
0154     void removeProperty(const QString & name);
0155 
0156     /**
0157      * Get the keys of all the properties in the object
0158      */
0159     virtual QList<QString> getPropertiesKeys() const;
0160 
0161     /**
0162      * Get a set of properties, which keys are prefixed with \p prefix. The settings object
0163      * \p config will have all these properties with the prefix stripped from them.
0164      */
0165     void getPrefixedProperties(const QString &prefix, KisPropertiesConfiguration *config) const;
0166 
0167     /**
0168      * A convenience override
0169      */
0170     void getPrefixedProperties(const QString &prefix, KisPropertiesConfigurationSP config) const;
0171 
0172     /**
0173      * Takes all the properties from \p config, adds \p prefix to all their keys and puths them
0174      * into this properties object
0175      */
0176     void setPrefixedProperties(const QString &prefix, const KisPropertiesConfiguration *config);
0177 
0178     /**
0179      * A convenience override
0180      */
0181     void setPrefixedProperties(const QString &prefix, const KisPropertiesConfigurationSP config);
0182 
0183     static QString escapeString(const QString &string);
0184     static QString unescapeString(const QString &string);
0185 
0186     void setProperty(const QString &name, const QStringList &value);
0187     QStringList getStringList(const QString &name, const QStringList &defaultValue = QStringList()) const;
0188     QStringList getPropertyLazy(const QString &name, const QStringList &defaultValue) const;
0189 
0190     /**
0191      * Structural comparison between two instances.
0192      */
0193     virtual bool compareTo(const KisPropertiesConfiguration* rhs) const;
0194 
0195 public:
0196 
0197     void dump() const;
0198 
0199 private:
0200 
0201     struct Private;
0202     Private* const d;
0203 };
0204 
0205 class KRITAIMAGE_EXPORT KisPropertiesConfigurationFactory : public KisSerializableConfigurationFactory
0206 {
0207 public:
0208     KisPropertiesConfigurationFactory();
0209     ~KisPropertiesConfigurationFactory() override;
0210     KisSerializableConfigurationSP createDefault() override;
0211     KisSerializableConfigurationSP create(const QDomElement& e) override;
0212 private:
0213     struct Private;
0214     Private* const d;
0215 };
0216 
0217 #endif