filters

svgexport.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Inge Wallin <inge@lysator.liu.se>
00003    Copyright (C) 2005 Fredrik Edemar <f_edemar@linux.se>
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 <qpicture.h>
00022 #include <qpainter.h>
00023 
00024 #include <kapplication.h>
00025 #include <kmessagebox.h>
00026 
00027 #include <KoFilterChain.h>
00028 #include <KoStore.h>
00029 //#include <KoStoreDevice.h>
00030 #include <kgenericfactory.h>
00031 
00032 #include <kformulacontainer.h>
00033 #include "kformuladocument.h"
00034 
00035 #include "svgexport.h"
00036 
00037 
00038 typedef KGenericFactory<SvgExport, KoFilter> SvgExportFactory;
00039 K_EXPORT_COMPONENT_FACTORY( libkfosvgexport, SvgExportFactory( "svgexport" ) )
00040 
00041 SvgExport::SvgExport(KoFilter *, const char *, const QStringList&) 
00042     : KoFilter()
00043 {
00044 }
00045 
00046 SvgExport::~SvgExport()
00047 {
00048 }
00049 
00050 
00051 KoFilter::ConversionStatus
00052 SvgExport::convert(const QCString& from, const QCString& to)
00053 {
00054     // Check for proper conversion.
00055     if ( from != "application/x-kformula" || to != "image/svg+xml" )
00056         return KoFilter::NotImplemented;
00057 
00058     // Read the contents of the KFormula file
00059     KoStoreDevice* storeIn = m_chain->storageFile( "root", KoStore::Read );
00060     if ( !storeIn ) {
00061     KMessageBox::error( 0, i18n("Failed to read data." ), 
00062                 i18n( "SVG Export Error" ) );
00063     return KoFilter::FileNotFound;
00064     }
00065 
00066     // Get the XML tree.
00067     QDomDocument  domIn;
00068     domIn.setContent( storeIn );
00069     QDomElement   docNode = domIn.documentElement();
00070 
00071     // Read the document from the XML tree.
00072     KFormula::DocumentWrapper* wrapper = new KFormula::DocumentWrapper( kapp->config(), 0 );
00073     KFormula::Document* kformulaDoc = new KFormula::Document;
00074     wrapper->document( kformulaDoc );
00075     KFormula::Container* formula = kformulaDoc->createFormula();
00076 
00077     if ( !kformulaDoc->loadXML( domIn ) ) {
00078         KMessageBox::error( 0, i18n( "Malformed XML data." ), 
00079                 i18n( "SVG Export Error" ) );
00080         return KoFilter::WrongFormat;
00081     }
00082 
00083     // Draw the actual bitmap.
00084     QPicture  picture;
00085     QPainter  painter(&picture);
00086     QRect     rect(QPoint(0, 0), QPoint(500, 400));
00087     formula->draw( painter, rect, false );
00088     painter.end();
00089 
00090     // Save the image.
00091     if ( !picture.save( m_chain->outputFile(), "SVG" ) ) {
00092         KMessageBox::error( 0, i18n( "Failed to write file." ), 
00093                 i18n( "SVG Export Error" ) );
00094     }
00095 
00096     delete formula;
00097     delete wrapper;
00098     return KoFilter::OK;
00099 }
00100 
00101 
00102 #include <svgexport.moc>
KDE Home | KDE Accessibility Home | Description of Access Keys