lib

kformuladocument.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 of the License, or (at your option) any later version.
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 #include <qptrlist.h>
00022 #include <qstringlist.h>
00023 
00024 #include <kdebug.h>
00025 #include <kglobal.h>
00026 #include <kiconloader.h>
00027 #include <klocale.h>
00028 #include <ksimpleconfig.h>
00029 #include <kstandarddirs.h>
00030 
00031 #include <KoDocument.h>
00032 
00033 #include "contextstyle.h"
00034 #include "creationstrategy.h"
00035 #include "kformulacontainer.h"
00036 #include "kformuladocument.h"
00037 #include "sequenceelement.h"
00038 #include "symboltable.h"
00039 #include "symbolaction.h"
00040 
00041 KFORMULA_NAMESPACE_BEGIN
00042 
00043 
00044 static const int CURRENT_SYNTAX_VERSION = 1;
00045 // Make sure an appropriate DTD is available in www/koffice/DTD if changing this value
00046 static const char * CURRENT_DTD_VERSION = "1.3";
00047 
00051 static OrdinaryCreationStrategy creationStrategy;
00052 
00053 
00054 int FormulaList::compareItems( QPtrCollection::Item a, QPtrCollection::Item b )
00055 {
00056     double ya = static_cast<Container*>( a )->getDocumentY();
00057     double yb = static_cast<Container*>( b )->getDocumentY();
00058     if ( fabs( ya-yb ) < 1e-4 ) {
00059         double xa = static_cast<Container*>( a )->getDocumentX();
00060         double xb = static_cast<Container*>( b )->getDocumentX();
00061         if ( xa < xb ) return -1;
00062         if ( xa > xb ) return 1;
00063         return 0;
00064     }
00065     if ( ya < yb ) return -1;
00066     return 1;
00067 }
00068 
00069 
00070 Document::Document( QObject *parent, const char *name,
00071                     const QStringList &/*args*/ )
00072     : QObject( parent, name ), m_wrapper( 0 ), m_formula( 0 )
00073 {
00074     m_contextStyle = new ContextStyle;
00075     SequenceElement::setCreationStrategy( &creationStrategy );
00076     formulae.setAutoDelete( false );
00077 }
00078 
00079 
00080 Document::~Document()
00081 {
00082     // Destroy remaining formulae. We do it backward because
00083     // the formulae remove themselves from this document upon
00084     // destruction.
00085     int count = formulae.count();
00086     for ( int i=count-1; i>=0; --i ) {
00087         delete formulae.at( i );
00088     }
00089     delete m_contextStyle;
00090 }
00091 
00092 
00093 bool Document::hasFormula()
00094 {
00095     return ( m_formula != 0 ) && ( m_formula->activeCursor() != 0 );
00096 }
00097 
00098 
00099 Container* Document::createFormula( int pos, bool registerMe )
00100 {
00101     Container* formula = new Container( this, pos, registerMe );
00102     formula->initialize();
00103     return formula;
00104 }
00105 
00106 
00107 QPtrListIterator<Container> Document::formulas()
00108 {
00109     return QPtrListIterator<Container>( formulae );
00110 }
00111 
00112 
00113 int Document::formulaPos( Container* formula )
00114 {
00115     return formulae.find( formula );
00116 }
00117 
00118 
00119 Container* Document::formulaAt( uint pos )
00120 {
00121     return formulae.at( pos );
00122 }
00123 
00124 
00125 int Document::formulaCount()
00126 {
00127     return formulae.count();
00128 }
00129 
00130 
00131 bool Document::loadXML( const QDomDocument& doc )
00132 {
00133     //clear();
00134     QDomElement root = doc.documentElement();
00135 
00136     // backward compatibility
00137     if ( root.tagName() == "FORMULA" ) {
00138         Container* formula = newFormula( 0 );
00139         return formula->load( root );
00140     }
00141 
00142     QDomNode node = root.firstChild();
00143     if ( node.isElement() ) {
00144         QDomElement element = node.toElement();
00145         if ( element.tagName() == "FORMULASETTINGS" ) {
00146             if ( !loadDocumentPart( element ) ) {
00147                 return false;
00148             }
00149         }
00150         node = node.nextSibling();
00151     }
00152     uint number = 0;
00153     while ( !node.isNull() ) {
00154         if ( node.isElement() ) {
00155             QDomElement element = node.toElement();
00156             Container* formula = newFormula( number );
00157             if ( !formula->load( element ) ) {
00158                 return false;
00159             }
00160             number += 1;
00161         }
00162         node = node.nextSibling();
00163     }
00164     return formulae.count() > 0;
00165 }
00166 
00167 bool Document::loadOasis( const QDomDocument& doc )
00168 {
00169    // ### TODO: not finished!
00170     KFormula::Container* formula = newFormula( 0 );
00171     return formula->loadMathML( doc, true );
00172 }
00173 
00174 bool Document::loadDocumentPart( QDomElement /*node*/ )
00175 {
00176     return true;
00177 }
00178 
00179 QDomDocument Document::saveXML()
00180 {
00181     QDomDocument doc = createDomDocument();
00182     QDomElement root = doc.documentElement();
00183     root.appendChild( saveDocumentPart( doc ) );
00184     uint count = formulae.count();
00185     for ( uint i=0; i<count; ++i ) {
00186         formulae.at( i )->save( root );
00187     }
00188     return doc;
00189 }
00190 
00191 
00192 QDomElement Document::saveDocumentPart( QDomDocument& doc )
00193 {
00194     QDomElement settings = doc.createElement( "FORMULASETTINGS" );
00195     return settings;
00196 }
00197 
00198 
00199 QDomDocument Document::createDomDocument()
00200 {
00201     return KoDocument::createDomDocument( "kformula", "KFORMULA",
00202                                           CURRENT_DTD_VERSION );
00203 }
00204 
00205 void Document::registerFormula( Container* f, int pos )
00206 {
00207     if ( ( pos > -1 ) &&
00208          ( static_cast<uint>( pos ) < formulae.count() ) ) {
00209         formulae.insert( pos, f );
00210         //emit sigInsertFormula( f, pos );
00211     }
00212     else {
00213         formulae.append( f );
00214         //emit sigInsertFormula( f, formulae.count()-1 );
00215     }
00216 }
00217 
00218 void Document::unregisterFormula( Container* f )
00219 {
00220     if ( m_formula == f ) {
00221         m_formula = 0;
00222     }
00223     formulae.removeRef( f );
00224 }
00225 
00226 void Document::activate(Container* f)
00227 {
00228     m_formula = f;
00229 }
00230 
00231 
00232 void Document::sortFormulaList()
00233 {
00234     formulae.sort();
00235 }
00236 
00237 
00238 Container* Document::newFormula( uint number )
00239 {
00240     if ( number < formulae.count() ) {
00241         return formulae.at( number );
00242     }
00243     return createFormula();
00244 }
00245 
00246 
00247 double Document::getXResolution() const
00248 {
00249     return m_contextStyle->zoomedResolutionX();
00250 }
00251 double Document::getYResolution() const
00252 {
00253     return m_contextStyle->zoomedResolutionY();
00254 }
00255 
00256 const SymbolTable& Document::getSymbolTable() const
00257 {
00258     return m_contextStyle->symbolTable();
00259 }
00260 
00261 ContextStyle& Document::getContextStyle( bool edit )
00262 {
00263     m_contextStyle->setEdit( edit );
00264     return *m_contextStyle;
00265 }
00266 
00267 void Document::setZoomAndResolution( int zoom, int dpiX, int dpiY )
00268 {
00269     m_contextStyle->setZoomAndResolution( zoom, dpiX, dpiY );
00270 }
00271 
00272 void Document::newZoomAndResolution( bool updateViews, bool /*forPrint*/ )
00273 {
00274     if ( updateViews ) {
00275         recalc();
00276     }
00277 }
00278 
00279 void Document::setZoomAndResolution( int zoom,
00280                                      double zoomX, double zoomY,
00281                                      bool updateViews, bool forPrint )
00282 {
00283     if ( getContextStyle( !forPrint ).setZoomAndResolution( zoom, zoomX, zoomY, updateViews, forPrint ) && updateViews ) {
00284         recalc();
00285     }
00286 }
00287 
00288 
00289 SymbolType Document::leftBracketChar()
00290 {
00291     return m_wrapper->leftBracketChar();
00292 }
00293 
00294 SymbolType Document::rightBracketChar()
00295 {
00296     return m_wrapper->rightBracketChar();
00297 }
00298 
00299 
00300 void Document::setEnabled( bool enabled )
00301 {
00302     m_wrapper->setEnabled( enabled );
00303 }
00304 
00305 
00306 KoCommandHistory* Document::getHistory() const
00307 {
00308     return m_wrapper->getHistory();
00309 }
00310 
00311 
00312 void Document::recalc()
00313 {
00314     for ( Container* f = formulae.first();
00315           f != 0;
00316           f=formulae.next() ) {
00317         f->recalc();
00318     }
00319 }
00320 
00321 
00322 void Document::updateConfig()
00323 {
00324     m_wrapper->updateConfig();
00325     recalc();
00326 }
00327 
00328 
00329 void Document::introduceWrapper( DocumentWrapper* wrapper, bool init )
00330 {
00331     m_wrapper = wrapper;
00332     m_contextStyle->readConfig( wrapper->config(), init );
00333     m_contextStyle->init( init );
00334 }
00335 
00336 
00338 
00339 DocumentWrapper::DocumentWrapper( KConfig* config,
00340                                   KActionCollection* collection,
00341                                   KoCommandHistory* history )
00342     : m_document( 0 ),
00343       m_leftBracketChar( LeftRoundBracket ),
00344       m_rightBracketChar( RightRoundBracket ),
00345       m_config( config ),
00346       m_hasActions( collection != 0 )
00347 {
00348     if ( m_hasActions ) {
00349         createActions( collection );
00350         enableMatrixActions( false );
00351     }
00352     setCommandStack( history );
00353 }
00354 
00355 
00356 DocumentWrapper::~DocumentWrapper()
00357 {
00358     delete m_document;
00359     if ( m_ownHistory ) {
00360         delete m_history;
00361     }
00362 
00363     if ( m_hasActions )
00364     {
00365         m_config->setGroup("General");
00366         m_config->writeEntry("syntaxHighlighting", m_syntaxHighlightingAction->isChecked() );
00367     }
00368 }
00369 
00370 
00371 void DocumentWrapper::document( Document* document, bool init )
00372 {
00373     m_document = document;
00374     m_document->introduceWrapper( this, init );
00375     initSymbolNamesAction();
00376     m_config->setGroup("General");
00377     if ( m_hasActions )
00378     {
00379         m_syntaxHighlightingAction->setChecked( m_config->readBoolEntry("syntaxHighlighting", true ) );
00380         if ( !m_syntaxHighlightingAction->isChecked() )
00381             toggleSyntaxHighlighting();
00382     }
00383     else if ( m_config->readBoolEntry("syntaxHighlighting", true ) )
00384     {
00385         m_document->m_contextStyle->setSyntaxHighlighting( true );
00386         // Only to notify all views. We don't expect to get new values.
00387         m_document->recalc();
00388     }
00389 }
00390 
00391 
00392 void DocumentWrapper::setCommandStack( KoCommandHistory* history )
00393 {
00394     if ( history == 0 ) {
00395         m_history = new KoCommandHistory;
00396         m_ownHistory = true;
00397     }
00398     else {
00399         m_history = history;
00400         m_ownHistory = false;
00401     }
00402 }
00403 
00404 
00405 void DocumentWrapper::createActions( KActionCollection* collection )
00406 {
00407     KGlobal::dirs()->addResourceType( "toolbar",
00408                                       KStandardDirs::kde_default("data") +
00409                                       "kformula/pics/" );
00410 
00411     m_addNegThinSpaceAction = new KAction( i18n( "Add Negative Thin Space" ),
00412                                     0,
00413                                     this, SLOT( addNegThinSpace() ),
00414                                     collection, "formula_addnegthinspace") ;
00415     m_addThinSpaceAction = new KAction( i18n( "Add Thin Space" ),
00416                                     0,
00417                                     this, SLOT( addThinSpace() ),
00418                                     collection, "formula_addthinspace") ;
00419     m_addMediumSpaceAction = new KAction( i18n( "Add Medium Space" ),
00420                                     0,
00421                                     this, SLOT( addMediumSpace() ),
00422                                     collection, "formula_addmediumspace" );
00423     m_addThickSpaceAction = new KAction( i18n( "Add Thick Space" ),
00424                                     0,
00425                                     this, SLOT( addThickSpace() ),
00426                                     collection, "formula_addthickspace" );
00427     m_addQuadSpaceAction = new KAction( i18n( "Add Quad Space" ),
00428                                     0,
00429                                     this, SLOT( addQuadSpace() ),
00430                                     collection, "formula_addquadspace" );
00431 
00432     m_addIntegralAction = new KAction(i18n("Add Integral"),
00433                                     "int",
00434                                     0,
00435                                     this, SLOT(addIntegral()),
00436                                     collection, "formula_addintegral");
00437     m_addSumAction      = new KAction(i18n("Add Sum"),
00438                                     "sum",
00439                                     0,
00440                                     this, SLOT(addSum()),
00441                                     collection, "formula_addsum");
00442     m_addProductAction  = new KAction(i18n("Add Product"),
00443                                     "prod",
00444                                     0,
00445                                     this, SLOT(addProduct()),
00446                                     collection, "formula_addproduct");
00447     m_addRootAction     = new KAction(i18n("Add Root"),
00448                                     "sqrt",
00449                                     0,
00450                                     this, SLOT(addRoot()),
00451                                     collection, "formula_addroot");
00452     m_addFractionAction = new KAction(i18n("Add Fraction"),
00453                                     "frac",
00454                                     0,
00455                                     this, SLOT(addFraction()),
00456                                     collection, "formula_addfrac");
00457     m_addBracketAction  = new KAction(i18n("Add Bracket"),
00458                                     "paren",
00459                                     0,
00460                                     this, SLOT(addDefaultBracket()),
00461                                     collection,"formula_addbra");
00462     m_addSBracketAction = new KAction(i18n("Add Square Bracket"),
00463                                     "brackets",
00464                                     0,
00465                                     this, SLOT(addSquareBracket()),
00466                                     collection,"formula_addsqrbra");
00467     m_addCBracketAction = new KAction(i18n("Add Curly Bracket"),
00468                                     "math_brace",
00469                                     0,
00470                                     this, SLOT(addCurlyBracket()),
00471                                     collection,"formula_addcurbra");
00472     m_addAbsAction      = new KAction(i18n("Add Abs"),
00473                                     "abs",
00474                                     0,
00475                                     this, SLOT(addLineBracket()),
00476                                     collection,"formula_addabsbra");
00477 
00478     m_addMatrixAction   = new KAction(i18n("Add Matrix..."),
00479                                     "matrix",
00480                                     0,
00481                                     this, SLOT(addMatrix()),
00482                                     collection, "formula_addmatrix");
00483 
00484     m_addOneByTwoMatrixAction   = new KAction(i18n("Add 1x2 Matrix"),
00485                                     "onetwomatrix",
00486                                     0,
00487                                     this, SLOT(addOneByTwoMatrix()),
00488                                     collection, "formula_add_one_by_two_matrix");
00489 
00490 
00491     m_addUpperLeftAction  = new KAction(i18n("Add Upper Left Index"),
00492                                       "lsup",
00493                                       0,
00494                                       this, SLOT(addUpperLeftIndex()),
00495                                       collection, "formula_addupperleft");
00496     m_addLowerLeftAction  = new KAction(i18n("Add Lower Left Index"),
00497                                       "lsub",
00498                                       0,
00499                                       this, SLOT(addLowerLeftIndex()),
00500                                       collection, "formula_addlowerleft");
00501     m_addUpperRightAction = new KAction(i18n("Add Upper Right Index"),
00502                                       "rsup",
00503                                       0,
00504                                       this, SLOT(addUpperRightIndex()),
00505                                       collection, "formula_addupperright");
00506     m_addLowerRightAction = new KAction(i18n("Add Lower Right Index"),
00507                                       "rsub",
00508                                       0,
00509                                       this, SLOT(addLowerRightIndex()),
00510                                       collection, "formula_addlowerright");
00511 
00512     m_addGenericUpperAction = new KAction(i18n("Add Upper Index"),
00513                                       "gsup",
00514                                               /*CTRL + Key_U*/0,
00515                                       this, SLOT(addGenericUpperIndex()),
00516                                       collection, "formula_addupperindex");
00517     m_addGenericLowerAction = new KAction(i18n("Add Lower Index"),
00518                                       "gsub",
00519                                       0,
00520                                       this, SLOT(addGenericLowerIndex()),
00521                                       collection, "formula_addlowerindex");
00522 
00523     m_addOverlineAction = new KAction(i18n("Add Overline"),
00524                                           "over",
00525                                           0,
00526                                           this, SLOT(addOverline()),
00527                                           collection, "formula_addoverline");
00528     m_addUnderlineAction = new KAction(i18n("Add Underline"),
00529                                            "under",
00530                                            0,
00531                                            this, SLOT(addUnderline()),
00532                                            collection, "formula_addunderline");
00533 
00534     m_addMultilineAction = new KAction(i18n("Add Multiline"),
00535                                            "multiline",
00536                                            0,
00537                                            this, SLOT(addMultiline()),
00538                                            collection, "formula_addmultiline");
00539 
00540     m_removeEnclosingAction = new KAction(i18n("Remove Enclosing Element"),
00541                                         0,
00542                                         this, SLOT(removeEnclosing()),
00543                                         collection, "formula_removeenclosing");
00544 
00545     m_makeGreekAction = new KAction(i18n("Convert to Greek"),
00546                                   0,
00547                                   this, SLOT(makeGreek()),
00548                                   collection, "formula_makegreek");
00549 
00550     m_appendColumnAction = new KAction( i18n( "Append Column" ),
00551                                             "inscol",
00552                                             0,
00553                                             this, SLOT( appendColumn() ),
00554                                             collection, "formula_appendcolumn" );
00555     m_insertColumnAction = new KAction( i18n( "Insert Column" ),
00556                                             "inscol",
00557                                             0,
00558                                             this, SLOT( insertColumn() ),
00559                                             collection, "formula_insertcolumn" );
00560     m_removeColumnAction = new KAction( i18n( "Remove Column" ),
00561                                             "remcol",
00562                                             0,
00563                                             this, SLOT( removeColumn() ),
00564                                             collection, "formula_removecolumn" );
00565     m_appendRowAction = new KAction( i18n( "Append Row" ),
00566                                          "insrow",
00567                                          0,
00568                                          this, SLOT( appendRow() ),
00569                                          collection, "formula_appendrow" );
00570     m_insertRowAction = new KAction( i18n( "Insert Row" ),
00571                                          "insrow",
00572                                          0,
00573                                          this, SLOT( insertRow() ),
00574                                          collection, "formula_insertrow" );
00575     m_removeRowAction = new KAction( i18n( "Remove Row" ),
00576                                          "remrow",
00577                                          0,
00578                                          this, SLOT( removeRow() ),
00579                                          collection, "formula_removerow" );
00580 
00581     m_syntaxHighlightingAction = new KToggleAction(i18n("Syntax Highlighting"),
00582                                                  0,
00583                                                  this, SLOT(toggleSyntaxHighlighting()),
00584                                                  collection, "formula_syntaxhighlighting");
00585     //m_syntaxHighlightingAction->setChecked( m_contextStyle->syntaxHighlighting() );
00586 
00587     m_formatBoldAction = new KToggleAction( i18n( "&Bold" ), "text_bold",
00588                                                 0, //CTRL + Key_B,
00589                                                 this, SLOT( textBold() ),
00590                                                 collection, "formula_format_bold" );
00591     m_formatItalicAction = new KToggleAction( i18n( "&Italic" ), "text_italic",
00592                                                   0, //CTRL + Key_I,
00593                                                   this, SLOT( textItalic() ),
00594                                                   collection, "formula_format_italic" );
00595     m_formatBoldAction->setEnabled( false );
00596     m_formatItalicAction->setEnabled( false );
00597 
00598     QStringList delimiter;
00599     delimiter.append(QString("("));
00600     delimiter.append(QString("["));
00601     delimiter.append(QString("{"));
00602     delimiter.append(QString("<"));
00603     delimiter.append(QString("/"));
00604     delimiter.append(QString("\\"));
00605     delimiter.append(QString("|"));
00606     delimiter.append(QString(" "));
00607     delimiter.append(QString(")"));
00608     delimiter.append(QString("]"));
00609     delimiter.append(QString("}"));
00610     delimiter.append(QString(">"));
00611     m_leftBracket = new KSelectAction(i18n("Left Delimiter"),
00612                                     0, this, SLOT(delimiterLeft()),
00613                                     collection, "formula_typeleft");
00614     m_leftBracket->setItems(delimiter);
00615     //leftBracket->setCurrentItem(0);
00616 
00617     delimiter.clear();
00618     delimiter.append(QString(")"));
00619     delimiter.append(QString("]"));
00620     delimiter.append(QString("}"));
00621     delimiter.append(QString(">"));
00622     delimiter.append(QString("/"));
00623     delimiter.append(QString("\\"));
00624     delimiter.append(QString("|"));
00625     delimiter.append(QString(" "));
00626     delimiter.append(QString("("));
00627     delimiter.append(QString("["));
00628     delimiter.append(QString("{"));
00629     delimiter.append(QString("<"));
00630     m_rightBracket = new KSelectAction(i18n("Right Delimiter"),
00631                                      0, this, SLOT(delimiterRight()),
00632                                      collection, "formula_typeright");
00633     m_rightBracket->setItems(delimiter);
00634     //rightBracket->setCurrentItem(0);
00635 
00636     m_insertSymbolAction = new KAction(i18n("Insert Symbol"),
00637                                            "key_enter",
00638                                            /*CTRL + Key_I*/0,
00639                                            this, SLOT(insertSymbol()),
00640                                            collection, "formula_insertsymbol");
00641     m_symbolNamesAction = new SymbolAction(i18n("Symbol Names"),
00642                                                0, this, SLOT(symbolNames()),
00643                                                collection, "formula_symbolnames");
00644 
00645     QStringList ff;
00646     ff.append( i18n( "Normal" ) );
00647     ff.append( i18n( "Script" ) );
00648     ff.append( i18n( "Fraktur" ) );
00649     ff.append( i18n( "Double Struck" ) );
00650     m_fontFamily = new KSelectAction(i18n("Font Family"),
00651                                          0, this, SLOT(fontFamily()),
00652                                          collection, "formula_fontfamily");
00653     m_fontFamily->setItems( ff );
00654     m_fontFamily->setEnabled( false );
00655 }
00656 
00657 
00658 void DocumentWrapper::paste()
00659 {
00660     if (hasFormula()) {
00661         formula()->paste();
00662     }
00663 }
00664 
00665 void DocumentWrapper::copy()
00666 {
00667     if (hasFormula()) {
00668         formula()->copy();
00669     }
00670 }
00671 
00672 void DocumentWrapper::cut()
00673 {
00674     if (hasFormula()) {
00675         formula()->cut();
00676     }
00677 }
00678 
00679 void DocumentWrapper::undo()
00680 {
00681     m_history->undo();
00682 }
00683 
00684 void DocumentWrapper::redo()
00685 {
00686     m_history->redo();
00687 }
00688 
00689 void DocumentWrapper::addNegThinSpace()
00690 {
00691     if (hasFormula()) {
00692         SpaceRequest r( NEGTHIN );
00693         formula()->performRequest( &r );
00694     }
00695 }
00696 void DocumentWrapper::addThinSpace()
00697 {
00698     if (hasFormula()) {
00699         SpaceRequest r( THIN );
00700         formula()->performRequest( &r );
00701     }
00702 }
00703 void DocumentWrapper::addMediumSpace()
00704 {
00705     if (hasFormula()) {
00706         SpaceRequest r( MEDIUM );
00707         formula()->performRequest( &r );
00708     }
00709 }
00710 void DocumentWrapper::addThickSpace()
00711 {
00712     if (hasFormula()) {
00713         SpaceRequest r( THICK );
00714         formula()->performRequest( &r );
00715     }
00716 }
00717 void DocumentWrapper::addQuadSpace()
00718 {
00719     if (hasFormula()) {
00720         SpaceRequest r( QUAD );
00721         formula()->performRequest( &r );
00722     }
00723 }
00724 
00725 void DocumentWrapper::addDefaultBracket()
00726 {
00727     if (hasFormula()) {
00728         BracketRequest r( m_leftBracketChar, m_rightBracketChar );
00729         formula()->performRequest( &r );
00730     }
00731 }
00732 
00733 void DocumentWrapper::addBracket( SymbolType left, SymbolType right )
00734 {
00735     if (hasFormula()) {
00736         BracketRequest r( left, right );
00737         formula()->performRequest( &r );
00738     }
00739 }
00740 
00741 void DocumentWrapper::addParenthesis()
00742 {
00743     if (hasFormula()) {
00744         BracketRequest r( LeftRoundBracket, RightRoundBracket );
00745         formula()->performRequest( &r );
00746     }
00747 }
00748 
00749 void DocumentWrapper::addSquareBracket()
00750 {
00751     if (hasFormula()) {
00752         BracketRequest r( LeftSquareBracket, RightSquareBracket );
00753         formula()->performRequest( &r );
00754     }
00755 }
00756 
00757 void DocumentWrapper::addCurlyBracket()
00758 {
00759     if (hasFormula()) {
00760         BracketRequest r( LeftCurlyBracket, RightCurlyBracket );
00761         formula()->performRequest( &r );
00762     }
00763 }
00764 
00765 void DocumentWrapper::addLineBracket()
00766 {
00767     if (hasFormula()) {
00768         BracketRequest r( LeftLineBracket, RightLineBracket );
00769         formula()->performRequest( &r );
00770     }
00771 }
00772 
00773 void DocumentWrapper::addFraction()
00774 {
00775     if (hasFormula()) {
00776         Request r( req_addFraction );
00777         formula()->performRequest( &r );
00778     }
00779 }
00780 
00781 void DocumentWrapper::addRoot()
00782 {
00783     if (hasFormula()) {
00784         Request r( req_addRoot );
00785         formula()->performRequest( &r );
00786     }
00787 }
00788 
00789 void DocumentWrapper::addIntegral()
00790 {
00791     if (hasFormula()) {
00792         SymbolRequest r( Integral );
00793         formula()->performRequest( &r );
00794     }
00795 }
00796 
00797 void DocumentWrapper::addProduct()
00798 {
00799     if (hasFormula()) {
00800         SymbolRequest r( Product );
00801         formula()->performRequest( &r );
00802     }
00803 }
00804 
00805 void DocumentWrapper::addSum()
00806 {
00807     if (hasFormula()) {
00808         SymbolRequest r( Sum );
00809         formula()->performRequest( &r );
00810     }
00811 }
00812 
00813 void DocumentWrapper::addMatrix( uint rows, uint columns )
00814 {
00815     if (hasFormula()) {
00816         MatrixRequest r( rows, columns );
00817         formula()->performRequest( &r );
00818     }
00819 }
00820 
00821 void DocumentWrapper::addOneByTwoMatrix()
00822 {
00823     if (hasFormula()) {
00824         Request r( req_addOneByTwoMatrix );
00825         formula()->performRequest( &r );
00826     }
00827 }
00828 
00829 void DocumentWrapper::addNameSequence()
00830 {
00831     if (hasFormula()) {
00832         Request r( req_addNameSequence );
00833         formula()->performRequest( &r );
00834     }
00835 }
00836 
00837 void DocumentWrapper::addLowerLeftIndex()
00838 {
00839     if (hasFormula()) {
00840         IndexRequest r( lowerLeftPos );
00841         formula()->performRequest( &r );
00842     }
00843 }
00844 
00845 void DocumentWrapper::addUpperLeftIndex()
00846 {
00847     if (hasFormula()) {
00848         IndexRequest r( upperLeftPos );
00849         formula()->performRequest( &r );
00850     }
00851 }
00852 
00853 void DocumentWrapper::addLowerRightIndex()
00854 {
00855     if (hasFormula()) {
00856         IndexRequest r( lowerRightPos );
00857         formula()->performRequest( &r );
00858     }
00859 }
00860 
00861 void DocumentWrapper::addUpperRightIndex()
00862 {
00863     if (hasFormula()) {
00864         IndexRequest r( upperRightPos );
00865         formula()->performRequest( &r );
00866     }
00867 }
00868 
00869 void DocumentWrapper::addGenericLowerIndex()
00870 {
00871     if (hasFormula()) {
00872         IndexRequest r( lowerMiddlePos );
00873         formula()->performRequest( &r );
00874     }
00875 }
00876 
00877 void DocumentWrapper::addGenericUpperIndex()
00878 {
00879     if (hasFormula()) {
00880         IndexRequest r( upperMiddlePos );
00881         formula()->performRequest( &r );
00882     }
00883 }
00884 
00885 void DocumentWrapper::addOverline()
00886 {
00887     if (hasFormula()) {
00888         Request r( req_addOverline );
00889         formula()->performRequest( &r );
00890     }
00891 }
00892 
00893 void DocumentWrapper::addUnderline()
00894 {
00895     if (hasFormula()) {
00896         Request r( req_addUnderline );
00897         formula()->performRequest( &r );
00898     }
00899 }
00900 
00901 void DocumentWrapper::addMultiline()
00902 {
00903     if (hasFormula()) {
00904         Request r( req_addMultiline );
00905         formula()->performRequest( &r );
00906     }
00907 }
00908 
00909 void DocumentWrapper::removeEnclosing()
00910 {
00911     if (hasFormula()) {
00912         DirectedRemove r( req_removeEnclosing, beforeCursor );
00913         formula()->performRequest( &r );
00914     }
00915 }
00916 
00917 void DocumentWrapper::makeGreek()
00918 {
00919     if (hasFormula()) {
00920         Request r( req_makeGreek );
00921         formula()->performRequest( &r );
00922     }
00923 }
00924 
00925 void DocumentWrapper::insertSymbol()
00926 {
00927     if ( hasFormula() &&
00928          m_document->m_contextStyle->symbolTable().contains( m_selectedName ) ) {
00929         QChar ch = m_document->m_contextStyle->symbolTable().unicode( m_selectedName );
00930         if ( ch != QChar::null ) {
00931             TextCharRequest r( ch, true );
00932             formula()->performRequest( &r );
00933         }
00934         else {
00935             TextRequest r( m_selectedName );
00936             formula()->performRequest( &r );
00937         }
00938     }
00939 }
00940 
00941 void DocumentWrapper::insertSymbol( QString name )
00942 {
00943     if ( hasFormula() ) {
00944         if ( m_document->m_contextStyle->symbolTable().contains( name ) ) {
00945             QChar ch = m_document->m_contextStyle->symbolTable().unicode( name );
00946             if ( ch != QChar::null ) {
00947                 TextCharRequest r( ch, true );
00948                 formula()->performRequest( &r );
00949                 return;
00950             }
00951         }
00952         TextRequest r( name );
00953         formula()->performRequest( &r );
00954     }
00955 }
00956 
00957 void DocumentWrapper::appendColumn()
00958 {
00959     if ( hasFormula() ) {
00960         Request r( req_appendColumn );
00961         formula()->performRequest( &r );
00962     }
00963 }
00964 
00965 void DocumentWrapper::insertColumn()
00966 {
00967     if ( hasFormula() ) {
00968         Request r( req_insertColumn );
00969         formula()->performRequest( &r );
00970     }
00971 }
00972 
00973 void DocumentWrapper::removeColumn()
00974 {
00975     if ( hasFormula() ) {
00976         Request r( req_removeColumn );
00977         formula()->performRequest( &r );
00978     }
00979 }
00980 
00981 void DocumentWrapper::appendRow()
00982 {
00983     if ( hasFormula() ) {
00984         Request r( req_appendRow );
00985         formula()->performRequest( &r );
00986     }
00987 }
00988 
00989 void DocumentWrapper::insertRow()
00990 {
00991     if ( hasFormula() ) {
00992         Request r( req_insertRow );
00993         formula()->performRequest( &r );
00994     }
00995 }
00996 
00997 void DocumentWrapper::removeRow()
00998 {
00999     if ( hasFormula() ) {
01000         Request r( req_removeRow );
01001         formula()->performRequest( &r );
01002     }
01003 }
01004 
01005 void DocumentWrapper::toggleSyntaxHighlighting()
01006 {
01007     m_document->m_contextStyle->setSyntaxHighlighting( m_syntaxHighlightingAction->isChecked() );
01008     // Only to notify all views. We don't expect to get new values.
01009     m_document->recalc();
01010 }
01011 
01012 void DocumentWrapper::textBold()
01013 {
01014     if ( hasFormula() ) {
01015         CharStyleRequest r( req_formatBold,
01016                             getFormatBoldAction()->isChecked(),
01017                             getFormatItalicAction()->isChecked() );
01018         formula()->performRequest( &r );
01019     }
01020 }
01021 
01022 void DocumentWrapper::textItalic()
01023 {
01024     if ( hasFormula() ) {
01025         CharStyleRequest r( req_formatItalic,
01026                             getFormatBoldAction()->isChecked(),
01027                             getFormatItalicAction()->isChecked() );
01028         formula()->performRequest( &r );
01029     }
01030 }
01031 
01032 void DocumentWrapper::delimiterLeft()
01033 {
01034     QString left = m_leftBracket->currentText();
01035     switch ( left.at(0).latin1() ) {
01036     case '[':
01037     case ']':
01038     case '{':
01039     case '}':
01040     case '<':
01041     case '>':
01042     case '(':
01043     case ')':
01044     case '/':
01045     case '\\':
01046         m_leftBracketChar = static_cast<SymbolType>( left.at(0).latin1() );
01047         break;
01048     case '|':
01049         m_leftBracketChar = LeftLineBracket;
01050         break;
01051     case ' ':
01052         m_leftBracketChar = EmptyBracket;
01053         break;
01054     }
01055 }
01056 
01057 void DocumentWrapper::delimiterRight()
01058 {
01059     QString right = m_rightBracket->currentText();
01060     switch ( right.at(0).latin1() ) {
01061     case '[':
01062     case ']':
01063     case '{':
01064     case '}':
01065     case '<':
01066     case '>':
01067     case '(':
01068     case ')':
01069     case '/':
01070     case '\\':
01071         m_rightBracketChar = static_cast<SymbolType>( right.at(0).latin1() );
01072         break;
01073     case '|':
01074         m_rightBracketChar = RightLineBracket;
01075         break;
01076     case ' ':
01077         m_rightBracketChar = EmptyBracket;
01078         break;
01079     }
01080 }
01081 
01082 void DocumentWrapper::symbolNames()
01083 {
01084     m_selectedName = m_symbolNamesAction->currentText();
01085 }
01086 
01087 
01088 void DocumentWrapper::fontFamily()
01089 {
01090     if ( hasFormula() ) {
01091         int i = m_fontFamily->currentItem();
01092         CharFamily cf = anyFamily;
01093         switch( i ) {
01094         case 0: cf = normalFamily; break;
01095         case 1: cf = scriptFamily; break;
01096         case 2: cf = frakturFamily; break;
01097         case 3: cf = doubleStruckFamily; break;
01098         }
01099         CharFamilyRequest r( cf );
01100         formula()->performRequest( &r );
01101     }
01102 }
01103 
01104 
01105 void DocumentWrapper::initSymbolNamesAction()
01106 {
01107     if ( m_hasActions ) {
01108         const SymbolTable& st = m_document->m_contextStyle->symbolTable();
01109 
01110         QStringList names = st.allNames();
01111         //QStringList i18nNames;
01112         QValueList<QFont> fonts;
01113         QMemArray<QChar> chars( names.count() );
01114 
01115         int i = 0;
01116         for ( QStringList::Iterator it = names.begin();
01117               it != names.end();
01118               ++it, ++i ) {
01119             QChar ch = st.unicode( *it );
01120             //i18nNames.push_back( i18n( ( *it ).latin1() ) );
01121 
01122             fonts.append( st.font( ch ) );
01123             chars[ i ] = st.character( ch );
01124         }
01125         m_symbolNamesAction->setSymbols( names, fonts, chars );
01126         m_selectedName = names[0];
01127     }
01128 }
01129 
01130 
01131 void DocumentWrapper::setEnabled( bool enabled )
01132 {
01133     kdDebug( DEBUGID ) << "DocumentWrapper::setEnabled " << enabled << endl;
01134     getAddNegThinSpaceAction()->setEnabled( enabled );
01135     getMakeGreekAction()->setEnabled( enabled );
01136     getAddGenericUpperAction()->setEnabled( enabled );
01137     getAddGenericLowerAction()->setEnabled( enabled );
01138     getAddOverlineAction()->setEnabled( enabled );
01139     getAddUnderlineAction()->setEnabled( enabled );
01140     getRemoveEnclosingAction()->setEnabled( enabled );
01141     getInsertSymbolAction()->setEnabled( enabled );
01142     getAddThinSpaceAction()->setEnabled( enabled );
01143     getAddMediumSpaceAction()->setEnabled( enabled );
01144     getAddThickSpaceAction()->setEnabled( enabled );
01145     getAddQuadSpaceAction()->setEnabled( enabled );
01146     getAddBracketAction()->setEnabled( enabled );
01147     getAddSBracketAction()->setEnabled( enabled );
01148     getAddCBracketAction()->setEnabled( enabled );
01149     getAddAbsAction()->setEnabled(enabled);
01150     getAddFractionAction()->setEnabled( enabled );
01151     getAddRootAction()->setEnabled( enabled );
01152     getAddSumAction()->setEnabled( enabled );
01153     getAddProductAction()->setEnabled( enabled );
01154     getAddIntegralAction()->setEnabled( enabled );
01155     getAddMatrixAction()->setEnabled( enabled );
01156     getAddOneByTwoMatrixAction()->setEnabled( enabled );
01157     getAddUpperLeftAction()->setEnabled( enabled );
01158     getAddLowerLeftAction()->setEnabled( enabled );
01159     getAddUpperRightAction()->setEnabled( enabled );
01160     getAddLowerRightAction()->setEnabled( enabled );
01161     
01162     getAddGenericUpperAction()->setEnabled( enabled );
01163     getAddGenericLowerAction()->setEnabled( enabled );
01164     
01165 
01166     if ( enabled ) {
01167         getAddGenericUpperAction()->
01168             setShortcut( KShortcut( CTRL + Key_U ) );
01169         getAddGenericLowerAction()->
01170             setShortcut( KShortcut( CTRL + Key_L ) );
01171         getRemoveEnclosingAction()->
01172             setShortcut( KShortcut( CTRL + Key_R ) );
01173         getMakeGreekAction()->
01174             setShortcut( KShortcut( CTRL + Key_G ) );
01175         getInsertSymbolAction()->
01176             setShortcut( KShortcut( CTRL + Key_I ) );
01177     }
01178     else {
01179         getAddGenericUpperAction()->setShortcut( KShortcut() );
01180         getAddGenericLowerAction()->setShortcut( KShortcut() );
01181         getRemoveEnclosingAction()->setShortcut( KShortcut() );
01182         getMakeGreekAction()->setShortcut( KShortcut() );
01183         getInsertSymbolAction()->setShortcut( KShortcut() );
01184     }
01185 }
01186 
01187 void DocumentWrapper::enableMatrixActions( bool b)
01188 {
01189     getAppendColumnAction()->setEnabled( b );
01190     getInsertColumnAction()->setEnabled( b );
01191     getRemoveColumnAction()->setEnabled( b );
01192     getAppendRowAction()->setEnabled( b );
01193     getInsertRowAction()->setEnabled( b );
01194     getRemoveRowAction()->setEnabled( b );
01195 }
01196 
01197 void DocumentWrapper::updateConfig()
01198 {
01199     m_syntaxHighlightingAction->
01200         setChecked( m_document->m_contextStyle->syntaxHighlighting() );
01201     initSymbolNamesAction();
01202 }
01203 
01204 
01205 KFORMULA_NAMESPACE_END
01206 
01207 using namespace KFormula;
01208 #include "kformuladocument.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys