00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <klocale.h>
00022
00023 #include <qvaluelist.h>
00024
00025 #include "formulacursor.h"
00026 #include "formulaelement.h"
00027 #include "indexelement.h"
00028 #include "kformulacommand.h"
00029 #include "matrixelement.h"
00030 #include "sequenceelement.h"
00031 #include "textelement.h"
00032
00033
00034 KFORMULA_NAMESPACE_BEGIN
00035
00036 int PlainCommand::evilDestructionCount = 0;
00037
00038 PlainCommand::PlainCommand( const QString& name )
00039 : KNamedCommand(name)
00040 {
00041 evilDestructionCount++;
00042 }
00043
00044 PlainCommand::~PlainCommand()
00045 {
00046 evilDestructionCount--;
00047 }
00048
00049 Command::Command(const QString &name, Container* document)
00050 : PlainCommand(name), cursordata(0), undocursor(0), doc(document)
00051 {
00052 }
00053
00054 Command::~Command()
00055 {
00056 delete undocursor;
00057 delete cursordata;
00058 }
00059
00060 FormulaCursor* Command::getExecuteCursor()
00061 {
00062 FormulaCursor* cursor = getActiveCursor();
00063 if (cursordata == 0) {
00064 setExecuteCursor(getActiveCursor());
00065 }
00066 else {
00067 cursor->setCursorData(cursordata);
00068 }
00069 return cursor;
00070 }
00071
00072 void Command::setExecuteCursor(FormulaCursor* cursor)
00073 {
00074
00075 cursordata = cursor->getCursorData();
00076 }
00077
00078 FormulaCursor* Command::getUnexecuteCursor()
00079 {
00080 FormulaCursor* cursor = getActiveCursor();
00081 cursor->setCursorData(undocursor);
00082 destroyUndoCursor();
00083 return cursor;
00084 }
00085
00086 void Command::setUnexecuteCursor(FormulaCursor* cursor)
00087 {
00088
00089 undocursor = cursor->getCursorData();
00090 }
00091
00092
00093
00094
00095 KFCAdd::KFCAdd(const QString &name, Container *document)
00096 : Command(name, document)
00097 {
00098 addList.setAutoDelete( true );
00099 }
00100
00101
00102 void KFCAdd::execute()
00103 {
00104 FormulaCursor* cursor = getExecuteCursor();
00105 cursor->insert(addList, beforeCursor);
00106 setUnexecuteCursor(cursor);
00107 cursor->setSelection(false);
00108 testDirty();
00109 }
00110
00111
00112 void KFCAdd::unexecute()
00113 {
00114 FormulaCursor* cursor = getUnexecuteCursor();
00115 cursor->remove(addList, beforeCursor);
00116
00117 cursor->normalize();
00118 testDirty();
00119 }
00120
00121
00122
00123
00124
00125 KFCRemoveSelection::KFCRemoveSelection(Container *document,
00126 Direction direction)
00127 : Command(i18n("Remove Selected Text"), document),
00128 dir(direction)
00129 {
00130 removedList.setAutoDelete( true );
00131 }
00132
00133 void KFCRemoveSelection::execute()
00134 {
00135 FormulaCursor* cursor = getExecuteCursor();
00136 cursor->remove(removedList, dir);
00137 setUnexecuteCursor(cursor);
00138 testDirty();
00139 }
00140
00141 void KFCRemoveSelection::unexecute()
00142 {
00143 FormulaCursor* cursor = getUnexecuteCursor();
00144 cursor->insert(removedList);
00145 cursor->setSelection(false);
00146 testDirty();
00147 }
00148
00149
00150
00151 KFCReplace::KFCReplace(const QString &name, Container* document)
00152 : KFCAdd(name, document), removeSelection(0)
00153 {
00154 }
00155
00156 KFCReplace::~KFCReplace()
00157 {
00158 delete removeSelection;
00159 }
00160
00161 void KFCReplace::execute()
00162 {
00163 if (getActiveCursor()->isSelection() && (removeSelection == 0)) {
00164 removeSelection = new KFCRemoveSelection(getDocument());
00165 }
00166 if (removeSelection != 0) {
00167 removeSelection->execute();
00168 }
00169 KFCAdd::execute();
00170 }
00171
00172 void KFCReplace::unexecute()
00173 {
00174 KFCAdd::unexecute();
00175 if (removeSelection != 0) {
00176 removeSelection->unexecute();
00177 }
00178 }
00179
00180
00181
00182 KFCRemove::KFCRemove(Container *document,
00183 Direction direction)
00184 : Command(i18n("Remove Selected Text"), document),
00185 element(0), simpleRemoveCursor(0), dir(direction)
00186 {
00187 removedList.setAutoDelete( true );
00188 }
00189
00190 KFCRemove::~KFCRemove()
00191 {
00192 delete simpleRemoveCursor;
00193 delete element;
00194 }
00195
00196 void KFCRemove::execute()
00197 {
00198 FormulaCursor* cursor = getExecuteCursor();
00199 cursor->remove(removedList, dir);
00200 if (cursor->elementIsSenseless()) {
00201 simpleRemoveCursor = cursor->getCursorData();
00202 element = cursor->replaceByMainChildContent();
00203 }
00204 setUnexecuteCursor(cursor);
00205 cursor->normalize( dir );
00206 testDirty();
00207 }
00208
00209 void KFCRemove::unexecute()
00210 {
00211 FormulaCursor* cursor = getUnexecuteCursor();
00212 if (element != 0) {
00213 cursor->replaceSelectionWith(element);
00214 element = 0;
00215
00216 cursor->setCursorData(simpleRemoveCursor);
00217 delete simpleRemoveCursor;
00218 simpleRemoveCursor = 0;
00219 }
00220 cursor->insert(removedList, dir);
00221 cursor->setSelection(false);
00222 testDirty();
00223 }
00224
00225
00226 KFCRemoveEnclosing::KFCRemoveEnclosing(Container* document,
00227 Direction dir)
00228 : Command(i18n("Remove Enclosing Element"), document),
00229 element(0), direction(dir)
00230 {
00231 }
00232
00233 KFCRemoveEnclosing::~KFCRemoveEnclosing()
00234 {
00235 delete element;
00236 }
00237
00238 void KFCRemoveEnclosing::execute()
00239 {
00240 FormulaCursor* cursor = getExecuteCursor();
00241 element = cursor->removeEnclosingElement(direction);
00242 setUnexecuteCursor(cursor);
00243
00244 cursor->setSelection(false);
00245 testDirty();
00246 }
00247
00248 void KFCRemoveEnclosing::unexecute()
00249 {
00250 FormulaCursor* cursor = getUnexecuteCursor();
00251 cursor->replaceSelectionWith(element);
00252 cursor->normalize();
00253 cursor->setSelection(false);
00254 element = 0;
00255 testDirty();
00256 }
00257
00258
00259
00260
00261 KFCAddReplacing::KFCAddReplacing(const QString &name, Container* document)
00262 : Command(name, document), element(0)
00263 {
00264 }
00265
00266 KFCAddReplacing::~KFCAddReplacing()
00267 {
00268 delete element;
00269 }
00270
00271
00272 void KFCAddReplacing::execute()
00273 {
00274 FormulaCursor* cursor = getExecuteCursor();
00275 cursor->replaceSelectionWith(element);
00276 setUnexecuteCursor(cursor);
00277 cursor->goInsideElement(element);
00278 element = 0;
00279 testDirty();
00280 }
00281
00282
00283 void KFCAddReplacing::unexecute()
00284 {
00285 FormulaCursor* cursor = getUnexecuteCursor();
00286 element = cursor->replaceByMainChildContent();
00287 cursor->normalize();
00288 testDirty();
00289 }
00290
00291
00292
00293
00294 KFCAddGenericIndex::KFCAddGenericIndex(Container* document, ElementIndexPtr _index)
00295 : KFCAdd(i18n("Add Index"), document), index(_index)
00296 {
00297 addElement(new SequenceElement());
00298 }
00299
00300 void KFCAddGenericIndex::execute()
00301 {
00302 index->setToIndex(getActiveCursor());
00303 KFCAdd::execute();
00304 }
00305
00306
00307 KFCAddIndex::KFCAddIndex(Container* document,
00308 IndexElement* element, ElementIndexPtr index)
00309 : KFCAddReplacing(i18n("Add Index"), document),
00310 addIndex(document, index)
00311 {
00312 setElement(element);
00313 }
00314
00315 KFCAddIndex::~KFCAddIndex()
00316 {
00317 }
00318
00319 void KFCAddIndex::execute()
00320 {
00321 KFCAddReplacing::execute();
00322 addIndex.execute();
00323 }
00324
00325 void KFCAddIndex::unexecute()
00326 {
00327 addIndex.unexecute();
00328 KFCAddReplacing::unexecute();
00329 }
00330
00331
00332 KFCChangeBaseSize::KFCChangeBaseSize( const QString& name, Container* document,
00333 FormulaElement* formula, int size )
00334 : PlainCommand( name ), m_document( document ), m_formula( formula ), m_size( size )
00335 {
00336 m_oldSize = formula->getBaseSize();
00337 }
00338
00339 void KFCChangeBaseSize::execute()
00340 {
00341 m_formula->setBaseSize( m_size );
00342 m_document->recalc();
00343 }
00344
00345 void KFCChangeBaseSize::unexecute()
00346 {
00347 m_formula->setBaseSize( m_oldSize );
00348 m_document->recalc();
00349 }
00350
00351
00352 FontCommand::FontCommand( const QString& name, Container* document )
00353 : Command( name, document )
00354 {
00355 list.setAutoDelete( false );
00356 elementList.setAutoDelete( false );
00357 }
00358
00359
00360 void FontCommand::collectChildren()
00361 {
00362 list.clear();
00363 uint count = elementList.count();
00364 for ( uint i=0; i<count; ++i ) {
00365 elementList.at( i )->dispatchFontCommand( this );
00366 }
00367 }
00368
00369
00370 void FontCommand::parseSequences( const QMap<SequenceElement*, int>& parents )
00371 {
00372 QValueList<SequenceElement*> sequences = parents.keys();
00373 for ( QValueList<SequenceElement*>::iterator i = sequences.begin();
00374 i != sequences.end();
00375 ++i ) {
00376 ( *i )->parse();
00377 }
00378 }
00379
00380
00381 CharStyleCommand::CharStyleCommand( CharStyle cs, const QString& name, Container* document )
00382 : FontCommand( name, document ), charStyle( cs )
00383 {
00384 }
00385
00386 void CharStyleCommand::execute()
00387 {
00388 collectChildren();
00389 QMap<SequenceElement*, int> parentCollector;
00390
00391 styleList.clear();
00392 uint count = childrenList().count();
00393 styleList.reserve( count );
00394 for ( uint i=0; i<count; ++i ) {
00395 TextElement* child = childrenList().at( i );
00396 styleList[i] = child->getCharStyle();
00397 child->setCharStyle( charStyle );
00398 parentCollector[static_cast<SequenceElement*>( child->getParent() )] = 1;
00399 }
00400 parseSequences( parentCollector );
00401 testDirty();
00402 }
00403
00404 void CharStyleCommand::unexecute()
00405 {
00406 QMap<SequenceElement*, int> parentCollector;
00407 uint count = childrenList().count();
00408
00409 for ( uint i=0; i<count; ++i ) {
00410 TextElement* child = childrenList().at( i );
00411 child->setCharStyle( styleList[i] );
00412 parentCollector[static_cast<SequenceElement*>( child->getParent() )] = 1;
00413 }
00414 parseSequences( parentCollector );
00415 testDirty();
00416 }
00417
00418
00419 CharFamilyCommand::CharFamilyCommand( CharFamily cf, const QString& name, Container* document )
00420 : FontCommand( name, document ), charFamily( cf )
00421 {
00422 }
00423
00424 void CharFamilyCommand::execute()
00425 {
00426 collectChildren();
00427
00428 QMap<SequenceElement*, int> parentCollector;
00429
00430 familyList.clear();
00431 uint count = childrenList().count();
00432 familyList.reserve( count );
00433 for ( uint i=0; i<count; ++i ) {
00434 TextElement* child = childrenList().at( i );
00435 familyList[i] = child->getCharFamily();
00436 child->setCharFamily( charFamily );
00437 parentCollector[static_cast<SequenceElement*>( child->getParent() )] = 1;
00438 }
00439 parseSequences( parentCollector );
00440 testDirty();
00441 }
00442
00443 void CharFamilyCommand::unexecute()
00444 {
00445 QMap<SequenceElement*, int> parentCollector;
00446 uint count = childrenList().count();
00447
00448 for ( uint i=0; i<count; ++i ) {
00449 TextElement* child = childrenList().at( i );
00450 child->setCharFamily( familyList[i] );
00451 parentCollector[static_cast<SequenceElement*>( child->getParent() )] = 1;
00452 }
00453 parseSequences( parentCollector );
00454 testDirty();
00455 }
00456
00457
00458 KFORMULA_NAMESPACE_END