00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "kformula_doc.h"
00023 #include "kformula_view.h"
00024 #include "kformula_factory.h"
00025
00026 #include <qbitmap.h>
00027 #include <qcolor.h>
00028 #include <qdom.h>
00029 #include <qpainter.h>
00030 #include <qpopupmenu.h>
00031 #include <qprinter.h>
00032 #include <qstring.h>
00033 #include <qwmatrix.h>
00034 #include <qfile.h>
00035
00036 #include <config.h>
00037 #include <unistd.h>
00038
00039 #include <kaboutdialog.h>
00040 #include <kaction.h>
00041 #include <kapplication.h>
00042 #include <kdebug.h>
00043 #include <KoGlobal.h>
00044 #include <kiconloader.h>
00045 #include <klocale.h>
00046 #include <kstdaction.h>
00047 #include <kstandarddirs.h>
00048 #include <kurl.h>
00049 #include <KoXmlWriter.h>
00050 #include <KoStoreDevice.h>
00051 #include <ktempfile.h>
00052 #include <KoMainWindow.h>
00053
00054 #include <kformulacontainer.h>
00055 #include <kformuladocument.h>
00056
00057
00058 KFormulaDoc::KFormulaDoc(QWidget *parentWidget, const char *widgetName, QObject* parent, const char* name, bool singleViewMode)
00059 : KoDocument(parentWidget, widgetName, parent, name, singleViewMode)
00060 {
00061 setInstance(KFormulaFactory::global(), false);
00062
00063
00064 history = new KoCommandHistory( actionCollection() );
00065 wrapper = new KFormula::DocumentWrapper( kapp->config(),
00066 actionCollection(),
00067 history );
00068 document = new KFormula::Document;
00069 wrapper->document( document );
00070 formula = document->createFormula();
00071
00072 document->setEnabled( true );
00073
00074
00075 connect(history, SIGNAL(commandExecuted()), this, SLOT(commandExecuted()));
00076 connect(history, SIGNAL(documentRestored()), this, SLOT(documentRestored()));
00077 dcopObject();
00078 }
00079
00080
00081 KFormulaDoc::~KFormulaDoc()
00082 {
00083 delete history;
00084 delete wrapper;
00085 }
00086
00087
00088 bool KFormulaDoc::saveOasis( KoStore* store, KoXmlWriter* manifestWriter )
00089 {
00090 if ( !store->open( "content.xml" ) )
00091 return false;
00092
00093 KoStoreDevice dev( store );
00094 KoXmlWriter* contentWriter = createOasisXmlWriter( &dev, "math:math" );
00095
00096
00097 KTempFile contentTmpFile;
00098 contentTmpFile.setAutoDelete( true );
00099 QFile* tmpFile = contentTmpFile.file();
00100
00101
00102 QTextStream stream(tmpFile);
00103 stream.setEncoding( QTextStream::UnicodeUTF8 );
00104 formula->saveMathML( stream, true );
00105
00106 tmpFile->close();
00107 contentWriter->addCompleteElement( tmpFile );
00108 contentTmpFile.close();
00109
00110
00111
00112 contentWriter->endElement();
00113 delete contentWriter;
00114
00115 if(!store->close())
00116 return false;
00117
00118 manifestWriter->addManifestEntry("content.xml", "text/xml");
00119
00120 setModified( false );
00121
00122 return true;
00123 }
00124
00125
00126 QDomDocument KFormulaDoc::saveXML()
00127 {
00128 QDomDocument doc = document->saveXML();
00129 history->documentSaved();
00130 return doc;
00131 }
00132
00133 bool KFormulaDoc::loadOasis( const QDomDocument& doc, KoOasisStyles&, const QDomDocument&, KoStore* )
00134 {
00135
00136
00137
00138
00139 if ( document->loadOasis( doc ) ) {
00140 history->clear();
00141 history->documentSaved();
00142 return true;
00143 }
00144 return false;
00145 }
00146
00147 bool KFormulaDoc::loadXML(QIODevice *, const QDomDocument& doc)
00148 {
00149 if ( doc.doctype().name().lower() == "math"
00150 || doc.documentElement().tagName().lower() == "math" )
00151 if ( document->loadOasis( doc ) ) {
00152 history->clear();
00153 history->documentSaved();
00154 return true;
00155 }
00156 if ( document->loadXML( doc ) ) {
00157 history->clear();
00158 history->documentSaved();
00159 return true;
00160 }
00161 return false;
00162 }
00163
00169 bool KFormulaDoc::saveNativeFormat( const QString & file )
00170 {
00171 QCString mimeType = outputMimeType();
00172 bool mathml = !mimeType.isEmpty() && mimeType.contains( "mathml", false );
00173 if ( mathml ) {
00174 QFile f( file );
00175 if ( f.open( IO_WriteOnly | IO_Translate ) )
00176 {
00177 QTextStream stream( &f );
00178 stream.setEncoding( QTextStream::UnicodeUTF8 );
00179 formula->saveMathML( stream, false );
00180 f.close();
00181 return true;
00182 }
00183 else
00184 return false;
00185 }
00186
00187 return KoDocument::saveNativeFormat( file );
00188 }
00189
00190 KoView* KFormulaDoc::createViewInstance(QWidget* _parent, const char *name)
00191 {
00192 return new KFormulaPartView(this, _parent, name);
00193 }
00194
00195 void KFormulaDoc::commandExecuted()
00196 {
00197 if (formula->isEmpty()) {
00198 setEmpty();
00199 }
00200 setModified(true);
00201 }
00202
00203 void KFormulaDoc::documentRestored()
00204 {
00205 setModified(false);
00206 }
00207
00208
00209 bool KFormulaDoc::initDoc(InitDocFlags , QWidget* )
00210 {
00211
00212 return TRUE;
00213 }
00214
00215 void KFormulaDoc::showStartUpWidget(KoMainWindow* parent, bool )
00216 {
00217 parent->setRootDocument( this );
00218 }
00219
00220 bool KFormulaDoc::showEmbedInitDialog(QWidget* )
00221 {
00222 return true;
00223 }
00224
00225 void KFormulaDoc::paintContent(QPainter& painter, const QRect& rect, bool transparent, double zoomX, double zoomY)
00226 {
00227
00228
00229
00230 bool forPrint = painter.device() && painter.device()->devType() == QInternal::Printer;
00231 document->setZoomAndResolution( 100, zoomX, zoomY, true, forPrint );
00232 if ( !transparent ) {
00233 painter.fillRect( rect, Qt::white );
00234 }
00235 formula->draw( painter, rect );
00236 }
00237
00238 QString KFormulaDoc::configFile() const
00239 {
00240
00241
00242
00243
00244 return QString::null;
00245 }
00246
00247 #include "kformula_doc.moc"