Warning, file /office/calligra/libs/widgetutils/KoProperties.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 * Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.org> 0003 * Copyright (C) 2006-2008 Thomas Zander <zander@kde.org> 0004 * 0005 * This library is free software; you can redistribute it and/or 0006 * modify it under the terms of the GNU Library General Public 0007 * License as published by the Free Software Foundation; either 0008 * version 2 of the License, or (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 * Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, write to 0017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 */ 0020 0021 #include "KoProperties.h" 0022 0023 #include <QDomDocument> 0024 #include <QDataStream> 0025 0026 class Q_DECL_HIDDEN KoProperties::Private 0027 { 0028 public: 0029 QMap<QString, QVariant> properties; 0030 }; 0031 0032 KoProperties::KoProperties() 0033 : d(new Private()) 0034 { 0035 } 0036 0037 KoProperties::KoProperties(const KoProperties & rhs) 0038 : d(new Private()) 0039 { 0040 d->properties = rhs.d->properties; 0041 } 0042 0043 KoProperties::~KoProperties() 0044 { 0045 delete d; 0046 } 0047 0048 QMapIterator<QString, QVariant> KoProperties::propertyIterator() const 0049 { 0050 return QMapIterator<QString, QVariant>(d->properties); 0051 } 0052 0053 0054 bool KoProperties::isEmpty() const 0055 { 0056 return d->properties.isEmpty(); 0057 } 0058 0059 void KoProperties::load(const QDomElement &root) 0060 { 0061 d->properties.clear(); 0062 0063 QDomElement e = root; 0064 QDomNode n = e.firstChild(); 0065 0066 while (!n.isNull()) { 0067 // We don't nest elements. 0068 QDomElement e = n.toElement(); 0069 if (!e.isNull()) { 0070 if (e.tagName() == "property") { 0071 const QString name = e.attribute("name"); 0072 const QString value = e.text(); 0073 QDataStream in(QByteArray::fromBase64(value.toLatin1())); 0074 QVariant v; 0075 in >> v; 0076 d->properties[name] = v; 0077 } 0078 } 0079 n = n.nextSibling(); 0080 } 0081 } 0082 0083 bool KoProperties::load(const QString & s) 0084 { 0085 QDomDocument doc; 0086 0087 if (!doc.setContent(s)) 0088 return false; 0089 load(doc.documentElement()); 0090 0091 return true; 0092 } 0093 0094 void KoProperties::save(QDomElement &root) const 0095 { 0096 QDomDocument doc = root.ownerDocument(); 0097 QMap<QString, QVariant>::ConstIterator it; 0098 for (it = d->properties.constBegin(); it != d->properties.constEnd(); ++it) { 0099 QDomElement e = doc.createElement("property"); 0100 e.setAttribute("name", QString(it.key().toLatin1())); 0101 QVariant v = it.value(); 0102 e.setAttribute("type", v.typeName()); 0103 0104 QByteArray bytes; 0105 QDataStream out(&bytes, QIODevice::WriteOnly); 0106 out << v; 0107 QDomText text = doc.createCDATASection(QString::fromLatin1(bytes.toBase64())); 0108 e.appendChild(text); 0109 root.appendChild(e); 0110 } 0111 } 0112 0113 QString KoProperties::store(const QString &s) const 0114 { 0115 QDomDocument doc = QDomDocument(s); 0116 QDomElement root = doc.createElement(s); 0117 doc.appendChild(root); 0118 0119 save(root); 0120 return doc.toString(); 0121 } 0122 0123 void KoProperties::setProperty(const QString & name, const QVariant & value) 0124 { 0125 // If there's an existing value for this name already, replace it. 0126 d->properties.insert(name, value); 0127 } 0128 0129 bool KoProperties::property(const QString & name, QVariant & value) const 0130 { 0131 QMap<QString, QVariant>::const_iterator it = d->properties.constFind(name); 0132 if (it == d->properties.constEnd()) { 0133 return false; 0134 } else { 0135 value = *it; 0136 return true; 0137 } 0138 } 0139 0140 QVariant KoProperties::property(const QString & name) const 0141 { 0142 return d->properties.value(name, QVariant()); 0143 } 0144 0145 0146 int KoProperties::intProperty(const QString & name, int def) const 0147 { 0148 const QVariant v = property(name); 0149 if (v.isValid()) 0150 return v.toInt(); 0151 else 0152 return def; 0153 0154 } 0155 0156 qreal KoProperties::doubleProperty(const QString & name, qreal def) const 0157 { 0158 const QVariant v = property(name); 0159 if (v.isValid()) 0160 return v.toDouble(); 0161 else 0162 return def; 0163 } 0164 0165 bool KoProperties::boolProperty(const QString & name, bool def) const 0166 { 0167 const QVariant v = property(name); 0168 if (v.isValid()) 0169 return v.toBool(); 0170 else 0171 return def; 0172 } 0173 0174 QString KoProperties::stringProperty(const QString & name, const QString & def) const 0175 { 0176 const QVariant v = property(name); 0177 if (v.isValid()) 0178 return v.toString(); 0179 else 0180 return def; 0181 } 0182 0183 bool KoProperties::contains(const QString & key) const 0184 { 0185 return d->properties.contains(key); 0186 } 0187 0188 QVariant KoProperties::value(const QString & key) const 0189 { 0190 return d->properties.value(key); 0191 } 0192 0193 bool KoProperties::operator==(const KoProperties &other) const 0194 { 0195 if (d->properties.count() != other.d->properties.count()) 0196 return false; 0197 QMapIterator<QString, QVariant> i(d->properties); 0198 while (i.hasNext()) { 0199 i.next(); 0200 if (other.d->properties.value(i.key()) != i.value()) 0201 return false; 0202 } 0203 return true; 0204 } 0205 0206 KoProperties KoProperties::operator=(const KoProperties &other) const 0207 { 0208 d->properties = other.d->properties; 0209 return *this; 0210 }