# HG changeset patch
# User insilmaril
# Date 1122490595 0
# Node ID 087e60400acc56622d7be8c46b2034c2105513aa
# Parent  9079931da6c3cde9271b5bf944c283d62ae7e5d8
1.7.2 fixed undo to select the right object before undoCommand

diff -r 9079931da6c3 -r 087e60400acc branchobj.cpp
--- a/branchobj.cpp	Wed Jul 27 18:56:32 2005 +0000
+++ b/branchobj.cpp	Wed Jul 27 18:56:35 2005 +0000
@@ -661,17 +661,22 @@
     return NULL;
 }
 
+int BranchObj::getDefHeadingSize()
+{
+	if (depth==0)
+		return 16;
+	else	
+		if (depth==1) 
+			return 12;
+		else
+			return 10;
+}
+
 void BranchObj::setHeading(QString s)
 {
     // Adjusting font size
     QFont font=heading->getFont();
-	if (depth==0)
-		font.setPointSize(16);
-	else	
-		if (depth>1) 
-			font.setPointSize(10);
-		else
-			font.setPointSize(12);
+	font.setPointSize(getDefHeadingSize() );
     heading->setFont(font);
     heading->setText(s);	// set new heading
 	calcBBoxSize();			// recalculate bbox
@@ -995,9 +1000,10 @@
 	// inserts its childs at the place of bo
 	BranchObj *bo;
 	bo=borem->getLastBranch();
+	int pos=borem->getNum();
 	while (bo)
 	{
-		bo->moveBranchTo (borem,1);
+		bo->moveBranchTo (this,pos+1);
 		bo=borem->getLastBranch();
 	}	
 	removeBranch (borem);
@@ -1107,10 +1113,13 @@
 	} else
 	{
 		// inserts me at pos in parent of dst
-		par=(BranchObj*)(dst->getParObj());
 		if (par)
-			return par->insertBranchPtr (this,pos);
-		else
+		{
+			BranchObj *bo=dst->insertBranchPtr (this,pos);
+			bo->setHeading (bo->getHeading());
+			return bo;
+
+		} else
 			return NULL;
 	}	
 }
diff -r 9079931da6c3 -r 087e60400acc branchobj.h
--- a/branchobj.h	Wed Jul 27 18:56:32 2005 +0000
+++ b/branchobj.h	Wed Jul 27 18:56:35 2005 +0000
@@ -58,6 +58,7 @@
     virtual void positionBBox();
     virtual void calcBBoxSize();
     virtual LinkableMapObj* findMapObj(QPoint,LinkableMapObj*);	// find MapObj 
+    virtual int getDefHeadingSize();
     virtual void setHeading (QString);
     virtual void setURL (QString);
     virtual QString getURL ();
diff -r 9079931da6c3 -r 087e60400acc linkablemapobj.cpp
--- a/linkablemapobj.cpp	Wed Jul 27 18:56:32 2005 +0000
+++ b/linkablemapobj.cpp	Wed Jul 27 18:56:35 2005 +0000
@@ -1,3 +1,5 @@
+#include <math.h>
+
 #include "linkablemapobj.h"
 #include "branchobj.h"
 #include "mapeditor.h"
@@ -651,7 +653,7 @@
 	// we want to block expensive repositioning, but just do it once at
 	// the end, thus check first:
 
-	if (mapEditor->blockReposition()) return;
+	if (mapEditor->isRepositionBlocked()) return;
 	
 	// Pass on the request to parental objects, if this hasn't been done yet
 	
diff -r 9079931da6c3 -r 087e60400acc mapeditor.cpp
--- a/mapeditor.cpp	Wed Jul 27 18:56:32 2005 +0000
+++ b/mapeditor.cpp	Wed Jul 27 18:56:35 2005 +0000
@@ -360,7 +360,8 @@
 	printFrame=true;
 	printFooter=true;
 
-	blockreposition=false;
+	blockReposition=false;
+	blockSaveState=false;
 	isInteractive=interactive;
 	if (isInteractive)
 		// Create temporary files
@@ -468,9 +469,9 @@
 	}
 }
 
-bool MapEditor::blockReposition()
+bool MapEditor::isRepositionBlocked()
 {
-	return blockreposition;
+	return blockReposition;
 }
 
 void MapEditor::makeTmpDirs()
@@ -564,7 +565,7 @@
 	s+=settings.getXMLData (destPath);
 
 	// Save selection
-	if (selection && !saveSelection) 
+	if (selection && !saveSelection ) 
 		s+=valueElement("select",selection->getSelectString());
 
 	decIndent();
@@ -577,29 +578,38 @@
 
 void MapEditor::saveState()
 {
-	saveState (CompleteMap,NULL,"");
+	saveState (CompleteMap,NULL,"","");
 }
 
 void MapEditor::saveState(LinkableMapObj *undoSel)
 {
-	saveState (PartOfMap,undoSel,"");
+	saveState (PartOfMap,undoSel,"","");
 }
 
-void MapEditor::saveState(const QString & c)
+void MapEditor::saveState(const QString & uc, const QString &rc)
 {
 	LinkableMapObj *undoSel;
 	if (selection)
 		undoSel=selection;
 	else
 		undoSel=NULL;
-	saveState (UndoCommand,undoSel,c);
+	saveState (UndoCommand,undoSel,uc,rc);
 }
 
-void MapEditor::saveState(const SaveMode &savemode, LinkableMapObj *undoSel, const QString &undoCom)
+void MapEditor::saveState(const SaveMode &savemode, LinkableMapObj *undoSel, const QString &undoCom, const QString &redoCom)
 {
+	if (blockSaveState) return;
+
 	setChanged();
 
-	// Save what is selected now
+	// Save current selection 
+	redoCommand=redoCom;
+	if (selection)
+		redoSelection=selection->getSelectString();
+	else	
+		redoSelection="";
+
+	// Save the object, which should be undone
 	if (undoSel)
 		undoSelection=undoSel->getSelectString();
 	else
@@ -619,10 +629,15 @@
 	{
 		undoCommand="undoMap ()";
 		backupXML=saveToDir (bakMapDir,mapName+"-",false, QPoint (),NULL);
+		undoSelection="";
 	}
 	/* FIXME testing
 	cout << "ME::saveState()\n";
 	cout << "  undoCom="<<undoCommand<<endl;
+	cout << "  undoSel="<<undoSelection<<endl;
+	cout << "  ---------------------------"<<endl;
+	cout << "  redoCom="<<redoCommand<<endl;
+	cout << "  redoSel="<<redoSelection<<endl<<endl;
 	*/
 }
 
@@ -644,8 +659,7 @@
 		moveBranchDown();
 	else if (com=="move")
 	{
-		if (api.checkParamCount(2) &&
-			selection )
+		if (api.checkParamCount(2) && selection )
 		{	
 			x=api.parInt (ok,0);
 			if (ok)
@@ -666,10 +680,15 @@
 				if (dst)
 				{	
 					if (typeid(*dst) == typeid(BranchObj) ) 
-						((BranchObj*)(selection))->moveBranchTo ((BranchObj*)(dst),-1);
-					if (typeid(*dst) == typeid(MapCenterObj) ) 
+					{
+						// Get number in parent
+						x=api.parInt (ok,1);
+						if (ok)
+							((BranchObj*)(selection))->moveBranchTo ((BranchObj*)(dst),x);
+					} else if (typeid(*dst) == typeid(MapCenterObj) ) 
 					{
 						((BranchObj*)(selection))->moveBranchTo ((BranchObj*)(dst),-1);
+						// Get coordinates of mainbranch
 						x=api.parInt (ok,2);
 						if (ok)
 						{
@@ -690,8 +709,10 @@
 	}	
 	// Internal commands, used for undo etc.	
 	else if (com==QString("undoMap"))
-		undoXML("");
-	else if (com==QString("undoPart"))
+	{
+		if (api.checkParamCount(0))
+			undoXML("");
+	} else if (com==QString("undoPart"))
 	{
 		if (api.checkParamCount(1))
 		{
@@ -862,7 +883,7 @@
 		returnCode=1;	
 	} else
 	{
-		blockreposition=true;
+		blockReposition=true;
 		QXmlInputSource source( file);
 		QXmlSimpleReader reader;
 		reader.setContentHandler( &handler );
@@ -871,7 +892,7 @@
 		handler.setTmpDir (filePath.left(filePath.findRev("/",-1)));	// needed to load files with rel. path
 		handler.setLoadMode (lmode);
 		bool ok = reader.parse( source );
-		blockreposition=false;
+		blockReposition=false;
 		file.close();
 		if ( ok ) 
 		{
@@ -1308,6 +1329,8 @@
 	actionEditUndo->setEnabled (false);
 	mapChanged=false;
 
+	if (!redoSelection.isEmpty())
+		select (redoSelection);
 }
 
 void MapEditor::undoXML(const QString &undoSel)
@@ -1327,6 +1350,7 @@
 		handler.setTmpDir ( bakMapDir );	// needed to load files with rel. path
 		if (undoSel.isEmpty())
 		{
+			unselect();
 			mapCenter->clear();
 			handler.setLoadMode (NewMap);
 		} else	
@@ -1334,9 +1358,11 @@
 			select (undoSel);
 			handler.setLoadMode (ImportReplace);
 		}	
-		blockreposition=true;
+		blockReposition=true;
+		blockSaveState=true;
 		bool ok = reader.parse( source );
-		blockreposition=false;
+		blockReposition=false;
+		blockSaveState=false;
 		if (! ok ) 
 		{	
 			// This should never ever happen
@@ -1450,12 +1476,12 @@
 	BranchObj* par;
 	if (typeid(*selection) == typeid(BranchObj)  ) 
 	{
-		saveState("moveBranchDown ()");
 		bo=(BranchObj*)(selection);
 		par=(BranchObj*)(bo->getParObj());
 		selection->unselect();
 		selection=par->moveBranchUp (bo);
 		selection->select();
+		saveState("moveBranchDown ()",bo->getSelectString());
 		mapCenter->reposition();
 		ensureSelectionVisible();
 	}
@@ -1470,12 +1496,12 @@
 	BranchObj* par;
 	if (typeid(*selection) == typeid(BranchObj)  ) 
 	{
-		saveState("moveBranchUp ()");
 		bo=(BranchObj*)(selection);
 		par=(BranchObj*)(bo->getParObj());
 		selection->unselect(); 
 		selection=par->moveBranchDown(bo);
 		selection->select();
+		saveState("moveBranchUp ()",bo->getSelectString());
 		mapCenter->reposition();
 		ensureSelectionVisible();
 	}	
@@ -1490,10 +1516,10 @@
 		 (typeid(*selection) == typeid(BranchObj) || 
 		  typeid(*selection) == typeid(MapCenterObj) ) ) 
 	{
-		saveState("setHeading (\""+((BranchObj*)(selection))->getHeading()+"\")");
+		editingBO=(BranchObj*)(selection);
+		saveState("setHeading (\""+((BranchObj*)(selection))->getHeading()+"\")",editingBO->getSelectString() );
 
 		ensureSelectionVisible();
-		editingBO=(BranchObj*)(selection);
 		QPoint p = worldMatrix().map(QPoint (editingBO->x(),editingBO->y()));
 		lineedit->setGeometry(p.x()-contentsX(),p.y()-contentsY(),200,25);
 		QString s=editingBO->getHeading();
@@ -1644,7 +1670,14 @@
 
 	if (selection  && typeid(*selection) ==typeid(BranchObj) ) 
 	{
-		saveState(selection->getParObj());
+		if (selection->getDepth()>1)
+			// Normal branch, save parent with childs
+			saveState(selection->getParObj());
+		else
+			// Mainbranch, save whole map
+			// FIXME Better would be to insert mainbranch again at pos
+			// But undoCommand is missing right now
+			saveState();
 		BranchObj* bo=dynamic_cast <BranchObj*> (selection);
 		BranchObj* par=(BranchObj*)(bo->getParObj());
 		bo->unselect();
@@ -1690,8 +1723,8 @@
 		adjustCanvasSize();
 		ensureSelectionVisible();
 		return true;
-	} else
-		return false;
+	} 
+	return false;
 }
 
 void MapEditor::unselect()
@@ -2353,10 +2386,13 @@
 {
 	if (selection && (typeid(*selection) == typeid(BranchObj) ))
 	{		
-		saveState(selection->getParObj());
-		QString sel=selection->getSelectString();
 		BranchObj* bo=(BranchObj*)(selection);
 		BranchObj* par=(BranchObj*)(bo->getParObj());
+		if (bo->getDepth()==1)
+			saveState();
+		else	
+			saveState(selection->getParObj());	// FIXME undoCommand
+		QString sel=selection->getSelectString();
 		unselect();
 		par->removeBranchHere(bo);
 		mapCenter->reposition();
@@ -2412,9 +2448,9 @@
 	// Finally show dialog
 	if (dia.exec() == QDialog::Accepted)
 	{
+		saveState();	//FIXME undoCommand
 		mapCenter->setAuthor (dia.getAuthor() );
 		mapCenter->setComment (dia.getComment() );
-		saveState();	//FIXME undoCommand
 	}
 }
 
@@ -2567,7 +2603,7 @@
 {
 	linkstyle=ls;
 
-	saveState();
+	saveState();	// FIXME undoCommand
 	BranchObj *bo;
 	bo=mapCenter->first();
 	bo=bo->next();
@@ -3151,8 +3187,8 @@
 
 		if (typeid(*selection) == typeid(FloatImageObj))
 		{
-			saveState("move "+qpointToString(movingObj_orgPos));
 			FloatObj *fo=(FloatObj*)(selection);
+			saveState("move "+qpointToString(movingObj_orgPos),fo->getSelectString() );
 			if (fo->getLinkStyle()==StyleUndef) 
 			{
 				fo->setLinkStyle(fo->getDefLinkStyle());
@@ -3203,7 +3239,7 @@
 				if (lmosel->getDepth()==1)
 				{
 					// depth==1, mainbranch
-					saveState("move "+qpointToString(movingObj_orgPos));
+					saveState("move "+qpointToString(movingObj_orgPos), lmosel->getSelectString() );
 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
 				} else
 				{
@@ -3348,30 +3384,31 @@
 			if (dst ) 
 			{
 				BranchObj* bs=((BranchObj*)(selection));
-				saveState ("linkBranchToPos (\""+ 
+				QString undoCom="linkBranchToPos (\""+ 
 					(bs->getParObj())->getSelectString()+
 					"\","+
 					QString("%1").arg(bs->getNum())+
 					","+
 					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+
-					")");
+					")";
 				// TODO we also could check, if dest and src are on same branch,
 				// then it would be sufficient to saveState of this branch
 
 				// Modifiers allow to insert above/below dst
 				if (e->state() & QMouseEvent::ShiftButton)
 				{
-					bs->moveBranchTo ( (BranchObj*)(dst), ((BranchObj*)(dst))->getNum());
+					bs->moveBranchTo ( (BranchObj*)(dst->getParObj()), ((BranchObj*)(dst))->getNum());
 				} else 
 				if (e->state() & QMouseEvent::ControlButton)
 				{
-					bs->moveBranchTo ( (BranchObj*)(dst), ((BranchObj*)(dst))->getNum()+1);
+					bs->moveBranchTo ( (BranchObj*)(dst->getParObj()), ((BranchObj*)(dst))->getNum()+1);
 				} else	
 				{
 					bs->moveBranchTo ((BranchObj*)(dst),-1);
 					if (dst->getDepth()==0) 
 						bs->move (savePos);
 				} 
+				saveState (undoCom,bs->getSelectString() );
 			}	
 			// Draw the original link, before selection was moved around
 			mapCenter->reposition();
diff -r 9079931da6c3 -r 087e60400acc mapeditor.h
--- a/mapeditor.h	Wed Jul 27 18:56:32 2005 +0000
+++ b/mapeditor.h	Wed Jul 27 18:56:35 2005 +0000
@@ -26,7 +26,7 @@
     MapCenterObj* getMapCenter();
 	QCanvas* getCanvas();
 	void adjustCanvasSize();// adjust canvas size to map and scrollview
-	bool blockReposition(); // block while load or undo
+	bool isRepositionBlocked(); // block while load or undo
 	
 private:
 	void makeTmpDirs();		// create temporary directories
@@ -36,8 +36,8 @@
     QString saveToDir(const QString&,const QString &,bool, const QPoint &,LinkableMapObj*);
     void saveState();		// save actual state to backup
     void saveState(LinkableMapObj *);
-    void saveState(const QString &);
-    void saveState(const SaveMode&, LinkableMapObj *, const QString &);
+    void saveState(const QString &, const QString &);
+    void saveState(const SaveMode&, LinkableMapObj *, const QString &, const QString &);
     void parseAtom(const QString &);	
 
     void addFloatImage(const QPixmap &img);
@@ -190,6 +190,7 @@
     LinkableMapObj* selection;		// select a LinkableMapObj
     LinkableMapObj* selectionLast;	// last selection 
 	QString undoSelection;			// selection for undo
+	QString redoSelection;			// selection for redo
     MapObj* movingObj;				// moving a MapObj
 	MapObj* linkingObj_src;			// part of a link
     QPoint movingObj_orgPos;		// org. pos of mouse before move
@@ -204,6 +205,7 @@
 	bool mapUnsaved;				// Flag if map should be saved
     QString backupXML;				// backup (XML) for undo
 	QString undoCommand;			// FIXME testing
+	QString redoCommand;			// FIXME testing
 									// if != NULL
 
 	bool printFrame;			// Print frame around map
@@ -219,7 +221,8 @@
 
 	bool isInteractive;			// non interactive don't need tmpdirs
 	QString bakMapDir;			// tmp directory with data for undo
-	bool blockreposition;		// block while load or undo
+	bool blockReposition;		// block while load or undo
+	bool blockSaveState;		// block while load or undo
 
 	BranchObj* itFind;			// next object in find process
 	bool EOFind;				// true, if search failed
diff -r 9079931da6c3 -r 087e60400acc misc.cpp
--- a/misc.cpp	Wed Jul 27 18:56:32 2005 +0000
+++ b/misc.cpp	Wed Jul 27 18:56:35 2005 +0000
@@ -1,3 +1,5 @@
+#include <math.h>
+
 #include <qregexp.h>
 #include <qpoint.h>
 
diff -r 9079931da6c3 -r 087e60400acc tex/vym.changelog
--- a/tex/vym.changelog	Wed Jul 27 18:56:32 2005 +0000
+++ b/tex/vym.changelog	Wed Jul 27 18:56:35 2005 +0000
@@ -1,3 +1,8 @@
+-------------------------------------------------------------------
+Mi Jul 27 17:27:14 CEST 2005
+
+- Bugfix: if selection changed before undo, undo worked on wrong branch
+
 -------------------------------------------------------------------
 Mon Jul 25 22:22:21 CEST 2005
 
diff -r 9079931da6c3 -r 087e60400acc version.h
--- a/version.h	Wed Jul 27 18:56:32 2005 +0000
+++ b/version.h	Wed Jul 27 18:56:35 2005 +0000
@@ -1,7 +1,7 @@
 #ifndef VERSION_H 
 #define VERSION_H
 
-#define __VYM_VERSION__ "1.7.1"
-#define __BUILD_DATE__ "July 25, 2005"
+#define __VYM_VERSION__ "1.7.2"
+#define __BUILD_DATE__ "July 27, 2005"
 
 #endif