00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <algorithm>
00021
00022 #include <qpainter.h>
00023
00024 #include <klocale.h>
00025
00026 #include "elementtype.h"
00027 #include "sequenceelement.h"
00028 #include "textelement.h"
00029 #include "fontstyle.h"
00030 #include "operatordictionary.h"
00031 #include "operatorelement.h"
00032 #include "identifierelement.h"
00033 #include "numberelement.h"
00034 #include "kformulacommand.h"
00035 #include "kformulacontainer.h"
00036 #include "kformuladocument.h"
00037 #include "formulaelement.h"
00038 #include "creationstrategy.h"
00039
00040 KFORMULA_NAMESPACE_BEGIN
00041
00042 OperatorElement::OperatorElement( BasicElement* parent ) : TokenElement( parent ),
00043 m_form( NoForm ),
00044 m_lspaceType( ThickMathSpace ),
00045 m_rspaceType( ThickMathSpace ),
00046 m_maxSizeType( InfinitySize ),
00047 m_minSizeType( RelativeSize ),
00048 m_minSize( 1 ),
00049 m_fence( false ),
00050 m_separator( false ),
00051 m_stretchy( false ),
00052 m_symmetric( true ),
00053 m_largeOp( false ),
00054 m_movableLimits( false ),
00055 m_accent( false ),
00056 m_customForm( false ),
00057 m_customFence( false ),
00058 m_customSeparator( false ),
00059 m_customLSpace( false ),
00060 m_customRSpace( false ),
00061 m_customStretchy( false ),
00062 m_customSymmetric( false ),
00063 m_customMaxSize( false ),
00064 m_customMinSize( false ),
00065 m_customLargeOp( false ),
00066 m_customMovableLimits( false ),
00067 m_customAccent( false )
00068 {
00069 }
00070
00071 void OperatorElement::setForm( FormType type )
00072 {
00073 if ( ! m_customForm ) {
00074 m_form = type;
00075 }
00076
00077 if ( ! isTextOnly() ) {
00078 return;
00079 }
00080 QString text;
00081 for ( uint i = 0; i < countChildren(); i++ ) {
00082 text.append( getChild( i )->getCharacter() );
00083 }
00084 QString form;
00085 switch ( m_form ) {
00086 case PrefixForm:
00087 form = "prefix";
00088 break;
00089 case InfixForm:
00090 form = "infix";
00091 break;
00092 case PostfixForm:
00093 form = "postfix";
00094 break;
00095 default:
00096
00097 kdWarning( DEBUGID ) << "Invalid `form' attribute value\n";
00098 return;
00099 }
00100 DictionaryKey key = { text.utf8(), form.ascii() };
00101 const OperatorDictionary* begin = operators;
00102 const OperatorDictionary* end = operators + OperatorDictionary::size();
00103 const OperatorDictionary* pos = std::lower_bound( begin, end, key );
00104 if ( pos != end && pos->key == key ) {
00105 if ( ! m_customFence ) {
00106 m_fence = pos->fence;
00107 }
00108 if ( ! m_customSeparator ) {
00109 m_separator = pos->separator;
00110 }
00111 if ( ! m_customLSpace ) {
00112 m_lspace = getSize( pos->lspace, &m_lspaceType );
00113 if ( m_lspaceType == NoSize ) {
00114 m_lspaceType = getSpace( pos->lspace );
00115 }
00116 }
00117 if ( ! m_customRSpace ) {
00118 m_rspace = getSize( pos->rspace, &m_rspaceType );
00119 if ( m_rspaceType == NoSize ) {
00120 m_rspaceType = getSpace( pos->rspace );
00121 }
00122 }
00123 if ( ! m_customStretchy ) {
00124 m_stretchy = pos->stretchy;
00125 }
00126 if ( ! m_customSymmetric ) {
00127 m_symmetric = pos->symmetric;
00128 }
00129 if ( ! m_customMaxSize ) {
00130 if ( qstrcmp( pos->maxsize, "infinity" ) == 0 ) {
00131 m_maxSizeType = InfinitySize;
00132 }
00133 else {
00134 m_maxSize = getSize( pos->maxsize, &m_maxSizeType );
00135 if ( m_maxSizeType == NoSize ) {
00136 m_maxSizeType = getSpace( pos->maxsize );
00137 }
00138 }
00139 }
00140 if ( ! m_customMinSize ) {
00141 m_minSize = getSize( pos->minsize, &m_minSizeType );
00142 if ( m_minSizeType == NoSize ) {
00143 m_minSizeType = getSpace( pos->minsize );
00144 }
00145 }
00146 if ( ! m_customLargeOp ) {
00147 m_largeOp = pos->largeop;
00148 }
00149 if ( ! m_customMovableLimits ) {
00150 m_movableLimits = pos->movablelimits;
00151 }
00152 if ( ! m_customAccent ) {
00153 m_accent = pos->accent;
00154 }
00155 }
00156 }
00157
00158
00159
00160
00161
00162
00163
00164 KCommand* OperatorElement::buildCommand( Container* container, Request* request )
00165 {
00166 FormulaCursor* cursor = container->activeCursor();
00167 if ( cursor->isReadOnly() ) {
00168 formula()->tell( i18n( "write protection" ) );
00169 return 0;
00170 }
00171
00172 if ( *request == req_addOperator ) {
00173 KFCReplace* command = new KFCReplace( i18n("Add Operator"), container );
00174 OperatorRequest* opr = static_cast<OperatorRequest*>( request );
00175 TextElement* element = creationStrategy->createTextElement( opr->ch(), true );
00176 command->addElement( element );
00177 return command;
00178 }
00179
00180 if ( countChildren() == 0 || cursor->getPos() == countChildren() ) {
00181
00182
00183 SequenceElement* parent = static_cast<SequenceElement*>( getParent() );
00184 if ( parent ) {
00185 uint pos = parent->childPos( this );
00186 cursor->setTo( parent, pos + 1);
00187 return parent->buildCommand( container, request );
00188 }
00189 }
00190 if ( cursor->getPos() == 0 ) {
00191 SequenceElement* parent = static_cast<SequenceElement*>( getParent() );
00192 if ( parent ) {
00193 uint pos = parent->childPos( this );
00194 cursor->setTo( parent, pos );
00195 return parent->buildCommand( container, request );
00196 }
00197 }
00198
00199
00200
00201
00202
00203
00204
00205 switch( *request ) {
00206 case req_addTextChar: {
00207 KFCSplitToken* command = new KFCSplitToken( i18n("Add Text"), container );
00208 TextCharRequest* tr = static_cast<TextCharRequest*>( request );
00209 IdentifierElement* id = creationStrategy->createIdentifierElement();
00210 TextElement* text = creationStrategy->createTextElement( tr->ch() );
00211 command->addCursor( cursor );
00212 command->addToken( id );
00213 command->addContent( id, text );
00214 SequenceElement* parent = static_cast< SequenceElement* >( getParent() );
00215 if ( parent ) {
00216 cursor->setTo( parent, parent->childPos( this ) + 1 );
00217 }
00218 return command;
00219 }
00220
00221 case req_addText: {
00222 KFCSplitToken* command = new KFCSplitToken( i18n("Add Text"), container );
00223 TextRequest* tr = static_cast<TextRequest*>( request );
00224 IdentifierElement* id = creationStrategy->createIdentifierElement();
00225 command->addCursor( cursor );
00226 command->addToken( id );
00227 for ( uint i = 0; i < tr->text().length(); i++ ) {
00228 TextElement* text = creationStrategy->createTextElement( tr->text()[i] );
00229 command->addContent( id, text );
00230 }
00231 SequenceElement* parent = static_cast< SequenceElement* >( getParent() );
00232 if ( parent ) {
00233 cursor->setTo( parent, parent->childPos( this ) + 1 );
00234 }
00235 return command;
00236 }
00237
00238 case req_addNumber: {
00239 KFCSplitToken* command = new KFCSplitToken( i18n("Add Number"), container );
00240 NumberRequest* nr = static_cast<NumberRequest*>( request );
00241 NumberElement* num = creationStrategy->createNumberElement();
00242 TextElement* text = creationStrategy->createTextElement( nr->ch() );
00243 command->addCursor( cursor );
00244 command->addToken( num );
00245 command->addContent( num, text );
00246 SequenceElement* parent = static_cast< SequenceElement* >( getParent() );
00247 if ( parent ) {
00248 cursor->setTo( parent, parent->childPos( this ) + 1 );
00249 }
00250 return command;
00251 }
00252 case req_addEmptyBox:
00253 case req_addNameSequence:
00254 case req_addBracket:
00255 case req_addSpace:
00256 case req_addFraction:
00257 case req_addRoot:
00258 case req_addSymbol:
00259 case req_addOneByTwoMatrix:
00260 case req_addMatrix: {
00261 uint pos = static_cast<SequenceElement*>(getParent())->childPos( this );
00262 cursor->setTo( getParent(), pos + 1);
00263 return getParent()->buildCommand( container, request );
00264 }
00265 default:
00266 return SequenceElement::buildCommand( container, request );
00267 }
00268 return 0;
00269 }
00270
00271
00272 bool OperatorElement::readAttributesFromMathMLDom( const QDomElement &element )
00273 {
00274 if ( ! BasicElement::readAttributesFromMathMLDom( element ) ) {
00275 return false;
00276 }
00277
00278 QString formStr = element.attribute( "form" ).stripWhiteSpace().lower();
00279 if ( ! formStr.isNull() ) {
00280 m_customForm = true;
00281 if ( formStr == "prefix" ) {
00282 m_form = PrefixForm;
00283 }
00284 else if ( formStr == "infix" ) {
00285 m_form = InfixForm;
00286 }
00287 else if ( formStr == "postfix" ) {
00288 m_form = PostfixForm;
00289 }
00290 else {
00291 kdWarning( DEBUGID ) << "Invalid value for attribute `form': " << formStr << endl;
00292 m_customForm = false;
00293 }
00294 }
00295 QString fenceStr = element.attribute( "fence" ).stripWhiteSpace().lower();
00296 if ( ! fenceStr.isNull() ) {
00297 m_customFence = true;
00298 if ( fenceStr == "true" ) {
00299 m_fence = true;
00300 }
00301 else if ( fenceStr == "false" ) {
00302 m_fence = false;
00303 }
00304 else {
00305 kdWarning( DEBUGID ) << "Invalid value for attribute `fence': " << fenceStr << endl;
00306 m_customFence = false;
00307 }
00308 }
00309 QString separatorStr = element.attribute( "separator" ).stripWhiteSpace().lower();
00310 if ( ! separatorStr.isNull() ) {
00311 m_customSeparator = true;
00312 if ( separatorStr == "true" ) {
00313 m_separator = true;
00314 }
00315 else if ( separatorStr == "false" ) {
00316 m_separator = false;
00317 }
00318 else {
00319 kdWarning( DEBUGID ) << "Invalid value for attribute `separator': " << separatorStr << endl;
00320 m_customSeparator = false;
00321 }
00322 }
00323 QString lspaceStr = element.attribute( "lspace" ).stripWhiteSpace().lower();
00324 if ( ! lspaceStr.isNull() ) {
00325 m_customLSpace = true;
00326 m_lspace = getSize( lspaceStr, &m_lspaceType );
00327 if ( m_lspaceType == NoSize ) {
00328 m_lspaceType = getSpace( lspaceStr );
00329 }
00330 }
00331 QString rspaceStr = element.attribute( "rspace" ).stripWhiteSpace().lower();
00332 if ( ! rspaceStr.isNull() ) {
00333 m_customRSpace = true;
00334 m_rspace = getSize( rspaceStr, &m_rspaceType );
00335 if ( m_rspaceType == NoSize ) {
00336 m_rspaceType = getSpace( rspaceStr );
00337 }
00338 }
00339 QString stretchyStr = element.attribute( "stretchy" ).stripWhiteSpace().lower();
00340 if ( ! stretchyStr.isNull() ) {
00341 m_customStretchy = true;
00342 if ( stretchyStr == "true" ) {
00343 m_stretchy = true;
00344 }
00345 else if ( stretchyStr == "false" ) {
00346 m_stretchy = false;
00347 }
00348 else {
00349 kdWarning( DEBUGID ) << "Invalid value for attribute `stretchy': " << stretchyStr << endl;
00350 m_customStretchy = false;
00351 }
00352 }
00353 QString symmetricStr = element.attribute( "symmetric" ).stripWhiteSpace().lower();
00354 if ( ! symmetricStr.isNull() ) {
00355 m_customSymmetric = true;
00356 if ( symmetricStr == "true" ) {
00357 m_symmetric = true;
00358 }
00359 else if ( symmetricStr == "false" ) {
00360 m_symmetric = false;
00361 }
00362 else {
00363 kdWarning( DEBUGID ) << "Invalid value for attribute `symmetric': " << symmetricStr << endl;
00364 m_customSymmetric = false;
00365 }
00366 }
00367 QString maxsizeStr = element.attribute( "maxsize" ).stripWhiteSpace().lower();
00368 if ( ! maxsizeStr.isNull() ) {
00369 m_customMaxSize = true;
00370 if ( maxsizeStr == "infinity" ) {
00371 m_maxSizeType = InfinitySize;
00372 }
00373 else {
00374 m_maxSize = getSize( maxsizeStr, &m_maxSizeType );
00375 if ( m_maxSizeType == NoSize ) {
00376 m_maxSizeType = getSpace( maxsizeStr );
00377 }
00378 }
00379 }
00380 QString minsizeStr = element.attribute( "minsize" ).stripWhiteSpace().lower();
00381 if ( ! minsizeStr.isNull() ) {
00382 m_customMinSize = true;
00383 m_minSize = getSize( minsizeStr, &m_minSizeType );
00384 if ( m_minSizeType == NoSize ) {
00385 m_minSizeType = getSpace( minsizeStr );
00386 }
00387 }
00388 QString largeopStr = element.attribute( "largeop" ).stripWhiteSpace().lower();
00389 if ( ! largeopStr.isNull() ) {
00390 m_customLargeOp = true;
00391 if ( largeopStr == "true" ) {
00392 m_largeOp = true;
00393 }
00394 else if ( largeopStr == "false" ) {
00395 m_largeOp = false;
00396 }
00397 else {
00398 kdWarning( DEBUGID ) << "Invalid value for attribute `largeop': " << largeopStr << endl;
00399 m_customLargeOp = false;
00400 }
00401 }
00402 QString movablelimitsStr = element.attribute( "movablelimits" ).stripWhiteSpace().lower();
00403 if ( ! movablelimitsStr.isNull() ) {
00404 m_customMovableLimits = true;
00405 if ( movablelimitsStr == "true" ) {
00406 m_movableLimits = true;
00407 }
00408 else if ( movablelimitsStr == "false" ) {
00409 m_movableLimits = false;
00410 }
00411 else {
00412 kdWarning( DEBUGID ) << "Invalid value for attribute `movablelimits': " << movablelimitsStr << endl;
00413 m_customMovableLimits = false;
00414 }
00415 }
00416 QString accentStr = element.attribute( "accent" ).stripWhiteSpace().lower();
00417 if ( ! accentStr.isNull() ) {
00418 m_customAccent = true;
00419 if ( accentStr == "true" ) {
00420 m_accent = true;
00421 }
00422 else if ( accentStr == "false" ) {
00423 m_accent = false;
00424 }
00425 else {
00426 kdWarning( DEBUGID ) << "Invalid value for attribute `accent': " << accentStr << endl;
00427 m_customAccent = false;
00428 }
00429 }
00430 return true;
00431 }
00432
00433 void OperatorElement::writeMathMLAttributes( QDomElement& element ) const
00434 {
00435 if ( m_customForm ) {
00436 switch ( m_form ) {
00437 case PrefixForm:
00438 element.setAttribute( "form", "prefix" );
00439 break;
00440 case InfixForm:
00441 element.setAttribute( "form", "infix" );
00442 break;
00443 case PostfixForm:
00444 element.setAttribute( "form", "postfix" );
00445 default:
00446 break;
00447 }
00448 }
00449 if ( m_customFence ) {
00450 element.setAttribute( "fence", m_fence ? "true" : "false" );
00451 }
00452 if ( m_customSeparator ) {
00453 element.setAttribute( "separator", m_separator ? "true" : "false" );
00454 }
00455 if ( m_customLSpace ) {
00456 writeSizeAttribute( element, "lspace", m_lspaceType, m_lspace );
00457 }
00458 if ( m_customRSpace ) {
00459 writeSizeAttribute( element, "rspace", m_rspaceType, m_rspace );
00460 }
00461 if ( m_customStretchy ) {
00462 element.setAttribute( "stretchy", m_stretchy ? "true" : "false" );
00463 }
00464 if ( m_customSymmetric ) {
00465 element.setAttribute( "symmetric", m_symmetric ? "true" : "false" );
00466 }
00467 if ( m_customMaxSize ) {
00468 writeSizeAttribute( element, "maxsize", m_maxSizeType, m_maxSize );
00469 }
00470 if ( m_customMinSize ) {
00471 writeSizeAttribute( element, "minsize", m_minSizeType, m_minSize );
00472 }
00473 if ( m_customLargeOp ) {
00474 element.setAttribute( "largeop", m_largeOp ? "true" : "false" );
00475 }
00476 if ( m_customMovableLimits ) {
00477 element.setAttribute( "movablelimits", m_movableLimits ? "true" : "false" );
00478 }
00479 if ( m_customAccent ) {
00480 element.setAttribute( "accent", m_accent ? "true" : "false" );
00481 }
00482 }
00483
00484 void OperatorElement::writeSizeAttribute( QDomElement& element, const QString &attr, SizeType type, double length ) const
00485 {
00486 switch ( type ) {
00487 case InfinitySize:
00488 element.setAttribute( attr, "infinity" );
00489 break;
00490 case AbsoluteSize:
00491 element.setAttribute( attr, QString( "%1pt" ).arg( length ) );
00492 break;
00493 case RelativeSize:
00494 element.setAttribute( attr, QString( "%1% " ).arg( length * 100.0 ) );
00495 break;
00496 case PixelSize:
00497 element.setAttribute( attr, QString( "%1px " ).arg( length ) );
00498 break;
00499 case NegativeVeryVeryThinMathSpace:
00500 element.setAttribute( attr, "negativeveryverythinmathspace" );
00501 break;
00502 case NegativeVeryThinMathSpace:
00503 element.setAttribute( attr, "negativeverythinmathspace" );
00504 break;
00505 case NegativeThinMathSpace:
00506 element.setAttribute( attr, "negativethinmathspace" );
00507 break;
00508 case NegativeMediumMathSpace:
00509 element.setAttribute( attr, "negativemediummathspace" );
00510 break;
00511 case NegativeThickMathSpace:
00512 element.setAttribute( attr, "negativethickmathspace" );
00513 break;
00514 case NegativeVeryThickMathSpace:
00515 element.setAttribute( attr, "negativeverythickmathspace" );
00516 break;
00517 case NegativeVeryVeryThickMathSpace:
00518 element.setAttribute( attr, "negativeveryverythickmathspace" );
00519 break;
00520 case VeryVeryThinMathSpace:
00521 element.setAttribute( attr, "veryverythinmathspace" );
00522 break;
00523 case VeryThinMathSpace:
00524 element.setAttribute( attr, "verythinmathspace" );
00525 break;
00526 case ThinMathSpace:
00527 element.setAttribute( attr, "thinmathspace" );
00528 break;
00529 case MediumMathSpace:
00530 element.setAttribute( attr, "mediummathspace" );
00531 break;
00532 case ThickMathSpace:
00533 element.setAttribute( attr, "thickmathspace" );
00534 break;
00535 case VeryThickMathSpace:
00536 element.setAttribute( attr, "verythickmathspace" );
00537 break;
00538 case VeryVeryThickMathSpace:
00539 element.setAttribute( attr, "veryverythickmathspace" );
00540 break;
00541 default:
00542 break;
00543 }
00544 }
00545
00546
00547 KFORMULA_NAMESPACE_END