00001 /* This file is part of the KDE project 00002 Copyright (C) 2006 Martin Pfeiffer <hubipete@gmx.net> 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 "FormulaTool.h" 00021 #include "FormulaShape.h" 00022 #include "FormulaCursor.h" 00023 #include "BasicElement.h" 00024 #include <KoCanvasBase.h> 00025 #include <KoSelection.h> 00026 #include <KoShapeManager.h> 00027 #include <QKeyEvent> 00028 00029 namespace KFormula { 00030 00031 FormulaTool::FormulaTool( KoCanvasBase* canvas ) : KoTool( canvas ), 00032 m_formulaShape( 0 ), 00033 m_formulaCursor( 0 ) 00034 { 00035 } 00036 00037 FormulaTool::~FormulaTool() 00038 { 00039 if( m_formulaCursor ) 00040 delete m_formulaCursor; 00041 } 00042 00043 void FormulaTool::activate( bool temporary ) 00044 { 00045 Q_UNUSED(temporary); 00046 KoSelection* selection = m_canvas->shapeManager()->selection(); 00047 foreach( KoShape* shape, selection->selectedShapes().toList()) 00048 { 00049 m_formulaShape = dynamic_cast<FormulaShape*>( shape ); 00050 if( m_formulaShape ) 00051 break; 00052 } 00053 if( m_formulaShape == 0 ) // none found 00054 { 00055 emit sigDone(); 00056 return; 00057 } 00058 00059 m_formulaCursor = new FormulaCursor( m_formulaShape->formulaElement() ); 00060 } 00061 00062 void FormulaTool::deactivate() 00063 { 00064 m_formulaShape = 0; 00065 delete m_formulaCursor; 00066 m_formulaCursor = 0; 00067 } 00068 00069 void FormulaTool::paint( QPainter &painter, KoViewConverter &converter) 00070 { 00071 // TODO do view conversions with converter 00072 m_formulaCursor->paint( painter ); 00073 } 00074 00075 void FormulaTool::mousePressEvent( KoPointerEvent *event ) 00076 { 00077 // TODO implement the action and the elementAt method in FormulaShape 00078 // m_formulaCursor->setCursorTo( m_formulaShape->elementAt( ) ); 00079 // 00080 // 00081 // from the old FormulaCursor implementation 00082 /* 00083 FormulaElement* formula = getElement()->formula(); 00084 formula->goToPos( this, pos ); 00085 00086 setCursorToElement( m_container->childElementAt( pos ) ); 00087 if (flag & SelectMovement) { 00088 setSelection(true); 00089 if (getMark() == -1) { 00090 setMark(getPos()); 00091 } 00092 } 00093 else { 00094 setSelection(false); 00095 setMark(getPos()); 00096 } 00097 */ 00098 } 00099 00100 void FormulaTool::mouseDoubleClickEvent( KoPointerEvent *event ) 00101 { 00102 // TODO select whole element 00103 } 00104 00105 void FormulaTool::mouseMoveEvent( KoPointerEvent *event ) 00106 { 00107 // TODO find the old implementation and use it 00108 // 00109 // the old implementation 00110 /* setSelection(true); 00111 BasicElement* element = getElement(); 00112 int mark = getMark(); 00113 00114 FormulaElement* formula = getElement()->formula(); 00115 formula->goToPos( this, point ); 00116 BasicElement* newElement = getElement(); 00117 int pos = getPos(); 00118 00119 BasicElement* posChild = 0; 00120 BasicElement* markChild = 0; 00121 while (element != newElement) { 00122 posChild = newElement; 00123 newElement = newElement->getParent(); 00124 if (newElement == 0) { 00125 posChild = 0; 00126 newElement = getElement(); 00127 markChild = element; 00128 element = element->getParent(); 00129 } 00130 } 00131 00132 if (dynamic_cast<SequenceElement*>(element) == 0) { 00133 element = element->getParent(); 00134 element->selectChild(this, newElement); 00135 } 00136 else { 00137 if (posChild != 0) { 00138 element->selectChild(this, posChild); 00139 pos = getPos(); 00140 } 00141 if (markChild != 0) { 00142 element->selectChild(this, markChild); 00143 mark = getMark(); 00144 } 00145 if (pos == mark) { 00146 if ((posChild == 0) && (markChild != 0)) { 00147 mark++; 00148 } 00149 else if ((posChild != 0) && (markChild == 0)) { 00150 mark--; 00151 } 00152 } 00153 else if (pos < mark) { 00154 if (posChild != 0) { 00155 pos--; 00156 } 00157 } 00158 setTo(element, pos, mark); 00159 }*/ 00160 } 00161 00162 void FormulaTool::mouseReleaseEvent( KoPointerEvent *event ) 00163 { 00164 // TODO what should happen here ? 00165 } 00166 00167 void FormulaTool::keyPressEvent( QKeyEvent *event ) 00168 { 00169 m_formulaCursor->setWordMovement( event->modifiers() & Qt::ControlModifier ); 00170 m_formulaCursor->setSelecting( event->modifiers() & Qt::ShiftModifier ); 00171 00172 switch( event->key() ) // map key to movement or action 00173 { 00174 case Qt::Key_Backspace: 00175 remove( true ); 00176 break; 00177 case Qt::Key_Delete: 00178 remove( false ); 00179 break; 00180 case Qt::Key_Left: 00181 m_formulaCursor->moveLeft(); 00182 break; 00183 case Qt::Key_Up: 00184 m_formulaCursor->moveUp(); 00185 break; 00186 case Qt::Key_Right: 00187 m_formulaCursor->moveRight(); 00188 break; 00189 case Qt::Key_Down: 00190 m_formulaCursor->moveDown(); 00191 break; 00192 case Qt::Key_End: 00193 m_formulaCursor->moveEnd(); 00194 break; 00195 case Qt::Key_Home: 00196 m_formulaCursor->moveHome(); 00197 break; 00198 /* default: 00199 if( event->text().length() == 0 ) 00200 return; 00201 insertText( event->text() );*/ 00202 } 00203 00204 event->accept(); 00205 } 00206 00207 void FormulaTool::keyReleaseEvent( QKeyEvent *event ) 00208 { 00209 event->accept(); 00210 } 00211 00212 void FormulaTool::remove( bool backSpace ) 00213 { 00214 if( m_formulaCursor->hasSelection() ) // remove the selection 00215 { 00216 // TODO set the cursor according to backSpace 00217 // m_formulaCursor->setCursorTo( ); 00218 } 00219 else // remove only the current element 00220 { 00221 // m_formulaCursor->currentElement()->parentElement()->removeChild( m_currentElement ); 00222 } 00223 } 00224 00225 } // namespace KFormula 00226