1 /*
2  *  Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.org>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 #ifndef _KIS_PROPERTIES_CONFIGURATION_H_
19 #define _KIS_PROPERTIES_CONFIGURATION_H_
20 
21 #include <QString>
22 #include <QMap>
23 #include <QVariant>
24 #include <kis_debug.h>
25 #include <kis_cubic_curve.h>
26 #include <KoColor.h>
27 
28 class QDomElement;
29 class QDomDocument;
30 
31 #include "kis_serializable_configuration.h"
32 #include "kritaimage_export.h"
33 #include "kis_types.h"
34 
35 
36 /**
37  * KisPropertiesConfiguration is a map-based properties class that can
38  * be serialized and deserialized.
39  *
40  * It differs from the base class KisSerializableConfiguration in that
41  * it provides a number of convenience methods to get at the data and
42  */
43 class KRITAIMAGE_EXPORT KisPropertiesConfiguration : public KisSerializableConfiguration
44 {
45 
46 public:
47 
48     /**
49      * Create a new properties  config.
50      */
51     KisPropertiesConfiguration();
52     ~KisPropertiesConfiguration() override;
53 
54     /**
55      * Deep copy the properties \p rhs
56      */
57     KisPropertiesConfiguration(const KisPropertiesConfiguration& rhs);
58 
59     /**
60      * Deep copy the properties \p rhs
61      */
62     KisPropertiesConfiguration& operator=(const KisPropertiesConfiguration& rhs);
63 
64 public:
65 
66 
67     /**
68      * Fill the properties  configuration object from the XML encoded representation in s.
69      * This function use the "Legacy" style XML of the 1.x .kra file format.
70      * @param xml the string that will be parsed as xml
71      * @param clear if true, the properties map will be emptied.
72      * @return true is the xml document could be parsed
73      */
74     bool fromXML(const QString& xml, bool clear = true) override;
75 
76     /**
77      * Fill the properties  configuration object from the XML encoded representation in s.
78      * This function use the "Legacy" style XML  of the 1.x .kra file format.
79      *
80      * Note: the existing properties will not be cleared
81      */
82     void fromXML(const QDomElement&) override;
83 
84     /**
85      * Create a serialized version of this properties  config
86      * This function use the "Legacy" style XML  of the 1.x .kra file format.
87      */
88     void toXML(QDomDocument&, QDomElement&) const override;
89 
90     /**
91      * Create a serialized version of this properties  config
92      * This function use the "Legacy" style XML  of the 1.x .kra file format.
93      */
94     QString toXML() const override;
95 
96     /**
97      * @return true if the map contains a property with the specified name
98      */
99     virtual bool hasProperty(const QString& name) const;
100 
101     /**
102      * Set the property with name to value.
103      */
104     virtual void setProperty(const QString & name, const QVariant & value);
105 
106     /**
107      * Set value to the value associated with property name
108      *
109      * XXX: API alert: a setter that is prefixed with get?
110      *
111      * @return false if the specified property did not exist.
112      */
113     virtual bool getProperty(const QString & name, QVariant & value) const;
114 
115     virtual QVariant getProperty(const QString & name) const;
116 
117     template <typename T>
getPropertyLazy(const QString & name,const T & defaultValue)118         T getPropertyLazy(const QString & name, const T &defaultValue) const {
119         QVariant value = getProperty(name);
120         return value.isValid() ? value.value<T>() : defaultValue;
121     }
122 
getPropertyLazy(const QString & name,const char * defaultValue)123     QString getPropertyLazy(const QString & name, const char *defaultValue) const {
124         return getPropertyLazy(name, QString(defaultValue));
125     }
126 
127     int getInt(const QString & name, int def = 0) const;
128 
129     double getDouble(const QString & name, double def = 0.0) const;
130 
131     float getFloat(const QString& name, float def = 0.0) const;
132 
133     bool getBool(const QString & name, bool def = false) const;
134 
135     QString getString(const QString & name, const QString & def = QString()) const;
136 
137     KisCubicCurve getCubicCurve(const QString & name, const KisCubicCurve & curve = KisCubicCurve()) const;
138 
139     /**
140      * @brief getColor fetch the given property as a KoColor.
141      *
142      * The color can be stored as
143      * <ul>
144      * <li>A KoColor
145      * <li>A QColor
146      * <li>A string that can be parsed as an XML color definition
147      * <li>A string that QColor can convert to a color (see https://doc.qt.io/qt-5/qcolor.html#setNamedColor)
148      * <li>An integer that QColor can convert to a color
149      * </ul>
150      *
151      * @param name the name of the property
152      * @param color the default value to be returned if the @param name does not exist.
153      * @return returns the named property as a KoColor if the value can be converted to a color,
154      * otherwise a empty KoColor is returned.
155      */
156     KoColor getColor(const QString& name, const KoColor& color = KoColor()) const;
157 
158     QMap<QString, QVariant> getProperties() const;
159 
160     /// Clear the map of properties
161     void clearProperties();
162 
163     /// Marks a property that should not be saved by toXML
164     void setPropertyNotSaved(const QString & name);
165 
166     void removeProperty(const QString & name);
167 
168     /**
169      * Get the keys of all the properties in the object
170      */
171     virtual QList<QString> getPropertiesKeys() const;
172 
173     /**
174      * Get a set of properties, which keys are prefixed with \p prefix. The settings object
175      * \p config will have all these properties with the prefix stripped from them.
176      */
177     void getPrefixedProperties(const QString &prefix, KisPropertiesConfiguration *config) const;
178 
179     /**
180      * A convenience override
181      */
182     void getPrefixedProperties(const QString &prefix, KisPropertiesConfigurationSP config) const;
183 
184     /**
185      * Takes all the properties from \p config, adds \p prefix to all their keys and puths them
186      * into this properties object
187      */
188     void setPrefixedProperties(const QString &prefix, const KisPropertiesConfiguration *config);
189 
190     /**
191      * A convenience override
192      */
193     void setPrefixedProperties(const QString &prefix, const KisPropertiesConfigurationSP config);
194 
195     static QString escapeString(const QString &string);
196     static QString unescapeString(const QString &string);
197 
198     void setProperty(const QString &name, const QStringList &value);
199     QStringList getStringList(const QString &name, const QStringList &defaultValue = QStringList()) const;
200     QStringList getPropertyLazy(const QString &name, const QStringList &defaultValue) const;
201 
202     /**
203      * Structural comparison between two instances.
204      */
205     virtual bool compareTo(const KisPropertiesConfiguration* rhs) const;
206 
207 public:
208 
209     void dump() const;
210 
211 private:
212 
213     struct Private;
214     Private* const d;
215 };
216 
217 class KRITAIMAGE_EXPORT KisPropertiesConfigurationFactory : public KisSerializableConfigurationFactory
218 {
219 public:
220     KisPropertiesConfigurationFactory();
221     ~KisPropertiesConfigurationFactory() override;
222     KisSerializableConfigurationSP createDefault() override;
223     KisSerializableConfigurationSP create(const QDomElement& e) override;
224 private:
225     struct Private;
226     Private* const d;
227 };
228 
229 #endif
230