1 /*
2  *  Copyright (c) 2007 Cyrille Berger <cberger@cberger.net>
3  *
4  *  This library is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU Lesser General Public License as published by
6  *  the Free Software Foundation; either version 2.1 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This library 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 Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser 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_META_DATA_VALUE_H_
19 #define _KIS_META_DATA_VALUE_H_
20 
21 #include <QList>
22 #include <QMap>
23 
24 #include <kritametadata_export.h>
25 #include <boost/operators.hpp>
26 
27 class QVariant;
28 
29 namespace KisMetaData
30 {
31 
32 struct Rational : public boost::equality_comparable<Rational>
33 {
numeratorRational34     explicit Rational(qint32 n = 0, qint32 d = 1) : numerator(n), denominator(d) {}
35     qint32 numerator;
36     qint32 denominator;
37     bool operator==(const Rational& ur) const {
38         return numerator == ur.numerator && denominator == ur.denominator;
39     }
40 };
41 
42 /**
43  * Value is build on top of QVariant to extend it to support the various types
44  * and extensions through property qualifiers.
45  */
46 class KRITAMETADATA_EXPORT Value
47 {
48     struct Private;
49 public:
50     /// Define the possible value type
51     enum ValueType {
52         Invalid,
53         Variant,
54         OrderedArray,
55         UnorderedArray,
56         AlternativeArray,
57         LangArray,
58         Structure,
59         Rational
60     };
61 public:
62     Value();
63     Value(const QVariant& value);
64     /**
65     * @param type is one of OrderedArray, UnorderedArray, AlternativeArray
66     * or LangArray
67     */
68     Value(const QList<Value>& array, ValueType type = OrderedArray);
69     Value(const QMap<QString, Value>& structure);
70     Value(const KisMetaData::Rational& rational);
71     Value(const Value& v);
72     Value& operator=(const Value& v);
73     ~Value();
74 public:
75     void addPropertyQualifier(const QString& _name, const Value&);
76     const QMap<QString, Value>& propertyQualifiers() const;
77 public:
78     /// @return the type of this Value
79     ValueType type() const;
80     /**
81     * @return the value as a double, or null if it's not possible, rationals are evaluated
82     */
83     double asDouble() const;
84     /**
85     * @return the value as an integer, or null if it's not possible, rationals are evaluated
86     */
87     int asInteger() const;
88     /**
89     * @return the Variant hold by this Value, or an empty QVariant if this Value is not a Variant
90     */
91     QVariant asVariant() const;
92     /**
93     * Set this Value to the given variant, or does nothing if this Value is not a Variant.
94     * @return true if the value was changed
95     */
96     bool setVariant(const QVariant& variant);
97     bool setStructureVariant(const QString& fieldNAme, const QVariant& variant);
98     bool setArrayVariant(int index, const QVariant& variant);
99     /**
100     * @return the Rational hold by this Value, or a null rational if this Value is not
101     * an Rational
102     */
103     KisMetaData::Rational asRational() const;
104     /**
105     * @return the array hold by this Value, or an empty array if this Value is not either
106     * an OrderedArray, UnorderedArray or AlternativeArray
107     */
108     QList<KisMetaData::Value> asArray() const;
109     /**
110     * @return true if this Value is either an OrderedArray, UnorderedArray or AlternativeArray
111     */
112     bool isArray() const;
113     /**
114     * @return the structure hold by this Value, or an empty structure if this Value is not a Structure
115     */
116     QMap<QString, KisMetaData::Value> asStructure() const;
117     /**
118     * It's a convenient function that build a map from a LangArray using the property
119     * qualifier "xml:lang" for the key of the map.
120     */
121     QMap<QString, KisMetaData::Value> asLangArray() const;
122     QString toString() const;
123 public:
124     bool operator==(const Value&) const;
125     Value& operator+=(const Value&);
126 private:
127     Private* const d;
128 };
129 }
130 
131 
132 KRITAMETADATA_EXPORT QDebug operator<<(QDebug debug, const KisMetaData::Value &v);
133 
134 #endif
135