File indexing completed on 2024-05-12 16:02:31

0001 /*
0002    SPDX-FileCopyrightText: 2006-2007 Boudewijn Rempt <boud@valdyas.org>
0003    SPDX-FileCopyrightText: 2006-2007 Thomas Zander <zander@kde.org>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 #ifndef _KO_PROPERTIES_H
0008 #define _KO_PROPERTIES_H
0009 
0010 #include <QString>
0011 #include <QMap>
0012 #include <QVariant>
0013 #include "kritawidgetutils_export.h"
0014 
0015 class QDomElement;
0016 
0017 /**
0018  * A KoProperties is the (de-)serializable representation of
0019  * a key-value map. The serialisation format is XML.
0020  */
0021 class KRITAWIDGETUTILS_EXPORT KoProperties
0022 {
0023 public:
0024 
0025     /**
0026      * Create a new properties object
0027      */
0028     KoProperties();
0029 
0030     /**
0031      * Copy constructor
0032      */
0033     KoProperties(const KoProperties &other);
0034 
0035     ~KoProperties();
0036 
0037 public:
0038 
0039     /**
0040      * Fill the properties object from the XML dom node.
0041      *
0042      * load() does not touch existing properties if loading fails.
0043      *
0044      * @param root the root node of the properties subtree.
0045      */
0046     void load(const QDomElement &root);
0047 
0048     /**
0049      * Fill the properties object from the XML encoded
0050      * representation in string.
0051      *
0052      * load() does not touch existing properties if loading fails.
0053      *
0054      * @param string the stored properties.
0055      * @return false if loading failing, true if it succeeded
0056      */
0057     bool load(const QString &string);
0058 
0059     /**
0060      * Returns an iterator over the properties. The iterator is not
0061      * suitable for adding or removing properties.
0062      */
0063     QMapIterator<QString, QVariant> propertyIterator() const;
0064 
0065     /**
0066      * @return true if this KoProperties object does not contain any
0067      * properties.
0068      */
0069     bool isEmpty() const;
0070 
0071     /**
0072      * @brief Create a serialized version of these properties (as XML) with root as the root element.
0073      * @param root as the root element in the generated XML.
0074      */
0075     QString store(const QString &root) const;
0076 
0077     void save(QDomElement &root) const;
0078 
0079     /**
0080      * Set the property with name to value.
0081      */
0082     void setProperty(const QString &name, const QVariant &value);
0083 
0084     /**
0085      * Set value to the value associated with property name
0086      * @return false if the specified property did not exist.
0087      */
0088     bool property(const QString &name, QVariant &value) const;
0089 
0090     /**
0091      * Return a property by name, wrapped in a QVariant.
0092      * A typical usage:
0093      *  @code
0094      *      KoProperties *props = new KoProperties();
0095      *      props->setProperty("name", "Marcy");
0096      *      props->setProperty("age", 25);
0097      *      QString name = props->property("name").toString();
0098      *      int age = props->property("age").toInt();
0099      *  @endcode
0100      * @return a property by name, wrapped in a QVariant.
0101      * @param name the name (or key) with which the variant was registered.
0102      * @see intProperty() stringProperty()
0103      */
0104     QVariant property(const QString &name) const;
0105 
0106     /**
0107      * Return an integer property by name.
0108      * A typical usage:
0109      *  @code
0110      *      KoProperties *props = new KoProperties();
0111      *      props->setProperty("age", 25);
0112      *      int age = props->intProperty("age");
0113      *  @endcode
0114      * @return an integer property by name
0115      * @param name the name (or key) with which the variant was registered.
0116      * @param defaultValue the default value, should there not be any property by the name this will be returned.
0117      * @see property() stringProperty()
0118      */
0119     int intProperty(const QString &name, int defaultValue = 0) const;
0120 
0121     /**
0122      * Return a qreal property by name.
0123      * @param name the name (or key) with which the variant was registered.
0124      * @param defaultValue the default value, should there not be any property by the name this will be returned.
0125      */
0126     qreal doubleProperty(const QString &name, qreal defaultValue = 0.0) const;
0127 
0128     /**
0129      * Return a boolean property by name.
0130      * @param name the name (or key) with which the variant was registered.
0131      * @param defaultValue the default value, should there not be any property by the name this will be returned.
0132      */
0133     bool boolProperty(const QString &name, bool defaultValue = false) const;
0134 
0135     /**
0136      * Return an QString property by name.
0137      * A typical usage:
0138      *  @code
0139      *      KoProperties *props = new KoProperties();
0140      *      props->setProperty("name", "Marcy");
0141      *      QString name = props->stringProperty("name");
0142      *  @endcode
0143      * @return an QString property by name
0144      * @param name the name (or key) with which the variant was registered.
0145      * @see property() intProperty()
0146      * @param defaultValue the default value, should there not be any property by the name this will be returned.
0147      */
0148     QString stringProperty(const QString &name, const QString &defaultValue = QString()) const;
0149 
0150     /**
0151      * Returns true if the specified key is present in this properties
0152      * object.
0153      */
0154     bool contains(const QString &key) const;
0155 
0156     /**
0157      * Returns the value assocatied with the specified key if this
0158      * properties object contains the specified key; otherwise return
0159      * an empty QVariant.
0160      */
0161     QVariant value(const QString &key) const;
0162 
0163     bool operator==(const KoProperties &other) const;
0164 
0165 private:
0166 
0167     class Private;
0168     Private * const d;
0169 };
0170 
0171 #endif // _KO_PROPERTIES_H