mapeditor.cpp
changeset 393 053b8645e3e9
parent 390 0e1aeb21cb78
child 394 67cfa6e6b863
     1.1 --- a/mapeditor.cpp	Mon Oct 16 12:42:54 2006 +0000
     1.2 +++ b/mapeditor.cpp	Wed Oct 18 10:45:00 2006 +0000
     1.3 @@ -372,7 +372,7 @@
     1.4  		mapCenter);
     1.5  }
     1.6  
     1.7 -void MapEditor::saveStatePart(LinkableMapObj *undoSel, const QString &comment)
     1.8 +void MapEditor::saveStateChangingPart(LinkableMapObj *undoSel, const QString &comment)
     1.9  {
    1.10  	// save the selected part of the map, Undo will replace part of map 
    1.11  	QString undoSelection="";
    1.12 @@ -385,6 +385,25 @@
    1.13  		undoSel);
    1.14  }
    1.15  
    1.16 +void MapEditor::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
    1.17 +{
    1.18 +	if (!redoSel  ||typeid(*redoSel) != typeid(BranchObj) ) 
    1.19 +	{
    1.20 +		qWarning ("MapEditor::saveStateRemovingPart  no undoSel given!");
    1.21 +		return;
    1.22 +	}
    1.23 +
    1.24 +	// save the selected part of the map, Undo will insert part of map 
    1.25 +	QString undoSelection=redoSel->getParObj()->getSelectString();
    1.26 +	QString redoSelection=redoSel->getSelectString();
    1.27 +
    1.28 +	saveState (PartOfMap,
    1.29 +		undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
    1.30 +		redoSelection, "delete ()", 
    1.31 +		comment, 
    1.32 +		redoSel);
    1.33 +}
    1.34 +
    1.35  void MapEditor::saveStateConstSelection(const QString &uc, const QString &rc, const QString &comment)
    1.36  {
    1.37  	// selection does not change during action,
    1.38 @@ -401,22 +420,6 @@
    1.39  		NULL);
    1.40  }
    1.41  
    1.42 -/* FIXME not used
    1.43 -void MapEditor::saveStateComData(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment, LinkableMapObj *saveSel) 
    1.44 -{
    1.45 -	QString redoSelection="";
    1.46 -	if (redoSel) redoSelection=redoSel->getSelectString();
    1.47 -	QString undoSelection="";
    1.48 -	if (undoSel) undoSelection=undoSel->getSelectString();
    1.49 -
    1.50 -	saveState (UndoCommand,
    1.51 -		undoSelection, uc,
    1.52 -		redoSelection, "FIXME-redoCom",	//FIXME
    1.53 -		comment, 
    1.54 -		saveSel);
    1.55 -}
    1.56 -*/
    1.57 -
    1.58  void MapEditor::saveState(LinkableMapObj *undoSel, const QString &uc, LinkableMapObj *redoSel, const QString &rc, const QString &comment) 
    1.59  {
    1.60  	// "Normal" savestate: save commands, selections and comment
    1.61 @@ -491,7 +494,8 @@
    1.62  	}	
    1.63  	else if (savemode==PartOfMap )
    1.64  	{
    1.65 -		undoCommand="undoPart (\""+ undoSelection+"\",\""+bakMapPath+"\")";
    1.66 +		undoCommand=undoCom;
    1.67 +		undoCommand.replace ("PATH",bakMapPath);
    1.68  	} else
    1.69  	{
    1.70  		undoCommand="undoMap (\""+bakMapPath+"\")";
    1.71 @@ -501,7 +505,7 @@
    1.72  		saveStringToDisk (QString(bakMapPath),backupXML);
    1.73  
    1.74  	// We would have to save all actions in a tree, to keep track of 
    1.75 -	// possible redos after a action. Too complex, forget about redos.
    1.76 +	// possible redos after a action. Possible, but we are too lazy: forget about redos.
    1.77  	redosAvail=0;
    1.78  
    1.79  	// Write the current state to disk
    1.80 @@ -545,51 +549,38 @@
    1.81  	QString com=api.command();
    1.82  	
    1.83  	// External commands
    1.84 -	if (com=="moveBranchUp")
    1.85 -		moveBranchUp();
    1.86 -	else if (com=="moveBranchDown")
    1.87 -		moveBranchDown();
    1.88 -	else if (com=="move")
    1.89 -	{
    1.90 -		if (api.checkParamCount(2) && selection )
    1.91 -		{	
    1.92 -			x=api.parInt (ok,0);
    1.93 -			if (ok)
    1.94 -			{
    1.95 -				y=api.parInt (ok,1);
    1.96 -				if (ok) move (x,y);
    1.97 -			}
    1.98 -		}	
    1.99 -	}
   1.100 -	else if (com=="moveRel")
   1.101 -	{
   1.102 -		if (api.checkParamCount(2) && selection )
   1.103 -		{	
   1.104 -			x=api.parInt (ok,0);
   1.105 -			if (ok)
   1.106 -			{
   1.107 -				y=api.parInt (ok,1);
   1.108 -				if (ok) moveRel (x,y);
   1.109 -			}
   1.110 -		}	
   1.111 -	}
   1.112 -	else if (com=="delete")
   1.113 -	{
   1.114 -		if (api.checkParamCount(1) && selection )
   1.115 -		{	
   1.116 -			s=api.parString(ok,0);
   1.117 -			if (ok &&select (s)) deleteSelection();
   1.118 -		}	
   1.119 -	}	
   1.120 -	else if (com=="addBranch")
   1.121 +	if (com=="addBranch")
   1.122  	{
   1.123  		if (api.checkParamCount(1) && selection )
   1.124  		{	
   1.125  			y=api.parInt (ok,0);
   1.126  			if (ok ) addNewBranchInt (y);
   1.127  		}	
   1.128 -	}	
   1.129 -	else if (com=="linkBranchToPos")
   1.130 +	} else if (com==QString("addMapReplace"))
   1.131 +	{
   1.132 +		if (api.checkParamCount(2))
   1.133 +		{
   1.134 +			s=api.parString (ok,0);	// selection
   1.135 +			t=api.parString (ok,1);	// path to map
   1.136 +			if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t);
   1.137 +			addMapReplace(s,t);	
   1.138 +		}
   1.139 +	} else if (com==QString("addMapInsert"))
   1.140 +	{
   1.141 +		if (api.checkParamCount(2))
   1.142 +		{
   1.143 +			t=api.parString (ok,0);	// path to map
   1.144 +			y=api.parInt(ok,1);		// position
   1.145 +			if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t);
   1.146 +			addMapInsert(t,y);	
   1.147 +		}
   1.148 +	} else if (com=="delete")
   1.149 +	{
   1.150 +		if (api.checkParamCount(0) && selection )
   1.151 +		{	
   1.152 +			deleteSelection();
   1.153 +		}	
   1.154 +	} else if (com=="linkBranchToPos")
   1.155  	{
   1.156  		if (selection && typeid(*selection) == typeid(BranchObj) ) 
   1.157  		{
   1.158 @@ -622,6 +613,33 @@
   1.159  				}	
   1.160  			}
   1.161  		}
   1.162 +	} else if (com=="moveBranchUp")
   1.163 +		moveBranchUp();
   1.164 +	else if (com=="moveBranchDown")
   1.165 +		moveBranchDown();
   1.166 +	else if (com=="move")
   1.167 +	{
   1.168 +		if (api.checkParamCount(2) && selection )
   1.169 +		{	
   1.170 +			x=api.parInt (ok,0);
   1.171 +			if (ok)
   1.172 +			{
   1.173 +				y=api.parInt (ok,1);
   1.174 +				if (ok) move (x,y);
   1.175 +			}
   1.176 +		}	
   1.177 +	}
   1.178 +	else if (com=="moveRel")
   1.179 +	{
   1.180 +		if (api.checkParamCount(2) && selection )
   1.181 +		{	
   1.182 +			x=api.parInt (ok,0);
   1.183 +			if (ok)
   1.184 +			{
   1.185 +				y=api.parInt (ok,1);
   1.186 +				if (ok) moveRel (x,y);
   1.187 +			}
   1.188 +		}	
   1.189  	} else if (com=="setHeading")
   1.190  	{
   1.191  		if (api.checkParamCount(1))
   1.192 @@ -676,22 +694,11 @@
   1.193  				}	
   1.194  			}	
   1.195  		}
   1.196 -	}	
   1.197 -	// Internal commands, used for undo etc.	
   1.198 -	else if (com==QString("undoMap"))
   1.199 +	// Internal commands
   1.200 +	} else if (com==QString("undoMap"))
   1.201  	{
   1.202  		if (api.checkParamCount(1))
   1.203 -			undoXML("",api.parString (ok,0));
   1.204 -	} else if (com==QString("undoPart"))
   1.205 -	{
   1.206 -		if (api.checkParamCount(2))
   1.207 -		{
   1.208 -			s=api.parString (ok,0);	// undo selection
   1.209 -			t=api.parString (ok,1);	// backup dir
   1.210 -			if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t);
   1.211 -			cout <<t.ascii()<<endl;
   1.212 -			undoXML(s,t);	
   1.213 -		}
   1.214 +			addMapReplace("",api.parString (ok,0));
   1.215  	} else if (com=="select")
   1.216  	{
   1.217  		if (api.checkParamCount(1))
   1.218 @@ -824,7 +831,7 @@
   1.219  		// (map state is set later at end of load...)
   1.220  	} else
   1.221  	{
   1.222 -		saveStatePart(selection,"Load map");
   1.223 +		saveStateChangingPart(selection,"Load map");
   1.224  	}	
   1.225  	
   1.226      
   1.227 @@ -1279,7 +1286,7 @@
   1.228  
   1.229  bool MapEditor::isRedoAvailable()
   1.230  {
   1.231 -	if (undoSet.readNumEntry("/history/undosAvail",0)>0)
   1.232 +	if (undoSet.readNumEntry("/history/redosAvail",0)>0)
   1.233  		return true;
   1.234  	else	
   1.235  		return false;
   1.236 @@ -1381,11 +1388,11 @@
   1.237  	// And ignore clicking the current row ;-)	
   1.238  }
   1.239  
   1.240 -void MapEditor::undoXML(const QString &undoSel, const QString &bakMapPath)
   1.241 +void MapEditor::addMapReplace(const QString &undoSel, const QString &path)
   1.242  {
   1.243 -	QString bakMapDir=bakMapPath.left(bakMapPath.findRev("/"));
   1.244 -	QDir d(bakMapDir);
   1.245 -	QFile file (bakMapPath);
   1.246 +	QString pathDir=path.left(path.findRev("/"));
   1.247 +	QDir d(pathDir);
   1.248 +	QFile file (path);
   1.249  
   1.250  	if (d.exists() )
   1.251  	{
   1.252 @@ -1396,7 +1403,7 @@
   1.253  		reader.setContentHandler( &handler );
   1.254  		reader.setErrorHandler( &handler );
   1.255  		handler.setMapEditor( this );
   1.256 -		handler.setTmpDir ( bakMapDir );	// needed to load files with rel. path
   1.257 +		handler.setTmpDir ( pathDir );	// needed to load files with rel. path
   1.258  		if (undoSel.isEmpty())
   1.259  		{
   1.260  			unselect();
   1.261 @@ -1413,18 +1420,59 @@
   1.262  		if (! ok ) 
   1.263  		{	
   1.264  			// This should never ever happen
   1.265 -			QMessageBox::critical( 0, tr( "Critical Parse Error by reading backupFile" ),
   1.266 -								   tr( handler.errorProtocol() )+" in "+bakMapDir );
   1.267 +			QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
   1.268 +								    handler.errorProtocol());
   1.269  		}
   1.270  	} else	
   1.271 +		QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
   1.272 +}
   1.273 +
   1.274 +void MapEditor::addMapInsert(const QString &path, int pos)
   1.275 +{
   1.276 +	if (selection && (typeid(*selection) == typeid(BranchObj) ||
   1.277 +				      typeid(*selection) == typeid(MapCenterObj))) 
   1.278  	{
   1.279 -		QMessageBox::critical( 0, tr( "Critical Error" ),
   1.280 -			tr("Temporary directory %1 used for undo is gone. \n"
   1.281 -			"I will create a new one, but at the moment no undo is available.\n"
   1.282 -			"Maybe you want to reload your original data.\n\n"
   1.283 -			"Sorry for any inconveniences.").arg(bakMapDir) );
   1.284 -		makeTmpDirs();	
   1.285 -	}							
   1.286 +		QString pathDir=path.left(path.findRev("/"));
   1.287 +		QDir d(pathDir);
   1.288 +		QFile file (path);
   1.289 +
   1.290 +		BranchObj *bo=addNewBranchInt (pos);
   1.291 +		if (!bo)
   1.292 +		{
   1.293 +			
   1.294 +			QMessageBox::critical( 0, tr( "Critical Error" ), 
   1.295 +				tr("Could insert branch at position %1\n in branch %2").arg(pos)
   1.296 +				.arg(((BranchObj*)selection)->getHeading()));
   1.297 +			return;	
   1.298 +		}
   1.299 +		unselect();
   1.300 +		selection=bo;
   1.301 +		selection->select();
   1.302 +
   1.303 +		
   1.304 +		if (d.exists() )
   1.305 +		{
   1.306 +			// We need to parse saved XML data
   1.307 +			mapBuilderHandler handler;
   1.308 +			QXmlInputSource source( file);
   1.309 +			QXmlSimpleReader reader;
   1.310 +			reader.setContentHandler( &handler );
   1.311 +			reader.setErrorHandler( &handler );
   1.312 +			handler.setMapEditor( this );
   1.313 +			handler.setTmpDir ( pathDir );	// needed to load files with rel. path
   1.314 +			handler.setLoadMode (ImportReplace);
   1.315 +			blockReposition=true;
   1.316 +			bool ok = reader.parse( source );
   1.317 +			blockReposition=false;
   1.318 +			if (! ok ) 
   1.319 +			{	
   1.320 +				// This should never ever happen
   1.321 +				QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
   1.322 +										handler.errorProtocol());
   1.323 +			}
   1.324 +		} else	
   1.325 +			QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
   1.326 +	}		
   1.327  }
   1.328  
   1.329  void MapEditor::pasteNoSave()
   1.330 @@ -1443,7 +1491,7 @@
   1.331  	if (selection && (typeid(*selection) == typeid(BranchObj) ||
   1.332  				      typeid(*selection) == typeid(MapCenterObj))) 
   1.333  	{
   1.334 -		saveStatePart(selection,QString("Paste to %1").arg( getName(selection)));
   1.335 +		saveStateChangingPart(selection,QString("Paste to %1").arg( getName(selection)));
   1.336  		pasteNoSave();
   1.337  		mapCenter->reposition();
   1.338  		adjustCanvasSize();
   1.339 @@ -1452,7 +1500,7 @@
   1.340  
   1.341  void MapEditor::cut()
   1.342  {
   1.343 -	saveStatePart(selection->getParObj(),QString("Cut %1").arg(getName(selection)));
   1.344 +	saveStateChangingPart(selection->getParObj(),QString("Cut %1").arg(getName(selection)));
   1.345  	copy();
   1.346  	cutNoSave();
   1.347  	mapCenter->reposition();
   1.348 @@ -1658,7 +1706,8 @@
   1.349  		if (newbo)
   1.350  		{
   1.351  			saveStateConstSelection (
   1.352 -				QString ("delete (\"%1\")").arg(newbo->getSelectString()),
   1.353 +				//QString ("delete (\"%1\")").arg(newbo->getSelectString()),
   1.354 +				"delete ()",
   1.355  				QString ("addBranch (%1)").arg(pos-2),
   1.356  				QString ("Add new branch to %1").arg(getName(bo)));	//TODO undoCommand
   1.357  
   1.358 @@ -1695,7 +1744,7 @@
   1.359  		 (typeid(*selection) == typeid(BranchObj) ) )
   1.360  	{
   1.361  		BranchObj* bo1 = (BranchObj*) selection;
   1.362 -		saveStatePart(selection, QString("Add new branch here").arg(getName(bo1)));
   1.363 +		saveStateChangingPart(selection, QString("Add new branch here").arg(getName(bo1)));
   1.364  
   1.365  		bool wasScrolled=false;
   1.366  		BranchObj *newbo=NULL;
   1.367 @@ -1739,12 +1788,10 @@
   1.368  		bo->unselect();
   1.369  		if (selection->getDepth()>1)
   1.370  			// Normal branch, save parent with childs
   1.371 -			saveStatePart(par,QString("Delete %1").arg(getName(bo)));
   1.372 -		//	naveState (UndoCom, par, QString("insertBranchAt (%1)", bo, "", QString ("Delete %1").arg(getName(bo))));
   1.373 +			saveStateRemovingPart (bo, QString ("Delete %1").arg(getName(bo)));
   1.374  		else
   1.375  			// Mainbranch, save whole map
   1.376 -			// TODO Better would be to insert mainbranch again at pos
   1.377 -			// But undoCommand is missing right now
   1.378 +			// TODO Better would be to insert mainbranch again at pos But undoCommand is missing right now
   1.379  			saveStateComplete(QString("Delete %1").arg(getName(bo)));
   1.380  		selection=NULL;
   1.381  		par->removeBranch(bo);
   1.382 @@ -1758,7 +1805,7 @@
   1.383  	{
   1.384  		FloatImageObj* fio=(FloatImageObj*)selection;
   1.385  		BranchObj* par=(BranchObj*)(fio->getParObj());
   1.386 -		saveStatePart(par, QString("Delete %1").arg(getName(fio)));
   1.387 +		saveStateChangingPart(par, QString("Delete %1").arg(getName(fio)));
   1.388  		fio->unselect();
   1.389  		selection=NULL;
   1.390  		par->removeFloatImage(fio);
   1.391 @@ -2226,7 +2273,7 @@
   1.392  			typeid(*selection) == typeid(MapCenterObj))
   1.393  		{
   1.394  			BranchObj *bo=(BranchObj*)selection;
   1.395 -			saveStatePart(selection, QString("Set color of %1 to %2").arg(getName(bo)).arg(actColor.name()));	//TODO undoCommand
   1.396 +			saveStateChangingPart(selection, QString("Set color of %1 to %2").arg(getName(bo)).arg(actColor.name()));	//TODO undoCommand
   1.397  			bo->setColor(actColor); // color branch
   1.398  		}    
   1.399  	}
   1.400 @@ -2240,7 +2287,7 @@
   1.401  			typeid(*selection) == typeid(MapCenterObj))
   1.402  		{
   1.403  			BranchObj *bo=(BranchObj*)selection;
   1.404 -			saveStatePart(selection, QString ("Set color of %1 and childs to %2").arg(getName(bo)).arg(actColor.name()));	//TODO undoCommand
   1.405 +			saveStateChangingPart(selection, QString ("Set color of %1 and childs to %2").arg(getName(bo)).arg(actColor.name()));	//TODO undoCommand
   1.406  			bo->setColorChilds(actColor); // color links, color childs
   1.407  		}    
   1.408  	}
   1.409 @@ -2541,7 +2588,7 @@
   1.410  		if (bo->getDepth()==1)
   1.411  			saveStateComplete(s);
   1.412  		else	
   1.413 -			saveStatePart(selection->getParObj(),s);	// TODO undoCommand
   1.414 +			saveStateChangingPart(selection->getParObj(),s);	// TODO undoCommand
   1.415  		QString sel=selection->getSelectString();
   1.416  		unselect();
   1.417  		par->removeBranchHere(bo);
   1.418 @@ -2554,7 +2601,7 @@
   1.419  {
   1.420  	if (selection && (typeid(*selection) == typeid(BranchObj) ))
   1.421  	{		
   1.422 -		saveStatePart(selection->getParObj(), QString("Remove childs of branch %1").arg(getName(selection)));
   1.423 +		saveStateChangingPart(selection->getParObj(), QString("Remove childs of branch %1").arg(getName(selection)));
   1.424  		((BranchObj*)selection)->removeChilds();
   1.425  		mapCenter->reposition();
   1.426  	}	
   1.427 @@ -2729,7 +2776,7 @@
   1.428  			s="Unscroll";
   1.429  		else	
   1.430  			s="Scroll";
   1.431 -		saveStatePart(selection, QString ("%1 %2").arg(s).arg(getName(bo)));
   1.432 +		saveStateChangingPart(selection, QString ("%1 %2").arg(s).arg(getName(bo)));
   1.433  		bo->toggleScroll();
   1.434  		adjustCanvasSize();
   1.435  		canvas()->update();
   1.436 @@ -2769,7 +2816,7 @@
   1.437  		QString fn;
   1.438  		if ( fd->exec() == QDialog::Accepted )
   1.439  		{
   1.440 -			saveStatePart(selection, QString("Add floatimage to %1").arg(getName(selection)));
   1.441 +			saveStateChangingPart(selection, QString("Add floatimage to %1").arg(getName(selection)));
   1.442  			// FIXME in QT4 use:	lastImageDir=fd->directory();
   1.443  			lastImageDir=QDir (fd->dirPath());
   1.444  			QStringList flist = fd->selectedFiles();
   1.445 @@ -3728,7 +3775,7 @@
   1.446        (typeid(*selection) == typeid(MapCenterObj))  )
   1.447    {
   1.448      BranchObj *bo=((BranchObj*)selection);
   1.449 -    saveStatePart(selection,QString("Add floatimage to %1").arg(getName(bo)));
   1.450 +    saveStateChangingPart(selection,QString("Add floatimage to %1").arg(getName(bo)));
   1.451      //QString fn=fd->selectedFile();
   1.452      //lastImageDir=fn.left(fn.findRev ("/"));
   1.453      bo->addFloatImage();