lib

formulaelement.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 <iostream>
00022 #include <qpainter.h>
00023 
00024 #include <kdebug.h>
00025 
00026 #include "contextstyle.h"
00027 #include "formulacursor.h"
00028 #include "formulaelement.h"
00029 #include "kformulacontainer.h"
00030 #include "kformuladocument.h"
00031 
00032 KFORMULA_NAMESPACE_BEGIN
00033 
00034 FormulaElement::FormulaElement(FormulaDocument* container)
00035     : document( container ), baseSize( 20 ), ownBaseSize( false )
00036 {
00037 }
00038 
00039 
00040 void FormulaElement::setBaseSize( int size )
00041 {
00042     if ( size > 0 ) {
00043         baseSize = size;
00044         ownBaseSize = true;
00045     }
00046     else {
00047         ownBaseSize = false;
00048     }
00049     document->baseSizeChanged( size, ownBaseSize );
00050 }
00051 
00052 
00056 BasicElement* FormulaElement::goToPos( FormulaCursor* cursor, const LuPixelPoint& point )
00057 {
00058     bool handled = false;
00059     BasicElement* element = inherited::goToPos(cursor, handled, point, LuPixelPoint());
00060     if (element == 0) {
00061         //if ((point.x() > getWidth()) || (point.y() > getHeight())) {
00062             cursor->setTo(this, countChildren());
00063             //}
00064             return this;
00065     }
00066     return element;
00067 }
00068 
00069 void FormulaElement::elementRemoval(BasicElement* child)
00070 {
00071     document->elementRemoval(child);
00072 }
00073 
00074 void FormulaElement::changed()
00075 {
00076     document->changed();
00077 }
00078 
00079 void FormulaElement::cursorHasMoved( FormulaCursor* cursor )
00080 {
00081     document->cursorHasMoved( cursor );
00082 }
00083 
00084 void FormulaElement::moveOutLeft( FormulaCursor* cursor )
00085 {
00086     document->moveOutLeft( cursor );
00087 }
00088 
00089 void FormulaElement::moveOutRight( FormulaCursor* cursor )
00090 {
00091     document->moveOutRight( cursor );
00092 }
00093 
00094 void FormulaElement::moveOutBelow( FormulaCursor* cursor )
00095 {
00096     document->moveOutBelow( cursor );
00097 }
00098 
00099 void FormulaElement::moveOutAbove( FormulaCursor* cursor )
00100 {
00101     document->moveOutAbove( cursor );
00102 }
00103 
00104 void FormulaElement::tell( const QString& msg )
00105 {
00106     document->tell( msg );
00107 }
00108 
00109 void FormulaElement::removeFormula( FormulaCursor* cursor )
00110 {
00111     document->removeFormula( cursor );
00112 }
00113 
00114 void FormulaElement::insertFormula( FormulaCursor* cursor )
00115 {
00116     document->insertFormula( cursor );
00117 }
00118 
00119 void FormulaElement::calcSizes( const ContextStyle& context,
00120                                 ContextStyle::TextStyle tstyle,
00121                                 ContextStyle::IndexStyle istyle,
00122                                 StyleAttributes& style )
00123 {
00124     inherited::calcSizes( context, tstyle, istyle, style );
00125 }
00126 
00127 
00128 void FormulaElement::draw( QPainter& painter, const LuPixelRect& r,
00129                            const ContextStyle& context,
00130                            ContextStyle::TextStyle tstyle,
00131                            ContextStyle::IndexStyle istyle,
00132                            StyleAttributes& style,
00133                            const LuPixelPoint& parentOrigin )
00134 {
00135     inherited::draw( painter, r, context, tstyle, istyle, style, parentOrigin );
00136 }
00137 
00138 
00142 void FormulaElement::calcSizes( ContextStyle& context )
00143 {
00144     //kdDebug( DEBUGID ) << "FormulaElement::calcSizes" << endl;
00145     if ( ownBaseSize ) {
00146         context.setSizeFactor( static_cast<double>( getBaseSize() )/context.baseSize() );
00147     }
00148     else {
00149         context.setSizeFactor( 1 );
00150     }
00151     StyleAttributes style;
00152     calcSizes( context, context.getBaseTextStyle(), ContextStyle::normal, style );
00153 }
00154 
00158 void FormulaElement::draw( QPainter& painter, const LuPixelRect& r,
00159                            ContextStyle& context )
00160 {
00161     //kdDebug( DEBUGID ) << "FormulaElement::draw" << endl;
00162     if ( ownBaseSize ) {
00163         context.setSizeFactor( static_cast<double>( getBaseSize() )/context.baseSize() );
00164     }
00165     else {
00166         context.setSizeFactor( 1 );
00167     }
00168     StyleAttributes style;
00169     draw( painter, r, context, context.getBaseTextStyle(),
00170           ContextStyle::normal, style, LuPixelPoint() );
00171 }
00172 
00173 KCommand* FormulaElement::buildCommand( Container* container, Request* request )
00174 {
00175     switch ( *request ) {
00176     case req_compactExpression:
00177         return 0;
00178     default:
00179         break;
00180     }
00181     return inherited::buildCommand( container, request );
00182 }
00183 
00184 const SymbolTable& FormulaElement::getSymbolTable() const
00185 {
00186     return document->getSymbolTable();
00187 }
00188 
00189 
00190 QDomElement FormulaElement::emptyFormulaElement( QDomDocument& doc )
00191 {
00192     QDomElement element = doc.createElement( getTagName() );
00193     /*
00194     element.setAttribute( "VERSION", "6" );
00195     if ( ownBaseSize ) {
00196         element.setAttribute( "BASESIZE", baseSize );
00197     }
00198     */
00199     return element;
00200 }
00201 
00202 KCommand* FormulaElement::input( Container* container, QKeyEvent* event )
00203 {
00204     QChar ch = event->text().at( 0 );
00205     if ( !ch.isPrint() ) {
00206         int action = event->key();
00207         //int state = event->state();
00208         //MoveFlag flag = movementFlag(state);
00209 
00210     switch ( action ) {
00211         case Qt::Key_Return:
00212         case Qt::Key_Enter: {
00213             FormulaCursor* cursor = container->activeCursor();
00214             insertFormula( cursor );
00215             return 0;
00216         }
00217         }
00218     }
00219     return inherited::input( container, event );
00220 }
00221 
00225 void FormulaElement::writeDom(QDomElement element)
00226 {
00227     inherited::writeDom(element);
00228     element.setAttribute( "VERSION", "6" );
00229     if ( ownBaseSize ) {
00230         element.setAttribute( "BASESIZE", baseSize );
00231     }
00232 }
00233 
00238 bool FormulaElement::readAttributesFromDom(QDomElement element)
00239 {
00240     if (!inherited::readAttributesFromDom(element)) {
00241         return false;
00242     }
00243     int version = -1;
00244     QString versionStr = element.attribute( "VERSION" );
00245     if ( !versionStr.isNull() ) {
00246         version = versionStr.toInt();
00247     }
00248     if ( version > -1 ) {
00249         // Version 6 added the MultilineElement (TabMarker)
00250         // Version 5 added under- and overlines
00251         if ( version < 4 ) {
00252             convertNames( element );
00253         }
00254     }
00255     QString baseSizeStr = element.attribute( "BASESIZE" );
00256     if ( !baseSizeStr.isNull() ) {
00257         ownBaseSize = true;
00258         baseSize = baseSizeStr.toInt();
00259     }
00260     else {
00261         ownBaseSize = false;
00262     }
00263     return true;
00264 }
00265 
00271 bool FormulaElement::readContentFromDom(QDomNode& node)
00272 {
00273     return inherited::readContentFromDom(node);
00274 }
00275 
00276 void FormulaElement::convertNames( QDomNode node )
00277 {
00278     if ( node.isElement() && ( node.nodeName().upper() == "TEXT" ) ) {
00279         QDomNamedNodeMap attr = node.attributes();
00280         QDomAttr ch = attr.namedItem( "CHAR" ).toAttr();
00281         if ( ch.value() == "\\" ) {
00282             QDomNode sequence = node.parentNode();
00283             QDomDocument doc = sequence.ownerDocument();
00284             QDomElement nameseq = doc.createElement( "NAMESEQUENCE" );
00285             sequence.replaceChild( nameseq, node );
00286 
00287             bool inName = true;
00288             while ( inName ) {
00289                 inName = false;
00290                 QDomNode n = nameseq.nextSibling();
00291                 if ( n.isElement() && ( n.nodeName().upper() == "TEXT" ) ) {
00292                     attr = n.attributes();
00293                     ch = attr.namedItem( "CHAR" ).toAttr();
00294                     if ( ch.value().at( 0 ).isLetter() ) {
00295                         nameseq.appendChild( sequence.removeChild( n ) );
00296                         inName = true;
00297                     }
00298                 }
00299             }
00300         }
00301     }
00302     if ( node.hasChildNodes() ) {
00303         QDomNode n = node.firstChild();
00304         while ( !n.isNull() ) {
00305             convertNames( n );
00306             n = n.nextSibling();
00307         }
00308     }
00309 }
00310 
00311 QString FormulaElement::toLatex()
00312 {
00313     return inherited::toLatex();   //Consider $$ sorround
00314 }
00315 
00316 void FormulaElement::writeMathML( QDomDocument& doc, QDomNode& parent, bool oasisFormat ) const
00317 {
00318     QDomElement de;
00319     if ( !oasisFormat ) {
00320         de = doc.createElementNS( "http://www.w3.org/1998/Math/MathML",
00321                                               "math" );
00322         parent.appendChild( de );
00323     }
00324     else {
00325         QDomElement element = doc.createElement( "math:semantics" );
00326         de = doc.createElement( "math:mrow" );
00327         parent.appendChild( element );
00328         element.appendChild( de );
00329     }
00330     for ( uint i = 0; i < countChildren(); ++i ) {
00331         const BasicElement* e = getChild( i );
00332         e->writeMathML( doc, de, oasisFormat );
00333     }
00334 }
00335 
00336 KFORMULA_NAMESPACE_END
KDE Home | KDE Accessibility Home | Description of Access Keys