lib

basicelement.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 <qstring.h>
00022 #include <kdebug.h>
00023 
00024 #include "contextstyle.h"
00025 #include "basicelement.h"
00026 #include "formulacursor.h"
00027 #include "formulaelement.h"
00028 #include "sequenceelement.h"
00029 
00030 KFORMULA_NAMESPACE_BEGIN
00031 using namespace std;
00032 
00033 
00034 int BasicElement::evilDestructionCount = 0;
00035 
00036 BasicElement::BasicElement( BasicElement* p )
00037         : parent( p ), m_baseline( 0 ), elementType( 0 )
00038 {
00039     setX( 0 );
00040     setY( 0 );
00041     setWidth( 0 );
00042     setHeight( 0 );
00043     evilDestructionCount++;
00044 }
00045 
00046 BasicElement::~BasicElement()
00047 {
00048     evilDestructionCount--;
00049 }
00050 
00051 BasicElement::BasicElement( const BasicElement& other )
00052     : parent( 0 ),
00053       m_baseline( other.m_baseline ),
00054       elementType( other.elementType )
00055 {
00056     setX( other.getX() );
00057     setY( other.getY() );
00058     setWidth( other.getWidth() );
00059     setHeight( other.getHeight() );
00060     evilDestructionCount++;
00061 }
00062 
00063 
00064 bool BasicElement::readOnly( const BasicElement* /*child*/ ) const
00065 {
00066     return parent->readOnly( this );
00067 }
00068 
00069 
00070 FormulaElement* BasicElement::formula()
00071 {
00072     //if ( parent != 0 ) {
00073         return parent->formula();
00074         //}
00075         //return 0;
00076 }
00077 
00078 
00082 BasicElement* BasicElement::goToPos( FormulaCursor*, bool&,
00083                                      const LuPixelPoint& point, const LuPixelPoint& parentOrigin )
00084 {
00085     luPixel x = point.x() - (parentOrigin.x() + getX());
00086     if ((x >= 0) && (x < getWidth())) {
00087         luPixel y = point.y() - (parentOrigin.y() + getY());
00088         if ((y >= 0) && (y < getHeight())) {
00089             return this;
00090         }
00091     }
00092     return 0;
00093 }
00094 
00098 LuPixelPoint BasicElement::widgetPos()
00099 {
00100     luPixel x = 0;
00101     luPixel y = 0;
00102     for (BasicElement* element = this; element != 0; element = element->parent) {
00103         x += element->getX();
00104         y += element->getY();
00105     }
00106     return LuPixelPoint(x, y);
00107 }
00108 
00109 
00114 void BasicElement::goInside(FormulaCursor* cursor)
00115 {
00116     BasicElement* mainChild = getMainChild();
00117     if (mainChild != 0) {
00118         mainChild->goInside(cursor);
00119     }
00120 }
00121 
00122 
00123 void BasicElement::entered( SequenceElement* /*child*/ )
00124 {
00125     formula()->tell( "" );
00126 }
00127 
00128 
00134 void BasicElement::moveLeft(FormulaCursor* cursor, BasicElement*)
00135 {
00136     getParent()->moveLeft(cursor, this);
00137 }
00138 
00139 
00145 void BasicElement::moveRight(FormulaCursor* cursor, BasicElement*)
00146 {
00147     getParent()->moveRight(cursor, this);
00148 }
00149 
00150 
00155 void BasicElement::normalize(FormulaCursor* cursor, Direction direction)
00156 {
00157     BasicElement* element = getMainChild();
00158     if (element != 0) {
00159         if (direction == beforeCursor) {
00160             element->moveLeft(cursor, this);
00161         }
00162         else {
00163             element->moveRight(cursor, this);
00164         }
00165     }
00166 }
00167 
00168 
00169 QDomElement BasicElement::getElementDom( QDomDocument& doc)
00170 {
00171     QDomElement de = doc.createElement(getTagName());
00172     writeDom(de);
00173     return de;
00174 }
00175 
00176 
00177 void BasicElement::writeMathML( QDomDocument& doc, QDomNode& parent, bool oasisFormat ) const
00178 {
00179     QDomElement de = doc.createElement( oasisFormat ? "math:" + getElementName() : getElementName() );
00180     writeMathMLAttributes( de );
00181     writeMathMLContent( doc, de, oasisFormat );
00182     parent.appendChild( de );
00183 }
00184 
00185 bool BasicElement::buildFromDom(QDomElement element)
00186 {
00187     if (element.tagName() != getTagName()) {
00188         kdWarning( DEBUGID ) << "Wrong tag name " << element.tagName().latin1() << " for " << getTagName().latin1() << ".\n";
00189         return false;
00190     }
00191     if (!readAttributesFromDom(element)) {
00192         return false;
00193     }
00194     QDomNode node = element.firstChild();
00195     return readContentFromDom(node);
00196 }
00197 
00198 int BasicElement::buildFromMathMLDom(QDomElement element)
00199 {/*
00200     if (element.tagName() != getTagName()) {
00201         kdWarning( DEBUGID ) << "Wrong tag name " << element.tagName().latin1() << " for " << getTagName().latin1() << ".\n";
00202         return false;
00203         }*/
00204     if (!readAttributesFromMathMLDom(element)) {
00205         return -1;
00206         }
00207     QDomNode node = element.firstChild();
00208     return readContentFromMathMLDom(node);
00209 }
00210 
00214 void BasicElement::writeDom(QDomElement)
00215 {
00216 }
00217 
00222 bool BasicElement::readAttributesFromDom(QDomElement)
00223 {
00224     return true;
00225 }
00226 
00232 bool BasicElement::readContentFromDom(QDomNode&)
00233 {
00234     return true;
00235 }
00236 
00241 bool BasicElement::buildChild( SequenceElement* child, QDomNode node, QString name )
00242 {
00243     if (node.isElement()) {
00244         QDomElement e = node.toElement();
00245         if (e.tagName().upper() == name) {
00246             QDomNode nodeInner = e.firstChild();
00247             if (nodeInner.isElement()) {
00248                 QDomElement element = nodeInner.toElement();
00249                 return child->buildFromDom( element );
00250             }
00251         }
00252     }
00253     return false;
00254 }
00255 
00260 bool BasicElement::readAttributesFromMathMLDom(const QDomElement& )
00261 {
00262     return true;
00263 }
00264 
00270 int BasicElement::readContentFromMathMLDom(QDomNode&)
00271 {
00272     return 1;
00273 }
00274 
00275 QString BasicElement::toLatex()
00276 {
00277     return "{}";
00278 }
00279 
00290 double BasicElement::getSize( const QString& str, SizeType* st )
00291 {
00292     int index = str.find( "%" );
00293     if ( index != -1 ) {
00294         return str2size( str, st, index, RelativeSize ) / 100.0;
00295     }
00296     index = str.find( "pt", 0, false );
00297     if ( index != -1 ) {
00298         return str2size( str, st, index, AbsoluteSize );
00299     }
00300     index = str.find( "mm", 0, false );
00301     if ( index != -1 ) {
00302         return str2size( str, st, index, AbsoluteSize ) * 72.0 / 20.54;
00303     }
00304     index = str.find( "cm", 0, false );
00305     if ( index != -1 ) {
00306         return str2size( str, st, index, AbsoluteSize ) * 72.0 / 2.54;
00307     }
00308     index = str.find( "in", 0, false );
00309     if ( index != -1 ) {
00310         return str2size( str, st, index, AbsoluteSize ) * 72.0;
00311     }
00312     index = str.find( "em", 0, false );
00313     if ( index != -1 ) {
00314         return str2size( str, st, index, RelativeSize );
00315     }
00316     index = str.find( "ex", 0, false );
00317     if ( index != -1 ) {
00318         return str2size( str, st, index, RelativeSize );
00319     }
00320     index = str.find( "pc", 0, false );
00321     if ( index != -1 ) {
00322         return str2size( str, st, index, AbsoluteSize ) * 12.0;
00323     }
00324     index = str.find( "px", 0, false );
00325     if ( index != -1 ) {
00326         return str2size( str, st, index, PixelSize );
00327     }
00328     // If there's no unit, assume 'pt'
00329     return str2size( str, st, str.length(),AbsoluteSize );
00330 }
00331 
00332 SizeType BasicElement::getSpace( const QString& str )
00333 {
00334     if ( str == "negativeveryverythinmathspace" ) {
00335         return NegativeVeryVeryThinMathSpace;
00336     }
00337     if ( str == "negativeverythinmathspace" ) {
00338         return NegativeVeryThinMathSpace;
00339     }
00340     if ( str == "negativethinmathspace" ) {
00341         return NegativeThinMathSpace;
00342     }
00343     if ( str == "negativemediummathspace" ) {
00344         return NegativeMediumMathSpace;
00345     }
00346     if ( str == "negativethickmathspace" ) {
00347         return NegativeThickMathSpace;
00348     }
00349     if ( str == "negativeverythickmathspace" ) {
00350         return NegativeVeryThickMathSpace;
00351     }
00352     if ( str == "negativeveryverythickmathspace" ) {
00353         return NegativeVeryVeryThickMathSpace;
00354     }
00355     if ( str == "veryverythinmathspace" ) {
00356         return VeryVeryThinMathSpace;
00357     }
00358     if ( str == "verythinmathspace" ) {
00359         return VeryThinMathSpace;
00360     }
00361     if ( str == "thinmathspace" ) {
00362         return ThinMathSpace;
00363     }
00364     if ( str == "mediummathspace" ) {
00365         return MediumMathSpace;
00366     }
00367     if ( str == "thickmathspace" ) {
00368         return ThickMathSpace;
00369     }
00370     if ( str == "verythickmathspace" ) {
00371         return VeryThickMathSpace;
00372     }
00373     if ( str == "veryverythickmathspace" ) {
00374         return VeryVeryThickMathSpace;
00375     }
00376     return NoSize;
00377 }
00378 
00379 
00383 double BasicElement::str2size( const QString& str, SizeType *st, uint index, SizeType type )
00384 {
00385     QString num = str.left( index );
00386     bool ok;
00387     double size = num.toDouble( &ok );
00388     if ( ok ) {
00389         if ( st ) {
00390             *st = type;
00391         }
00392         return size;
00393     }
00394     if ( st ) {
00395         *st = NoSize;
00396     }
00397     return -1;
00398 }
00399 
00400 KFORMULA_NAMESPACE_END
KDE Home | KDE Accessibility Home | Description of Access Keys