kformula

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