Krita Source Code Documentation
Loading...
Searching...
No Matches
KoProperties Class Reference

#include <KoProperties.h>

+ Inheritance diagram for KoProperties:

Public Member Functions

bool boolProperty (const QString &name, bool defaultValue=false) const
 
bool contains (const QString &key) const
 
qreal doubleProperty (const QString &name, qreal defaultValue=0.0) const
 
int intProperty (const QString &name, int defaultValue=0) const
 
bool isEmpty () const
 
 KoProperties ()
 
 KoProperties (const KoProperties &other)
 
void load (const QDomElement &root)
 
bool load (const QString &string)
 
bool operator== (const KoProperties &other) const
 
QVariant property (const QString &name) const
 
bool property (const QString &name, QVariant &value) const
 
QMapIterator< QString, QVariant > propertyIterator () const
 
void save (QDomElement &root) const
 
void setProperty (const QString &name, const QVariant &value)
 
QString store (const QString &root) const
 Create a serialized version of these properties (as XML) with root as the root element.
 
QString stringProperty (const QString &name, const QString &defaultValue=QString()) const
 
QVariant value (const QString &key) const
 
 ~KoProperties ()
 

Public Attributes

QMap< QString, QVariant > properties
 

Private Attributes

Private *const d
 
- Private Attributes inherited from Private
KisCanvas2canvas
 
int displayedFrame
 
int intendedFrame
 

Additional Inherited Members

- Private Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Detailed Description

A KoProperties is the (de-)serializable representation of a key-value map. The serialisation format is XML.

Definition at line 14 of file KoProperties.cpp.

Constructor & Destructor Documentation

◆ KoProperties() [1/2]

KoProperties::KoProperties ( )

Create a new properties object

Definition at line 20 of file KoProperties.cpp.

21 : d(new Private())
22{
23}
Private *const d

◆ KoProperties() [2/2]

KoProperties::KoProperties ( const KoProperties & other)

Copy constructor

Definition at line 25 of file KoProperties.cpp.

26 : d(new Private())
27{
28 d->properties = rhs.d->properties;
29}

References d.

◆ ~KoProperties()

KoProperties::~KoProperties ( )

Definition at line 31 of file KoProperties.cpp.

32{
33 delete d;
34}

References d.

Member Function Documentation

◆ boolProperty()

bool KoProperties::boolProperty ( const QString & name,
bool defaultValue = false ) const

Return a boolean property by name.

Parameters
namethe name (or key) with which the variant was registered.
defaultValuethe default value, should there not be any property by the name this will be returned.

Definition at line 154 of file KoProperties.cpp.

155{
156 const QVariant v = property(name);
157 if (v.isValid())
158 return v.toBool();
159 else
160 return def;
161}
qreal v
bool property(const QString &name, QVariant &value) const

References property(), and v.

◆ contains()

bool KoProperties::contains ( const QString & key) const

Returns true if the specified key is present in this properties object.

Definition at line 172 of file KoProperties.cpp.

173{
174 return d->properties.contains(key);
175}

References d.

◆ doubleProperty()

qreal KoProperties::doubleProperty ( const QString & name,
qreal defaultValue = 0.0 ) const

Return a qreal property by name.

Parameters
namethe name (or key) with which the variant was registered.
defaultValuethe default value, should there not be any property by the name this will be returned.

Definition at line 145 of file KoProperties.cpp.

146{
147 const QVariant v = property(name);
148 if (v.isValid())
149 return v.toDouble();
150 else
151 return def;
152}

References property(), and v.

◆ intProperty()

int KoProperties::intProperty ( const QString & name,
int defaultValue = 0 ) const

Return an integer property by name. A typical usage:

KoProperties *props = new KoProperties();
props->setProperty("age", 25);
int age = props->intProperty("age");
int intProperty(const QString &name, int defaultValue=0) const
void setProperty(const QString &name, const QVariant &value)
Returns
an integer property by name
Parameters
namethe name (or key) with which the variant was registered.
defaultValuethe default value, should there not be any property by the name this will be returned.
See also
property() stringProperty()

Definition at line 135 of file KoProperties.cpp.

136{
137 const QVariant v = property(name);
138 if (v.isValid())
139 return v.toInt();
140 else
141 return def;
142
143}

References property(), and v.

◆ isEmpty()

bool KoProperties::isEmpty ( ) const
Returns
true if this KoProperties object does not contain any properties.

Definition at line 42 of file KoProperties.cpp.

43{
44 return d->properties.isEmpty();
45}

References d.

◆ load() [1/2]

void KoProperties::load ( const QDomElement & root)

Fill the properties object from the XML dom node.

load() does not touch existing properties if loading fails.

Parameters
rootthe root node of the properties subtree.

Definition at line 47 of file KoProperties.cpp.

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}
QVariant value(const QString &key) const
const char * name(StandardAction id)

References d, v, and value().

◆ load() [2/2]

bool KoProperties::load ( const QString & string)

Fill the properties object from the XML encoded representation in string.

load() does not touch existing properties if loading fails.

Parameters
stringthe stored properties.
Returns
false if loading failing, true if it succeeded

Definition at line 72 of file KoProperties.cpp.

73{
74 QDomDocument doc;
75
76 if (!doc.setContent(s))
77 return false;
78 load(doc.documentElement());
79
80 return true;
81}
void load(const QDomElement &root)

References load().

◆ operator==()

bool KoProperties::operator== ( const KoProperties & other) const

Definition at line 182 of file KoProperties.cpp.

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}

References d.

◆ property() [1/2]

QVariant KoProperties::property ( const QString & name) const

Return a property by name, wrapped in a QVariant. A typical usage:

KoProperties *props = new KoProperties();
props->setProperty("name", "Marcy");
props->setProperty("age", 25);
QString name = props->property("name").toString();
int age = props->property("age").toInt();
Returns
a property by name, wrapped in a QVariant.
Parameters
namethe name (or key) with which the variant was registered.
See also
intProperty() stringProperty()

Definition at line 129 of file KoProperties.cpp.

130{
131 return d->properties.value(name, QVariant());
132}

References d.

◆ property() [2/2]

bool KoProperties::property ( const QString & name,
QVariant & value ) const

Set value to the value associated with property name

Returns
false if the specified property did not exist.

Definition at line 118 of file KoProperties.cpp.

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}

References d, and value().

◆ propertyIterator()

QMapIterator< QString, QVariant > KoProperties::propertyIterator ( ) const

Returns an iterator over the properties. The iterator is not suitable for adding or removing properties.

Definition at line 36 of file KoProperties.cpp.

37{
38 return QMapIterator<QString, QVariant>(d->properties);
39}

References d.

◆ save()

void KoProperties::save ( QDomElement & root) const

Definition at line 83 of file KoProperties.cpp.

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}

References d, and v.

◆ setProperty()

void KoProperties::setProperty ( const QString & name,
const QVariant & value )

Set the property with name to value.

Definition at line 112 of file KoProperties.cpp.

113{
114 // If there's an existing value for this name already, replace it.
115 d->properties.insert(name, value);
116}

References d, and value().

◆ store()

QString KoProperties::store ( const QString & root) const

Create a serialized version of these properties (as XML) with root as the root element.

Parameters
rootas the root element in the generated XML.

Definition at line 102 of file KoProperties.cpp.

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}
void save(QDomElement &root) const

References save().

◆ stringProperty()

QString KoProperties::stringProperty ( const QString & name,
const QString & defaultValue = QString() ) const

Return an QString property by name. A typical usage:

KoProperties *props = new KoProperties();
props->setProperty("name", "Marcy");
QString name = props->stringProperty("name");
QString stringProperty(const QString &name, const QString &defaultValue=QString()) const
Returns
an QString property by name
Parameters
namethe name (or key) with which the variant was registered.
See also
property() intProperty()
Parameters
defaultValuethe default value, should there not be any property by the name this will be returned.

Definition at line 163 of file KoProperties.cpp.

164{
165 const QVariant v = property(name);
166 if (v.isValid())
167 return v.toString();
168 else
169 return def;
170}

References property(), and v.

◆ value()

QVariant KoProperties::value ( const QString & key) const

Returns the value associated with the specified key if this properties object contains the specified key; otherwise return an empty QVariant.

Definition at line 177 of file KoProperties.cpp.

178{
179 return d->properties.value(key);
180}

References d.

Member Data Documentation

◆ d

Private* const KoProperties::d
private

Definition at line 168 of file KoProperties.h.

◆ properties

QMap<QString, QVariant> KoProperties::properties

Definition at line 17 of file KoProperties.cpp.


The documentation for this class was generated from the following files: