lib

kformuladocument.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 of the License, or (at your option) any later version.
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
21#include <tqptrlist.h>
22#include <tqstringlist.h>
23
24#include <kdebug.h>
25#include <tdeglobal.h>
26#include <kiconloader.h>
27#include <tdelocale.h>
28#include <ksimpleconfig.h>
29#include <kstandarddirs.h>
30
31#include <KoDocument.h>
32
33#include "contextstyle.h"
34#include "creationstrategy.h"
35#include "oasiscreationstrategy.h"
36#include "kformulacontainer.h"
37#include "kformuladocument.h"
38#include "sequenceelement.h"
39#include "symboltable.h"
40#include "symbolaction.h"
41
42KFORMULA_NAMESPACE_BEGIN
43
44static const int CURRENT_SYNTAX_VERSION = 1;
45// Make sure an appropriate DTD is available in www/koffice/DTD if changing this value
46static const char * CURRENT_DTD_VERSION = "1.3";
47
48int FormulaList::compareItems( TQPtrCollection::Item a, TQPtrCollection::Item b )
49{
50 double ya = static_cast<Container*>( a )->getDocumentY();
51 double yb = static_cast<Container*>( b )->getDocumentY();
52 if ( fabs( ya-yb ) < 1e-4 ) {
53 double xa = static_cast<Container*>( a )->getDocumentX();
54 double xb = static_cast<Container*>( b )->getDocumentX();
55 if ( xa < xb ) return -1;
56 if ( xa > xb ) return 1;
57 return 0;
58 }
59 if ( ya < yb ) return -1;
60 return 1;
61}
62
63
64Document::Document( TQObject *parent, const char *name,
65 const TQStringList &/*args*/ )
66 : TQObject( parent, name ), m_wrapper( 0 ), m_formula( 0 ), creationStrategy( 0 )
67{
68 m_contextStyle = new ContextStyle;
69 setCreationStrategy( "Oasis" );
70 formulae.setAutoDelete( false );
71}
72
73
74Document::~Document()
75{
76 // Destroy remaining formulae. We do it backward because
77 // the formulae remove themselves from this document upon
78 // destruction.
79 int count = formulae.count();
80 for ( int i=count-1; i>=0; --i ) {
81 delete formulae.at( i );
82 }
83 delete m_contextStyle;
84}
85
86
87bool Document::hasFormula()
88{
89 return ( m_formula != 0 ) && ( m_formula->activeCursor() != 0 );
90}
91
92
93Container* Document::createFormula( int pos, bool registerMe )
94{
95 Container* formula = new Container( this, pos, registerMe );
96 formula->initialize();
97 return formula;
98}
99
100
101TQPtrListIterator<Container> Document::formulas()
102{
103 return TQPtrListIterator<Container>( formulae );
104}
105
106
107int Document::formulaPos( Container* formula )
108{
109 return formulae.find( formula );
110}
111
112
113Container* Document::formulaAt( uint pos )
114{
115 return formulae.at( pos );
116}
117
118
120{
121 return formulae.count();
122}
123
124
125bool Document::loadXML( const TQDomDocument& doc )
126{
127 setCreationStrategy( "Ordinary" );
128
129 //clear();
130 TQDomElement root = doc.documentElement();
131
132 // backward compatibility
133 if ( root.tagName() == "FORMULA" ) {
134 Container* formula = newFormula( 0 );
135 return formula->load( root );
136 }
137
138 TQDomNode node = root.firstChild();
139 if ( node.isElement() ) {
140 TQDomElement element = node.toElement();
141 if ( element.tagName() == "FORMULASETTINGS" ) {
142 if ( !loadDocumentPart( element ) ) {
143 return false;
144 }
145 }
146 node = node.nextSibling();
147 }
148 uint number = 0;
149 while ( !node.isNull() ) {
150 if ( node.isElement() ) {
151 TQDomElement element = node.toElement();
152 Container* formula = newFormula( number );
153 if ( !formula->load( element ) ) {
154 return false;
155 }
156 number += 1;
157 }
158 node = node.nextSibling();
159 }
160 return formulae.count() > 0;
161}
162
163bool Document::loadOasis( const TQDomDocument& doc )
164{
165 // ### TODO: not finished!
166 setCreationStrategy( "Oasis" );
167 KFormula::Container* formula = newFormula( 0 );
168 return formula->loadMathML( doc, true );
169}
170
171bool Document::loadDocumentPart( TQDomElement /*node*/ )
172{
173 return true;
174}
175
176TQDomDocument Document::saveXML()
177{
178 TQDomDocument doc = createDomDocument();
179 TQDomElement root = doc.documentElement();
180 root.appendChild( saveDocumentPart( doc ) );
181 uint count = formulae.count();
182 for ( uint i=0; i<count; ++i ) {
183 formulae.at( i )->save( root );
184 }
185 return doc;
186}
187
188
189TQDomElement Document::saveDocumentPart( TQDomDocument& doc )
190{
191 TQDomElement settings = doc.createElement( "FORMULASETTINGS" );
192 return settings;
193}
194
195
197{
198 return KoDocument::createDomDocument( "kformula", "KFORMULA",
199 CURRENT_DTD_VERSION );
200}
201
207{
208 TQDomDocumentType dt =
209 TQDomImplementation().createDocumentType( "math",
210 "-//W3C//DTD MathML 2.0//EN",
211 "http://www.w3.org/TR/MathML2/dtd/mathml2.dtd");
212 TQDomDocument doc( dt );
213 doc.insertBefore( doc.createProcessingInstruction( "xml",
214 "version=\"1.0\" encoding=\"UTF-8\"" ),
215 doc.documentElement() );
216 return doc;
217}
218
225void Document::setCreationStrategy( TQString strategy )
226{
227 if ( !creationStrategy || creationStrategy->type() != strategy ) {
228 delete creationStrategy;
229 if ( strategy == "Ordinary" )
230 creationStrategy = new OrdinaryCreationStrategy;
231 else
232 creationStrategy = new OasisCreationStrategy;
233 SequenceElement::setCreationStrategy( creationStrategy );
234 }
235}
236
237void Document::registerFormula( Container* f, int pos )
238{
239 if ( ( pos > -1 ) &&
240 ( static_cast<uint>( pos ) < formulae.count() ) ) {
241 formulae.insert( pos, f );
242 //emit sigInsertFormula( f, pos );
243 }
244 else {
245 formulae.append( f );
246 //emit sigInsertFormula( f, formulae.count()-1 );
247 }
248}
249
250void Document::unregisterFormula( Container* f )
251{
252 if ( m_formula == f ) {
253 m_formula = 0;
254 }
255 formulae.removeRef( f );
256}
257
258void Document::activate(Container* f)
259{
260 m_formula = f;
261}
262
263
265{
266 formulae.sort();
267}
268
269
270Container* Document::newFormula( uint number )
271{
272 if ( number < formulae.count() ) {
273 return formulae.at( number );
274 }
275 return createFormula();
276}
277
278
279double Document::getXResolution() const
280{
281 return m_contextStyle->zoomedResolutionX();
282}
283double Document::getYResolution() const
284{
285 return m_contextStyle->zoomedResolutionY();
286}
287
289{
290 return m_contextStyle->symbolTable();
291}
292
294{
295 m_contextStyle->setEdit( edit );
296 return *m_contextStyle;
297}
298
299void Document::setZoomAndResolution( int zoom, int dpiX, int dpiY )
300{
301 m_contextStyle->setZoomAndResolution( zoom, dpiX, dpiY );
302}
303
304void Document::newZoomAndResolution( bool updateViews, bool /*forPrint*/ )
305{
306 if ( updateViews ) {
307 recalc();
308 }
309}
310
312 double zoomX, double zoomY,
313 bool updateViews, bool forPrint )
314{
315 if ( getContextStyle( !forPrint ).setZoomAndResolution( zoom, zoomX, zoomY, updateViews, forPrint ) && updateViews ) {
316 recalc();
317 }
318}
319
320
321SymbolType Document::leftBracketChar()
322{
323 return m_wrapper->leftBracketChar();
324}
325
326SymbolType Document::rightBracketChar()
327{
328 return m_wrapper->rightBracketChar();
329}
330
331
332void Document::setEnabled( bool enabled )
333{
334 m_wrapper->setEnabled( enabled );
335}
336
337
339{
340 return m_wrapper->getHistory();
341}
342
343
344void Document::recalc()
345{
346 for ( Container* f = formulae.first();
347 f != 0;
348 f=formulae.next() ) {
349 f->recalc();
350 }
351}
352
353
355{
356 m_wrapper->updateConfig();
357 recalc();
358}
359
360
361void Document::introduceWrapper( DocumentWrapper* wrapper, bool init )
362{
363 m_wrapper = wrapper;
364 m_contextStyle->readConfig( wrapper->config(), init );
365 m_contextStyle->init( init );
366}
367
368
370
371DocumentWrapper::DocumentWrapper( TDEConfig* config,
372 TDEActionCollection* collection,
373 KoCommandHistory* history )
374 : m_document( 0 ),
375 m_leftBracketChar( LeftRoundBracket ),
376 m_rightBracketChar( RightRoundBracket ),
377 m_config( config ),
378 m_hasActions( collection != 0 )
379{
380 if ( m_hasActions ) {
381 createActions( collection );
382 enableMatrixActions( false );
383 }
384 setCommandStack( history );
385}
386
387
388DocumentWrapper::~DocumentWrapper()
389{
390 delete m_document;
391 if ( m_ownHistory ) {
392 delete m_history;
393 }
394
395 if ( m_hasActions )
396 {
397 m_config->setGroup("General");
398 m_config->writeEntry("syntaxHighlighting", m_syntaxHighlightingAction->isChecked() );
399 }
400}
401
402
404{
405 m_document = document;
406 m_document->introduceWrapper( this, init );
407 initSymbolNamesAction();
408 m_config->setGroup("General");
409 if ( m_hasActions )
410 {
411 m_syntaxHighlightingAction->setChecked( m_config->readBoolEntry("syntaxHighlighting", true ) );
412 if ( !m_syntaxHighlightingAction->isChecked() )
413 toggleSyntaxHighlighting();
414 }
415 else if ( m_config->readBoolEntry("syntaxHighlighting", true ) )
416 {
417 m_document->m_contextStyle->setSyntaxHighlighting( true );
418 // Only to notify all views. We don't expect to get new values.
419 m_document->recalc();
420 }
421}
422
423
424void DocumentWrapper::setCommandStack( KoCommandHistory* history )
425{
426 if ( history == 0 ) {
427 m_history = new KoCommandHistory;
428 m_ownHistory = true;
429 }
430 else {
431 m_history = history;
432 m_ownHistory = false;
433 }
434}
435
436
437void DocumentWrapper::createActions( TDEActionCollection* collection )
438{
439 TDEGlobal::dirs()->addResourceType( "toolbar",
440 TDEStandardDirs::kde_default("data") +
441 "kformula/pics/" );
442
443 m_addNegThinSpaceAction = new TDEAction( i18n( "Add Negative Thin Space" ),
444 0,
445 this, TQ_SLOT( addNegThinSpace() ),
446 collection, "formula_addnegthinspace") ;
447 m_addThinSpaceAction = new TDEAction( i18n( "Add Thin Space" ),
448 0,
449 this, TQ_SLOT( addThinSpace() ),
450 collection, "formula_addthinspace") ;
451 m_addMediumSpaceAction = new TDEAction( i18n( "Add Medium Space" ),
452 0,
453 this, TQ_SLOT( addMediumSpace() ),
454 collection, "formula_addmediumspace" );
455 m_addThickSpaceAction = new TDEAction( i18n( "Add Thick Space" ),
456 0,
457 this, TQ_SLOT( addThickSpace() ),
458 collection, "formula_addthickspace" );
459 m_addQuadSpaceAction = new TDEAction( i18n( "Add Quad Space" ),
460 0,
461 this, TQ_SLOT( addQuadSpace() ),
462 collection, "formula_addquadspace" );
463
464 m_addIntegralAction = new TDEAction(i18n("Add Integral"),
465 "int",
466 0,
467 this, TQ_SLOT(addIntegral()),
468 collection, "formula_addintegral");
469 m_addSumAction = new TDEAction(i18n("Add Sum"),
470 "sum",
471 0,
472 this, TQ_SLOT(addSum()),
473 collection, "formula_addsum");
474 m_addProductAction = new TDEAction(i18n("Add Product"),
475 "prod",
476 0,
477 this, TQ_SLOT(addProduct()),
478 collection, "formula_addproduct");
479 m_addRootAction = new TDEAction(i18n("Add Root"),
480 "sqrt",
481 0,
482 this, TQ_SLOT(addRoot()),
483 collection, "formula_addroot");
484 m_addFractionAction = new TDEAction(i18n("Add Fraction"),
485 "frac",
486 0,
487 this, TQ_SLOT(addFraction()),
488 collection, "formula_addfrac");
489 m_addBracketAction = new TDEAction(i18n("Add Bracket"),
490 "paren",
491 0,
492 this, TQ_SLOT(addDefaultBracket()),
493 collection,"formula_addbra");
494 m_addSBracketAction = new TDEAction(i18n("Add Square Bracket"),
495 "brackets",
496 0,
497 this, TQ_SLOT(addSquareBracket()),
498 collection,"formula_addsqrbra");
499 m_addCBracketAction = new TDEAction(i18n("Add Curly Bracket"),
500 "math_brace",
501 0,
502 this, TQ_SLOT(addCurlyBracket()),
503 collection,"formula_addcurbra");
504 m_addAbsAction = new TDEAction(i18n("Add Abs"),
505 "abs",
506 0,
507 this, TQ_SLOT(addLineBracket()),
508 collection,"formula_addabsbra");
509
510 m_addMatrixAction = new TDEAction(i18n("Add Matrix..."),
511 "matrix",
512 0,
513 this, TQ_SLOT(addMatrix()),
514 collection, "formula_addmatrix");
515
516 m_addOneByTwoMatrixAction = new TDEAction(i18n("Add 1x2 Matrix"),
517 "onetwomatrix",
518 0,
519 this, TQ_SLOT(addOneByTwoMatrix()),
520 collection, "formula_add_one_by_two_matrix");
521
522
523 m_addUpperLeftAction = new TDEAction(i18n("Add Upper Left Index"),
524 "lsup",
525 0,
526 this, TQ_SLOT(addUpperLeftIndex()),
527 collection, "formula_addupperleft");
528 m_addLowerLeftAction = new TDEAction(i18n("Add Lower Left Index"),
529 "lsub",
530 0,
531 this, TQ_SLOT(addLowerLeftIndex()),
532 collection, "formula_addlowerleft");
533 m_addUpperRightAction = new TDEAction(i18n("Add Upper Right Index"),
534 "rsup",
535 0,
536 this, TQ_SLOT(addUpperRightIndex()),
537 collection, "formula_addupperright");
538 m_addLowerRightAction = new TDEAction(i18n("Add Lower Right Index"),
539 "rsub",
540 0,
541 this, TQ_SLOT(addLowerRightIndex()),
542 collection, "formula_addlowerright");
543
544 m_addGenericUpperAction = new TDEAction(i18n("Add Upper Index"),
545 "gsup",
546 /*CTRL + Key_U*/0,
547 this, TQ_SLOT(addGenericUpperIndex()),
548 collection, "formula_addupperindex");
549 m_addGenericLowerAction = new TDEAction(i18n("Add Lower Index"),
550 "gsub",
551 0,
552 this, TQ_SLOT(addGenericLowerIndex()),
553 collection, "formula_addlowerindex");
554
555 m_addOverlineAction = new TDEAction(i18n("Add Overline"),
556 "over",
557 0,
558 this, TQ_SLOT(addOverline()),
559 collection, "formula_addoverline");
560 m_addUnderlineAction = new TDEAction(i18n("Add Underline"),
561 "under",
562 0,
563 this, TQ_SLOT(addUnderline()),
564 collection, "formula_addunderline");
565
566 m_addMultilineAction = new TDEAction(i18n("Add Multiline"),
567 "multiline",
568 0,
569 this, TQ_SLOT(addMultiline()),
570 collection, "formula_addmultiline");
571
572 m_removeEnclosingAction = new TDEAction(i18n("Remove Enclosing Element"),
573 0,
574 this, TQ_SLOT(removeEnclosing()),
575 collection, "formula_removeenclosing");
576
577 m_makeGreekAction = new TDEAction(i18n("Convert to Greek"),
578 0,
579 this, TQ_SLOT(makeGreek()),
580 collection, "formula_makegreek");
581
582 m_appendColumnAction = new TDEAction( i18n( "Append Column" ),
583 "inscol",
584 0,
585 this, TQ_SLOT( appendColumn() ),
586 collection, "formula_appendcolumn" );
587 m_insertColumnAction = new TDEAction( i18n( "Insert Column" ),
588 "inscol",
589 0,
590 this, TQ_SLOT( insertColumn() ),
591 collection, "formula_insertcolumn" );
592 m_removeColumnAction = new TDEAction( i18n( "Remove Column" ),
593 "remcol",
594 0,
595 this, TQ_SLOT( removeColumn() ),
596 collection, "formula_removecolumn" );
597 m_appendRowAction = new TDEAction( i18n( "Append Row" ),
598 "insrow",
599 0,
600 this, TQ_SLOT( appendRow() ),
601 collection, "formula_appendrow" );
602 m_insertRowAction = new TDEAction( i18n( "Insert Row" ),
603 "insrow",
604 0,
605 this, TQ_SLOT( insertRow() ),
606 collection, "formula_insertrow" );
607 m_removeRowAction = new TDEAction( i18n( "Remove Row" ),
608 "remrow",
609 0,
610 this, TQ_SLOT( removeRow() ),
611 collection, "formula_removerow" );
612
613 m_syntaxHighlightingAction = new TDEToggleAction(i18n("Syntax Highlighting"),
614 0,
615 this, TQ_SLOT(toggleSyntaxHighlighting()),
616 collection, "formula_syntaxhighlighting");
617 //m_syntaxHighlightingAction->setChecked( m_contextStyle->syntaxHighlighting() );
618
619 m_formatBoldAction = new TDEToggleAction( i18n( "&Bold" ), "format-text-bold",
620 0, //CTRL + Key_B,
621 this, TQ_SLOT( textBold() ),
622 collection, "formula_format_bold" );
623 m_formatItalicAction = new TDEToggleAction( i18n( "&Italic" ), "format-text-italic",
624 0, //CTRL + Key_I,
625 this, TQ_SLOT( textItalic() ),
626 collection, "formula_format_italic" );
627 m_formatBoldAction->setEnabled( false );
628 m_formatItalicAction->setEnabled( false );
629
630 TQStringList delimiter;
631 delimiter.append(TQString("("));
632 delimiter.append(TQString("["));
633 delimiter.append(TQString("{"));
634 delimiter.append(TQString("<"));
635 delimiter.append(TQString("/"));
636 delimiter.append(TQString("\\"));
637 delimiter.append(TQString("|"));
638 delimiter.append(TQString(" "));
639 delimiter.append(TQString(")"));
640 delimiter.append(TQString("]"));
641 delimiter.append(TQString("}"));
642 delimiter.append(TQString(">"));
643 m_leftBracket = new TDESelectAction(i18n("Left Delimiter"),
644 0, this, TQ_SLOT(delimiterLeft()),
645 collection, "formula_typeleft");
646 m_leftBracket->setItems(delimiter);
647 //leftBracket->setCurrentItem(0);
648
649 delimiter.clear();
650 delimiter.append(TQString(")"));
651 delimiter.append(TQString("]"));
652 delimiter.append(TQString("}"));
653 delimiter.append(TQString(">"));
654 delimiter.append(TQString("/"));
655 delimiter.append(TQString("\\"));
656 delimiter.append(TQString("|"));
657 delimiter.append(TQString(" "));
658 delimiter.append(TQString("("));
659 delimiter.append(TQString("["));
660 delimiter.append(TQString("{"));
661 delimiter.append(TQString("<"));
662 m_rightBracket = new TDESelectAction(i18n("Right Delimiter"),
663 0, this, TQ_SLOT(delimiterRight()),
664 collection, "formula_typeright");
665 m_rightBracket->setItems(delimiter);
666 //rightBracket->setCurrentItem(0);
667
668 m_insertSymbolAction = new TDEAction(i18n("Insert Symbol"),
669 "key_enter",
670 /*CTRL + Key_I*/0,
671 this, TQ_SLOT(insertSymbol()),
672 collection, "formula_insertsymbol");
673 m_symbolNamesAction = new SymbolAction(i18n("Symbol Names"),
674 0, this, TQ_SLOT(symbolNames()),
675 collection, "formula_symbolnames");
676
677 TQStringList ff;
678 ff.append( i18n( "Normal" ) );
679 ff.append( i18n( "Script" ) );
680 ff.append( i18n( "Fraktur" ) );
681 ff.append( i18n( "Double Struck" ) );
682 m_fontFamily = new TDESelectAction(i18n("Font Family"),
683 0, this, TQ_SLOT(fontFamily()),
684 collection, "formula_fontfamily");
685 m_fontFamily->setItems( ff );
686 m_fontFamily->setEnabled( false );
687
688 TQStringList et;
689 et.append( i18n( "Identifier" ) );
690 et.append( i18n( "Operator" ) );
691 et.append( i18n( "Number" ) );
692 et.append( i18n( "Text" ) );
693 m_tokenElement = new TDESelectAction( i18n( "Token Type" ),
694 0, this, TQ_SLOT( tokenElement() ),
695 collection, "formula_tokenelement" );
696 m_tokenElement->setItems( et );
697// m_tokenElements->setEnabled( true );
698}
699
700
701void DocumentWrapper::paste()
702{
703 if (hasFormula()) {
704 formula()->paste();
705 }
706}
707
708void DocumentWrapper::copy()
709{
710 if (hasFormula()) {
711 formula()->copy();
712 }
713}
714
715void DocumentWrapper::cut()
716{
717 if (hasFormula()) {
718 formula()->cut();
719 }
720}
721
722void DocumentWrapper::undo()
723{
724 m_history->undo();
725}
726
727void DocumentWrapper::redo()
728{
729 m_history->redo();
730}
731
732void DocumentWrapper::addNegThinSpace()
733{
734 if (hasFormula()) {
735 SpaceRequest r( NEGTHIN );
736 formula()->performRequest( &r );
737 }
738}
739void DocumentWrapper::addThinSpace()
740{
741 if (hasFormula()) {
742 SpaceRequest r( THIN );
743 formula()->performRequest( &r );
744 }
745}
746void DocumentWrapper::addMediumSpace()
747{
748 if (hasFormula()) {
749 SpaceRequest r( MEDIUM );
750 formula()->performRequest( &r );
751 }
752}
753void DocumentWrapper::addThickSpace()
754{
755 if (hasFormula()) {
756 SpaceRequest r( THICK );
757 formula()->performRequest( &r );
758 }
759}
760void DocumentWrapper::addQuadSpace()
761{
762 if (hasFormula()) {
763 SpaceRequest r( QUAD );
764 formula()->performRequest( &r );
765 }
766}
767
768void DocumentWrapper::addDefaultBracket()
769{
770 if (hasFormula()) {
771 BracketRequest r( m_leftBracketChar, m_rightBracketChar );
772 formula()->performRequest( &r );
773 }
774}
775
776void DocumentWrapper::addBracket( SymbolType left, SymbolType right )
777{
778 if (hasFormula()) {
779 BracketRequest r( left, right );
780 formula()->performRequest( &r );
781 }
782}
783
784void DocumentWrapper::addParenthesis()
785{
786 if (hasFormula()) {
787 BracketRequest r( LeftRoundBracket, RightRoundBracket );
788 formula()->performRequest( &r );
789 }
790}
791
792void DocumentWrapper::addSquareBracket()
793{
794 if (hasFormula()) {
795 BracketRequest r( LeftSquareBracket, RightSquareBracket );
796 formula()->performRequest( &r );
797 }
798}
799
800void DocumentWrapper::addCurlyBracket()
801{
802 if (hasFormula()) {
803 BracketRequest r( LeftCurlyBracket, RightCurlyBracket );
804 formula()->performRequest( &r );
805 }
806}
807
808void DocumentWrapper::addLineBracket()
809{
810 if (hasFormula()) {
811 BracketRequest r( LeftLineBracket, RightLineBracket );
812 formula()->performRequest( &r );
813 }
814}
815
816void DocumentWrapper::addFraction()
817{
818 if (hasFormula()) {
819 Request r( req_addFraction );
820 formula()->performRequest( &r );
821 }
822}
823
824void DocumentWrapper::addRoot()
825{
826 if (hasFormula()) {
827 Request r( req_addRoot );
828 formula()->performRequest( &r );
829 }
830}
831
832void DocumentWrapper::addIntegral()
833{
834 if (hasFormula()) {
835 SymbolRequest r( Integral );
836 formula()->performRequest( &r );
837 }
838}
839
840void DocumentWrapper::addProduct()
841{
842 if (hasFormula()) {
843 SymbolRequest r( Product );
844 formula()->performRequest( &r );
845 }
846}
847
848void DocumentWrapper::addSum()
849{
850 if (hasFormula()) {
851 SymbolRequest r( Sum );
852 formula()->performRequest( &r );
853 }
854}
855
856void DocumentWrapper::addMatrix( uint rows, uint columns )
857{
858 if (hasFormula()) {
859 MatrixRequest r( rows, columns );
860 formula()->performRequest( &r );
861 }
862}
863
864void DocumentWrapper::addOneByTwoMatrix()
865{
866 if (hasFormula()) {
867 Request r( req_addOneByTwoMatrix );
868 formula()->performRequest( &r );
869 }
870}
871
872void DocumentWrapper::addNameSequence()
873{
874 if (hasFormula()) {
875 Request r( req_addNameSequence );
876 formula()->performRequest( &r );
877 }
878}
879
880void DocumentWrapper::addLowerLeftIndex()
881{
882 if (hasFormula()) {
883 IndexRequest r( lowerLeftPos );
884 formula()->performRequest( &r );
885 }
886}
887
888void DocumentWrapper::addUpperLeftIndex()
889{
890 if (hasFormula()) {
891 IndexRequest r( upperLeftPos );
892 formula()->performRequest( &r );
893 }
894}
895
896void DocumentWrapper::addLowerRightIndex()
897{
898 if (hasFormula()) {
899 IndexRequest r( lowerRightPos );
900 formula()->performRequest( &r );
901 }
902}
903
904void DocumentWrapper::addUpperRightIndex()
905{
906 if (hasFormula()) {
907 IndexRequest r( upperRightPos );
908 formula()->performRequest( &r );
909 }
910}
911
912void DocumentWrapper::addGenericLowerIndex()
913{
914 if (hasFormula()) {
915 IndexRequest r( lowerMiddlePos );
916 formula()->performRequest( &r );
917 }
918}
919
920void DocumentWrapper::addGenericUpperIndex()
921{
922 if (hasFormula()) {
923 IndexRequest r( upperMiddlePos );
924 formula()->performRequest( &r );
925 }
926}
927
928void DocumentWrapper::addOverline()
929{
930 if (hasFormula()) {
931 Request r( req_addOverline );
932 formula()->performRequest( &r );
933 }
934}
935
936void DocumentWrapper::addUnderline()
937{
938 if (hasFormula()) {
939 Request r( req_addUnderline );
940 formula()->performRequest( &r );
941 }
942}
943
944void DocumentWrapper::addMultiline()
945{
946 if (hasFormula()) {
947 Request r( req_addMultiline );
948 formula()->performRequest( &r );
949 }
950}
951
952void DocumentWrapper::removeEnclosing()
953{
954 if (hasFormula()) {
955 DirectedRemove r( req_removeEnclosing, beforeCursor );
956 formula()->performRequest( &r );
957 }
958}
959
960void DocumentWrapper::makeGreek()
961{
962 if (hasFormula()) {
963 Request r( req_makeGreek );
964 formula()->performRequest( &r );
965 }
966}
967
968void DocumentWrapper::insertSymbol()
969{
970 if ( hasFormula() &&
971 m_document->m_contextStyle->symbolTable().contains( m_selectedName ) ) {
972 TQChar ch = m_document->m_contextStyle->symbolTable().unicode( m_selectedName );
973 if ( ch != TQChar::null ) {
974 TextCharRequest r( ch, true );
975 formula()->performRequest( &r );
976 }
977 else {
978 TextRequest r( m_selectedName );
979 formula()->performRequest( &r );
980 }
981 }
982}
983
984void DocumentWrapper::insertSymbol( TQString name )
985{
986 if ( hasFormula() ) {
987 if ( m_document->m_contextStyle->symbolTable().contains( name ) ) {
988 TQChar ch = m_document->m_contextStyle->symbolTable().unicode( name );
989 if ( ch != TQChar::null ) {
990 TextCharRequest r( ch, true );
991 formula()->performRequest( &r );
992 return;
993 }
994 }
995 TextRequest r( name );
996 formula()->performRequest( &r );
997 }
998}
999
1000void DocumentWrapper::appendColumn()
1001{
1002 if ( hasFormula() ) {
1003 Request r( req_appendColumn );
1004 formula()->performRequest( &r );
1005 }
1006}
1007
1008void DocumentWrapper::insertColumn()
1009{
1010 if ( hasFormula() ) {
1011 Request r( req_insertColumn );
1012 formula()->performRequest( &r );
1013 }
1014}
1015
1016void DocumentWrapper::removeColumn()
1017{
1018 if ( hasFormula() ) {
1019 Request r( req_removeColumn );
1020 formula()->performRequest( &r );
1021 }
1022}
1023
1024void DocumentWrapper::appendRow()
1025{
1026 if ( hasFormula() ) {
1027 Request r( req_appendRow );
1028 formula()->performRequest( &r );
1029 }
1030}
1031
1032void DocumentWrapper::insertRow()
1033{
1034 if ( hasFormula() ) {
1035 Request r( req_insertRow );
1036 formula()->performRequest( &r );
1037 }
1038}
1039
1040void DocumentWrapper::removeRow()
1041{
1042 if ( hasFormula() ) {
1043 Request r( req_removeRow );
1044 formula()->performRequest( &r );
1045 }
1046}
1047
1048void DocumentWrapper::toggleSyntaxHighlighting()
1049{
1050 m_document->m_contextStyle->setSyntaxHighlighting( m_syntaxHighlightingAction->isChecked() );
1051 // Only to notify all views. We don't expect to get new values.
1052 m_document->recalc();
1053}
1054
1055void DocumentWrapper::textBold()
1056{
1057 if ( hasFormula() ) {
1058 CharStyleRequest r( req_formatBold,
1059 getFormatBoldAction()->isChecked(),
1060 getFormatItalicAction()->isChecked() );
1061 formula()->performRequest( &r );
1062 }
1063}
1064
1065void DocumentWrapper::textItalic()
1066{
1067 if ( hasFormula() ) {
1068 CharStyleRequest r( req_formatItalic,
1069 getFormatBoldAction()->isChecked(),
1070 getFormatItalicAction()->isChecked() );
1071 formula()->performRequest( &r );
1072 }
1073}
1074
1075void DocumentWrapper::delimiterLeft()
1076{
1077 TQString left = m_leftBracket->currentText();
1078 switch ( left.at(0).latin1() ) {
1079 case '[':
1080 case ']':
1081 case '{':
1082 case '}':
1083 case '<':
1084 case '>':
1085 case '(':
1086 case ')':
1087 case '/':
1088 case '\\':
1089 m_leftBracketChar = static_cast<SymbolType>( left.at(0).latin1() );
1090 break;
1091 case '|':
1092 m_leftBracketChar = LeftLineBracket;
1093 break;
1094 case ' ':
1095 m_leftBracketChar = EmptyBracket;
1096 break;
1097 }
1098}
1099
1100void DocumentWrapper::delimiterRight()
1101{
1102 TQString right = m_rightBracket->currentText();
1103 switch ( right.at(0).latin1() ) {
1104 case '[':
1105 case ']':
1106 case '{':
1107 case '}':
1108 case '<':
1109 case '>':
1110 case '(':
1111 case ')':
1112 case '/':
1113 case '\\':
1114 m_rightBracketChar = static_cast<SymbolType>( right.at(0).latin1() );
1115 break;
1116 case '|':
1117 m_rightBracketChar = RightLineBracket;
1118 break;
1119 case ' ':
1120 m_rightBracketChar = EmptyBracket;
1121 break;
1122 }
1123}
1124
1125void DocumentWrapper::symbolNames()
1126{
1127 m_selectedName = m_symbolNamesAction->currentText();
1128}
1129
1130
1131void DocumentWrapper::fontFamily()
1132{
1133 if ( hasFormula() ) {
1134 int i = m_fontFamily->currentItem();
1135 CharFamily cf = anyFamily;
1136 switch( i ) {
1137 case 0: cf = normalFamily; break;
1138 case 1: cf = scriptFamily; break;
1139 case 2: cf = frakturFamily; break;
1140 case 3: cf = doubleStruckFamily; break;
1141 }
1142 CharFamilyRequest r( cf );
1143 formula()->performRequest( &r );
1144 }
1145}
1146
1147
1148void DocumentWrapper::tokenElement()
1149{
1150 if ( hasFormula() ) {
1151 int i = m_tokenElement->currentItem();
1152 TokenElementType te = anyElement;
1153 switch( i ) {
1154 case 0: te = identifierElement; break;
1155 case 1: te = operatorElement; break;
1156 case 2: te = numberElement; break;
1157 case 3: te = textElement; break;
1158 }
1159 TokenElementRequest r( te );
1160 formula()->performRequest( &r );
1161 }
1162}
1163
1164
1165void DocumentWrapper::initSymbolNamesAction()
1166{
1167 if ( m_hasActions ) {
1168 const SymbolTable& st = m_document->m_contextStyle->symbolTable();
1169 TQStringList names = st.allNames();
1170 TQFont font( m_document->m_contextStyle->getFontStyle() );
1171 TQMemArray<TQChar> chars( names.count() );
1172
1173 uint i = 0;
1174 for ( TQStringList::Iterator it = names.begin();
1175 it != names.end();
1176 ++it, ++i ) {
1177 chars[ i ] = st.unicode( *it );
1178 }
1179 m_symbolNamesAction->setSymbols( names, m_document->m_contextStyle->getMathFont(), chars );
1180 m_selectedName = names[0];
1181 }
1182}
1183
1184
1186{
1187 kdDebug( DEBUGID ) << "DocumentWrapper::setEnabled " << enabled << endl;
1188 getAddNegThinSpaceAction()->setEnabled( enabled );
1189 getMakeGreekAction()->setEnabled( enabled );
1190 getAddGenericUpperAction()->setEnabled( enabled );
1191 getAddGenericLowerAction()->setEnabled( enabled );
1192 getAddOverlineAction()->setEnabled( enabled );
1193 getAddUnderlineAction()->setEnabled( enabled );
1194 getRemoveEnclosingAction()->setEnabled( enabled );
1195 getInsertSymbolAction()->setEnabled( enabled );
1196 getAddThinSpaceAction()->setEnabled( enabled );
1197 getAddMediumSpaceAction()->setEnabled( enabled );
1198 getAddThickSpaceAction()->setEnabled( enabled );
1199 getAddQuadSpaceAction()->setEnabled( enabled );
1200 getAddBracketAction()->setEnabled( enabled );
1201 getAddSBracketAction()->setEnabled( enabled );
1202 getAddCBracketAction()->setEnabled( enabled );
1203 getAddAbsAction()->setEnabled(enabled);
1204 getAddFractionAction()->setEnabled( enabled );
1205 getAddRootAction()->setEnabled( enabled );
1206 getAddSumAction()->setEnabled( enabled );
1207 getAddProductAction()->setEnabled( enabled );
1208 getAddIntegralAction()->setEnabled( enabled );
1209 getAddMatrixAction()->setEnabled( enabled );
1210 getAddOneByTwoMatrixAction()->setEnabled( enabled );
1211 getAddUpperLeftAction()->setEnabled( enabled );
1212 getAddLowerLeftAction()->setEnabled( enabled );
1213 getAddUpperRightAction()->setEnabled( enabled );
1214 getAddLowerRightAction()->setEnabled( enabled );
1215
1216 getAddGenericUpperAction()->setEnabled( enabled );
1217 getAddGenericLowerAction()->setEnabled( enabled );
1218
1219
1220 if ( enabled ) {
1221 getAddGenericUpperAction()->
1222 setShortcut( TDEShortcut( CTRL + Key_U ) );
1223 getAddGenericLowerAction()->
1224 setShortcut( TDEShortcut( CTRL + Key_L ) );
1225 getRemoveEnclosingAction()->
1226 setShortcut( TDEShortcut( CTRL + Key_R ) );
1227 getMakeGreekAction()->
1228 setShortcut( TDEShortcut( CTRL + Key_G ) );
1229 getInsertSymbolAction()->
1230 setShortcut( TDEShortcut( CTRL + Key_I ) );
1231 getAddOverlineAction()->
1232 setShortcut( TDEShortcut( CTRL + SHIFT + Key_O ) );
1233 getAddUnderlineAction()->
1234 setShortcut( TDEShortcut( CTRL + SHIFT + Key_U ) );
1235 getAddBracketAction()->
1236 setShortcut( TDEShortcut( CTRL + SHIFT + Key_B ) );
1237 getAddSBracketAction()->
1238 setShortcut( TDEShortcut( CTRL + SHIFT + Key_D ) );
1239 getAddCBracketAction()->
1240 setShortcut( TDEShortcut( CTRL + SHIFT + Key_C ) );
1241 getAddAbsAction()->
1242 setShortcut( TDEShortcut( CTRL + SHIFT + Key_A ) );
1243 getAddFractionAction()->
1244 setShortcut( TDEShortcut( CTRL + SHIFT + Key_F ) );
1245 getAddRootAction()->
1246 setShortcut( TDEShortcut( CTRL + SHIFT + Key_R ) );
1247 getAddSumAction()->
1248 setShortcut( TDEShortcut( CTRL + SHIFT + Key_S ) );
1249 getAddProductAction()->
1250 setShortcut( TDEShortcut( CTRL + SHIFT + Key_P ) );
1251 getAddIntegralAction()->
1252 setShortcut( TDEShortcut( CTRL + SHIFT + Key_I ) );
1253 getAddMatrixAction()->
1254 setShortcut( TDEShortcut( CTRL + SHIFT + Key_M ) );
1255
1256 }
1257 else {
1258 getAddGenericUpperAction()->setShortcut( TDEShortcut() );
1259 getAddGenericLowerAction()->setShortcut( TDEShortcut() );
1260 getRemoveEnclosingAction()->setShortcut( TDEShortcut() );
1261 getMakeGreekAction()->setShortcut( TDEShortcut() );
1262 getInsertSymbolAction()->setShortcut( TDEShortcut() );
1263 getAddOverlineAction()->setShortcut( TDEShortcut() );
1264 getAddUnderlineAction()->setShortcut( TDEShortcut() );
1265 getAddBracketAction()->setShortcut( TDEShortcut() );
1266 getAddSBracketAction()->setShortcut( TDEShortcut() );
1267 getAddCBracketAction()->setShortcut( TDEShortcut() );
1268 getAddAbsAction()->setShortcut( TDEShortcut() );
1269 getAddFractionAction()->setShortcut( TDEShortcut() );
1270 getAddRootAction()->setShortcut( TDEShortcut() );
1271 getAddSumAction()->setShortcut( TDEShortcut() );
1272 getAddProductAction()->setShortcut( TDEShortcut() );
1273 getAddIntegralAction()->setShortcut( TDEShortcut() );
1274 getAddMatrixAction()->setShortcut( TDEShortcut() );
1275 }
1276}
1277
1278void DocumentWrapper::enableMatrixActions( bool b)
1279{
1280 getAppendColumnAction()->setEnabled( b );
1281 getInsertColumnAction()->setEnabled( b );
1282 getRemoveColumnAction()->setEnabled( b );
1283 getAppendRowAction()->setEnabled( b );
1284 getInsertRowAction()->setEnabled( b );
1285 getRemoveRowAction()->setEnabled( b );
1286}
1287
1288void DocumentWrapper::updateConfig()
1289{
1290 m_syntaxHighlightingAction->
1291 setChecked( m_document->m_contextStyle->syntaxHighlighting() );
1292 initSymbolNamesAction();
1293}
1294
1295
1296KFORMULA_NAMESPACE_END
1297
1298using namespace KFormula;
1299#include "kformuladocument.moc"
The document.
void initialize()
Needs to be called before anything else can be done with a newly created formula!
bool load(const TQDomElement &fe)
Loads a formula from the document.
Contains all the style information for the formela.
void init(bool init=true)
void readConfig(TDEConfig *config, bool init=true)
A Wrapper that constracts the actions and must be given a real document to work with.
void setEnabled(bool enabled)
Enables our action according to enabled.
Document * document() const
A document that can contain a lot of formulas (container).
Document(TQObject *parent=0, const char *name=0, const TQStringList &args=TQStringList())
Creates a formula document.
void setEnabled(bool enabled)
Enables our action according to enabled.
void updateConfig()
Gets called when the configuration changed.
bool loadOasis(const TQDomDocument &doc)
Load a OASIS content.xml DomDocument.
KoCommandHistory * getHistory() const
virtual Container * createFormula(int pos=-1, bool registerMe=true)
Factory method.
void activate(Container *formula)
Sets a new formula.
Container * formulaAt(uint pos)
bool loadDocumentPart(TQDomElement node)
Load the document settings.
virtual void unregisterFormula(Container *)
Removes a formula from this document.
void sortFormulaList()
Sorts the list of formulas according to their screen positions.
int formulaPos(Container *formula)
ContextStyle & getContextStyle(bool edit=false)
TQDomDocument createMathMLDomDocument()
Create a MathML Dom Document, deprecates KFO Dom Document for internal layout TODO: Shouldn't this go...
TQDomDocument saveXML()
Save the document with all its formulae.
const SymbolTable & getSymbolTable() const
static TQDomDocument createDomDocument()
Return a kformula DomDocument.
bool loadXML(const TQDomDocument &doc)
Load a kformula DomDocument with all its formulas.
TQPtrListIterator< Container > formulas()
virtual void registerFormula(Container *, int pos=-1)
Registers a new formula to be part of this document.
void setCreationStrategy(TQString strategy)
Set formula creation strategy: old KFormula or MathML/ODF.
TQDomElement saveDocumentPart(TQDomDocument &doc)
Save the document settings.
void setZoomAndResolution(int zoom, int dpiX, int dpiY)
Change the zoom factor to z (e.g.
The command history stores a (user) configurable amount of Commands.
TQDomDocument createDomDocument(const TQString &tagName, const TQString &version) const
Return a correctly created TQDomDocument for this KoDocument, including processing instruction,...
The strategy to be used for OASIS OpenDocument / MathML.
The ordinary strategy to be used for plain kformula.
The symbol table.
Definition symboltable.h:77
TQStringList allNames() const
TQChar unicode(TQString name) const
virtual void setEnabled(bool enable)