File indexing completed on 2024-12-01 13:11:44

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 "KoOdfParagraphProperties.h"
0024 
0025 // Qt
0026 #include <QString>
0027 
0028 // odflib
0029 #include <KoXmlStreamReader.h>
0030 #include <KoXmlWriter.h>
0031 
0032 #include "Odf2Debug.h"
0033 
0034 // ----------------------------------------------------------------
0035 //                         private class
0036 
0037 
0038 class Q_DECL_HIDDEN KoOdfParagraphProperties::Private
0039 {
0040 public:
0041     Private();
0042     ~Private();
0043 
0044     // NYI: Background Image
0045     KoOdfStyleDropCap         *dropCap;
0046     QList<KoOdfStyleTabStop*>  tabStops;
0047 };
0048 
0049 KoOdfParagraphProperties::Private::Private()
0050     : dropCap(0)
0051 {
0052 }
0053 
0054 KoOdfParagraphProperties::Private::~Private()
0055 {
0056     if (dropCap) {
0057         delete dropCap;
0058         dropCap = 0;
0059     }
0060     qDeleteAll(tabStops);
0061 }
0062 
0063 
0064 // ----------------------------------------------------------------
0065 
0066 
0067 KoOdfParagraphProperties::KoOdfParagraphProperties()
0068     : KoOdfStyleProperties()
0069     , d(new KoOdfParagraphProperties::Private())
0070 {
0071 }
0072 
0073 KoOdfParagraphProperties::~KoOdfParagraphProperties()
0074 {
0075     delete d;
0076 }
0077 
0078 
0079 void KoOdfParagraphProperties::clear()
0080 {
0081     KoOdfStyleProperties::clear();
0082 
0083     // FIXME: background image
0084     if (d->dropCap) {
0085         delete d->dropCap;
0086         d->dropCap = 0;
0087     }
0088     qDeleteAll(d->tabStops);
0089 }
0090 
0091 
0092 bool KoOdfParagraphProperties::readOdf(KoXmlStreamReader &reader)
0093 {
0094     bool retval = readAttributes(reader);
0095     if (!retval) {
0096         return false;
0097     }
0098 
0099     // Load child elements.  For paragraph-properties, these are:
0100     //  - style:background-image
0101     //  - style:drop-cap
0102     //  - style:tab-stops
0103     while (reader.readNextStartElement()) {
0104         QString child = reader.qualifiedName().toString();
0105 
0106         if (child == "style:background-image") {
0107             // FIXME: NYI
0108         }
0109         else if (child == "style:drop-cap") {
0110             if (d->dropCap) {
0111                 d->dropCap->attributes.clear();
0112             }
0113             else {
0114                 d->dropCap = new KoOdfStyleDropCap();
0115             }
0116             copyAttributes(reader, d->dropCap->attributes);
0117         }
0118         else if (child == "style:tab-stops") {
0119             while (reader.readNextStartElement()) {
0120                 if (reader.qualifiedName() == "style:tab-stop") {
0121                     KoOdfStyleTabStop  *tabStop = new KoOdfStyleTabStop;
0122 
0123                     copyAttributes(reader, tabStop->attributes);
0124                     d->tabStops.append(tabStop);
0125                 }
0126             }
0127         }
0128 
0129         // Skip rest of each element including children that are not read yet (shouldn't be any).
0130         reader.skipCurrentElement();
0131     }
0132 
0133     return retval;
0134 }
0135 
0136 bool KoOdfParagraphProperties::saveOdf(const QString &propertySet, KoXmlWriter *writer)
0137 {
0138     Q_UNUSED(propertySet);
0139 
0140     writer->startElement("style:paragraph-properties");
0141     saveAttributes(writer);
0142 
0143     // Save child elements of style:paragraph-properties
0144     // FIXME NYI: background image
0145     if (d->dropCap) {
0146         writer->startElement("style:drop-cap");
0147         ::saveAttributes(d->dropCap->attributes, writer);
0148         writer->endElement(); // style:drop-cap
0149     }
0150     if (!d->tabStops.isEmpty()) {
0151         writer->startElement("style:tab-stops");
0152         foreach (KoOdfStyleTabStop *tabStop, d->tabStops) {
0153             writer->startElement("style:tab-stop");
0154             ::saveAttributes(tabStop->attributes, writer);
0155             writer->endElement(); // style:tab-stop
0156         }
0157         writer->endElement(); // style:tab-stops
0158     }
0159 
0160     writer->endElement(); // style:paragraph-properties
0161 
0162     return true;
0163 }