• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • calligra API Reference
  • KDE Home
  • Contact Us
 

KoWidgetUtils

  • sources
  • kfour-appscomplete
  • calligra
  • libs
  • widgetutils
KoProperties.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006 Boudewijn Rempt <[email protected]>
3  * Copyright (C) 2006-2008 Thomas Zander <[email protected]>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "KoProperties.h"
22 
23 #include <QDomDocument>
24 #include <QDataStream>
25 
26 class Q_DECL_HIDDEN KoProperties::Private
27 {
28 public:
29  QMap<QString, QVariant> properties;
30 };
31 
32 KoProperties::KoProperties()
33  : d(new Private())
34 {
35 }
36 
37 KoProperties::KoProperties(const KoProperties & rhs)
38  : d(new Private())
39 {
40  d->properties = rhs.d->properties;
41 }
42 
43 KoProperties::~KoProperties()
44 {
45  delete d;
46 }
47 
48 QMapIterator<QString, QVariant> KoProperties::propertyIterator() const
49 {
50  return QMapIterator<QString, QVariant>(d->properties);
51 }
52 
53 
54 bool KoProperties::isEmpty() const
55 {
56  return d->properties.isEmpty();
57 }
58 
59 void KoProperties::load(const QDomElement &root)
60 {
61  d->properties.clear();
62 
63  QDomElement e = root;
64  QDomNode n = e.firstChild();
65 
66  while (!n.isNull()) {
67  // We don't nest elements.
68  QDomElement e = n.toElement();
69  if (!e.isNull()) {
70  if (e.tagName() == QLatin1String("property")) {
71  const QString name = e.attribute(QStringLiteral("name"));
72  const QString value = e.text();
73  QDataStream in(QByteArray::fromBase64(value.toLatin1()));
74  QVariant v;
75  in >> v;
76  d->properties[name] = v;
77  }
78  }
79  n = n.nextSibling();
80  }
81 }
82 
83 bool KoProperties::load(const QString & s)
84 {
85  QDomDocument doc;
86 
87  if (!doc.setContent(s))
88  return false;
89  load(doc.documentElement());
90 
91  return true;
92 }
93 
94 void KoProperties::save(QDomElement &root) const
95 {
96  QDomDocument doc = root.ownerDocument();
97  QMap<QString, QVariant>::ConstIterator it;
98  for (it = d->properties.constBegin(); it != d->properties.constEnd(); ++it) {
99  QDomElement e = doc.createElement(QStringLiteral("property"));
100  e.setAttribute(QStringLiteral("name"), QString(it.key().toLatin1()));
101  QVariant v = it.value();
102  e.setAttribute(QStringLiteral("type"), v.typeName());
103 
104  QByteArray bytes;
105  QDataStream out(&bytes, QIODevice::WriteOnly);
106  out << v;
107  QDomText text = doc.createCDATASection(QString::fromLatin1(bytes.toBase64()));
108  e.appendChild(text);
109  root.appendChild(e);
110  }
111 }
112 
113 QString KoProperties::store(const QString &s) const
114 {
115  QDomDocument doc = QDomDocument(s);
116  QDomElement root = doc.createElement(s);
117  doc.appendChild(root);
118 
119  save(root);
120  return doc.toString();
121 }
122 
123 void KoProperties::setProperty(const QString & name, const QVariant & value)
124 {
125  // If there's an existing value for this name already, replace it.
126  d->properties.insert(name, value);
127 }
128 
129 bool KoProperties::property(const QString & name, QVariant & value) const
130 {
131  QMap<QString, QVariant>::const_iterator it = d->properties.constFind(name);
132  if (it == d->properties.constEnd()) {
133  return false;
134  } else {
135  value = *it;
136  return true;
137  }
138 }
139 
140 QVariant KoProperties::property(const QString & name) const
141 {
142  return d->properties.value(name, QVariant());
143 }
144 
145 
146 int KoProperties::intProperty(const QString & name, int def) const
147 {
148  const QVariant v = property(name);
149  if (v.isValid())
150  return v.toInt();
151  else
152  return def;
153 
154 }
155 
156 qreal KoProperties::doubleProperty(const QString & name, qreal def) const
157 {
158  const QVariant v = property(name);
159  if (v.isValid())
160  return v.toDouble();
161  else
162  return def;
163 }
164 
165 bool KoProperties::boolProperty(const QString & name, bool def) const
166 {
167  const QVariant v = property(name);
168  if (v.isValid())
169  return v.toBool();
170  else
171  return def;
172 }
173 
174 QString KoProperties::stringProperty(const QString & name, const QString & def) const
175 {
176  const QVariant v = property(name);
177  if (v.isValid())
178  return v.toString();
179  else
180  return def;
181 }
182 
183 bool KoProperties::contains(const QString & key) const
184 {
185  return d->properties.contains(key);
186 }
187 
188 QVariant KoProperties::value(const QString & key) const
189 {
190  return d->properties.value(key);
191 }
192 
193 bool KoProperties::operator==(const KoProperties &other) const
194 {
195  if (d->properties.count() != other.d->properties.count())
196  return false;
197  QMapIterator<QString, QVariant> i(d->properties);
198  while (i.hasNext()) {
199  i.next();
200  if (other.d->properties.value(i.key()) != i.value())
201  return false;
202  }
203  return true;
204 }
205 
206 KoProperties KoProperties::operator=(const KoProperties &other) const
207 {
208  d->properties = other.d->properties;
209  return *this;
210 }
QDomDocument::toString
QString toString(int indent) const
QDomElement::text
QString text() const
QVariant::isValid
bool isValid() const
QDomNode::firstChild
QDomNode firstChild() const
QDomNode::toElement
QDomElement toElement() const
QDomText
QDomNode
QDomElement::tagName
QString tagName() const
QMap::value
const T value(const Key &key) const
QVariant::value
T value() const
QDomNode::isNull
bool isNull() const
QDataStream
QMapIterator
QDomDocument::setContent
bool setContent(const QByteArray &data, bool namespaceProcessing, QString *errorMsg, int *errorLine, int *errorColumn)
QString::toLatin1
QByteArray toLatin1() const
QDomDocument::createElement
QDomElement createElement(const QString &tagName)
QMap::constFind
const_iterator constFind(const Key &key) const
QDomElement::setAttribute
void setAttribute(const QString &name, const QString &value)
QVariant::toDouble
double toDouble(bool *ok) const
KoProperties.h
QMap::constEnd
const_iterator constEnd() const
QString
QDomNode::ownerDocument
QDomDocument ownerDocument() const
QByteArray::fromBase64
QByteArray fromBase64(const QByteArray &base64)
QVariant::toInt
int toInt(bool *ok) const
QDomDocument::documentElement
QDomElement documentElement() const
QLatin1String
QMap::key
const Key key(const T &value) const
QVariant::toBool
bool toBool() const
QMap< QString, QVariant >
QDomElement
QDomDocument::createCDATASection
QDomCDATASection createCDATASection(const QString &value)
QDomNode::appendChild
QDomNode appendChild(const QDomNode &newChild)
QString::insert
QString & insert(int position, QChar ch)
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QByteArray::toBase64
QByteArray toBase64() const
QDomNode::nextSibling
QDomNode nextSibling() const
QDomDocument
QDomElement::attribute
QString attribute(const QString &name, const QString &defValue) const
QVariant
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QVariant::typeName
const char * typeName() const
QByteArray
QVariant::toString
QString toString() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Apr 22 2021 23:25:41 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KoWidgetUtils

Skip menu "KoWidgetUtils"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

calligra API Reference

Skip menu "calligra API Reference"
  • Braindump
  • filters
  •   MSO
  •   KoMSOOXML
  •   KoOdf2
  •   KoOdfReader
  • Karbon
  • libs
  •   BasicFlakes
  •   Flake
  •   KoKross
  •   KUndo2
  •   KoMain
  •   KoOdf
  •   KoPageApp
  •   Pigment
  •   KoPlugin
  •   KoRdf
  •   KoStore
  •   KoText
  •   KoTextLayout
  •   KoVectorImage
  •   KoWidgets
  •   KoWidgetUtils
  • plugins
  •   formulashape
  •   musicshape
  • Sheets
  • Stage
  • Words
  •   part
  •     scripting

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal