diff --git a/libs/image/kis_properties_configuration.h b/libs/image/kis_properties_configuration.h index 0b8ba8169e..c7e6004dc3 100644 --- a/libs/image/kis_properties_configuration.h +++ b/libs/image/kis_properties_configuration.h @@ -1,164 +1,164 @@ /* * Copyright (c) 2006 Boudewijn Rempt * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef _KIS_PROPERTIES_CONFIGURATION_H_ #define _KIS_PROPERTIES_CONFIGURATION_H_ #include #include #include #include #include #include class QDomElement; class QDomDocument; #include "kis_serializable_configuration.h" #include "kritaimage_export.h" #include "kis_types.h" /** * KisPropertiesConfiguration is a map-based properties class that can * be serialized and deserialized. * * It differs from the base class KisSerializableConfiguration in that * it provides a number of convenience methods to get at the data and */ class KRITAIMAGE_EXPORT KisPropertiesConfiguration : public KisSerializableConfiguration { public: /** * Create a new properties config. */ KisPropertiesConfiguration(); virtual ~KisPropertiesConfiguration(); /** * Deep copy the properties configFile */ KisPropertiesConfiguration(const KisPropertiesConfiguration& rhs); public: /** * Fill the properties configuration object from the XML encoded representation in s. * This function use the "Legacy" style XML of the 1.x .kra file format. * @param xml the string that will be parsed as xml * @param clear if true, the properties map will be emptied. * @return true is the xml document could be parsed */ virtual bool fromXML(const QString& xml, bool clear = true); /** * Fill the properties configuration object from the XML encoded representation in s. * This function use the "Legacy" style XML of the 1.x .kra file format. * * Note: the existing properties will not be cleared */ virtual void fromXML(const QDomElement&); /** * Create a serialized version of this properties config * This function use the "Legacy" style XML of the 1.x .kra file format. */ virtual void toXML(QDomDocument&, QDomElement&) const; /** * Create a serialized version of this properties config * This function use the "Legacy" style XML of the 1.x .kra file format. */ virtual QString toXML() const; /** * @return true if the map contains a property with the specified name */ bool hasProperty(const QString& name) const; /** * Set the property with name to value. */ virtual void setProperty(const QString & name, const QVariant & value); /** * Set value to the value associated with property name * * XXX: API alert: a setter that is prefixed with get? * * @return false if the specified property did not exist. */ virtual bool getProperty(const QString & name, QVariant & value) const; virtual QVariant getProperty(const QString & name) const; template T getPropertyLazy(const QString & name, const T &defaultValue) const { QVariant value = getProperty(name); return value.isValid() ? value.value() : defaultValue; } int getInt(const QString & name, int def = 0) const; double getDouble(const QString & name, double def = 0.0) const; float getFloat(const QString& name, float def = 0.0) const; bool getBool(const QString & name, bool def = false) const; QString getString(const QString & name, const QString & def = QString()) const; KisCubicCurve getCubicCurve(const QString & name, const KisCubicCurve & curve = KisCubicCurve()) const; KoColor getColor(const QString& name, const KoColor& color = KoColor()) const; QMap getProperties() const; /// Clear the map of properties void clearProperties(); - ///Marks a property that should not be saved by toXML + /// Marks a property that should not be saved by toXML void setPropertyNotSaved(const QString & name); void removeProperty(const QString & name); public: void dump() const; private: struct Private; Private* const d; }; class KRITAIMAGE_EXPORT KisPropertiesConfigurationFactory : public KisSerializableConfigurationFactory { public: KisPropertiesConfigurationFactory(); virtual ~KisPropertiesConfigurationFactory(); virtual KisSerializableConfigurationSP createDefault(); virtual KisSerializableConfigurationSP create(const QDomElement& e); private: struct Private; Private* const d; }; #endif diff --git a/libs/image/tests/kis_properties_configuration_test.cpp b/libs/image/tests/kis_properties_configuration_test.cpp index 6c870a8fb0..87ca007a4e 100644 --- a/libs/image/tests/kis_properties_configuration_test.cpp +++ b/libs/image/tests/kis_properties_configuration_test.cpp @@ -1,96 +1,111 @@ /* * Copyright (c) 2007 Cyrille Berger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "kis_properties_configuration_test.h" #include #include "kis_properties_configuration.h" KisPropertiesConfigurationTest::KisPropertiesConfigurationTest() : v1(10), v2("hello"), v3(1242.0), v4(true) { QList pts; pts.push_back(QPointF(0.2, 0.3)); pts.push_back(QPointF(0.5, 0.7)); v5.setPoints(pts); } void KisPropertiesConfigurationTest::testSerialization() { KisPropertiesConfigurationSP config = createConfig(); QString xml = config->toXML(); KisPropertiesConfigurationSP decodedConfig = new KisPropertiesConfiguration(); decodedConfig->fromXML(xml); testConfig(decodedConfig); } void KisPropertiesConfigurationTest::testSetGet() { KisPropertiesConfigurationSP config = createConfig(); testConfig(config); } void KisPropertiesConfigurationTest::testDefaultValues() { KisPropertiesConfigurationSP config = new KisPropertiesConfiguration(); QVERIFY(config->getInt("bouh", v1) == v1); QVERIFY(config->getString("bouh", v2) == v2); QVERIFY(config->getDouble("bouh", v3) == v3); QVERIFY(config->getBool("bouh", v4) == v4); QVERIFY(config->getCubicCurve("bouh", v5) == v5); } KisPropertiesConfigurationSP KisPropertiesConfigurationTest::createConfig() { KisPropertiesConfigurationSP config = new KisPropertiesConfiguration(); config->setProperty("v1", v1); config->setProperty("v2", v2); config->setProperty("v3", v3); config->setProperty("v4", v4); config->setProperty("v5", qVariantFromValue(v5)); return config; } void KisPropertiesConfigurationTest::testConfig(KisPropertiesConfigurationSP config) { QVERIFY(config->getInt("v1", 0) == v1); QVERIFY(config->getString("v2", QString()) == v2); QVERIFY(config->getDouble("v3", 0.0) == v3); QVERIFY(config->getBool("v4", !v4) == v4); QVERIFY(config->getCubicCurve("v5") == v5); } void KisPropertiesConfigurationTest::testNotSavedValues() { KisPropertiesConfigurationSP config = createConfig(); config->setPropertyNotSaved("v3"); testConfig(config); QString s = config->toXML(); config = new KisPropertiesConfiguration(); config->fromXML(s); QVERIFY(config->getInt("v1", 0) == v1); QVERIFY(config->getString("v2", QString()) == v2); QVERIFY(config->hasProperty("v3") == false); QVERIFY(config->getBool("v4", !v4) == v4); QVERIFY(config->getCubicCurve("v5") == v5); } +void KisPropertiesConfigurationTest::testCopy() +{ + KisPropertiesConfigurationSP p1 = createConfig(); + p1->setProperty("v6", "bla"); + p1->setPropertyNotSaved("v6"); + KisPropertiesConfiguration config = KisPropertiesConfiguration(*p1.data()); + QVERIFY(config.getInt("v1", 0) == v1); + QVERIFY(config.getString("v2", QString()) == v2); + QVERIFY(config.getDouble("v3", 0.0) == v3); + QVERIFY(config.getBool("v4", !v4) == v4); + QVERIFY(config.getCubicCurve("v5") == v5); + QVERIFY(config.hasProperty("v6") == false); + +} + QTEST_MAIN(KisPropertiesConfigurationTest) diff --git a/libs/image/tests/kis_properties_configuration_test.h b/libs/image/tests/kis_properties_configuration_test.h index c9b26d4551..7528c8cebe 100644 --- a/libs/image/tests/kis_properties_configuration_test.h +++ b/libs/image/tests/kis_properties_configuration_test.h @@ -1,49 +1,50 @@ /* * Copyright (c) 2007 Cyrille Berger * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KIS_SERIALIZABLE_CONFIGURATION_TEST_H #define KIS_SERIALIZABLE_CONFIGURATION_TEST_H #include #include "kis_cubic_curve.h" #include "kis_properties_configuration.h" class KisPropertiesConfigurationTest : public QObject { Q_OBJECT public: KisPropertiesConfigurationTest(); private Q_SLOTS: void testSetGet(); void testSerialization(); void testDefaultValues(); void testNotSavedValues(); + void testCopy(); private: KisPropertiesConfigurationSP createConfig(); void testConfig(KisPropertiesConfigurationSP config); private: int v1; QString v2; double v3; bool v4; KisCubicCurve v5; }; #endif