filters

kformula/svg/svgexport.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Inge Wallin <inge@lysator.liu.se>
3 Copyright (C) 2005 Fredrik Edemar <f_edemar@linux.se>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301 USA.
19*/
20
21#include <tqpicture.h>
22#include <tqpainter.h>
23
24#include <tdeapplication.h>
25#include <tdemessagebox.h>
26
27#include <KoFilterChain.h>
28#include <KoStore.h>
29//#include <KoStoreDevice.h>
30#include <kgenericfactory.h>
31
32#include <kformulacontainer.h>
33#include "kformuladocument.h"
34
35#include "svgexport.h"
36
37
38typedef KGenericFactory<SvgExport, KoFilter> SvgExportFactory;
39K_EXPORT_COMPONENT_FACTORY( libkfosvgexport, SvgExportFactory( "svgexport" ) )
40
41SvgExport::SvgExport(KoFilter *, const char *, const TQStringList&)
42 : KoFilter()
43{
44}
45
46SvgExport::~SvgExport()
47{
48}
49
50
51KoFilter::ConversionStatus
52SvgExport::convert(const TQCString& from, const TQCString& to)
53{
54 // Check for proper conversion.
55 if ( from != "application/x-kformula" || to != "image/svg+xml" )
56 return KoFilter::NotImplemented;
57
58 // Read the contents of the KFormula file
59 KoStoreDevice* storeIn = m_chain->storageFile( "root", KoStore::Read );
60 if ( !storeIn ) {
61 KMessageBox::error( 0, i18n("Failed to read data." ),
62 i18n( "SVG Export Error" ) );
63 return KoFilter::FileNotFound;
64 }
65
66 // Get the XML tree.
67 TQDomDocument domIn;
68 domIn.setContent( storeIn );
69 TQDomElement docNode = domIn.documentElement();
70
71 // Read the document from the XML tree.
72 KFormula::DocumentWrapper* wrapper = new KFormula::DocumentWrapper( kapp->config(), 0 );
73 KFormula::Document* kformulaDoc = new KFormula::Document;
74 wrapper->document( kformulaDoc );
75 KFormula::Container* formula = kformulaDoc->createFormula();
76
77 if ( !kformulaDoc->loadXML( domIn ) ) {
78 KMessageBox::error( 0, i18n( "Malformed XML data." ),
79 i18n( "SVG Export Error" ) );
80 return KoFilter::WrongFormat;
81 }
82
83 // Draw the actual bitmap.
84 TQPicture picture;
85 TQPainter painter(&picture);
86 TQRect rect(TQPoint(0, 0), TQPoint(500, 400));
87 formula->draw( painter, rect, false );
88 painter.end();
89
90 // Save the image.
91 if ( !picture.save( m_chain->outputFile(), "SVG" ) ) {
92 KMessageBox::error( 0, i18n( "Failed to write file." ),
93 i18n( "SVG Export Error" ) );
94 }
95
96 delete formula;
97 delete wrapper;
98 return KoFilter::OK;
99}
100
101
102#include <svgexport.moc>