Krita Source Code Documentation
Loading...
Searching...
No Matches
KoProperties.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006 Boudewijn Rempt <boud@valdyas.org>
3 * SPDX-FileCopyrightText: 2006-2008 Thomas Zander <zander@kde.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8#include "KoProperties.h"
9
10#include <QDomDocument>
11#include <QDataStream>
12#include <QIODevice>
13
14class Q_DECL_HIDDEN KoProperties::Private
15{
16public:
17 QMap<QString, QVariant> properties;
18};
19
21 : d(new Private())
22{
23}
24
26 : d(new Private())
27{
28 d->properties = rhs.d->properties;
29}
30
32{
33 delete d;
34}
35
36QMapIterator<QString, QVariant> KoProperties::propertyIterator() const
37{
38 return QMapIterator<QString, QVariant>(d->properties);
39}
40
41
43{
44 return d->properties.isEmpty();
45}
46
47void KoProperties::load(const QDomElement &root)
48{
49 d->properties.clear();
50
51 QDomElement e = root;
52 QDomNode n = e.firstChild();
53
54 while (!n.isNull()) {
55 // We don't nest elements.
56 QDomElement e = n.toElement();
57 if (!e.isNull()) {
58 if (e.tagName() == "property") {
59 const QString name = e.attribute("name");
60 const QString type = e.attribute("type");
61 const QString value = e.text();
62 QDataStream in(QByteArray::fromBase64(value.toLatin1()));
63 QVariant v;
64 in >> v;
65 d->properties[name] = v;
66 }
67 }
68 n = n.nextSibling();
69 }
70}
71
72bool KoProperties::load(const QString & s)
73{
74 QDomDocument doc;
75
76 if (!doc.setContent(s))
77 return false;
78 load(doc.documentElement());
79
80 return true;
81}
82
83void KoProperties::save(QDomElement &root) const
84{
85 QDomDocument doc = root.ownerDocument();
86 QMap<QString, QVariant>::Iterator it;
87 for (it = d->properties.begin(); it != d->properties.end(); ++it) {
88 QDomElement e = doc.createElement("property");
89 e.setAttribute("name", QString(it.key().toLatin1()));
90 QVariant v = it.value();
91 e.setAttribute("type", v.typeName());
92
93 QByteArray bytes;
94 QDataStream out(&bytes, QIODevice::WriteOnly);
95 out << v;
96 QDomText text = doc.createCDATASection(QString::fromLatin1(bytes.toBase64()));
97 e.appendChild(text);
98 root.appendChild(e);
99 }
100}
101
102QString KoProperties::store(const QString &s) const
103{
104 QDomDocument doc = QDomDocument(s);
105 QDomElement root = doc.createElement(s);
106 doc.appendChild(root);
107
108 save(root);
109 return doc.toString();
110}
111
112void KoProperties::setProperty(const QString & name, const QVariant & value)
113{
114 // If there's an existing value for this name already, replace it.
115 d->properties.insert(name, value);
116}
117
118bool KoProperties::property(const QString & name, QVariant & value) const
119{
120 QMap<QString, QVariant>::const_iterator it = d->properties.constFind(name);
121 if (it == d->properties.constEnd()) {
122 return false;
123 } else {
124 value = *it;
125 return true;
126 }
127}
128
129QVariant KoProperties::property(const QString & name) const
130{
131 return d->properties.value(name, QVariant());
132}
133
134
135int KoProperties::intProperty(const QString & name, int def) const
136{
137 const QVariant v = property(name);
138 if (v.isValid())
139 return v.toInt();
140 else
141 return def;
142
143}
144
145qreal KoProperties::doubleProperty(const QString & name, qreal def) const
146{
147 const QVariant v = property(name);
148 if (v.isValid())
149 return v.toDouble();
150 else
151 return def;
152}
153
154bool KoProperties::boolProperty(const QString & name, bool def) const
155{
156 const QVariant v = property(name);
157 if (v.isValid())
158 return v.toBool();
159 else
160 return def;
161}
162
163QString KoProperties::stringProperty(const QString & name, const QString & def) const
164{
165 const QVariant v = property(name);
166 if (v.isValid())
167 return v.toString();
168 else
169 return def;
170}
171
172bool KoProperties::contains(const QString & key) const
173{
174 return d->properties.contains(key);
175}
176
177QVariant KoProperties::value(const QString & key) const
178{
179 return d->properties.value(key);
180}
181
183{
184 if (d->properties.count() != other.d->properties.count())
185 return false;
186 Q_FOREACH (const QString & key, d->properties.keys()) {
187 if (other.d->properties.value(key) != d->properties.value(key))
188 return false;
189 }
190 return true;
191}
float value(const T *src, size_t ch)
qreal v
QVariant value(const QString &key) const
void save(QDomElement &root) const
bool operator==(const KoProperties &other) const
void load(const QDomElement &root)
QMap< QString, QVariant > properties
QString store(const QString &root) const
Create a serialized version of these properties (as XML) with root as the root element.
int intProperty(const QString &name, int defaultValue=0) const
bool boolProperty(const QString &name, bool defaultValue=false) const
Private *const d
bool contains(const QString &key) const
QMapIterator< QString, QVariant > propertyIterator() const
void setProperty(const QString &name, const QVariant &value)
QString stringProperty(const QString &name, const QString &defaultValue=QString()) const
bool property(const QString &name, QVariant &value) const
qreal doubleProperty(const QString &name, qreal defaultValue=0.0) const
bool isEmpty() const