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 "KoOdfStyle.h"
0024 
0025 // Qt
0026 #include <QString>
0027 
0028 // Odflib
0029 #include "KoXmlStreamReader.h"
0030 #include "KoXmlWriter.h"
0031 #include "KoOdfStyleProperties.h"
0032 #include "KoOdfTextProperties.h"
0033 #include "KoOdfParagraphProperties.h"
0034 #include "KoOdfGraphicProperties.h"
0035 
0036 #include "Odf2Debug.h"
0037 
0038 // ================================================================
0039 //                         class KoOdfStyle
0040 
0041 
0042 class Q_DECL_HIDDEN KoOdfStyle::Private
0043 {
0044 public:
0045     Private();
0046     ~Private();
0047 
0048     QString family;
0049     QString parent;
0050 
0051     QHash<QString, KoOdfStyleProperties*> properties;  // e.g. "text-properties",
0052 };
0053 
0054 KoOdfStyle::Private::Private()
0055 {
0056 }
0057 
0058 KoOdfStyle::Private::~Private()
0059 {
0060     qDeleteAll(properties);
0061 }
0062 
0063 
0064 // ----------------------------------------------------------------
0065 
0066 
0067 KoOdfStyle::KoOdfStyle()
0068     : KoOdfStyleBase(StyleStyle)
0069     , d(new KoOdfStyle::Private())
0070 {
0071 }
0072 
0073 KoOdfStyle::~KoOdfStyle()
0074 {
0075     delete d;
0076 }
0077 
0078 
0079 QString KoOdfStyle::family() const
0080 {
0081     return d->family;
0082 }
0083 
0084 void KoOdfStyle::setFamily(const QString &family)
0085 {
0086     d->family = family;
0087 }
0088 
0089 QString KoOdfStyle::parent() const
0090 {
0091     return d->parent;
0092 }
0093 
0094 void KoOdfStyle::setParent(const QString &parent)
0095 {
0096     d->parent = parent;
0097 }
0098 
0099 
0100 QHash<QString, KoOdfStyleProperties*> KoOdfStyle::properties() const
0101 {
0102     return d->properties;
0103 }
0104 
0105 KoOdfStyleProperties *KoOdfStyle::properties(const QString& name) const
0106 {
0107     return d->properties.value(name, 0);
0108 }
0109 
0110 QString KoOdfStyle::property(const QString &propertySet, const QString &property) const
0111 {
0112     KoOdfStyleProperties *props = d->properties.value(propertySet, 0);
0113     if (props)
0114         return props->attribute(property);
0115     else
0116         return QString();
0117 }
0118 
0119 void KoOdfStyle::setProperty(const QString &propertySet, const QString &property, const QString &value)
0120 {
0121     KoOdfStyleProperties *props = d->properties.value(propertySet);
0122     if (!props)
0123         props = new KoOdfStyleProperties();
0124     props->setAttribute(property, value);
0125 }
0126 
0127 
0128 bool KoOdfStyle::readOdf(KoXmlStreamReader &reader)
0129 {
0130     // Load style attributes.
0131     KoXmlStreamAttributes  attrs = reader.attributes();
0132 
0133     setName(attrs.value("style:name").toString());
0134     setDisplayName(attrs.value("style:display-name").toString());
0135     setFamily(attrs.value("style:family").toString());
0136     setParent(attrs.value("style:parent-style-name").toString());
0137 
0138     debugOdf2 << "Style:" << name() << family() << parent() << displayName();
0139 
0140     // Load child elements: property sets and other children.
0141     while (reader.readNextStartElement()) {
0142 
0143         // So far we only have support for text-, paragraph- and graphic-properties
0144         const QString propertiesType = reader.qualifiedName().toString();
0145         // Create a new propertyset variable depending on the type of properties.
0146         KoOdfStyleProperties *properties = 0;
0147         if (propertiesType == "style:text-properties") {
0148             properties = new KoOdfTextProperties();
0149         } else if (propertiesType == "style:paragraph-properties") {
0150             properties = new KoOdfParagraphProperties();
0151         } else if (propertiesType == "style:graphic-properties") {
0152             properties = new KoOdfGraphicProperties();
0153         }
0154 
0155         if (properties) {
0156             if (!properties->readOdf(reader)) {
0157                 delete properties;
0158                 return false;
0159             }
0160             d->properties[propertiesType] = properties;
0161         }
0162     }
0163 
0164     return true;
0165 }
0166 
0167 bool KoOdfStyle::saveOdf(KoXmlWriter *writer)
0168 {
0169     if (isDefaultStyle()) {
0170         writer->startElement("style:default-style");
0171     }
0172     else {
0173         writer->startElement("style:style");
0174         writer->addAttribute("style:name", name());
0175     }
0176 
0177     // Write style attributes
0178     writer->addAttribute("style:family", family());
0179     if (!d->parent.isEmpty()) {
0180         writer->addAttribute("style:parent-style-name", d->parent);
0181     }
0182     if (!displayName().isEmpty()) {
0183         writer->addAttribute("style:display-name", displayName());
0184     }
0185 
0186     // Write properties
0187     foreach(const QString &propertySet, d->properties.keys()) {
0188         d->properties.value(propertySet)->saveOdf(propertySet, writer);
0189     }
0190 
0191     writer->endElement();  // style:{default-,}style
0192     return true;
0193 }