File indexing completed on 2024-05-12 15:59:17

0001 /*
0002  *  SPDX-FileCopyrightText: 2007 Cyrille Berger <cberger@cberger.net>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 #ifndef _KIS_META_DATA_VALUE_H_
0007 #define _KIS_META_DATA_VALUE_H_
0008 
0009 #include <QList>
0010 #include <QMap>
0011 
0012 #include <kritametadata_export.h>
0013 #include <boost/operators.hpp>
0014 
0015 class QVariant;
0016 
0017 namespace KisMetaData
0018 {
0019 
0020 struct Rational : public boost::equality_comparable<Rational>
0021 {
0022     explicit Rational(qint32 n = 0, qint32 d = 1) : numerator(n), denominator(d) {}
0023     qint32 numerator;
0024     qint32 denominator;
0025     bool operator==(const Rational& ur) const {
0026         return numerator == ur.numerator && denominator == ur.denominator;
0027     }
0028 };
0029 
0030 /**
0031  * Value is build on top of QVariant to extend it to support the various types
0032  * and extensions through property qualifiers.
0033  */
0034 class KRITAMETADATA_EXPORT Value
0035 {
0036     struct Private;
0037 public:
0038     /// Define the possible value type
0039     enum ValueType {
0040         Invalid,
0041         Variant,
0042         OrderedArray,
0043         UnorderedArray,
0044         AlternativeArray,
0045         LangArray,
0046         Structure,
0047         Rational
0048     };
0049 public:
0050     Value();
0051     Value(const QVariant& value);
0052     /**
0053     * @param type is one of OrderedArray, UnorderedArray, AlternativeArray
0054     * or LangArray
0055     */
0056     Value(const QList<Value>& array, ValueType type = OrderedArray);
0057     Value(const QMap<QString, Value>& structure);
0058     Value(const KisMetaData::Rational& rational);
0059     Value(const Value& v);
0060     Value& operator=(const Value& v);
0061     ~Value();
0062 public:
0063     void addPropertyQualifier(const QString& _name, const Value&);
0064     const QMap<QString, Value>& propertyQualifiers() const;
0065 public:
0066     /// @return the type of this Value
0067     ValueType type() const;
0068     /**
0069     * @return the value as a double, or null if it's not possible, rationals are evaluated
0070     */
0071     double asDouble() const;
0072     /**
0073     * @return the value as an integer, or null if it's not possible, rationals are evaluated
0074     */
0075     int asInteger() const;
0076     /**
0077     * @return the Variant hold by this Value, or an empty QVariant if this Value is not a Variant
0078     */
0079     QVariant asVariant() const;
0080     /**
0081     * Set this Value to the given variant, or does nothing if this Value is not a Variant.
0082     * @return true if the value was changed
0083     */
0084     bool setVariant(const QVariant& variant);
0085     bool setStructureVariant(const QString& fieldNAme, const QVariant& variant);
0086     bool setArrayVariant(int index, const QVariant& variant);
0087     /**
0088     * @return the Rational hold by this Value, or a null rational if this Value is not
0089     * an Rational
0090     */
0091     KisMetaData::Rational asRational() const;
0092     /**
0093     * @return the array hold by this Value, or an empty array if this Value is not either
0094     * an OrderedArray, UnorderedArray or AlternativeArray
0095     */
0096     QList<KisMetaData::Value> asArray() const;
0097     /**
0098     * @return true if this Value is either an OrderedArray, UnorderedArray or AlternativeArray
0099     */
0100     bool isArray() const;
0101     /**
0102     * @return the structure hold by this Value, or an empty structure if this Value is not a Structure
0103     */
0104     QMap<QString, KisMetaData::Value> asStructure() const;
0105     /**
0106     * It's a convenient function that build a map from a LangArray using the property
0107     * qualifier "xml:lang" for the key of the map.
0108     */
0109     QMap<QString, KisMetaData::Value> asLangArray() const;
0110     QString toString() const;
0111 public:
0112     bool operator==(const Value&) const;
0113     Value& operator+=(const Value&);
0114 private:
0115     Private* const d;
0116 };
0117 }
0118 
0119 
0120 KRITAMETADATA_EXPORT QDebug operator<<(QDebug debug, const KisMetaData::Value &v);
0121 
0122 #endif