mapeditor.cpp
changeset 139 087e60400acc
parent 136 b0d8b00fb937
child 143 56c57552f1d2
     1.1 --- a/mapeditor.cpp	Wed Jul 27 18:56:32 2005 +0000
     1.2 +++ b/mapeditor.cpp	Wed Jul 27 18:56:35 2005 +0000
     1.3 @@ -360,7 +360,8 @@
     1.4  	printFrame=true;
     1.5  	printFooter=true;
     1.6  
     1.7 -	blockreposition=false;
     1.8 +	blockReposition=false;
     1.9 +	blockSaveState=false;
    1.10  	isInteractive=interactive;
    1.11  	if (isInteractive)
    1.12  		// Create temporary files
    1.13 @@ -468,9 +469,9 @@
    1.14  	}
    1.15  }
    1.16  
    1.17 -bool MapEditor::blockReposition()
    1.18 +bool MapEditor::isRepositionBlocked()
    1.19  {
    1.20 -	return blockreposition;
    1.21 +	return blockReposition;
    1.22  }
    1.23  
    1.24  void MapEditor::makeTmpDirs()
    1.25 @@ -564,7 +565,7 @@
    1.26  	s+=settings.getXMLData (destPath);
    1.27  
    1.28  	// Save selection
    1.29 -	if (selection && !saveSelection) 
    1.30 +	if (selection && !saveSelection ) 
    1.31  		s+=valueElement("select",selection->getSelectString());
    1.32  
    1.33  	decIndent();
    1.34 @@ -577,29 +578,38 @@
    1.35  
    1.36  void MapEditor::saveState()
    1.37  {
    1.38 -	saveState (CompleteMap,NULL,"");
    1.39 +	saveState (CompleteMap,NULL,"","");
    1.40  }
    1.41  
    1.42  void MapEditor::saveState(LinkableMapObj *undoSel)
    1.43  {
    1.44 -	saveState (PartOfMap,undoSel,"");
    1.45 +	saveState (PartOfMap,undoSel,"","");
    1.46  }
    1.47  
    1.48 -void MapEditor::saveState(const QString & c)
    1.49 +void MapEditor::saveState(const QString & uc, const QString &rc)
    1.50  {
    1.51  	LinkableMapObj *undoSel;
    1.52  	if (selection)
    1.53  		undoSel=selection;
    1.54  	else
    1.55  		undoSel=NULL;
    1.56 -	saveState (UndoCommand,undoSel,c);
    1.57 +	saveState (UndoCommand,undoSel,uc,rc);
    1.58  }
    1.59  
    1.60 -void MapEditor::saveState(const SaveMode &savemode, LinkableMapObj *undoSel, const QString &undoCom)
    1.61 +void MapEditor::saveState(const SaveMode &savemode, LinkableMapObj *undoSel, const QString &undoCom, const QString &redoCom)
    1.62  {
    1.63 +	if (blockSaveState) return;
    1.64 +
    1.65  	setChanged();
    1.66  
    1.67 -	// Save what is selected now
    1.68 +	// Save current selection 
    1.69 +	redoCommand=redoCom;
    1.70 +	if (selection)
    1.71 +		redoSelection=selection->getSelectString();
    1.72 +	else	
    1.73 +		redoSelection="";
    1.74 +
    1.75 +	// Save the object, which should be undone
    1.76  	if (undoSel)
    1.77  		undoSelection=undoSel->getSelectString();
    1.78  	else
    1.79 @@ -619,10 +629,15 @@
    1.80  	{
    1.81  		undoCommand="undoMap ()";
    1.82  		backupXML=saveToDir (bakMapDir,mapName+"-",false, QPoint (),NULL);
    1.83 +		undoSelection="";
    1.84  	}
    1.85  	/* FIXME testing
    1.86  	cout << "ME::saveState()\n";
    1.87  	cout << "  undoCom="<<undoCommand<<endl;
    1.88 +	cout << "  undoSel="<<undoSelection<<endl;
    1.89 +	cout << "  ---------------------------"<<endl;
    1.90 +	cout << "  redoCom="<<redoCommand<<endl;
    1.91 +	cout << "  redoSel="<<redoSelection<<endl<<endl;
    1.92  	*/
    1.93  }
    1.94  
    1.95 @@ -644,8 +659,7 @@
    1.96  		moveBranchDown();
    1.97  	else if (com=="move")
    1.98  	{
    1.99 -		if (api.checkParamCount(2) &&
   1.100 -			selection )
   1.101 +		if (api.checkParamCount(2) && selection )
   1.102  		{	
   1.103  			x=api.parInt (ok,0);
   1.104  			if (ok)
   1.105 @@ -666,10 +680,15 @@
   1.106  				if (dst)
   1.107  				{	
   1.108  					if (typeid(*dst) == typeid(BranchObj) ) 
   1.109 -						((BranchObj*)(selection))->moveBranchTo ((BranchObj*)(dst),-1);
   1.110 -					if (typeid(*dst) == typeid(MapCenterObj) ) 
   1.111 +					{
   1.112 +						// Get number in parent
   1.113 +						x=api.parInt (ok,1);
   1.114 +						if (ok)
   1.115 +							((BranchObj*)(selection))->moveBranchTo ((BranchObj*)(dst),x);
   1.116 +					} else if (typeid(*dst) == typeid(MapCenterObj) ) 
   1.117  					{
   1.118  						((BranchObj*)(selection))->moveBranchTo ((BranchObj*)(dst),-1);
   1.119 +						// Get coordinates of mainbranch
   1.120  						x=api.parInt (ok,2);
   1.121  						if (ok)
   1.122  						{
   1.123 @@ -690,8 +709,10 @@
   1.124  	}	
   1.125  	// Internal commands, used for undo etc.	
   1.126  	else if (com==QString("undoMap"))
   1.127 -		undoXML("");
   1.128 -	else if (com==QString("undoPart"))
   1.129 +	{
   1.130 +		if (api.checkParamCount(0))
   1.131 +			undoXML("");
   1.132 +	} else if (com==QString("undoPart"))
   1.133  	{
   1.134  		if (api.checkParamCount(1))
   1.135  		{
   1.136 @@ -862,7 +883,7 @@
   1.137  		returnCode=1;	
   1.138  	} else
   1.139  	{
   1.140 -		blockreposition=true;
   1.141 +		blockReposition=true;
   1.142  		QXmlInputSource source( file);
   1.143  		QXmlSimpleReader reader;
   1.144  		reader.setContentHandler( &handler );
   1.145 @@ -871,7 +892,7 @@
   1.146  		handler.setTmpDir (filePath.left(filePath.findRev("/",-1)));	// needed to load files with rel. path
   1.147  		handler.setLoadMode (lmode);
   1.148  		bool ok = reader.parse( source );
   1.149 -		blockreposition=false;
   1.150 +		blockReposition=false;
   1.151  		file.close();
   1.152  		if ( ok ) 
   1.153  		{
   1.154 @@ -1308,6 +1329,8 @@
   1.155  	actionEditUndo->setEnabled (false);
   1.156  	mapChanged=false;
   1.157  
   1.158 +	if (!redoSelection.isEmpty())
   1.159 +		select (redoSelection);
   1.160  }
   1.161  
   1.162  void MapEditor::undoXML(const QString &undoSel)
   1.163 @@ -1327,6 +1350,7 @@
   1.164  		handler.setTmpDir ( bakMapDir );	// needed to load files with rel. path
   1.165  		if (undoSel.isEmpty())
   1.166  		{
   1.167 +			unselect();
   1.168  			mapCenter->clear();
   1.169  			handler.setLoadMode (NewMap);
   1.170  		} else	
   1.171 @@ -1334,9 +1358,11 @@
   1.172  			select (undoSel);
   1.173  			handler.setLoadMode (ImportReplace);
   1.174  		}	
   1.175 -		blockreposition=true;
   1.176 +		blockReposition=true;
   1.177 +		blockSaveState=true;
   1.178  		bool ok = reader.parse( source );
   1.179 -		blockreposition=false;
   1.180 +		blockReposition=false;
   1.181 +		blockSaveState=false;
   1.182  		if (! ok ) 
   1.183  		{	
   1.184  			// This should never ever happen
   1.185 @@ -1450,12 +1476,12 @@
   1.186  	BranchObj* par;
   1.187  	if (typeid(*selection) == typeid(BranchObj)  ) 
   1.188  	{
   1.189 -		saveState("moveBranchDown ()");
   1.190  		bo=(BranchObj*)(selection);
   1.191  		par=(BranchObj*)(bo->getParObj());
   1.192  		selection->unselect();
   1.193  		selection=par->moveBranchUp (bo);
   1.194  		selection->select();
   1.195 +		saveState("moveBranchDown ()",bo->getSelectString());
   1.196  		mapCenter->reposition();
   1.197  		ensureSelectionVisible();
   1.198  	}
   1.199 @@ -1470,12 +1496,12 @@
   1.200  	BranchObj* par;
   1.201  	if (typeid(*selection) == typeid(BranchObj)  ) 
   1.202  	{
   1.203 -		saveState("moveBranchUp ()");
   1.204  		bo=(BranchObj*)(selection);
   1.205  		par=(BranchObj*)(bo->getParObj());
   1.206  		selection->unselect(); 
   1.207  		selection=par->moveBranchDown(bo);
   1.208  		selection->select();
   1.209 +		saveState("moveBranchUp ()",bo->getSelectString());
   1.210  		mapCenter->reposition();
   1.211  		ensureSelectionVisible();
   1.212  	}	
   1.213 @@ -1490,10 +1516,10 @@
   1.214  		 (typeid(*selection) == typeid(BranchObj) || 
   1.215  		  typeid(*selection) == typeid(MapCenterObj) ) ) 
   1.216  	{
   1.217 -		saveState("setHeading (\""+((BranchObj*)(selection))->getHeading()+"\")");
   1.218 +		editingBO=(BranchObj*)(selection);
   1.219 +		saveState("setHeading (\""+((BranchObj*)(selection))->getHeading()+"\")",editingBO->getSelectString() );
   1.220  
   1.221  		ensureSelectionVisible();
   1.222 -		editingBO=(BranchObj*)(selection);
   1.223  		QPoint p = worldMatrix().map(QPoint (editingBO->x(),editingBO->y()));
   1.224  		lineedit->setGeometry(p.x()-contentsX(),p.y()-contentsY(),200,25);
   1.225  		QString s=editingBO->getHeading();
   1.226 @@ -1644,7 +1670,14 @@
   1.227  
   1.228  	if (selection  && typeid(*selection) ==typeid(BranchObj) ) 
   1.229  	{
   1.230 -		saveState(selection->getParObj());
   1.231 +		if (selection->getDepth()>1)
   1.232 +			// Normal branch, save parent with childs
   1.233 +			saveState(selection->getParObj());
   1.234 +		else
   1.235 +			// Mainbranch, save whole map
   1.236 +			// FIXME Better would be to insert mainbranch again at pos
   1.237 +			// But undoCommand is missing right now
   1.238 +			saveState();
   1.239  		BranchObj* bo=dynamic_cast <BranchObj*> (selection);
   1.240  		BranchObj* par=(BranchObj*)(bo->getParObj());
   1.241  		bo->unselect();
   1.242 @@ -1690,8 +1723,8 @@
   1.243  		adjustCanvasSize();
   1.244  		ensureSelectionVisible();
   1.245  		return true;
   1.246 -	} else
   1.247 -		return false;
   1.248 +	} 
   1.249 +	return false;
   1.250  }
   1.251  
   1.252  void MapEditor::unselect()
   1.253 @@ -2353,10 +2386,13 @@
   1.254  {
   1.255  	if (selection && (typeid(*selection) == typeid(BranchObj) ))
   1.256  	{		
   1.257 -		saveState(selection->getParObj());
   1.258 -		QString sel=selection->getSelectString();
   1.259  		BranchObj* bo=(BranchObj*)(selection);
   1.260  		BranchObj* par=(BranchObj*)(bo->getParObj());
   1.261 +		if (bo->getDepth()==1)
   1.262 +			saveState();
   1.263 +		else	
   1.264 +			saveState(selection->getParObj());	// FIXME undoCommand
   1.265 +		QString sel=selection->getSelectString();
   1.266  		unselect();
   1.267  		par->removeBranchHere(bo);
   1.268  		mapCenter->reposition();
   1.269 @@ -2412,9 +2448,9 @@
   1.270  	// Finally show dialog
   1.271  	if (dia.exec() == QDialog::Accepted)
   1.272  	{
   1.273 +		saveState();	//FIXME undoCommand
   1.274  		mapCenter->setAuthor (dia.getAuthor() );
   1.275  		mapCenter->setComment (dia.getComment() );
   1.276 -		saveState();	//FIXME undoCommand
   1.277  	}
   1.278  }
   1.279  
   1.280 @@ -2567,7 +2603,7 @@
   1.281  {
   1.282  	linkstyle=ls;
   1.283  
   1.284 -	saveState();
   1.285 +	saveState();	// FIXME undoCommand
   1.286  	BranchObj *bo;
   1.287  	bo=mapCenter->first();
   1.288  	bo=bo->next();
   1.289 @@ -3151,8 +3187,8 @@
   1.290  
   1.291  		if (typeid(*selection) == typeid(FloatImageObj))
   1.292  		{
   1.293 -			saveState("move "+qpointToString(movingObj_orgPos));
   1.294  			FloatObj *fo=(FloatObj*)(selection);
   1.295 +			saveState("move "+qpointToString(movingObj_orgPos),fo->getSelectString() );
   1.296  			if (fo->getLinkStyle()==StyleUndef) 
   1.297  			{
   1.298  				fo->setLinkStyle(fo->getDefLinkStyle());
   1.299 @@ -3203,7 +3239,7 @@
   1.300  				if (lmosel->getDepth()==1)
   1.301  				{
   1.302  					// depth==1, mainbranch
   1.303 -					saveState("move "+qpointToString(movingObj_orgPos));
   1.304 +					saveState("move "+qpointToString(movingObj_orgPos), lmosel->getSelectString() );
   1.305  					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
   1.306  				} else
   1.307  				{
   1.308 @@ -3348,30 +3384,31 @@
   1.309  			if (dst ) 
   1.310  			{
   1.311  				BranchObj* bs=((BranchObj*)(selection));
   1.312 -				saveState ("linkBranchToPos (\""+ 
   1.313 +				QString undoCom="linkBranchToPos (\""+ 
   1.314  					(bs->getParObj())->getSelectString()+
   1.315  					"\","+
   1.316  					QString("%1").arg(bs->getNum())+
   1.317  					","+
   1.318  					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+
   1.319 -					")");
   1.320 +					")";
   1.321  				// TODO we also could check, if dest and src are on same branch,
   1.322  				// then it would be sufficient to saveState of this branch
   1.323  
   1.324  				// Modifiers allow to insert above/below dst
   1.325  				if (e->state() & QMouseEvent::ShiftButton)
   1.326  				{
   1.327 -					bs->moveBranchTo ( (BranchObj*)(dst), ((BranchObj*)(dst))->getNum());
   1.328 +					bs->moveBranchTo ( (BranchObj*)(dst->getParObj()), ((BranchObj*)(dst))->getNum());
   1.329  				} else 
   1.330  				if (e->state() & QMouseEvent::ControlButton)
   1.331  				{
   1.332 -					bs->moveBranchTo ( (BranchObj*)(dst), ((BranchObj*)(dst))->getNum()+1);
   1.333 +					bs->moveBranchTo ( (BranchObj*)(dst->getParObj()), ((BranchObj*)(dst))->getNum()+1);
   1.334  				} else	
   1.335  				{
   1.336  					bs->moveBranchTo ((BranchObj*)(dst),-1);
   1.337  					if (dst->getDepth()==0) 
   1.338  						bs->move (savePos);
   1.339  				} 
   1.340 +				saveState (undoCom,bs->getSelectString() );
   1.341  			}	
   1.342  			// Draw the original link, before selection was moved around
   1.343  			mapCenter->reposition();