diff -r 18f824bd3070 -r 053b8645e3e9 mapeditor.cpp --- a/mapeditor.cpp Mon Oct 16 12:42:54 2006 +0000 +++ b/mapeditor.cpp Wed Oct 18 10:45:00 2006 +0000 @@ -372,7 +372,7 @@ mapCenter); } -void MapEditor::saveStatePart(LinkableMapObj *undoSel, const QString &comment) +void MapEditor::saveStateChangingPart(LinkableMapObj *undoSel, const QString &comment) { // save the selected part of the map, Undo will replace part of map QString undoSelection=""; @@ -385,6 +385,25 @@ undoSel); } +void MapEditor::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment) +{ + if (!redoSel ||typeid(*redoSel) != typeid(BranchObj) ) + { + qWarning ("MapEditor::saveStateRemovingPart no undoSel given!"); + return; + } + + // save the selected part of the map, Undo will insert part of map + QString undoSelection=redoSel->getParObj()->getSelectString(); + QString redoSelection=redoSel->getSelectString(); + + saveState (PartOfMap, + undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()), + redoSelection, "delete ()", + comment, + redoSel); +} + void MapEditor::saveStateConstSelection(const QString &uc, const QString &rc, const QString &comment) { // selection does not change during action, @@ -401,22 +420,6 @@ NULL); } -/* FIXME not used -void MapEditor::saveStateComData(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment, LinkableMapObj *saveSel) -{ - QString redoSelection=""; - if (redoSel) redoSelection=redoSel->getSelectString(); - QString undoSelection=""; - if (undoSel) undoSelection=undoSel->getSelectString(); - - saveState (UndoCommand, - undoSelection, uc, - redoSelection, "FIXME-redoCom", //FIXME - comment, - saveSel); -} -*/ - void MapEditor::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment) { // "Normal" savestate: save commands, selections and comment @@ -491,7 +494,8 @@ } else if (savemode==PartOfMap ) { - undoCommand="undoPart (\""+ undoSelection+"\",\""+bakMapPath+"\")"; + undoCommand=undoCom; + undoCommand.replace ("PATH",bakMapPath); } else { undoCommand="undoMap (\""+bakMapPath+"\")"; @@ -501,7 +505,7 @@ saveStringToDisk (QString(bakMapPath),backupXML); // We would have to save all actions in a tree, to keep track of - // possible redos after a action. Too complex, forget about redos. + // possible redos after a action. Possible, but we are too lazy: forget about redos. redosAvail=0; // Write the current state to disk @@ -545,51 +549,38 @@ QString com=api.command(); // External commands - if (com=="moveBranchUp") - moveBranchUp(); - else if (com=="moveBranchDown") - moveBranchDown(); - else if (com=="move") - { - if (api.checkParamCount(2) && selection ) - { - x=api.parInt (ok,0); - if (ok) - { - y=api.parInt (ok,1); - if (ok) move (x,y); - } - } - } - else if (com=="moveRel") - { - if (api.checkParamCount(2) && selection ) - { - x=api.parInt (ok,0); - if (ok) - { - y=api.parInt (ok,1); - if (ok) moveRel (x,y); - } - } - } - else if (com=="delete") - { - if (api.checkParamCount(1) && selection ) - { - s=api.parString(ok,0); - if (ok &&select (s)) deleteSelection(); - } - } - else if (com=="addBranch") + if (com=="addBranch") { if (api.checkParamCount(1) && selection ) { y=api.parInt (ok,0); if (ok ) addNewBranchInt (y); } - } - else if (com=="linkBranchToPos") + } else if (com==QString("addMapReplace")) + { + if (api.checkParamCount(2)) + { + s=api.parString (ok,0); // selection + t=api.parString (ok,1); // path to map + if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t); + addMapReplace(s,t); + } + } else if (com==QString("addMapInsert")) + { + if (api.checkParamCount(2)) + { + t=api.parString (ok,0); // path to map + y=api.parInt(ok,1); // position + if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t); + addMapInsert(t,y); + } + } else if (com=="delete") + { + if (api.checkParamCount(0) && selection ) + { + deleteSelection(); + } + } else if (com=="linkBranchToPos") { if (selection && typeid(*selection) == typeid(BranchObj) ) { @@ -622,6 +613,33 @@ } } } + } else if (com=="moveBranchUp") + moveBranchUp(); + else if (com=="moveBranchDown") + moveBranchDown(); + else if (com=="move") + { + if (api.checkParamCount(2) && selection ) + { + x=api.parInt (ok,0); + if (ok) + { + y=api.parInt (ok,1); + if (ok) move (x,y); + } + } + } + else if (com=="moveRel") + { + if (api.checkParamCount(2) && selection ) + { + x=api.parInt (ok,0); + if (ok) + { + y=api.parInt (ok,1); + if (ok) moveRel (x,y); + } + } } else if (com=="setHeading") { if (api.checkParamCount(1)) @@ -676,22 +694,11 @@ } } } - } - // Internal commands, used for undo etc. - else if (com==QString("undoMap")) + // Internal commands + } else if (com==QString("undoMap")) { if (api.checkParamCount(1)) - undoXML("",api.parString (ok,0)); - } else if (com==QString("undoPart")) - { - if (api.checkParamCount(2)) - { - s=api.parString (ok,0); // undo selection - t=api.parString (ok,1); // backup dir - if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t); - cout <0) + if (undoSet.readNumEntry("/history/redosAvail",0)>0) return true; else return false; @@ -1381,11 +1388,11 @@ // And ignore clicking the current row ;-) } -void MapEditor::undoXML(const QString &undoSel, const QString &bakMapPath) +void MapEditor::addMapReplace(const QString &undoSel, const QString &path) { - QString bakMapDir=bakMapPath.left(bakMapPath.findRev("/")); - QDir d(bakMapDir); - QFile file (bakMapPath); + QString pathDir=path.left(path.findRev("/")); + QDir d(pathDir); + QFile file (path); if (d.exists() ) { @@ -1396,7 +1403,7 @@ reader.setContentHandler( &handler ); reader.setErrorHandler( &handler ); handler.setMapEditor( this ); - handler.setTmpDir ( bakMapDir ); // needed to load files with rel. path + handler.setTmpDir ( pathDir ); // needed to load files with rel. path if (undoSel.isEmpty()) { unselect(); @@ -1413,18 +1420,59 @@ if (! ok ) { // This should never ever happen - QMessageBox::critical( 0, tr( "Critical Parse Error by reading backupFile" ), - tr( handler.errorProtocol() )+" in "+bakMapDir ); + QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path), + handler.errorProtocol()); } } else + QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path)); +} + +void MapEditor::addMapInsert(const QString &path, int pos) +{ + if (selection && (typeid(*selection) == typeid(BranchObj) || + typeid(*selection) == typeid(MapCenterObj))) { - QMessageBox::critical( 0, tr( "Critical Error" ), - tr("Temporary directory %1 used for undo is gone. \n" - "I will create a new one, but at the moment no undo is available.\n" - "Maybe you want to reload your original data.\n\n" - "Sorry for any inconveniences.").arg(bakMapDir) ); - makeTmpDirs(); - } + QString pathDir=path.left(path.findRev("/")); + QDir d(pathDir); + QFile file (path); + + BranchObj *bo=addNewBranchInt (pos); + if (!bo) + { + + QMessageBox::critical( 0, tr( "Critical Error" ), + tr("Could insert branch at position %1\n in branch %2").arg(pos) + .arg(((BranchObj*)selection)->getHeading())); + return; + } + unselect(); + selection=bo; + selection->select(); + + + if (d.exists() ) + { + // We need to parse saved XML data + mapBuilderHandler handler; + QXmlInputSource source( file); + QXmlSimpleReader reader; + reader.setContentHandler( &handler ); + reader.setErrorHandler( &handler ); + handler.setMapEditor( this ); + handler.setTmpDir ( pathDir ); // needed to load files with rel. path + handler.setLoadMode (ImportReplace); + blockReposition=true; + bool ok = reader.parse( source ); + blockReposition=false; + if (! ok ) + { + // This should never ever happen + QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path), + handler.errorProtocol()); + } + } else + QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path)); + } } void MapEditor::pasteNoSave() @@ -1443,7 +1491,7 @@ if (selection && (typeid(*selection) == typeid(BranchObj) || typeid(*selection) == typeid(MapCenterObj))) { - saveStatePart(selection,QString("Paste to %1").arg( getName(selection))); + saveStateChangingPart(selection,QString("Paste to %1").arg( getName(selection))); pasteNoSave(); mapCenter->reposition(); adjustCanvasSize(); @@ -1452,7 +1500,7 @@ void MapEditor::cut() { - saveStatePart(selection->getParObj(),QString("Cut %1").arg(getName(selection))); + saveStateChangingPart(selection->getParObj(),QString("Cut %1").arg(getName(selection))); copy(); cutNoSave(); mapCenter->reposition(); @@ -1658,7 +1706,8 @@ if (newbo) { saveStateConstSelection ( - QString ("delete (\"%1\")").arg(newbo->getSelectString()), + //QString ("delete (\"%1\")").arg(newbo->getSelectString()), + "delete ()", QString ("addBranch (%1)").arg(pos-2), QString ("Add new branch to %1").arg(getName(bo))); //TODO undoCommand @@ -1695,7 +1744,7 @@ (typeid(*selection) == typeid(BranchObj) ) ) { BranchObj* bo1 = (BranchObj*) selection; - saveStatePart(selection, QString("Add new branch here").arg(getName(bo1))); + saveStateChangingPart(selection, QString("Add new branch here").arg(getName(bo1))); bool wasScrolled=false; BranchObj *newbo=NULL; @@ -1739,12 +1788,10 @@ bo->unselect(); if (selection->getDepth()>1) // Normal branch, save parent with childs - saveStatePart(par,QString("Delete %1").arg(getName(bo))); - // naveState (UndoCom, par, QString("insertBranchAt (%1)", bo, "", QString ("Delete %1").arg(getName(bo)))); + saveStateRemovingPart (bo, QString ("Delete %1").arg(getName(bo))); else // Mainbranch, save whole map - // TODO Better would be to insert mainbranch again at pos - // But undoCommand is missing right now + // TODO Better would be to insert mainbranch again at pos But undoCommand is missing right now saveStateComplete(QString("Delete %1").arg(getName(bo))); selection=NULL; par->removeBranch(bo); @@ -1758,7 +1805,7 @@ { FloatImageObj* fio=(FloatImageObj*)selection; BranchObj* par=(BranchObj*)(fio->getParObj()); - saveStatePart(par, QString("Delete %1").arg(getName(fio))); + saveStateChangingPart(par, QString("Delete %1").arg(getName(fio))); fio->unselect(); selection=NULL; par->removeFloatImage(fio); @@ -2226,7 +2273,7 @@ typeid(*selection) == typeid(MapCenterObj)) { BranchObj *bo=(BranchObj*)selection; - saveStatePart(selection, QString("Set color of %1 to %2").arg(getName(bo)).arg(actColor.name())); //TODO undoCommand + saveStateChangingPart(selection, QString("Set color of %1 to %2").arg(getName(bo)).arg(actColor.name())); //TODO undoCommand bo->setColor(actColor); // color branch } } @@ -2240,7 +2287,7 @@ typeid(*selection) == typeid(MapCenterObj)) { BranchObj *bo=(BranchObj*)selection; - saveStatePart(selection, QString ("Set color of %1 and childs to %2").arg(getName(bo)).arg(actColor.name())); //TODO undoCommand + saveStateChangingPart(selection, QString ("Set color of %1 and childs to %2").arg(getName(bo)).arg(actColor.name())); //TODO undoCommand bo->setColorChilds(actColor); // color links, color childs } } @@ -2541,7 +2588,7 @@ if (bo->getDepth()==1) saveStateComplete(s); else - saveStatePart(selection->getParObj(),s); // TODO undoCommand + saveStateChangingPart(selection->getParObj(),s); // TODO undoCommand QString sel=selection->getSelectString(); unselect(); par->removeBranchHere(bo); @@ -2554,7 +2601,7 @@ { if (selection && (typeid(*selection) == typeid(BranchObj) )) { - saveStatePart(selection->getParObj(), QString("Remove childs of branch %1").arg(getName(selection))); + saveStateChangingPart(selection->getParObj(), QString("Remove childs of branch %1").arg(getName(selection))); ((BranchObj*)selection)->removeChilds(); mapCenter->reposition(); } @@ -2729,7 +2776,7 @@ s="Unscroll"; else s="Scroll"; - saveStatePart(selection, QString ("%1 %2").arg(s).arg(getName(bo))); + saveStateChangingPart(selection, QString ("%1 %2").arg(s).arg(getName(bo))); bo->toggleScroll(); adjustCanvasSize(); canvas()->update(); @@ -2769,7 +2816,7 @@ QString fn; if ( fd->exec() == QDialog::Accepted ) { - saveStatePart(selection, QString("Add floatimage to %1").arg(getName(selection))); + saveStateChangingPart(selection, QString("Add floatimage to %1").arg(getName(selection))); // FIXME in QT4 use: lastImageDir=fd->directory(); lastImageDir=QDir (fd->dirPath()); QStringList flist = fd->selectedFiles(); @@ -3728,7 +3775,7 @@ (typeid(*selection) == typeid(MapCenterObj)) ) { BranchObj *bo=((BranchObj*)selection); - saveStatePart(selection,QString("Add floatimage to %1").arg(getName(bo))); + saveStateChangingPart(selection,QString("Add floatimage to %1").arg(getName(bo))); //QString fn=fd->selectedFile(); //lastImageDir=fn.left(fn.findRev ("/")); bo->addFloatImage();