lib

identifierelement.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Alfredo Beaumont Sainz <alfredo.beaumont@gmail.com>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <klocale.h>
00021 
00022 #include "kformuladefs.h"
00023 #include "textelement.h"
00024 #include "identifierelement.h"
00025 #include "operatorelement.h"
00026 #include "numberelement.h"
00027 #include "kformulacommand.h"
00028 #include "kformulacontainer.h"
00029 #include "kformuladocument.h"
00030 #include "formulaelement.h"
00031 #include "creationstrategy.h"
00032 
00033 KFORMULA_NAMESPACE_BEGIN
00034 
00035 IdentifierElement::IdentifierElement( BasicElement* parent ) : TokenElement( parent ) {}
00036 
00037 /*
00038  * Token elements' content has to be of homogeneous type. Every token element
00039  * must (TODO: check this) appear inside a non-token sequence, and thus, if
00040  * the command asks for a different content, a new element has to be created in
00041  * parent sequence.
00042  */
00043 KCommand* IdentifierElement::buildCommand( Container* container, Request* request )
00044 {
00045     FormulaCursor* cursor = container->activeCursor();
00046     if ( cursor->isReadOnly() ) {
00047         formula()->tell( i18n( "write protection" ) );
00048         return 0;
00049     }
00050 
00051     if ( *request == req_addText ) {
00052         KFCReplace* command = new KFCReplace( i18n("Add Text"), container );
00053         TextRequest* tr = static_cast<TextRequest*>( request );
00054         for ( uint i = 0; i < tr->text().length(); i++ ) {
00055             TextElement* element = creationStrategy->createTextElement( tr->text()[i] );
00056             command->addElement( element );
00057         }
00058         return command;
00059     }
00060 
00061     else if ( *request ==  req_addTextChar ) {
00062         KFCReplace* command = new KFCReplace( i18n("Add Text"), container );
00063         TextCharRequest* tr = static_cast<TextCharRequest*>( request );
00064         TextElement* element = creationStrategy->createTextElement( tr->ch(), tr->isSymbol() );
00065         command->addElement( element );
00066         return command;
00067     }
00068 
00069     if ( countChildren() == 0 || cursor->getPos() == countChildren() ) {
00070         // We are in the last position, so it's easy, call the parent to 
00071         // create a new child
00072         SequenceElement* parent = static_cast<SequenceElement*>( getParent() );
00073         if ( parent ) {
00074             uint pos = parent->childPos( this );
00075             cursor->setTo( parent, pos + 1);
00076             return parent->buildCommand( container, request );
00077         }
00078     }
00079     if ( cursor->getPos() == 0 ) {
00080         SequenceElement* parent = static_cast<SequenceElement*>( getParent() );
00081         if ( parent ) {
00082             uint pos = parent->childPos( this );
00083             cursor->setTo( parent, pos );
00084             return parent->buildCommand( container, request );
00085         }
00086     }
00087 
00088     // We are in the middle of a token, so:
00089     // a) Cut from mark to the end
00090     // b) Create a new token and add an element from key pressed
00091     // c) Create a new token and add elements cut previously
00092     // d) Move cursor to parent so that it command execution works fine
00093 
00094     switch( *request ) {
00095     case req_addOperator: {
00096         KFCSplitToken* command = new KFCSplitToken( i18n("Add Operator"), container );
00097         OperatorRequest* opr = static_cast<OperatorRequest*>( request );
00098         OperatorElement* op = creationStrategy->createOperatorElement();
00099         TextElement* text = creationStrategy->createTextElement( opr->ch() );
00100         command->addCursor( cursor );
00101         command->addToken( op );
00102         command->addContent( op, text );
00103         SequenceElement* parent = static_cast< SequenceElement* >( getParent() );
00104         if ( parent ) {
00105             cursor->setTo( parent, parent->childPos( this ) + 1 );
00106         }
00107         return command;
00108     }
00109     case req_addNumber: {
00110         KFCSplitToken* command = new KFCSplitToken( i18n("Add Number"), container );
00111         NumberRequest* nr = static_cast<NumberRequest*>( request );
00112         NumberElement* num = creationStrategy->createNumberElement();
00113         TextElement* text = creationStrategy->createTextElement( nr->ch() );
00114         command->addCursor( cursor );
00115         command->addToken( num );
00116         command->addContent( num, text );
00117         SequenceElement* parent = static_cast< SequenceElement* >( getParent() );
00118         if ( parent ) {
00119             cursor->setTo( parent, parent->childPos( this ) + 1 );
00120         }
00121         return command;
00122     }
00123     case req_addEmptyBox:
00124     case req_addNameSequence:
00125     case req_addBracket:
00126     case req_addSpace:
00127     case req_addFraction:
00128     case req_addRoot:
00129     case req_addSymbol:
00130     case req_addOneByTwoMatrix:
00131     case req_addMatrix: {
00132         uint pos = static_cast<SequenceElement*>(getParent())->childPos( this );
00133         cursor->setTo( getParent(), pos + 1);
00134         return getParent()->buildCommand( container, request );
00135     }
00136     default:
00137         return SequenceElement::buildCommand( container, request );
00138     }
00139     return 0;
00140 }
00141 
00142 void IdentifierElement::setStyleVariant( StyleAttributes& style )
00143 {
00144     if ( customMathVariant() ) {
00145         style.setCustomMathVariant ( true );
00146         style.setCustomFontWeight( false );
00147         style.setCustomFontStyle( false );
00148         style.setCustomFont( false );
00149         if ( customMathVariant() ) {
00150             style.setCharFamily ( charFamily() );
00151             style.setCharStyle( charStyle() );
00152         }
00153         else {
00154             style.setCharFamily( style.charFamily() );
00155             style.setCharStyle( style.charStyle() );
00156         }
00157     }
00158     else {
00159         style.setCustomMathVariant( false );
00160         if ( customFontFamily() ) {
00161             style.setCustomFont( true );
00162             style.setFont( QFont(fontFamily()) );
00163         }
00164 
00165         bool fontweight = false;
00166         if ( customFontWeight() || style.customFontWeight() ) {
00167             style.setCustomFontWeight( true );
00168             if ( customFontWeight() ) {
00169                 fontweight = fontWeight();
00170             }
00171             else {
00172                 fontweight = style.customFontWeight();
00173             }
00174             style.setFontWeight( fontweight );
00175         }
00176         else {
00177             style.setCustomFontWeight( false );
00178         }
00179 
00180         bool fontstyle;
00181         if ( customFontStyle() ) {
00182             fontstyle = fontStyle();
00183         }
00184         else if ( countChildren() == 1 ) {
00185             fontstyle = true;
00186         }
00187         else {
00188             fontstyle = false;
00189         }
00190 
00191         if ( fontweight && fontstyle ) {
00192             style.setCharStyle( boldItalicChar );
00193         }
00194         else if ( fontweight && ! fontstyle ) {
00195             style.setCharStyle( boldChar );
00196         }
00197         else if ( ! fontweight && fontstyle ) {
00198             style.setCharStyle( italicChar );
00199         }
00200         else {
00201             style.setCharStyle( normalChar );
00202         }
00203     }
00204 }
00205 
00206 KFORMULA_NAMESPACE_END
KDE Home | KDE Accessibility Home | Description of Access Keys