File indexing completed on 2024-05-12 16:02:31
0001 /* 0002 * SPDX-FileCopyrightText: 2006 Boudewijn Rempt <boud@valdyas.org> 0003 * SPDX-FileCopyrightText: 2006-2008 Thomas Zander <zander@kde.org> 0004 * 0005 * SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "KoProperties.h" 0009 0010 #include <QDomDocument> 0011 #include <QDataStream> 0012 0013 class Q_DECL_HIDDEN KoProperties::Private 0014 { 0015 public: 0016 QMap<QString, QVariant> properties; 0017 }; 0018 0019 KoProperties::KoProperties() 0020 : d(new Private()) 0021 { 0022 } 0023 0024 KoProperties::KoProperties(const KoProperties & rhs) 0025 : d(new Private()) 0026 { 0027 d->properties = rhs.d->properties; 0028 } 0029 0030 KoProperties::~KoProperties() 0031 { 0032 delete d; 0033 } 0034 0035 QMapIterator<QString, QVariant> KoProperties::propertyIterator() const 0036 { 0037 return QMapIterator<QString, QVariant>(d->properties); 0038 } 0039 0040 0041 bool KoProperties::isEmpty() const 0042 { 0043 return d->properties.isEmpty(); 0044 } 0045 0046 void KoProperties::load(const QDomElement &root) 0047 { 0048 d->properties.clear(); 0049 0050 QDomElement e = root; 0051 QDomNode n = e.firstChild(); 0052 0053 while (!n.isNull()) { 0054 // We don't nest elements. 0055 QDomElement e = n.toElement(); 0056 if (!e.isNull()) { 0057 if (e.tagName() == "property") { 0058 const QString name = e.attribute("name"); 0059 const QString type = e.attribute("type"); 0060 const QString value = e.text(); 0061 QDataStream in(QByteArray::fromBase64(value.toLatin1())); 0062 QVariant v; 0063 in >> v; 0064 d->properties[name] = v; 0065 } 0066 } 0067 n = n.nextSibling(); 0068 } 0069 } 0070 0071 bool KoProperties::load(const QString & s) 0072 { 0073 QDomDocument doc; 0074 0075 if (!doc.setContent(s)) 0076 return false; 0077 load(doc.documentElement()); 0078 0079 return true; 0080 } 0081 0082 void KoProperties::save(QDomElement &root) const 0083 { 0084 QDomDocument doc = root.ownerDocument(); 0085 QMap<QString, QVariant>::Iterator it; 0086 for (it = d->properties.begin(); it != d->properties.end(); ++it) { 0087 QDomElement e = doc.createElement("property"); 0088 e.setAttribute("name", QString(it.key().toLatin1())); 0089 QVariant v = it.value(); 0090 e.setAttribute("type", v.typeName()); 0091 0092 QByteArray bytes; 0093 QDataStream out(&bytes, QIODevice::WriteOnly); 0094 out << v; 0095 QDomText text = doc.createCDATASection(QString::fromLatin1(bytes.toBase64())); 0096 e.appendChild(text); 0097 root.appendChild(e); 0098 } 0099 } 0100 0101 QString KoProperties::store(const QString &s) const 0102 { 0103 QDomDocument doc = QDomDocument(s); 0104 QDomElement root = doc.createElement(s); 0105 doc.appendChild(root); 0106 0107 save(root); 0108 return doc.toString(); 0109 } 0110 0111 void KoProperties::setProperty(const QString & name, const QVariant & value) 0112 { 0113 // If there's an existing value for this name already, replace it. 0114 d->properties.insert(name, value); 0115 } 0116 0117 bool KoProperties::property(const QString & name, QVariant & value) const 0118 { 0119 QMap<QString, QVariant>::const_iterator it = d->properties.constFind(name); 0120 if (it == d->properties.constEnd()) { 0121 return false; 0122 } else { 0123 value = *it; 0124 return true; 0125 } 0126 } 0127 0128 QVariant KoProperties::property(const QString & name) const 0129 { 0130 return d->properties.value(name, QVariant()); 0131 } 0132 0133 0134 int KoProperties::intProperty(const QString & name, int def) const 0135 { 0136 const QVariant v = property(name); 0137 if (v.isValid()) 0138 return v.toInt(); 0139 else 0140 return def; 0141 0142 } 0143 0144 qreal KoProperties::doubleProperty(const QString & name, qreal def) const 0145 { 0146 const QVariant v = property(name); 0147 if (v.isValid()) 0148 return v.toDouble(); 0149 else 0150 return def; 0151 } 0152 0153 bool KoProperties::boolProperty(const QString & name, bool def) const 0154 { 0155 const QVariant v = property(name); 0156 if (v.isValid()) 0157 return v.toBool(); 0158 else 0159 return def; 0160 } 0161 0162 QString KoProperties::stringProperty(const QString & name, const QString & def) const 0163 { 0164 const QVariant v = property(name); 0165 if (v.isValid()) 0166 return v.toString(); 0167 else 0168 return def; 0169 } 0170 0171 bool KoProperties::contains(const QString & key) const 0172 { 0173 return d->properties.contains(key); 0174 } 0175 0176 QVariant KoProperties::value(const QString & key) const 0177 { 0178 return d->properties.value(key); 0179 } 0180 0181 bool KoProperties::operator==(const KoProperties &other) const 0182 { 0183 if (d->properties.count() != other.d->properties.count()) 0184 return false; 0185 Q_FOREACH (const QString & key, d->properties.keys()) { 0186 if (other.d->properties.value(key) != d->properties.value(key)) 0187 return false; 0188 } 0189 return true; 0190 }