kformula/flake

FixedElement.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2009 Jeremias Epperlein <jeeree@web.de>
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 "FixedElement.h"
00021 #include "FormulaCursor.h"
00022 #include <KoXmlWriter.h>
00023 #include <KoXmlReader.h>
00024 #include <QPainter>
00025 
00026 #include <kdebug.h>
00027 
00028 FixedElement::FixedElement( BasicElement* parent ) : BasicElement( parent )
00029 {
00030 }
00031 
00032 FixedElement::~FixedElement()
00033 {
00034 }
00035 
00036 
00037 BasicElement* FixedElement::elementAfter ( int position ) const
00038 {
00039     if (position % 2 == 0) {
00040         return elementNext(position);
00041     } else {
00042         return 0;
00043     }
00044 }
00045 
00046 BasicElement* FixedElement::elementBefore ( int position ) const
00047 {
00048     if (position % 2 == 1) {
00049         return elementNext(position);
00050     } else {
00051         return 0;
00052     }
00053 }
00054 
00055 BasicElement* FixedElement::elementNext ( int position ) const
00056 {
00057         return childElements()[position/2];
00058 }
00059 
00060 
00061 QPainterPath FixedElement::selectionRegion(const int pos1, const int pos2) const 
00062 {
00063     QPainterPath temp;
00064     Q_UNUSED(pos1);
00065     Q_UNUSED(pos2);
00066     return temp;
00067 }
00068 
00069 bool FixedElement::moveHorSituation(FormulaCursor& newcursor, FormulaCursor& oldcursor, int pos1, int pos2) {
00070     if ((newcursor.position()/2==pos1 && newcursor.direction()==MoveUp) ||
00071         (newcursor.position()/2==pos2 && newcursor.direction()==MoveDown) ||
00072         (newcursor.position()==2*pos1 && newcursor.direction()==MoveLeft) ||
00073         (newcursor.position()==2*pos2+1 && newcursor.direction()==MoveRight) ) {
00074         return false;
00075     }
00076     switch (newcursor.direction()) {
00077     case MoveLeft:
00078         if (newcursor.position()==2*pos2+1) {
00079             newcursor.moveTo(newcursor.currentElement()->childElements()[pos2]);
00080         } else {
00081             newcursor.moveTo(newcursor.currentElement()->childElements()[pos1]);
00082         }
00083         break;
00084     case MoveRight:
00085         if (newcursor.position()==2*pos1) {
00086             newcursor.moveTo(newcursor.currentElement()->childElements()[pos1]);
00087         } else {
00088             newcursor.moveTo(newcursor.currentElement()->childElements()[pos2]);
00089         }
00090         break;
00091     case MoveUp:
00092     case MoveDown:
00093         return newcursor.moveCloseTo(childElements()[newcursor.direction()==MoveUp ? pos1 : pos2],oldcursor);
00094     case NoDirection:
00095         break;
00096     }
00097     return true;
00098 }
00099 
00100 bool FixedElement::moveVertSituation(FormulaCursor& newcursor, FormulaCursor& oldcursor, int pos1, int pos2) {
00101     if ((newcursor.position()/2==pos1 && newcursor.direction()==MoveUp) ||
00102         (newcursor.position()/2==pos2 && newcursor.direction()==MoveDown) ||
00103         (newcursor.position()%2==0 && newcursor.direction()==MoveLeft) ||
00104         (newcursor.position()%2==1 && newcursor.direction()==MoveRight) ) {
00105         return false;
00106     }
00107     switch (newcursor.direction()) {
00108     case MoveLeft:
00109     case MoveRight:
00110         if (newcursor.position()/2==pos1) {
00111             newcursor.moveTo(newcursor.currentElement()->childElements()[pos1]);
00112         } else {
00113             newcursor.moveTo(newcursor.currentElement()->childElements()[pos2]);
00114         }
00115         break;
00116     case MoveUp:
00117     case MoveDown:
00118         return newcursor.moveCloseTo(childElements()[newcursor.direction()==MoveUp ? pos1 : pos2],oldcursor);
00119     case NoDirection:
00120         break;
00121     }
00122     return true;
00123 }
00124 
00125 bool FixedElement::moveSingleSituation ( FormulaCursor& newcursor, FormulaCursor& oldcursor, int pos )
00126 {
00127     Q_UNUSED( oldcursor )
00128     switch (newcursor.direction()) {
00129     case MoveLeft:
00130         if (newcursor.position()%2==1) {
00131             newcursor.moveTo(newcursor.currentElement()->childElements()[pos]);
00132             break;
00133         }
00134         return false;
00135     case MoveRight:
00136         if (newcursor.position()%2==0) {
00137             newcursor.moveTo(newcursor.currentElement()->childElements()[pos]);
00138             break;
00139         }
00140     case MoveUp:
00141     case MoveDown:
00142         return false;
00143     case NoDirection:
00144         break;
00145     }
00146     return true;
00147 }
00148 
00149 
00150 bool FixedElement::acceptCursor ( const FormulaCursor& cursor )
00151 {
00152     Q_UNUSED (cursor)
00153     return false;
00154 }
00155 
00156 QLineF FixedElement::cursorLine ( int position ) const
00157 {
00158     QRectF tmp;
00159     if (position%2==1) {
00160         tmp=elementBefore(position)->absoluteBoundingRect();
00161         return QLineF(tmp.topRight(),tmp.bottomRight());
00162     } else {
00163         tmp=elementAfter(position)->absoluteBoundingRect();
00164         return QLineF(tmp.topLeft(),tmp.bottomLeft());
00165     }
00166 }
00167 
00168 int FixedElement::positionOfChild ( BasicElement* child ) const
00169 {
00170     int tmp=childElements().indexOf(child);
00171     if (tmp==-1) {
00172         return -1;
00173     } else {
00174         return 2*tmp;
00175     }
00176 }
00177 
00178 bool FixedElement::loadElement ( KoXmlElement& tmp, RowElement** child )
00179 {
00180     BasicElement *element;
00181     element = ElementFactory::createElement( tmp.tagName(), this );
00182     if( !element->readMathML( tmp ) ) {
00183         return false;
00184     }
00185     if (element->elementType()==Row) {
00186         delete (*child);
00187         (*child)=static_cast<RowElement*>(element);
00188     } else {
00189         (*child)->insertChild(0,element);
00190     }
00191     return true;
00192 }
00193 
00194 
00195 int FixedElement::endPosition() const
00196 {
00197     return childElements().length()*2-1;
00198 }