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_view.cc 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. class KPrinter;
  18. #include <tqpainter.h>
  19. #include <tqpopupmenu.h>
  20. #include <tqtextedit.h>
  21. #include <tqtimer.h>
  22. #include <tdeaction.h>
  23. #include <kdebug.h>
  24. #include <kiconloader.h>
  25. #include <tdelocale.h>
  26. #include <kstdaction.h>
  27. #include <ktip.h>
  28. //#include <tdeglobal.h>
  29. #include <kformulacontainer.h>
  30. #include <kformuladocument.h>
  31. #include "formulastring.h"
  32. #include "fsparser.h"
  33. #include "kfconfig.h"
  34. #include "kformula_doc.h"
  35. #include "kformula_factory.h"
  36. #include "kformula_view.h"
  37. #include "kformula_view_iface.h"
  38. #include "kformulawidget.h"
  39. bool KFormulaPartView::first_window = true;
  40. KFormulaPartView::KFormulaPartView(KFormulaDoc* _doc, TQWidget* _parent, const char* _name)
  41. : KoView( _doc, _parent, _name ), m_pDoc(_doc)
  42. {
  43. setInstance(KFormulaFactory::global());
  44. if ( !_doc->isReadWrite() )
  45. setXMLFile("kformula_readonly.rc");
  46. else
  47. setXMLFile("kformula.rc");
  48. m_dcop = 0;
  49. dcopObject(); // build it
  50. scrollview = new TQScrollView(this, "scrollview");
  51. formulaWidget = new KFormulaWidget(_doc->getFormula(), scrollview->viewport(), "formulaWidget");
  52. scrollview->addChild(formulaWidget);
  53. scrollview->viewport()->setFocusProxy( scrollview );
  54. scrollview->viewport()->setFocusPolicy( TQ_WheelFocus );
  55. scrollview->setFocusPolicy( TQ_NoFocus );
  56. formulaWidget->setFocus();
  57. // Nice parts start in read only mode.
  58. formulaWidget->setReadOnly(true);
  59. KFormula::Container* formula = m_pDoc->getFormula();
  60. KFormula::Document* document = m_pDoc->getDocument();
  61. // copy&paste
  62. cutAction = KStdAction::cut(document->wrapper(), TQT_SLOT(cut()), actionCollection());
  63. copyAction = KStdAction::copy(document->wrapper(), TQT_SLOT(copy()), actionCollection());
  64. pasteAction = KStdAction::paste(document->wrapper(), TQT_SLOT(paste()), actionCollection());
  65. cutAction->setEnabled(false);
  66. copyAction->setEnabled(false);
  67. // tip of the day
  68. KStdAction::tipOfDay( TQT_TQOBJECT(this), TQT_SLOT( slotShowTip() ), actionCollection() );
  69. // elements
  70. addBracketAction = document->wrapper()->getAddBracketAction();
  71. addFractionAction = document->wrapper()->getAddFractionAction();
  72. addRootAction = document->wrapper()->getAddRootAction();
  73. addSumAction = document->wrapper()->getAddSumAction();
  74. addProductAction = document->wrapper()->getAddProductAction();
  75. addIntegralAction = document->wrapper()->getAddIntegralAction();
  76. addMatrixAction = document->wrapper()->getAddMatrixAction();
  77. addUpperLeftAction = document->wrapper()->getAddUpperLeftAction();
  78. addLowerLeftAction = document->wrapper()->getAddLowerLeftAction();
  79. addUpperRightAction = document->wrapper()->getAddUpperRightAction();
  80. addLowerRightAction = document->wrapper()->getAddLowerRightAction();
  81. addGenericUpperAction = document->wrapper()->getAddGenericUpperAction();
  82. addGenericLowerAction = document->wrapper()->getAddGenericLowerAction();
  83. removeEnclosingAction = document->wrapper()->getRemoveEnclosingAction();
  84. (void) KStdAction::selectAll(TQT_TQOBJECT(formulaWidget), TQT_SLOT(slotSelectAll()), actionCollection());
  85. //------------------------ Settings menu
  86. KStdAction::preferences( TQT_TQOBJECT(this), TQT_SLOT(configure()), actionCollection(), "configure" );
  87. // font stuff
  88. // TDEFontAction* actionElement_Text_Font = new TDEFontAction(i18n( "Font Family" ),0,
  89. // actionCollection(),"textfont");
  90. // connect( actionElement_Text_Font, TQT_SIGNAL( activated( const TQString& ) ), TQT_TQOBJECT(this), TQT_SLOT( fontSelected( const TQString& ) ) );
  91. TDEFontSizeAction* actionTextSize = new TDEFontSizeAction(i18n( "Size" ),0,
  92. actionCollection(),"formula_textsize");
  93. actionTextSize->setFontSize( m_pDoc->getFormula()->fontSize() );
  94. connect( actionTextSize, TQT_SIGNAL( fontSizeChanged( int ) ), TQT_TQOBJECT(this), TQT_SLOT( sizeSelected( int ) ) );
  95. connect( formula, TQT_SIGNAL( baseSizeChanged( int ) ), actionTextSize, TQT_SLOT( setFontSize( int ) ) );
  96. // TDEToggleAction* actionElement_Text_Bold = new TDEToggleAction(i18n( "Bold" ),
  97. // "bold",
  98. // 0,
  99. // actionCollection(),"textbold");
  100. // connect( actionElement_Text_Bold, TQT_SIGNAL( toggled( bool ) ), TQT_TQOBJECT(this), TQT_SLOT( bold( bool ) ) );
  101. // TDEToggleAction* actionElement_Text_Italic = new TDEToggleAction(i18n( "Italic" ),
  102. // "italic",
  103. // 0,
  104. // actionCollection(),"textitalic");
  105. // connect( actionElement_Text_Italic, TQT_SIGNAL( toggled( bool ) ), TQT_TQOBJECT(this), TQT_SLOT( italic( bool ) ) );
  106. // TDEToggleAction* actionElement_Text_Under = new TDEToggleAction(i18n( "Underlined" ),
  107. // "underl",
  108. // 0,
  109. // actionCollection(),"textunder");
  110. // connect(actionElement_Text_Under, TQT_SIGNAL( toggled( bool ) ), TQT_TQOBJECT(this), TQT_SLOT( underline( bool ) ) );
  111. formulaStringAction = new TDEAction( i18n( "Edit Formula String..." ),
  112. 0,
  113. TQT_TQOBJECT(this), TQT_SLOT( formulaString() ),
  114. actionCollection(), "formula_formulastring" );
  115. // notify on cursor change
  116. connect(formulaWidget, TQT_SIGNAL(cursorChanged(bool, bool)),
  117. TQT_TQOBJECT(this), TQT_SLOT(cursorChanged(bool, bool)));
  118. connect( formula, TQT_SIGNAL( statusMsg( const TQString& ) ),
  119. TQT_TQOBJECT(this), TQT_SLOT( slotActionStatusText( const TQString& ) ) );
  120. if ( !_doc->isEmbedded() && first_window ) {
  121. TQTimer::singleShot( 200, TQT_TQOBJECT(this), TQT_SLOT(slotShowTipOnStart()) );
  122. first_window = false;
  123. }
  124. }
  125. KFormulaPartView::~KFormulaPartView()
  126. {
  127. delete m_dcop;
  128. }
  129. DCOPObject* KFormulaPartView::dcopObject()
  130. {
  131. if ( !m_dcop )
  132. m_dcop = new KformulaViewIface( this );
  133. return m_dcop;
  134. }
  135. void KFormulaPartView::focusInEvent(TQFocusEvent*)
  136. {
  137. formulaWidget->setFocus();
  138. }
  139. void KFormulaPartView::updateReadWrite(bool readwrite)
  140. {
  141. formulaWidget->setReadOnly(!readwrite);
  142. setEnabled(readwrite);
  143. }
  144. void KFormulaPartView::slotShowTipOnStart() {
  145. KTipDialog::showTip( this );
  146. }
  147. void KFormulaPartView::slotShowTip() {
  148. KTipDialog::showTip( this, TQString(), true );
  149. }
  150. void KFormulaPartView::setEnabled(bool enabled)
  151. {
  152. addBracketAction->setEnabled(enabled);
  153. addFractionAction->setEnabled(enabled);
  154. addRootAction->setEnabled(enabled);
  155. addSumAction->setEnabled(enabled);
  156. addIntegralAction->setEnabled(enabled);
  157. addMatrixAction->setEnabled(enabled);
  158. addUpperLeftAction->setEnabled(enabled);
  159. addLowerLeftAction->setEnabled(enabled);
  160. addUpperRightAction->setEnabled(enabled);
  161. addLowerRightAction->setEnabled(enabled);
  162. addGenericUpperAction->setEnabled(enabled);
  163. addGenericLowerAction->setEnabled(enabled);
  164. removeEnclosingAction->setEnabled(enabled);
  165. }
  166. void KFormulaPartView::resizeEvent( TQResizeEvent * )
  167. {
  168. scrollview->setGeometry(0, 0, width(), height());
  169. }
  170. void KFormulaPartView::setupPrinter(KPrinter&)
  171. {
  172. }
  173. void KFormulaPartView::print(KPrinter& printer)
  174. {
  175. m_pDoc->getFormula()->print(printer);
  176. }
  177. const KFormula::View* KFormulaPartView::formulaView() const { return formulaWidget->view(); }
  178. KFormula::View* KFormulaPartView::formulaView() { return formulaWidget->view(); }
  179. void KFormulaPartView::cursorChanged(bool visible, bool selecting)
  180. {
  181. cutAction->setEnabled(visible && selecting);
  182. copyAction->setEnabled(visible && selecting);
  183. removeEnclosingAction->setEnabled(!selecting);
  184. if (visible) {
  185. int x = formulaWidget->getCursorPoint().x();
  186. int y = formulaWidget->getCursorPoint().y();
  187. scrollview->ensureVisible(x, y);
  188. }
  189. KFormula::Document* doc = document()->getDocument();
  190. doc->wrapper()->getFormatBoldAction()->setEnabled( selecting );
  191. doc->wrapper()->getFormatItalicAction()->setEnabled( selecting );
  192. doc->wrapper()->getFontFamilyAction()->setEnabled( selecting );
  193. if ( !selecting ) {
  194. doc->wrapper()->getFormatBoldAction()->setChecked( false );
  195. doc->wrapper()->getFormatItalicAction()->setChecked( false );
  196. doc->wrapper()->getFontFamilyAction()->setCurrentItem( 0 );
  197. }
  198. }
  199. void KFormulaPartView::formulaString()
  200. {
  201. FormulaString dia( this );
  202. dia.textWidget->setText( document()->getFormula()->formulaString() );
  203. if ( dia.exec() ) {
  204. // How lovely.
  205. }
  206. }
  207. void KFormulaPartView::sizeSelected( int size )
  208. {
  209. document()->getFormula()->setFontSize( size );
  210. formulaWidget->setFocus();
  211. }
  212. TQStringList KFormulaPartView::readFormulaString( TQString text )
  213. {
  214. FormulaStringParser parser( document()->getDocument()->getSymbolTable(), text );
  215. TQDomDocument formula = parser.parse();
  216. TQStringList errorList = parser.errorList();
  217. //if ( errorList.count() == 0 ) {
  218. formulaView()->slotSelectAll();
  219. document()->getFormula()->paste( formula, i18n( "Read Formula String" ) );
  220. //}
  221. return errorList;
  222. }
  223. void KFormulaPartView::configure()
  224. {
  225. KFConfig configDia( this );
  226. configDia.exec();
  227. }
  228. #include "kformula_view.moc"