File indexing completed on 2024-05-12 16:33:27

0001 /* This file is part of the KDE project
0002 
0003    Copyright 2008 Johannes Simon <johannes.simon@gmail.com>
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 // Own
0022 #include "ChartDocument.h"
0023 
0024 // Qt
0025 #include <QWidget>
0026 #include <QIODevice>
0027 #include <QPainter>
0028 
0029 // Calligra
0030 #include <KoDocument.h>
0031 #include <KoXmlWriter.h>
0032 #include <KoOdfReadStore.h>
0033 #include <KoOdfWriteStore.h>
0034 #include <KoOdfLoadingContext.h>
0035 #include <KoShapeLoadingContext.h>
0036 #include <KoShapeSavingContext.h>
0037 #include <KoXmlNS.h>
0038 #include <KoGenStyles.h>
0039 #include <KoEmbeddedDocumentSaver.h>
0040 #include <KoView.h>
0041 
0042 // KoChart
0043 #include "ChartShape.h"
0044 #include "ChartPart.h"
0045 #include "ChartDebug.h"
0046 
0047 
0048 namespace KoChart {
0049 
0050 class ChartDocument::Private
0051 {
0052 public:
0053     Private();
0054     ~Private();
0055 
0056     ChartShape *parent;
0057 };
0058 
0059 ChartDocument::Private::Private()
0060 {
0061 }
0062 
0063 ChartDocument::Private::~Private()
0064 {
0065 }
0066 
0067 ChartDocument::ChartDocument(ChartShape *parent)
0068     : KoDocument(new ChartPart(0))
0069     , d (new Private)
0070 {
0071     d->parent = parent;
0072     // Needed by KoDocument::nativeOasisMimeType().
0073     // KoEmbeddedDocumentSaver uses that method to
0074     // get the mimetype of the embedded document.
0075 }
0076 
0077 ChartDocument::~ChartDocument()
0078 {
0079     delete d;
0080 }
0081 
0082 
0083 bool ChartDocument::loadOdf(KoOdfReadStore &odfStore)
0084 {
0085     KoXmlDocument doc = odfStore.contentDoc();
0086     KoXmlNode bodyNode = doc.documentElement().namedItemNS(KoXmlNS::office, "body");
0087     if (bodyNode.isNull()) {
0088         errorChart << "No <office:body> element found.";
0089         return false;
0090     }
0091     KoXmlNode chartElementParentNode = bodyNode.namedItemNS(KoXmlNS::office, "chart");
0092     if (chartElementParentNode.isNull()) {
0093         errorChart << "No <office:chart> element found.";
0094         return false;
0095     }
0096     KoXmlElement chartElement = chartElementParentNode.namedItemNS(KoXmlNS::chart, "chart").toElement();
0097     if (chartElement.isNull()) {
0098         errorChart << "No <chart:chart> element found.";
0099         return false;
0100     }
0101     KoOdfLoadingContext odfLoadingContext(odfStore.styles(), odfStore.store());
0102     KoShapeLoadingContext context(odfLoadingContext, d->parent->resourceManager());
0103 
0104     return d->parent->loadOdfChartElement(chartElement, context);
0105 }
0106 
0107 bool ChartDocument::loadXML(const KoXmlDocument &doc, KoStore *)
0108 {
0109     Q_UNUSED(doc);
0110 
0111     // We don't support the old XML format any more.
0112     return false;
0113 }
0114 
0115 bool ChartDocument::saveOdf(SavingContext &context)
0116 {
0117     KoOdfWriteStore &odfStore = context.odfStore;
0118     KoStore *store = odfStore.store();
0119     KoXmlWriter *manifestWriter = odfStore.manifestWriter();
0120     KoXmlWriter *contentWriter  = odfStore.contentWriter();
0121     if (!contentWriter)
0122         return false;
0123 
0124     KoGenStyles mainStyles;
0125     KoXmlWriter *bodyWriter = odfStore.bodyWriter();
0126     if (!bodyWriter)
0127         return false;
0128 
0129     KoEmbeddedDocumentSaver& embeddedSaver = context.embeddedSaver;
0130 
0131     KoShapeSavingContext savingContext(*bodyWriter, mainStyles, embeddedSaver);
0132 
0133     bodyWriter->startElement("office:body");
0134     bodyWriter->startElement("office:chart");
0135 
0136     d->parent->saveOdf(savingContext);
0137 
0138     bodyWriter->endElement(); // office:chart
0139     bodyWriter->endElement(); // office:body
0140 
0141     mainStyles.saveOdfStyles(KoGenStyles::DocumentAutomaticStyles, contentWriter);
0142     odfStore.closeContentWriter();
0143 
0144     // Add manifest line for content.xml and styles.xml
0145     manifestWriter->addManifestEntry(url().path() + "/content.xml", "text/xml");
0146     manifestWriter->addManifestEntry(url().path() + "/styles.xml", "text/xml");
0147 
0148     // save the styles.xml
0149     if (!mainStyles.saveOdfStylesDotXml(store, manifestWriter))
0150         return false;
0151 
0152     if (!savingContext.saveDataCenter(store, manifestWriter)) {
0153         return false;
0154     }
0155 
0156     return true;
0157 }
0158 
0159 
0160 void ChartDocument::paintContent(QPainter &painter, const QRect &rect)
0161 {
0162     Q_UNUSED(painter);
0163     Q_UNUSED(rect);
0164 }
0165 
0166 } // namespace KoChart
0167