1
/* This file is part of the KDE project
3
Copyright 2008 Johannes Simon <johannes.simon@gmail.com>
4
Copyright 2010 Inge Wallin <inge@lysator.liu.se>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public
8
License as published by the Free Software Foundation; either
9
version 2 of the License, or (at your option) any later version.
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Library General Public License
17
along with this library; see the file COPYING.LIB. If not, write to
18
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
* Boston, MA 02110-1301, USA.
23
#include "FormulaDocument.h"
32
#include <KoDocument.h>
33
#include <KoXmlWriter.h>
34
#include <KoOdfReadStore.h>
35
#include <KoOdfWriteStore.h>
36
#include <KoOdfLoadingContext.h>
37
#include <KoShapeLoadingContext.h>
38
#include <KoShapeSavingContext.h>
40
#include <KoOdfStylesReader.h>
41
#include <KoGenStyles.h>
42
#include <KoEmbeddedDocumentSaver.h>
44
#include <KComponentData>
48
#include "KoFormulaShape.h"
51
class FormulaDocument::Private
57
KoFormulaShape *parent;
60
FormulaDocument::Private::Private()
64
FormulaDocument::Private::~Private()
68
FormulaDocument::FormulaDocument( KoFormulaShape *parent )
73
// Needed by KoDocument::nativeOasisMimeType().
74
// KoEmbeddedDocumentSaver uses that method to
75
// get the mimetype of the embedded document.
76
setComponentData( KComponentData( "math" ) );
79
FormulaDocument::~FormulaDocument()
85
bool FormulaDocument::loadOdf( KoOdfReadStore &odfStore )
87
KoXmlDocument doc = odfStore.contentDoc();
88
KoXmlElement bodyElement = doc.documentElement();
90
kDebug(31000) << bodyElement.nodeName();
92
if (bodyElement.nodeName() != "math:math") {
93
kError(35001) << "No <math:math> element found.";
97
// When the formula is stored in an embedded document, it seems to
98
// always have a <math:semantics> element that surrounds the
99
// actual formula. I have to check with the MathML spec what this
100
// actually means and if it is obligatory. /iw
101
KoXmlNode semanticsNode = bodyElement.namedItemNS( KoXmlNS::math, "semantics" );
102
if ( !semanticsNode.isNull() ) {
103
bodyElement = semanticsNode.toElement();
106
KoOdfLoadingContext odfLoadingContext( odfStore.styles(), odfStore.store() );
107
KoShapeLoadingContext context(odfLoadingContext, d->parent->resourceManager());
109
return d->parent->loadOdfEmbedded( bodyElement, context );
112
bool FormulaDocument::loadXML( const KoXmlDocument &doc, KoStore *)
116
// We don't support the old XML format any more.
120
bool FormulaDocument::saveOdf( SavingContext &context )
122
// FIXME: This code is copied from ChartDocument, so it needs to
123
// be adapted to the needs of the KoFormulaShape.
125
KoOdfWriteStore &odfStore = context.odfStore;
126
KoStore *store = odfStore.store();
127
KoXmlWriter *manifestWriter = odfStore.manifestWriter();
128
KoXmlWriter *contentWriter = odfStore.contentWriter();
129
if ( !contentWriter )
132
KoGenStyles mainStyles;
133
KoXmlWriter *bodyWriter = odfStore.bodyWriter();
137
KoEmbeddedDocumentSaver& embeddedSaver = context.embeddedSaver;
139
KoShapeSavingContext savingContext( *bodyWriter, mainStyles, embeddedSaver );
141
bodyWriter->startElement( "office:body" );
142
bodyWriter->startElement( "office:formula" );
144
d->parent->saveOdf( savingContext );
146
bodyWriter->endElement(); // office:formula
147
bodyWriter->endElement(); // office:body
149
mainStyles.saveOdfStyles( KoGenStyles::DocumentAutomaticStyles, contentWriter );
150
odfStore.closeContentWriter();
152
// Add manifest line for content.xml and styles.xml
153
manifestWriter->addManifestEntry( url().path() + "/content.xml", "text/xml" );
154
manifestWriter->addManifestEntry( url().path() + "/styles.xml", "text/xml" );
156
// save the styles.xml
157
if ( !mainStyles.saveOdfStylesDotXml( store, manifestWriter ) )
160
if ( !savingContext.saveDataCenter( store, manifestWriter ) ) {
167
KoView *FormulaDocument::createViewInstance( QWidget *parent )
174
void FormulaDocument::paintContent( QPainter &painter, const QRect &rect )