kformula

kformula_doc.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
3 Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
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.
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
22#include "kformula_doc.h"
23#include "kformula_view.h"
24#include "kformula_factory.h"
25
26#include <tqbitmap.h>
27#include <tqcolor.h>
28#include <tqdom.h>
29#include <tqpainter.h>
30#include <tqpopupmenu.h>
31#include <tqprinter.h>
32#include <tqstring.h>
33#include <tqwmatrix.h>
34#include <tqfile.h>
35
36#include <config.h>
37#include <unistd.h>
38
39#include <tdeaboutdialog.h>
40#include <tdeaction.h>
41#include <tdeapplication.h>
42#include <kdebug.h>
43#include <KoGlobal.h>
44#include <kiconloader.h>
45#include <tdelocale.h>
46#include <kstdaction.h>
47#include <kstandarddirs.h>
48#include <kurl.h>
49#include <KoXmlWriter.h>
50#include <KoStoreDevice.h>
51#include <tdetempfile.h>
52#include <KoMainWindow.h>
53
54#include <kformulacontainer.h>
55#include <kformuladocument.h>
56
57
58KFormulaDoc::KFormulaDoc(TQWidget *parentWidget, const char *widgetName, TQObject* parent, const char* name, bool singleViewMode)
59 : KoDocument(parentWidget, widgetName, parent, name, singleViewMode)
60{
61 setInstance(KFormulaFactory::global(), false);
62 //kdDebug(39001) << "General Settings" << endl;
63
64 history = new KoCommandHistory( actionCollection() );
65 wrapper = new KFormula::DocumentWrapper( kapp->config(),
66 actionCollection(),
67 history );
68 document = new KFormula::Document;
69 wrapper->document( document );
70 formula = document->createFormula();
71
72 document->setEnabled( true );
73
74 // the modify flag
75 connect(history, TQ_SIGNAL(commandExecuted()), this, TQ_SLOT(commandExecuted()));
76 connect(history, TQ_SIGNAL(documentRestored()), this, TQ_SLOT(documentRestored()));
77 dcopObject();
78}
79
80
81KFormulaDoc::~KFormulaDoc()
82{
83 delete history;
84 delete wrapper;
85}
86
87
88bool KFormulaDoc::saveOasis( KoStore* store, KoXmlWriter* manifestWriter )
89{
90 if ( !store->open( "content.xml" ) )
91 return false;
92
93 KoStoreDevice dev( store );
94 KoXmlWriter* contentWriter = createOasisXmlWriter( &dev, "math:math" );
95
96
97 KTempFile contentTmpFile;
98 contentTmpFile.setAutoDelete( true );
99 TQFile* tmpFile = contentTmpFile.file();
100
101 //todo save content
102 TQTextStream stream(tmpFile);
103 stream.setEncoding( TQTextStream::UnicodeUTF8 );
104 formula->saveMathML( stream, true );
105
106 tmpFile->close();
107 contentWriter->addCompleteElement( tmpFile );
108 contentTmpFile.close();
109
110
111
112 contentWriter->endElement();
113 delete contentWriter;
114
115 if(!store->close())
116 return false;
117
118 manifestWriter->addManifestEntry("content.xml", "text/xml");
119
120 setModified( false );
121
122 return true;
123}
124
125
126TQDomDocument KFormulaDoc::saveXML()
127{
128 TQDomDocument doc = document->saveXML();
129 history->documentSaved();
130 return doc;
131}
132
133bool KFormulaDoc::loadOasis( const TQDomDocument& doc, KoOasisStyles&, const TQDomDocument&, KoStore* )
134{
135 // we don't have style into this format
136 // we don't have settings into kformula (for the moment)
137 // necessary to adapt kformula code to load MathML format with Oasis Extension.
138
139 if ( document->loadOasis( doc ) ) {
140 history->clear();
141 history->documentSaved();
142 return true;
143 }
144 return false;
145}
146
147bool KFormulaDoc::loadXML(TQIODevice *, const TQDomDocument& doc)
148{
149 if ( doc.doctype().name().lower() == "math"
150 || doc.documentElement().tagName().lower() == "math" )
151 if ( document->loadOasis( doc ) ) {
152 history->clear();
153 history->documentSaved();
154 return true;
155 }
156 if ( document->loadXML( doc ) ) {
157 history->clear();
158 history->documentSaved();
159 return true;
160 }
161 return false;
162}
163
169bool KFormulaDoc::saveNativeFormat( const TQString & file )
170{
171 TQCString mimeType = outputMimeType();
172 bool mathml = !mimeType.isEmpty() && mimeType.contains( "mathml", false );
173 if ( mathml ) {
174 TQFile f( file );
175 if ( f.open( IO_WriteOnly | IO_Translate ) )
176 {
177 TQTextStream stream( &f );
178 stream.setEncoding( TQTextStream::UnicodeUTF8 );
179 formula->saveMathML( stream, false );
180 f.close();
181 return true;
182 }
183 else
184 return false;
185 }
186 // If it's not MathML, let the parent handle it
187 return KoDocument::saveNativeFormat( file );
188}
189
190KoView* KFormulaDoc::createViewInstance(TQWidget* _parent, const char *name)
191{
192 return new KFormulaPartView(this, _parent, name);
193}
194
195void KFormulaDoc::commandExecuted()
196{
197 if (formula->isEmpty()) {
198 setEmpty();
199 }
200 setModified(true);
201}
202
203void KFormulaDoc::documentRestored()
204{
205 setModified(false);
206}
207
208
209bool KFormulaDoc::initDoc(InitDocFlags /*flags*/, TQWidget* /*parentWidget*/)
210{
211 // If nothing is loaded, do initialize here
212 return true;
213}
214
215void KFormulaDoc::showStartUpWidget(KoMainWindow* parent, bool /*alwaysShow*/)
216{
217 parent->setRootDocument( this );
218}
219
220bool KFormulaDoc::showEmbedInitDialog(TQWidget* /*parent*/)
221{
222 return true;
223}
224
225void KFormulaDoc::paintContent(TQPainter& painter, const TQRect& rect, bool transparent, double zoomX, double zoomY)
226{
227 // ####### handle transparency and zoom
228 // Need to draw only the document rectangle described in the parameter rect.
229
230 bool forPrint = painter.device() && painter.device()->devType() == TQInternal::Printer;
231 document->setZoomAndResolution( 100, zoomX, zoomY, true, forPrint );
232 if ( !transparent ) {
233 painter.fillRect( rect, TQt::white );
234 }
235 formula->draw( painter, rect );
236}
237
238TQString KFormulaDoc::configFile() const
239{
240// return readConfigFile( locate( "data", "kformula/kformula.rc",
241// KFormulaFactory::global() ) );
242
243// return readConfigFile( "kformula.rc" );
244 return TQString();
245}
246
247#include "kformula_doc.moc"
virtual bool saveNativeFormat(const TQString &file)
Saves the document in native format, to a given file.
The parts view.