kformula/flake

KoFormulaShape.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Martin Pfeiffer <hubipete@gmx.net>
00003                  2009 Jeremias Epperlein <jeeree@web.de>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "FormulaData.h"
00022 #include "KoFormulaShape.h"
00023 #include "FormulaElement.h"
00024 #include "FormulaRenderer.h"
00025 #include <KoShapeSavingContext.h>
00026 #include <KoShapeLoadingContext.h>
00027 #include <KoXmlWriter.h>
00028 #include <KoXmlReader.h>
00029 #include <KoXmlNS.h>
00030 #include <kdebug.h>
00031 
00032 
00033 KoFormulaShape::KoFormulaShape()
00034 : KoFrameShape( KoXmlNS::draw, "object" )
00035 {
00036     FormulaElement* element= new FormulaElement();
00037     m_formulaData = new FormulaData(element);
00038     m_formulaRenderer = new FormulaRenderer();
00039 }
00040 
00041 KoFormulaShape::~KoFormulaShape()
00042 {
00043     delete m_formulaData;
00044     delete m_formulaRenderer;
00045 }
00046 
00047 void KoFormulaShape::paint( QPainter &painter, const KoViewConverter &converter )
00048 {
00049     painter.save();
00050     applyConversion( painter, converter );   // apply zooming and coordinate translation
00051     m_formulaRenderer->layoutElement(  m_formulaData->formulaElement() );
00052     m_formulaRenderer->paintElement( painter,  m_formulaData->formulaElement() );  // paint the formula
00053     painter.restore();
00054 }
00055 
00056 void KoFormulaShape::updateLayout() {
00057     m_formulaRenderer->layoutElement( m_formulaData->formulaElement() );
00058 
00059      KoShape::setSize(m_formulaData->formulaElement()->boundingRect().size());
00060 }
00061 
00062 
00063 BasicElement* KoFormulaShape::elementAt( const QPointF& p )
00064 {
00065     return m_formulaData->formulaElement()->childElementAt( p );
00066 }
00067 
00068 void KoFormulaShape::resize( const QSizeF& )
00069 { /* do nothing as FormulaShape is fixed size */ }
00070 
00071 FormulaData* KoFormulaShape::formulaData() const
00072 {
00073     return  m_formulaData;
00074 }
00075 
00076 FormulaRenderer* KoFormulaShape::formulaRenderer() const
00077 {
00078     return m_formulaRenderer;
00079 }
00080 
00081 bool KoFormulaShape::loadOdf( const KoXmlElement& element, KoShapeLoadingContext &context )
00082 {
00083     kDebug() <<"Loading ODF in Formula";
00084     loadOdfAttributes(element, context, OdfAllAttributes);
00085     return loadOdfFrame(element, context);
00086 }
00087 
00088 bool KoFormulaShape::loadOdfFrameElement( const KoXmlElement & element, KoShapeLoadingContext &/*context*/ )
00089 {
00090     const KoXmlElement &topLevelElement = KoXml::namedItemNS(element, "http://www.w3.org/1998/Math/MathML", "math");
00091     // This is only true when loading as embedded in the main doc
00092     if (topLevelElement.isNull()) {
00093         kWarning() << "no math element as first child";
00094         return false;
00095     }
00096     delete m_formulaData->formulaElement();
00097     FormulaElement* formulaElement = new FormulaElement();     // create a new root element
00098     formulaElement->readMathML( topLevelElement );     // and load the new formula
00099     m_formulaData->setFormulaElement(formulaElement);
00100     m_formulaData->notifyDataChange(0,false);
00101     return true;
00102 }
00103 
00104 void KoFormulaShape::saveOdf( KoShapeSavingContext& context ) const
00105 {
00106     kDebug() <<"Saving ODF in Formula";
00107     KoXmlWriter& writer = context.xmlWriter();
00108     writer.startElement("draw:frame");
00109     saveOdfAttributes(context, OdfAllAttributes);
00110     writer.startElement( "draw:object" );
00111     // TODO add some namespace magic to avoid adding "math:" namespace everywhere
00112     formulaData()->formulaElement()->writeMathML( &context.xmlWriter() );
00113     writer.endElement(); // draw:object
00114     writer.endElement(); // draw:frame
00115 }
00116