kformula

kformula_doc.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
00003                   Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.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.
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 
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     //kdDebug(39001) << "General Settings" << endl;
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     // the modify flag
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     KoXmlWriter contentTmpWriter( tmpFile, 1 );
00101 
00102     //todo save content
00103     QTextStream stream(tmpFile);
00104     stream.setEncoding( QTextStream::UnicodeUTF8 );
00105     formula->saveMathML( stream, true );
00106 
00107     tmpFile->close();
00108     contentWriter->addCompleteElement( tmpFile );
00109     contentTmpFile.close();
00110 
00111 
00112 
00113     contentWriter->endElement();
00114     delete contentWriter;
00115 
00116     if(!store->close())
00117         return false;
00118 
00119     manifestWriter->addManifestEntry("content.xml", "text/xml");
00120 
00121     setModified( false );
00122 
00123     return true;
00124 }
00125 
00126 
00127 QDomDocument KFormulaDoc::saveXML()
00128 {
00129     QDomDocument doc = document->saveXML();
00130     history->documentSaved();
00131     return doc;
00132 }
00133 
00134 bool KFormulaDoc::loadOasis( const QDomDocument& doc, KoOasisStyles&, const QDomDocument&, KoStore* )
00135 {
00136     // we don't have style into this format
00137     // we don't have settings into kformula (for the moment)
00138     // necessary to adapt kformula code to load MathML format with Oasis Extension.
00139     
00140     if ( document->loadOasis( doc ) ) {
00141         history->clear();
00142         history->documentSaved();
00143         return true;
00144     }
00145     return false;
00146 }
00147 
00148 bool KFormulaDoc::loadXML(QIODevice *, const QDomDocument& doc)
00149 {
00150     if ( document->loadXML( doc ) ) {
00151         history->clear();
00152         history->documentSaved();
00153         return true;
00154     }
00155     return false;
00156 }
00157 
00158 
00159 KoView* KFormulaDoc::createViewInstance(QWidget* _parent, const char *name)
00160 {
00161     return new KFormulaPartView(this, _parent, name);
00162 }
00163 
00164 void KFormulaDoc::commandExecuted()
00165 {
00166     if (formula->isEmpty()) {
00167         setEmpty();
00168     }
00169     setModified(true);
00170 }
00171 
00172 void KFormulaDoc::documentRestored()
00173 {
00174     setModified(false);
00175 }
00176 
00177 
00178 bool KFormulaDoc::initDoc(InitDocFlags /*flags*/, QWidget* /*parentWidget*/)
00179 {
00180     // If nothing is loaded, do initialize here
00181     return TRUE;
00182 }
00183 
00184 void KFormulaDoc::showStartUpWidget(KoMainWindow* parent, bool /*alwaysShow*/)
00185 {
00186     parent->setRootDocument( this );
00187 }
00188 
00189 bool KFormulaDoc::showEmbedInitDialog(QWidget* /*parent*/)
00190 {
00191   return true;
00192 }
00193 
00194 void KFormulaDoc::paintContent(QPainter& painter, const QRect& rect, bool transparent, double zoomX, double zoomY)
00195 {
00196     // ####### handle transparency and zoom
00197     // Need to draw only the document rectangle described in the parameter rect.
00198 
00199     bool forPrint = painter.device() && painter.device()->devType() == QInternal::Printer;
00200     document->setZoomAndResolution( 100, zoomX, zoomY, true, forPrint );
00201     if ( !transparent ) {
00202         painter.fillRect( rect, Qt::white );
00203     }
00204     formula->draw( painter, rect );
00205 }
00206 
00207 QString KFormulaDoc::configFile() const
00208 {
00209 //    return readConfigFile( locate( "data", "kformula/kformula.rc",
00210 //                                 KFormulaFactory::global() ) );
00211 
00212 //    return readConfigFile( "kformula.rc" );
00213     return QString::null;
00214 }
00215 
00216 #include "kformula_doc.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys