mapeditor.cpp
changeset 394 67cfa6e6b863
parent 393 053b8645e3e9
child 395 7ced3733ba60
     1.1 --- a/mapeditor.cpp	Wed Oct 18 10:45:00 2006 +0000
     1.2 +++ b/mapeditor.cpp	Tue Oct 24 15:36:38 2006 +0000
     1.3 @@ -387,21 +387,33 @@
     1.4  
     1.5  void MapEditor::saveStateRemovingPart(LinkableMapObj *redoSel, const QString &comment)
     1.6  {
     1.7 -	if (!redoSel  ||typeid(*redoSel) != typeid(BranchObj) ) 
     1.8 +	if (!redoSel)
     1.9  	{
    1.10 -		qWarning ("MapEditor::saveStateRemovingPart  no undoSel given!");
    1.11 +		qWarning ("MapEditor::saveStateRemovingPart  no redoSel given!");
    1.12  		return;
    1.13  	}
    1.14 -
    1.15 -	// save the selected part of the map, Undo will insert part of map 
    1.16  	QString undoSelection=redoSel->getParObj()->getSelectString();
    1.17  	QString redoSelection=redoSel->getSelectString();
    1.18 -
    1.19 -	saveState (PartOfMap,
    1.20 -		undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
    1.21 -		redoSelection, "delete ()", 
    1.22 -		comment, 
    1.23 -		redoSel);
    1.24 +	if (typeid(*redoSel) == typeid(BranchObj) && redoSel->getDepth()>1 ) 
    1.25 +	{
    1.26 +		// save the selected branch of the map, Undo will insert part of map 
    1.27 +		saveState (PartOfMap,
    1.28 +			undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
    1.29 +			redoSelection, "delete ()", 
    1.30 +			comment, 
    1.31 +			redoSel);
    1.32 +	} else if (typeid(*redoSel) == typeid(BranchObj) )
    1.33 +	{
    1.34 +		// save the selected mainbranch of the map, Undo will insert part of map 
    1.35 +		saveState (PartOfMap,
    1.36 +			undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(((BranchObj*)redoSel)->getNum()),
    1.37 +//			undoSelection, QString("addMapInsert (\"PATH\",%1,%2)").arg(((BranchObj*)redoSel)->getNum())
    1.38 +//				.arg(((BranchObj*)redoSel)->x()).arg(((BranchObj*)redoSel)->y()),
    1.39 +			redoSelection, "delete ()", 
    1.40 +			comment, 
    1.41 +			redoSel);
    1.42 +		
    1.43 +	}
    1.44  }
    1.45  
    1.46  void MapEditor::saveStateConstSelection(const QString &uc, const QString &rc, const QString &comment)
    1.47 @@ -545,81 +557,185 @@
    1.48  	bool ok;
    1.49  
    1.50  	// Split string s into command and parameters
    1.51 -	api.parseCommand (atom);
    1.52 +	api.parseInput (atom);
    1.53  	QString com=api.command();
    1.54  	
    1.55  	// External commands
    1.56  	if (com=="addBranch")
    1.57  	{
    1.58 -		if (api.checkParamCount(1) && selection )
    1.59 +		if (!selection)
    1.60 +		{
    1.61 +			api.setError (Aborted,"Nothing selected");
    1.62 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
    1.63 +					 typeid(*selection) != typeid(MapCenterObj)) )
    1.64 +		{				  
    1.65 +			api.setError (Aborted,"Type of selection is not a branch");
    1.66 +		} else 
    1.67  		{	
    1.68 -			y=api.parInt (ok,0);
    1.69 -			if (ok ) addNewBranchInt (y);
    1.70 -		}	
    1.71 +			QList <int> pl;
    1.72 +			pl << 0 <<1;
    1.73 +			if (api.checkParamCount(pl))
    1.74 +			{
    1.75 +				if (api.paramCount()==0)
    1.76 +					addNewBranchInt (-2);
    1.77 +				else
    1.78 +				{
    1.79 +					y=api.parInt (ok,0);
    1.80 +					if (ok ) addNewBranchInt (y);
    1.81 +				}
    1.82 +			}
    1.83 +		}
    1.84 +	} else if (com=="addBranchBefore")
    1.85 +	{
    1.86 +		if (!selection)
    1.87 +		{
    1.88 +			api.setError (Aborted,"Nothing selected");
    1.89 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
    1.90 +					 typeid(*selection) != typeid(MapCenterObj)) )
    1.91 +		{				  
    1.92 +			api.setError (Aborted,"Type of selection is not a branch");
    1.93 +		} else 
    1.94 +		{	
    1.95 +			if (api.paramCount()==0)
    1.96 +			{
    1.97 +				addNewBranchBefore ();
    1.98 +			}	
    1.99 +		}
   1.100  	} else if (com==QString("addMapReplace"))
   1.101  	{
   1.102 -		if (api.checkParamCount(2))
   1.103 +		if (!selection)
   1.104 +		{
   1.105 +			api.setError (Aborted,"Nothing selected");
   1.106 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
   1.107 +					 typeid(*selection) != typeid(MapCenterObj)) )
   1.108 +		{				  
   1.109 +			api.setError (Aborted,"Type of selection is not a branch");
   1.110 +		} else if (api.checkParamCount(2))
   1.111  		{
   1.112  			s=api.parString (ok,0);	// selection
   1.113  			t=api.parString (ok,1);	// path to map
   1.114  			if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t);
   1.115 -			addMapReplace(s,t);	
   1.116 +			addMapReplaceInt(s,t);	
   1.117  		}
   1.118  	} else if (com==QString("addMapInsert"))
   1.119  	{
   1.120 -		if (api.checkParamCount(2))
   1.121 +		if (!selection)
   1.122  		{
   1.123 -			t=api.parString (ok,0);	// path to map
   1.124 -			y=api.parInt(ok,1);		// position
   1.125 -			if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t);
   1.126 -			addMapInsert(t,y);	
   1.127 +			api.setError (Aborted,"Nothing selected");
   1.128 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
   1.129 +					 typeid(*selection) != typeid(MapCenterObj)) )
   1.130 +		{				  
   1.131 +			api.setError (Aborted,"Type of selection is not a branch");
   1.132 +		} else 
   1.133 +		{	
   1.134 +			if (api.checkParamCount(2))
   1.135 +			{
   1.136 +				t=api.parString (ok,0);	// path to map
   1.137 +				y=api.parInt(ok,1);		// position
   1.138 +				if (QDir::isRelativePath(t)) t=QDir::convertSeparators (tmpMapDir + "/"+t);
   1.139 +				addMapInsertInt(t,y);	
   1.140 +			}
   1.141  		}
   1.142  	} else if (com=="delete")
   1.143  	{
   1.144 -		if (api.checkParamCount(0) && selection )
   1.145 +		if (!selection)
   1.146 +		{
   1.147 +			api.setError (Aborted,"Nothing selected");
   1.148 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
   1.149 +					 typeid(*selection) != typeid(MapCenterObj)) )
   1.150 +		{				  
   1.151 +			api.setError (Aborted,"Type of selection is not a branch");
   1.152 +		} else if (api.checkParamCount(0))
   1.153  		{	
   1.154  			deleteSelection();
   1.155  		}	
   1.156 +	} else if (com=="deleteKeepChilds")
   1.157 +	{
   1.158 +		if (!selection)
   1.159 +		{
   1.160 +			api.setError (Aborted,"Nothing selected");
   1.161 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
   1.162 +					 typeid(*selection) != typeid(MapCenterObj)) )
   1.163 +		{				  
   1.164 +			api.setError (Aborted,"Type of selection is not a branch");
   1.165 +		} else if (api.checkParamCount(0))
   1.166 +		{	
   1.167 +			removeBranchKeepChilds();
   1.168 +		}	
   1.169  	} else if (com=="linkBranchToPos")
   1.170  	{
   1.171 -		if (selection && typeid(*selection) == typeid(BranchObj) ) 
   1.172 +		if (!selection)
   1.173  		{
   1.174 -			if (api.checkParamCount(4))
   1.175 -			{
   1.176 -				// 0	selectstring of parent
   1.177 -				// 1	num in parent (for branches)
   1.178 -				// 2,3	x,y of mainbranch or mapcenter
   1.179 -				s=api.parString(ok,0);
   1.180 -				LinkableMapObj *dst=mapCenter->findObjBySelect (s);
   1.181 -				if (dst)
   1.182 -				{	
   1.183 -					if (typeid(*dst) == typeid(BranchObj) ) 
   1.184 +			api.setError (Aborted,"Nothing selected");
   1.185 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
   1.186 +					 typeid(*selection) != typeid(MapCenterObj)) )
   1.187 +		{				  
   1.188 +			api.setError (Aborted,"Type of selection is not a branch");
   1.189 +		} else if (api.checkParamCount(4))
   1.190 +		{
   1.191 +			// 0	selectstring of parent
   1.192 +			// 1	num in parent (for branches)
   1.193 +			// 2,3	x,y of mainbranch or mapcenter
   1.194 +			s=api.parString(ok,0);
   1.195 +			LinkableMapObj *dst=mapCenter->findObjBySelect (s);
   1.196 +			if (dst)
   1.197 +			{	
   1.198 +				if (typeid(*dst) == typeid(BranchObj) ) 
   1.199 +				{
   1.200 +					// Get number in parent
   1.201 +					x=api.parInt (ok,1);
   1.202 +					if (ok)
   1.203 +						((BranchObj*)selection)->moveBranchTo ((BranchObj*)(dst),x);
   1.204 +				} else if (typeid(*dst) == typeid(MapCenterObj) ) 
   1.205 +				{
   1.206 +					((BranchObj*)selection)->moveBranchTo ((BranchObj*)(dst),-1);
   1.207 +					// Get coordinates of mainbranch
   1.208 +					x=api.parInt (ok,2);
   1.209 +					if (ok)
   1.210  					{
   1.211 -						// Get number in parent
   1.212 -						x=api.parInt (ok,1);
   1.213 -						if (ok)
   1.214 -							((BranchObj*)selection)->moveBranchTo ((BranchObj*)(dst),x);
   1.215 -					} else if (typeid(*dst) == typeid(MapCenterObj) ) 
   1.216 -					{
   1.217 -						((BranchObj*)selection)->moveBranchTo ((BranchObj*)(dst),-1);
   1.218 -						// Get coordinates of mainbranch
   1.219 -						x=api.parInt (ok,2);
   1.220 -						if (ok)
   1.221 -						{
   1.222 -							y=api.parInt (ok,3);
   1.223 -							if (ok) ((BranchObj*)selection)->move (x,y);
   1.224 -						}
   1.225 -					}	
   1.226 +						y=api.parInt (ok,3);
   1.227 +						if (ok) ((BranchObj*)selection)->move (x,y);
   1.228 +					}
   1.229  				}	
   1.230 -			}
   1.231 +			}	
   1.232  		}
   1.233  	} else if (com=="moveBranchUp")
   1.234 -		moveBranchUp();
   1.235 -	else if (com=="moveBranchDown")
   1.236 -		moveBranchDown();
   1.237 -	else if (com=="move")
   1.238  	{
   1.239 -		if (api.checkParamCount(2) && selection )
   1.240 +		if (!selection)
   1.241 +		{
   1.242 +			api.setError (Aborted,"Nothing selected");
   1.243 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
   1.244 +					 typeid(*selection) != typeid(MapCenterObj)) )
   1.245 +		{				  
   1.246 +			api.setError (Aborted,"Type of selection is not a branch");
   1.247 +		} else if (api.checkParamCount(0))
   1.248 +		{
   1.249 +			moveBranchUp();
   1.250 +		}	
   1.251 +	} else if (com=="moveBranchDown")
   1.252 +	{
   1.253 +		if (!selection)
   1.254 +		{
   1.255 +			api.setError (Aborted,"Nothing selected");
   1.256 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
   1.257 +					 typeid(*selection) != typeid(MapCenterObj)) )
   1.258 +		{				  
   1.259 +			api.setError (Aborted,"Type of selection is not a branch");
   1.260 +		} else if (api.checkParamCount(0))
   1.261 +		{
   1.262 +			moveBranchDown();
   1.263 +		}	
   1.264 +	} else if (com=="move")
   1.265 +	{
   1.266 +		if (!selection)
   1.267 +		{
   1.268 +			api.setError (Aborted,"Nothing selected");
   1.269 +		} else if ( typeid(*selection) != typeid(BranchObj) && 
   1.270 +					typeid(*selection) != typeid(MapCenterObj) &&
   1.271 +					typeid(*selection) != typeid(FloatImageObj) )
   1.272 +		{				  
   1.273 +			api.setError (Aborted,"Type of selection is not a branch or floatimage");
   1.274 +		} else if (api.checkParamCount(2))
   1.275  		{	
   1.276  			x=api.parInt (ok,0);
   1.277  			if (ok)
   1.278 @@ -631,7 +747,15 @@
   1.279  	}
   1.280  	else if (com=="moveRel")
   1.281  	{
   1.282 -		if (api.checkParamCount(2) && selection )
   1.283 +		if (!selection)
   1.284 +		{
   1.285 +			api.setError (Aborted,"Nothing selected");
   1.286 +		} else if ( typeid(*selection) != typeid(BranchObj) && 
   1.287 +					typeid(*selection) != typeid(MapCenterObj) &&
   1.288 +					typeid(*selection) != typeid(FloatImageObj) )
   1.289 +		{				  
   1.290 +			api.setError (Aborted,"Type of selection is not a branch or floatimage");
   1.291 +		} else if (api.checkParamCount(2))
   1.292  		{	
   1.293  			x=api.parInt (ok,0);
   1.294  			if (ok)
   1.295 @@ -642,7 +766,14 @@
   1.296  		}	
   1.297  	} else if (com=="setHeading")
   1.298  	{
   1.299 -		if (api.checkParamCount(1))
   1.300 +		if (!selection)
   1.301 +		{
   1.302 +			api.setError (Aborted,"Nothing selected");
   1.303 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
   1.304 +					 typeid(*selection) != typeid(MapCenterObj)) )
   1.305 +		{				  
   1.306 +			api.setError (Aborted,"Type of selection is not a branch");
   1.307 +		} else if (api.checkParamCount(1))
   1.308  		{
   1.309  			s=api.parString (ok,0);
   1.310  			if (ok) 
   1.311 @@ -650,55 +781,98 @@
   1.312  		}	
   1.313  	} else if (com=="setURL")
   1.314  	{
   1.315 -		if (api.checkParamCount(1))
   1.316 +		if (!selection)
   1.317 +		{
   1.318 +			api.setError (Aborted,"Nothing selected");
   1.319 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
   1.320 +					 typeid(*selection) != typeid(MapCenterObj)) )
   1.321 +		{				  
   1.322 +			api.setError (Aborted,"Type of selection is not a branch");
   1.323 +		} else if (api.checkParamCount(1))
   1.324  		{
   1.325  			s=api.parString (ok,0);
   1.326  			if (ok) setURLInt(s);
   1.327  		}	
   1.328  	} else if (com=="setVymLink")
   1.329  	{
   1.330 -		if (api.checkParamCount(1))
   1.331 +		if (!selection)
   1.332 +		{
   1.333 +			api.setError (Aborted,"Nothing selected");
   1.334 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
   1.335 +					 typeid(*selection) != typeid(MapCenterObj)) )
   1.336 +		{				  
   1.337 +			api.setError (Aborted,"Type of selection is not a branch");
   1.338 +		} else if (api.checkParamCount(1))
   1.339  		{
   1.340  			s=api.parString (ok,0);
   1.341  			if (ok) setVymLinkInt(s);
   1.342  		}	
   1.343  	}
   1.344 +	else if (com=="setHideExport")
   1.345 +	{
   1.346 +		if (!selection)
   1.347 +		{
   1.348 +			api.setError (Aborted,"Nothing selected");
   1.349 +		} else if ( typeid(*selection) != typeid(BranchObj) && 
   1.350 +					typeid(*selection) != typeid(MapCenterObj) &&
   1.351 +					typeid(*selection) != typeid(FloatImageObj) )
   1.352 +		{				  
   1.353 +			api.setError (Aborted,"Type of selection is not a branch or floatimage");
   1.354 +		} else if (api.checkParamCount(2))
   1.355 +		{
   1.356 +			s=api.parString(ok,0);
   1.357 +			if (ok) 
   1.358 +			{
   1.359 +				BranchObj* bo=(BranchObj*)selection;
   1.360 +				bo->activateStandardFlag(s);
   1.361 +				bo->updateFlagsToolbar();
   1.362 +			}	
   1.363 +		}
   1.364 +	}	
   1.365  	else if (com=="setFlag")
   1.366  	{
   1.367 -		if (selection && typeid(*selection) == typeid(BranchObj) ) 
   1.368 +		if (!selection)
   1.369  		{
   1.370 -			if (api.checkParamCount(1) )
   1.371 -			{	
   1.372 -				s=api.parString(ok,0);
   1.373 -				if (ok) 
   1.374 -				{
   1.375 -					BranchObj* bo=(BranchObj*)selection;
   1.376 -					bo->activateStandardFlag(s);
   1.377 -					bo->updateFlagsToolbar();
   1.378 -				}	
   1.379 +			api.setError (Aborted,"Nothing selected");
   1.380 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
   1.381 +					 typeid(*selection) != typeid(MapCenterObj)) )
   1.382 +		{				  
   1.383 +			api.setError (Aborted,"Type of selection is not a branch");
   1.384 +		} else if (api.checkParamCount(1))
   1.385 +		{
   1.386 +			s=api.parString(ok,0);
   1.387 +			if (ok) 
   1.388 +			{
   1.389 +				BranchObj* bo=(BranchObj*)selection;
   1.390 +				bo->activateStandardFlag(s);
   1.391 +				bo->updateFlagsToolbar();
   1.392  			}	
   1.393  		}
   1.394  	}	
   1.395  	else if (com=="unsetFlag")
   1.396  	{
   1.397 -		if (selection && typeid(*selection) == typeid(BranchObj) ) 
   1.398 +		if (!selection)
   1.399  		{
   1.400 -			if (api.checkParamCount(1) )
   1.401 -			{	
   1.402 -				s=api.parString(ok,0);
   1.403 -				if (ok) 
   1.404 -				{
   1.405 -					BranchObj* bo=(BranchObj*)selection;
   1.406 -					bo->deactivateStandardFlag(s);
   1.407 -					bo->updateFlagsToolbar();
   1.408 -				}	
   1.409 +			api.setError (Aborted,"Nothing selected");
   1.410 +		} else if ( (typeid(*selection) != typeid(BranchObj) && 
   1.411 +					 typeid(*selection) != typeid(MapCenterObj)) )
   1.412 +		{				  
   1.413 +			api.setError (Aborted,"Type of selection is not a branch");
   1.414 +		} else if (api.checkParamCount(1))
   1.415 +		{
   1.416 +			s=api.parString(ok,0);
   1.417 +			if (ok) 
   1.418 +			{
   1.419 +				BranchObj* bo=(BranchObj*)selection;
   1.420 +				bo->deactivateStandardFlag(s);
   1.421 +				bo->updateFlagsToolbar();
   1.422  			}	
   1.423  		}
   1.424  	// Internal commands
   1.425  	} else if (com==QString("undoMap"))
   1.426  	{
   1.427  		if (api.checkParamCount(1))
   1.428 -			addMapReplace("",api.parString (ok,0));
   1.429 +			addMapReplaceInt("",api.parString (ok,0));
   1.430  	} else if (com=="select")
   1.431  	{
   1.432  		if (api.checkParamCount(1))
   1.433 @@ -709,16 +883,18 @@
   1.434  	}	
   1.435  	else
   1.436  	{
   1.437 -		api.setError ("Unknown command in: "+atom);
   1.438 +		api.setError (Aborted,"Unknown command");
   1.439  	}
   1.440  
   1.441  	// Any errors?
   1.442 -	if (api.error())
   1.443 +	if (api.errorLevel()==NoError)
   1.444 +		setChanged();
   1.445 +	else	
   1.446  	{
   1.447  		// TODO Error handling
   1.448  		qWarning("MapEditor::parseAtom: Error!");
   1.449 -		qWarning(api.errorDesc());
   1.450 -	}	
   1.451 +		qWarning(api.errorMessage());
   1.452 +	} 
   1.453  }
   1.454  
   1.455  void MapEditor::toggleHistoryWindow()
   1.456 @@ -1388,7 +1564,7 @@
   1.457  	// And ignore clicking the current row ;-)	
   1.458  }
   1.459  
   1.460 -void MapEditor::addMapReplace(const QString &undoSel, const QString &path)
   1.461 +void MapEditor::addMapReplaceInt(const QString &undoSel, const QString &path)
   1.462  {
   1.463  	QString pathDir=path.left(path.findRev("/"));
   1.464  	QDir d(pathDir);
   1.465 @@ -1427,7 +1603,7 @@
   1.466  		QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
   1.467  }
   1.468  
   1.469 -void MapEditor::addMapInsert(const QString &path, int pos)
   1.470 +void MapEditor::addMapInsertInt (const QString &path, int pos)
   1.471  {
   1.472  	if (selection && (typeid(*selection) == typeid(BranchObj) ||
   1.473  				      typeid(*selection) == typeid(MapCenterObj))) 
   1.474 @@ -1436,20 +1612,6 @@
   1.475  		QDir d(pathDir);
   1.476  		QFile file (path);
   1.477  
   1.478 -		BranchObj *bo=addNewBranchInt (pos);
   1.479 -		if (!bo)
   1.480 -		{
   1.481 -			
   1.482 -			QMessageBox::critical( 0, tr( "Critical Error" ), 
   1.483 -				tr("Could insert branch at position %1\n in branch %2").arg(pos)
   1.484 -				.arg(((BranchObj*)selection)->getHeading()));
   1.485 -			return;	
   1.486 -		}
   1.487 -		unselect();
   1.488 -		selection=bo;
   1.489 -		selection->select();
   1.490 -
   1.491 -		
   1.492  		if (d.exists() )
   1.493  		{
   1.494  			// We need to parse saved XML data
   1.495 @@ -1460,7 +1622,7 @@
   1.496  			reader.setErrorHandler( &handler );
   1.497  			handler.setMapEditor( this );
   1.498  			handler.setTmpDir ( pathDir );	// needed to load files with rel. path
   1.499 -			handler.setLoadMode (ImportReplace);
   1.500 +			handler.setLoadMode (ImportAdd);
   1.501  			blockReposition=true;
   1.502  			bool ok = reader.parse( source );
   1.503  			blockReposition=false;
   1.504 @@ -1470,6 +1632,8 @@
   1.505  				QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
   1.506  										handler.errorProtocol());
   1.507  			}
   1.508 +			if (selection!=mapCenter)
   1.509 +				((BranchObj*)selection)->getLastBranch()->moveBranchTo ((BranchObj*)(selection),pos);
   1.510  		} else	
   1.511  			QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
   1.512  	}		
   1.513 @@ -1689,18 +1853,19 @@
   1.514  	return newbo;
   1.515  }	
   1.516  
   1.517 -void MapEditor::addNewBranch(int pos)
   1.518 +BranchObj* MapEditor::addNewBranch(int pos)
   1.519  {
   1.520  	// Different meaning than num in addNewBranchInt!
   1.521  	// -1	add above
   1.522  	//  0	add as child
   1.523  	// +1	add below
   1.524 +	BranchObj *bo = (BranchObj*) selection;
   1.525 +	BranchObj *newbo=NULL;
   1.526 +
   1.527  	if (selection  &&  
   1.528  		 (typeid(*selection) == typeid(BranchObj) || 
   1.529  		  typeid(*selection) == typeid(MapCenterObj) ) ) 
   1.530  	{
   1.531 -		BranchObj *bo = (BranchObj*) selection;
   1.532 -		BranchObj *newbo;
   1.533  		newbo=addNewBranchInt (pos-2);
   1.534  
   1.535  		if (newbo)
   1.536 @@ -1711,72 +1876,44 @@
   1.537  				QString ("addBranch (%1)").arg(pos-2),
   1.538  				QString ("Add new branch to %1").arg(getName(bo)));	//TODO undoCommand
   1.539  
   1.540 -			LinkableMapObj *oldselection=selection;
   1.541 -
   1.542  			mapCenter->reposition();
   1.543  			adjustCanvasSize();
   1.544 -
   1.545 -
   1.546 -			if (mainWindow->autoEdit() ||
   1.547 -				mainWindow->autoSelectHeading() )
   1.548 -			{
   1.549 -				selection->unselect();
   1.550 -				selection=newbo;
   1.551 -				selection->select();
   1.552 -				if (mainWindow->autoEdit() )
   1.553 -					mainWindow->editHeading();
   1.554 -				if (!mainWindow->autoSelectHeading()
   1.555 -					)//&& !wasScrolled)  //FIXME wasScrolled was moved to addNewBranchInt
   1.556 -				{
   1.557 -					selection->unselect();
   1.558 -					selection=oldselection;
   1.559 -					selection->select();
   1.560 -				}
   1.561 -			}	
   1.562  		}
   1.563  	}	
   1.564 +	return newbo;
   1.565  }
   1.566  
   1.567  
   1.568 -void MapEditor::addNewBranchHere()
   1.569 +BranchObj* MapEditor::addNewBranchBefore()
   1.570  {
   1.571 +	BranchObj *newbo=NULL;
   1.572  	if (selection  &&  
   1.573  		 (typeid(*selection) == typeid(BranchObj) ) )
   1.574 +		 // We accept no MapCenterObj here, so we _have_ a parent
   1.575  	{
   1.576 -		BranchObj* bo1 = (BranchObj*) selection;
   1.577 -		saveStateChangingPart(selection, QString("Add new branch here").arg(getName(bo1)));
   1.578 -
   1.579 -		bool wasScrolled=false;
   1.580 -		BranchObj *newbo=NULL;
   1.581 +		BranchObj* bo = (BranchObj*) selection;
   1.582 +		QPoint p=bo->getRelPos();
   1.583 +
   1.584 +
   1.585  		BranchObj *parbo=(BranchObj*)(selection->getParObj());
   1.586 -		if (parbo)
   1.587 +
   1.588 +		// add below selection
   1.589 +		newbo=parbo->insertBranch(bo->getNum()+1);
   1.590 +		if (newbo)
   1.591  		{
   1.592 -			// add below selection
   1.593 -			newbo=parbo->insertBranch(bo1->getNum()+1);
   1.594 -		} 
   1.595 -
   1.596 -		LinkableMapObj *oldselection=selection;
   1.597 -		((BranchObj*)selection)->moveBranchTo (newbo,-1);
   1.598 -
   1.599 -		mapCenter->reposition();
   1.600 -		adjustCanvasSize();
   1.601 -		if (mainWindow->autoEdit() ||
   1.602 -			mainWindow->autoSelectHeading() )
   1.603 -		{
   1.604 -			selection->unselect();
   1.605 -			selection=newbo;
   1.606 -			selection->select();
   1.607 -			if (mainWindow->autoEdit() )
   1.608 -				mainWindow->editHeading();
   1.609 -			if (!mainWindow->autoSelectHeading()
   1.610 -				&& !wasScrolled)
   1.611 -			{
   1.612 -				selection->unselect();
   1.613 -				selection=oldselection;
   1.614 -				selection->select();
   1.615 -			}
   1.616 -		}	
   1.617 +			newbo->move2RelPos (p);
   1.618 +
   1.619 +			// Move selection to new branch
   1.620 +			((BranchObj*)selection)->moveBranchTo (newbo,-1);
   1.621 +
   1.622 +			saveState (newbo, "deleteKeepChilds ()", newbo, "addBranchBefore ()", 
   1.623 +				QString ("Add branch before %1").arg(getName(bo)));
   1.624 +
   1.625 +			mapCenter->reposition();
   1.626 +			adjustCanvasSize();
   1.627 +		}
   1.628  	}	
   1.629 +	return newbo;
   1.630  }
   1.631  
   1.632  void MapEditor::deleteSelection()
   1.633 @@ -1786,13 +1923,7 @@
   1.634  		BranchObj* bo=(BranchObj*)selection;
   1.635  		BranchObj* par=(BranchObj*)(bo->getParObj());
   1.636  		bo->unselect();
   1.637 -		if (selection->getDepth()>1)
   1.638 -			// Normal branch, save parent with childs
   1.639 -			saveStateRemovingPart (bo, QString ("Delete %1").arg(getName(bo)));
   1.640 -		else
   1.641 -			// Mainbranch, save whole map
   1.642 -			// TODO Better would be to insert mainbranch again at pos But undoCommand is missing right now
   1.643 -			saveStateComplete(QString("Delete %1").arg(getName(bo)));
   1.644 +		saveStateRemovingPart (bo, QString ("Delete %1").arg(getName(bo)));
   1.645  		selection=NULL;
   1.646  		par->removeBranch(bo);
   1.647  		selection=par;
   1.648 @@ -2584,16 +2715,21 @@
   1.649  	{		
   1.650  		BranchObj* bo=(BranchObj*)selection;
   1.651  		BranchObj* par=(BranchObj*)(bo->getParObj());
   1.652 +		QPoint p=bo->getRelPos();
   1.653  		QString s=QString("Remove %1 and keep its childs").arg(getName(bo));
   1.654  		if (bo->getDepth()==1)
   1.655  			saveStateComplete(s);
   1.656  		else	
   1.657  			saveStateChangingPart(selection->getParObj(),s);	// TODO undoCommand
   1.658 +
   1.659  		QString sel=selection->getSelectString();
   1.660  		unselect();
   1.661  		par->removeBranchHere(bo);
   1.662  		mapCenter->reposition();
   1.663  		select (sel);
   1.664 +		((BranchObj*)selection)->move2RelPos (p);
   1.665 +		mapCenter->reposition();
   1.666 +		adjustCanvasSize();
   1.667  	}	
   1.668  }
   1.669  
   1.670 @@ -2601,6 +2737,7 @@
   1.671  {
   1.672  	if (selection && (typeid(*selection) == typeid(BranchObj) ))
   1.673  	{		
   1.674 +		// TODO undoCommand
   1.675  		saveStateChangingPart(selection->getParObj(), QString("Remove childs of branch %1").arg(getName(selection)));
   1.676  		((BranchObj*)selection)->removeChilds();
   1.677  		mapCenter->reposition();
   1.678 @@ -3048,6 +3185,15 @@
   1.679  {
   1.680  	cout << "MapEditor::testFunction() called\n";
   1.681  
   1.682 +	if (selection && 
   1.683 +		(typeid(*selection) == typeid(BranchObj)) || 
   1.684 +		(typeid(*selection) == typeid(MapCenterObj))  )
   1.685 +	{
   1.686 +		BranchObj* bo=(BranchObj*)selection;
   1.687 +		cout << bo->getHeading().ascii() <<" is scrolled: "<<bo->isScrolled()<<endl;
   1.688 +	}
   1.689 +	return;
   1.690 +	
   1.691  	WarningDialog dia;
   1.692  	dia.showCancelButton (true);
   1.693  	dia.setText("This is a longer \nWarning");