lib

numberelement.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 "numberelement.h"
00023 #include "kformuladefs.h"
00024 #include "textelement.h"
00025 #include "identifierelement.h"
00026 #include "operatorelement.h"
00027 #include "kformulacommand.h"
00028 #include "kformulacontainer.h"
00029 #include "formulaelement.h"
00030 #include "creationstrategy.h"
00031 
00032 KFORMULA_NAMESPACE_BEGIN
00033 
00034 NumberElement::NumberElement( BasicElement* parent ) : TokenElement( parent ) {}
00035 
00036 /*
00037  * Token elements' content has to be of homogeneous type. Every token element
00038  * must (TODO: check this) appear inside a non-token sequence, and thus, if
00039  * the command asks for a different content, a new element has to be created in
00040  * parent sequence.
00041  */
00042 KCommand* NumberElement::buildCommand( Container* container, Request* request )
00043 {
00044     FormulaCursor* cursor = container->activeCursor();
00045     if ( cursor->isReadOnly() ) {
00046         formula()->tell( i18n( "write protection" ) );
00047         return 0;
00048     }
00049 
00050     if ( *request == req_addNumber ) {
00051         KFCReplace* command = new KFCReplace( i18n("Add Number"), container );
00052         NumberRequest* nr = static_cast<NumberRequest*>( request );
00053         TextElement* element = creationStrategy->createTextElement( nr->ch(), false );
00054         command->addElement( element );
00055         return command;
00056     }
00057 
00058     if ( countChildren() == 0 || cursor->getPos() == countChildren() ) {
00059         // We are in the last position, so it's easy, call the parent to 
00060         // create a new child
00061         SequenceElement* parent = static_cast<SequenceElement*>( getParent() );
00062         if ( parent ) {
00063             uint pos = parent->childPos( this );
00064             cursor->setTo( parent, pos + 1);
00065             return parent->buildCommand( container, request );
00066         }
00067     }
00068     if ( cursor->getPos() == 0 ) {
00069         SequenceElement* parent = static_cast<SequenceElement*>( getParent() );
00070         if ( parent ) {
00071             uint pos = parent->childPos( this );
00072             cursor->setTo( parent, pos );
00073             return parent->buildCommand( container, request );
00074         }
00075     }
00076 
00077     // We are in the middle of a token, so:
00078     // a) Cut from mark to the end
00079     // b) Create a new token and add an element from key pressed
00080     // c) Create a new token and add elements cut previously
00081     // d) Move cursor to parent so that it command execution works fine
00082 
00083     switch( *request ) {
00084     case req_addTextChar: {
00085         KFCSplitToken* command = new KFCSplitToken( i18n("Add Text"), container );
00086         TextCharRequest* tr = static_cast<TextCharRequest*>( request );
00087         IdentifierElement* id = creationStrategy->createIdentifierElement();
00088         TextElement* text = creationStrategy->createTextElement( tr->ch() );
00089         command->addCursor( cursor );
00090         command->addToken( id );
00091         command->addContent( id, text );
00092         SequenceElement* parent = static_cast< SequenceElement* >( getParent() );
00093         if ( parent ) {
00094             cursor->setTo( parent, parent->childPos( this ) + 1 );
00095         }
00096         return command;
00097     }
00098 
00099     case req_addText: {
00100         KFCSplitToken* command = new KFCSplitToken( i18n("Add Text"), container );
00101         TextRequest* tr = static_cast<TextRequest*>( request );
00102         IdentifierElement* id = creationStrategy->createIdentifierElement();
00103         command->addCursor( cursor );
00104         command->addToken( id );
00105         for ( uint i = 0; i < tr->text().length(); i++ ) {
00106             TextElement* text = creationStrategy->createTextElement( tr->text()[i] );
00107             command->addContent( id, text );
00108         }
00109         SequenceElement* parent = static_cast< SequenceElement* >( getParent() );
00110         if ( parent ) {
00111             cursor->setTo( parent, parent->childPos( this ) + 1 );
00112         }
00113         return command;
00114     }
00115 
00116     case req_addOperator: {
00117         KFCSplitToken* command = new KFCSplitToken( i18n("Add Operator"), container );
00118         OperatorRequest* opr = static_cast<OperatorRequest*>( request );
00119         OperatorElement* op = creationStrategy->createOperatorElement();
00120         TextElement* text = creationStrategy->createTextElement( opr->ch() );
00121         command->addCursor( cursor );
00122         command->addToken( op );
00123         command->addContent( op, text );
00124         SequenceElement* parent = static_cast< SequenceElement* >( getParent() );
00125         if ( parent ) {
00126             cursor->setTo( parent, parent->childPos( this ) + 1 );
00127         }
00128         return command;
00129     }
00130     case req_addEmptyBox:
00131     case req_addNameSequence:
00132     case req_addBracket:
00133     case req_addSpace:
00134     case req_addFraction:
00135     case req_addRoot:
00136     case req_addSymbol:
00137     case req_addOneByTwoMatrix:
00138     case req_addMatrix: {
00139         SequenceElement* parent = static_cast<SequenceElement*>( getParent() );
00140         if ( parent ) {
00141             uint pos = parent->childPos( this );
00142             cursor->setTo( parent, pos + 1);
00143             return parent->buildCommand( container, request );
00144         }
00145     }
00146     default:
00147         return SequenceElement::buildCommand( container, request );
00148     }
00149     return 0;
00150 }
00151 
00152 
00153 KFORMULA_NAMESPACE_END
KDE Home | KDE Accessibility Home | Description of Access Keys