KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kformula_doc.cc 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. * Boston, MA 02110-1301, USA.
  16. */
  17. #include "kformula_doc.h"
  18. #include "kformula_view.h"
  19. #include "kformula_factory.h"
  20. #include <tqbitmap.h>
  21. #include <tqcolor.h>
  22. #include <tqdom.h>
  23. #include <tqpainter.h>
  24. #include <tqpopupmenu.h>
  25. #include <tqprinter.h>
  26. #include <tqstring.h>
  27. #include <tqwmatrix.h>
  28. #include <tqfile.h>
  29. #include <config.h>
  30. #include <unistd.h>
  31. #include <tdeaboutdialog.h>
  32. #include <tdeaction.h>
  33. #include <tdeapplication.h>
  34. #include <kdebug.h>
  35. #include <KoGlobal.h>
  36. #include <kiconloader.h>
  37. #include <tdelocale.h>
  38. #include <kstdaction.h>
  39. #include <kstandarddirs.h>
  40. #include <kurl.h>
  41. #include <KoXmlWriter.h>
  42. #include <KoStoreDevice.h>
  43. #include <tdetempfile.h>
  44. #include <KoMainWindow.h>
  45. #include <kformulacontainer.h>
  46. #include <kformuladocument.h>
  47. KFormulaDoc::KFormulaDoc(TQWidget *parentWidget, const char *widgetName, TQObject* parent, const char* name, bool singleViewMode)
  48. : KoDocument(parentWidget, widgetName, parent, name, singleViewMode)
  49. {
  50. setInstance(KFormulaFactory::global(), false);
  51. //kdDebug(39001) << "General Settings" << endl;
  52. history = new KoCommandHistory( actionCollection() );
  53. wrapper = new KFormula::DocumentWrapper( kapp->config(),
  54. actionCollection(),
  55. history );
  56. document = new KFormula::Document;
  57. wrapper->document( document );
  58. formula = document->createFormula();
  59. document->setEnabled( true );
  60. // the modify flag
  61. connect(history, TQT_SIGNAL(commandExecuted()), this, TQT_SLOT(commandExecuted()));
  62. connect(history, TQT_SIGNAL(documentRestored()), this, TQT_SLOT(documentRestored()));
  63. dcopObject();
  64. }
  65. KFormulaDoc::~KFormulaDoc()
  66. {
  67. delete history;
  68. delete wrapper;
  69. }
  70. bool KFormulaDoc::saveOasis( KoStore* store, KoXmlWriter* manifestWriter )
  71. {
  72. if ( !store->open( "content.xml" ) )
  73. return false;
  74. KoStoreDevice dev( store );
  75. KoXmlWriter* contentWriter = createOasisXmlWriter( &dev, "math:math" );
  76. KTempFile contentTmpFile;
  77. contentTmpFile.setAutoDelete( true );
  78. TQFile* tmpFile = contentTmpFile.file();
  79. //todo save content
  80. TQTextStream stream(tmpFile);
  81. stream.setEncoding( TQTextStream::UnicodeUTF8 );
  82. formula->saveMathML( stream, true );
  83. tmpFile->close();
  84. contentWriter->addCompleteElement( TQT_TQIODEVICE(tmpFile) );
  85. contentTmpFile.close();
  86. contentWriter->endElement();
  87. delete contentWriter;
  88. if(!store->close())
  89. return false;
  90. manifestWriter->addManifestEntry("content.xml", "text/xml");
  91. setModified( false );
  92. return true;
  93. }
  94. TQDomDocument KFormulaDoc::saveXML()
  95. {
  96. TQDomDocument doc = document->saveXML();
  97. history->documentSaved();
  98. return doc;
  99. }
  100. bool KFormulaDoc::loadOasis( const TQDomDocument& doc, KoOasisStyles&, const TQDomDocument&, KoStore* )
  101. {
  102. // we don't have style into this format
  103. // we don't have settings into kformula (for the moment)
  104. // necessary to adapt kformula code to load MathML format with Oasis Extension.
  105. if ( document->loadOasis( doc ) ) {
  106. history->clear();
  107. history->documentSaved();
  108. return true;
  109. }
  110. return false;
  111. }
  112. bool KFormulaDoc::loadXML(TQIODevice *, const TQDomDocument& doc)
  113. {
  114. if ( doc.doctype().name().lower() == "math"
  115. || doc.documentElement().tagName().lower() == "math" )
  116. if ( document->loadOasis( doc ) ) {
  117. history->clear();
  118. history->documentSaved();
  119. return true;
  120. }
  121. if ( document->loadXML( doc ) ) {
  122. history->clear();
  123. history->documentSaved();
  124. return true;
  125. }
  126. return false;
  127. }
  128. /**
  129. * Saves the document in native format, to a given file.
  130. * It is reimplemented to handle the special case of MathML, since it is
  131. * a native format but not compressed.
  132. */
  133. bool KFormulaDoc::saveNativeFormat( const TQString & file )
  134. {
  135. TQCString mimeType = outputMimeType();
  136. bool mathml = !mimeType.isEmpty() && mimeType.contains( "mathml", false );
  137. if ( mathml ) {
  138. TQFile f( file );
  139. if ( f.open( IO_WriteOnly | IO_Translate ) )
  140. {
  141. TQTextStream stream( &f );
  142. stream.setEncoding( TQTextStream::UnicodeUTF8 );
  143. formula->saveMathML( stream, false );
  144. f.close();
  145. return true;
  146. }
  147. else
  148. return false;
  149. }
  150. // If it's not MathML, let the parent handle it
  151. return KoDocument::saveNativeFormat( file );
  152. }
  153. KoView* KFormulaDoc::createViewInstance(TQWidget* _parent, const char *name)
  154. {
  155. return new KFormulaPartView(this, _parent, name);
  156. }
  157. void KFormulaDoc::commandExecuted()
  158. {
  159. if (formula->isEmpty()) {
  160. setEmpty();
  161. }
  162. setModified(true);
  163. }
  164. void KFormulaDoc::documentRestored()
  165. {
  166. setModified(false);
  167. }
  168. bool KFormulaDoc::initDoc(InitDocFlags /*flags*/, TQWidget* /*parentWidget*/)
  169. {
  170. // If nothing is loaded, do initialize here
  171. return TRUE;
  172. }
  173. void KFormulaDoc::showStartUpWidget(KoMainWindow* parent, bool /*alwaysShow*/)
  174. {
  175. parent->setRootDocument( this );
  176. }
  177. bool KFormulaDoc::showEmbedInitDialog(TQWidget* /*parent*/)
  178. {
  179. return true;
  180. }
  181. void KFormulaDoc::paintContent(TQPainter& painter, const TQRect& rect, bool transparent, double zoomX, double zoomY)
  182. {
  183. // ####### handle transparency and zoom
  184. // Need to draw only the document rectangle described in the parameter rect.
  185. bool forPrint = painter.device() && painter.device()->devType() == TQInternal::Printer;
  186. document->setZoomAndResolution( 100, zoomX, zoomY, true, forPrint );
  187. if ( !transparent ) {
  188. painter.fillRect( rect, TQt::white );
  189. }
  190. formula->draw( painter, rect );
  191. }
  192. TQString KFormulaDoc::configFile() const
  193. {
  194. // return readConfigFile( locate( "data", "kformula/kformula.rc",
  195. // KFormulaFactory::global() ) );
  196. // return readConfigFile( "kformula.rc" );
  197. return TQString();
  198. }
  199. #include "kformula_doc.moc"