File indexing completed on 2024-05-12 16:29:17

0001 /* This file is part of the KDE project
0002  *
0003  * Copyright (C) 2013 Inge Wallin <inge@lysator.liu.se>
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 
0022 // Own
0023 #include "KoOdfStyleProperties.h"
0024 
0025 // odflib
0026 #include <KoXmlStreamReader.h>
0027 #include <KoXmlWriter.h>
0028 
0029 #include "Odf2Debug.h"
0030 
0031 // ----------------------------------------------------------------
0032 //                         private class
0033 
0034 
0035 class Q_DECL_HIDDEN KoOdfStyleProperties::Private
0036 {
0037 public:
0038     Private() {};
0039     ~Private() {};
0040 
0041     AttributeSet attributes;  // name, value
0042 };
0043 
0044 
0045 // ----------------------------------------------------------------
0046 
0047 
0048 KoOdfStyleProperties::KoOdfStyleProperties()
0049     : d(new KoOdfStyleProperties::Private())
0050 {
0051 }
0052 
0053 KoOdfStyleProperties::~KoOdfStyleProperties()
0054 {
0055     delete d;
0056 }
0057 
0058 
0059 QString KoOdfStyleProperties::attribute(const QString &property) const
0060 {
0061     debugOdf2 << d->attributes;
0062     return d->attributes.value(property, QString());
0063 }
0064 
0065 void KoOdfStyleProperties::setAttribute(const QString &property, const QString &value)
0066 {
0067     d->attributes[property] = value;
0068 }
0069 
0070 
0071 void KoOdfStyleProperties::clear()
0072 {
0073     d->attributes.clear();
0074 }
0075 
0076 
0077 bool KoOdfStyleProperties::readOdf(KoXmlStreamReader &reader)
0078 {
0079     // The default implementation just read the attributes.  The
0080     // inheriting classes will also read various types of children.
0081     bool retval = readAttributes(reader);
0082     reader.skipCurrentElement();
0083 
0084     return retval;
0085 }
0086 
0087 bool KoOdfStyleProperties::saveOdf(const QString &propertySet, KoXmlWriter *writer)
0088 {
0089     writer->startElement(propertySet.toLatin1()); // e.g. style:text-properties
0090     saveAttributes(writer);
0091     writer->endElement(); // propertySet
0092 
0093     return true;
0094 }
0095 
0096 void KoOdfStyleProperties::copyPropertiesFrom(const KoOdfStyleProperties &sourceProperties)
0097 {
0098     d->attributes = sourceProperties.d->attributes;
0099 }
0100 
0101 // ----------------------------------------------------------------
0102 //                         protected functions
0103 
0104 
0105 bool KoOdfStyleProperties::readAttributes(KoXmlStreamReader &reader)
0106 {
0107     copyAttributes(reader, d->attributes);
0108 
0109     //debugOdf2 << "read attributes: " << d->attributes;
0110 
0111     return true;
0112 }
0113 
0114 bool KoOdfStyleProperties::saveAttributes(KoXmlWriter *writer)
0115 {
0116     foreach (const QString &property, d->attributes.keys()) {
0117         writer->addAttribute(property.toLatin1(), d->attributes[property]);
0118     }
0119 
0120     return true;
0121 }
0122 
0123 
0124 // ----------------------------------------------------------------
0125 
0126 
0127 void copyAttributes(KoXmlStreamReader &reader, AttributeSet &attributes)
0128 {
0129     KoXmlStreamAttributes attrs = reader.attributes();
0130     foreach (const KoXmlStreamAttribute &attr, attrs) {
0131         attributes.insert(attr.qualifiedName().toString(), attr.value().toString());
0132     }
0133 }
0134 
0135 void saveAttributes(AttributeSet &attributes, KoXmlWriter *writer)
0136 {
0137     foreach (const QString &property, attributes.keys()) {
0138         writer->addAttribute(property.toLatin1(), attributes[property]);
0139     }
0140 }