File indexing completed on 2024-05-12 16:34:02

0001 /* This file is part of the KDE project
0002 
0003    Copyright 2008 Johannes Simon <johannes.simon@gmail.com>
0004    Copyright 2010 Inge Wallin <inge@lysator.liu.se>
0005 
0006    This library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public
0008    License as published by the Free Software Foundation; either
0009    version 2 of the License, or (at your option) any later version.
0010 
0011    This library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public License
0017    along with this library; see the file COPYING.LIB.  If not, write to
0018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020 */
0021 
0022 // Own
0023 #include "FormulaDocument.h"
0024 
0025 // Qt
0026 #include <QWidget>
0027 #include <QIODevice>
0028 #include <QPainter>
0029 
0030 // Calligra
0031 #include <KoDocument.h>
0032 #include <KoXmlWriter.h>
0033 #include <KoOdfReadStore.h>
0034 #include <KoOdfWriteStore.h>
0035 #include <KoOdfLoadingContext.h>
0036 #include <KoShapeLoadingContext.h>
0037 #include <KoShapeSavingContext.h>
0038 #include <KoXmlNS.h>
0039 #include <KoGenStyles.h>
0040 #include <KoEmbeddedDocumentSaver.h>
0041 #include <KoView.h>
0042 
0043 // KFormula
0044 #include "FormulaDebug.h"
0045 #include "KoFormulaShape.h"
0046 #include "FormulaPart.h"
0047 
0048 class FormulaDocument::Private
0049 {
0050 public:
0051     Private();
0052     ~Private();
0053 
0054     KoFormulaShape *parent;
0055 };
0056 
0057 FormulaDocument::Private::Private()
0058 {
0059 }
0060 
0061 FormulaDocument::Private::~Private()
0062 {
0063 }
0064 
0065 FormulaDocument::FormulaDocument( KoFormulaShape *parent )
0066     : KoDocument(new FormulaPart(0))
0067     , d ( new Private )
0068 {
0069     d->parent = parent;
0070 }
0071 
0072 FormulaDocument::~FormulaDocument()
0073 {
0074     delete d;
0075 }
0076 
0077 
0078 bool FormulaDocument::loadOdf( KoOdfReadStore &odfStore )
0079 {
0080     KoXmlDocument doc = odfStore.contentDoc();
0081     KoXmlElement  bodyElement = doc.documentElement();
0082 
0083     debugFormula << bodyElement.nodeName();
0084 
0085     if (bodyElement.localName() != "math" || bodyElement.namespaceURI() != KoXmlNS::math) {
0086         errorFormula << "No <math:math> element found.";
0087         return false;
0088     }
0089 
0090     // When the formula is stored in an embedded document, it seems to
0091     // always have a <math:semantics> element that surrounds the
0092     // actual formula.  I have to check with the MathML spec what this
0093     // actually means and if it is obligatory.  /iw
0094     KoXmlNode semanticsNode = bodyElement.namedItemNS( KoXmlNS::math, "semantics" );
0095     if ( !semanticsNode.isNull() ) {
0096         bodyElement = semanticsNode.toElement();
0097     }
0098 
0099     KoOdfLoadingContext   odfLoadingContext( odfStore.styles(), odfStore.store() );
0100     KoShapeLoadingContext context(odfLoadingContext, d->parent->resourceManager());
0101 
0102     return d->parent->loadOdfEmbedded( bodyElement, context );
0103 }
0104 
0105 bool FormulaDocument::loadXML( const KoXmlDocument &doc, KoStore *)
0106 {
0107     Q_UNUSED( doc );
0108 
0109     // We don't support the old XML format any more.
0110     return false;
0111 }
0112 
0113 bool FormulaDocument::saveOdf( SavingContext &context )
0114 {
0115     // FIXME: This code is copied from ChartDocument, so it needs to
0116     // be adapted to the needs of the KoFormulaShape.
0117 
0118     KoOdfWriteStore &odfStore = context.odfStore;
0119     KoStore *store = odfStore.store();
0120     KoXmlWriter *manifestWriter = odfStore.manifestWriter();
0121     KoXmlWriter *contentWriter  = odfStore.contentWriter();
0122     if ( !contentWriter )
0123         return false;
0124 
0125     KoGenStyles mainStyles;
0126     KoXmlWriter *bodyWriter = odfStore.bodyWriter();
0127     if ( !bodyWriter )
0128         return false;
0129 
0130     KoEmbeddedDocumentSaver& embeddedSaver = context.embeddedSaver;
0131 
0132     KoShapeSavingContext savingContext( *bodyWriter, mainStyles, embeddedSaver );
0133 
0134     bodyWriter->startElement( "office:body" );
0135     bodyWriter->startElement( "office:formula" );
0136 
0137     d->parent->saveOdf( savingContext );
0138 
0139     bodyWriter->endElement(); // office:formula
0140     bodyWriter->endElement(); // office:body
0141 
0142     mainStyles.saveOdfStyles( KoGenStyles::DocumentAutomaticStyles, contentWriter );
0143     odfStore.closeContentWriter();
0144 
0145     // Add manifest line for content.xml and styles.xml
0146     manifestWriter->addManifestEntry( url().path() + "/content.xml", "text/xml" );
0147     manifestWriter->addManifestEntry( url().path() + "/styles.xml", "text/xml" );
0148 
0149     // save the styles.xml
0150     if ( !mainStyles.saveOdfStylesDotXml( store, manifestWriter ) )
0151         return false;
0152 
0153     if ( !savingContext.saveDataCenter( store, manifestWriter ) ) {
0154         return false;
0155     }
0156 
0157     return true;
0158 }
0159 
0160 void FormulaDocument::paintContent( QPainter &painter, const QRect &rect )
0161 {
0162     Q_UNUSED( painter );
0163     Q_UNUSED( rect );
0164 }
0165 
0166