# HG changeset patch
# User insilmaril
# Date 1181816501 0
# Node ID f3465a5f0dc44cbf81a55212db46a78bb87967ca
# Parent  6ced87c58cea854abc5dc788c1d27918bef1e160
Fixed undo/redo of copy/paste

diff -r 6ced87c58cea -r f3465a5f0dc4 file.cpp
--- a/file.cpp	Thu Jun 14 10:21:40 2007 +0000
+++ b/file.cpp	Thu Jun 14 10:21:41 2007 +0000
@@ -126,6 +126,51 @@
 		qWarning ("removeDir("+d.path()+") failed!");
 }		
 
+void copyDir (QDir src, QDir dst)
+{
+	system ("cp -r "+src.path()+"/* "+dst.path());
+
+	/*
+	ErrorCode err=success;
+
+	Process *cpProc=new Process ();
+	QStringList args;
+	cpProc->setWorkingDirectory (src.path());
+	args <<"-r";
+	args <<src.path();
+	args <<dst.path();
+
+	cpProc->start ("cp",args);
+	if (!cpProc->waitForStarted() )
+	{	
+		// zip could not be started
+		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
+					   QObject::tr("Couldn't start zip to compress data."));
+		err=aborted;
+	} else
+	{
+		// zip could be started
+		cpProc->waitForFinished();
+		if (cpProc->exitStatus()!=QProcess::NormalExit )
+		{
+			QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
+						   QObject::tr("cp didn't exit normally")+
+						   "\n" + cpProc->getErrout());
+			err=aborted;
+		} else
+		{
+			if (cpProc->exitCode()>0)
+			{
+				QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
+						   QString("cp exit code:  %1").arg(cpProc->exitCode() )+
+						   "\n" + cpProc->getErrout() );
+				err=aborted;
+			}
+		}
+	}	// cp could be started
+	*/
+}
+
 void makeSubDirs (const QString &s)
 {
 	QDir d(s);
diff -r 6ced87c58cea -r f3465a5f0dc4 file.h
--- a/file.h	Thu Jun 14 10:21:40 2007 +0000
+++ b/file.h	Thu Jun 14 10:21:41 2007 +0000
@@ -13,6 +13,7 @@
 QString convertToRel (const QString &,const QString &);
 QString makeUniqueDir (bool &,QString);
 void removeDir(QDir);
+void copyDir (QDir src,QDir dst);
 void makeSubDirs (const QString &);
 ErrorCode zipDir (const QDir &,const QString&);
 ErrorCode unzipDir (const QDir &,const QString&);
diff -r 6ced87c58cea -r f3465a5f0dc4 highlighter.cpp
--- a/highlighter.cpp	Thu Jun 14 10:21:40 2007 +0000
+++ b/highlighter.cpp	Thu Jun 14 10:21:41 2007 +0000
@@ -42,10 +42,11 @@
 					<< "\\baddMapReplace\\b"
                     << "\\bcolorBranch\\b" 
 					<< "\\bcolorSubtree\\b"
+					<< "\\bcopy\\b"
                     << "\\bcut\\b" 
 					<< "\\bdelete\\b" 
-					<< "\\bdeletepKeepChilds\\b" 
-					<< "\\bdeletepChilds\\b"
+					<< "\\bdeleteKeepChilds\\b" 
+					<< "\\bdeleteChilds\\b"
 					<< "\\bimportDir\\b"
 					<< "\\blinkTo\\b" 
 					<< "\\bloadImage\\b"
@@ -53,7 +54,9 @@
 					<< "\\bmoveBranchDown\\b"
 					<< "\\bmove\\b" 
 					<< "\\bmoveRel\\b"
+					<< "\\bnop\\b"
 					<< "\\bpaste\\b" 
+					<< "\\bqa\\b" 
 					<< "\\bsaveImage\\b" 
 					<< "\\bscroll\\b" 
 					<< "\\bselect\\b" 
diff -r 6ced87c58cea -r f3465a5f0dc4 mainwindow.cpp
--- a/mainwindow.cpp	Thu Jun 14 10:21:40 2007 +0000
+++ b/mainwindow.cpp	Thu Jun 14 10:21:41 2007 +0000
@@ -33,6 +33,7 @@
 extern QString vymName;
 extern QString vymVersion;
 extern QString vymBuildDate;
+extern bool debug;
 
 QMenu* branchContextMenu;
 QMenu* branchAddContextMenu;
@@ -83,10 +84,11 @@
 		qWarning ("Mainwindow: Could not create temporary directory, failed to start vym");
 		exit (1);
 	}
+	if (debug) qDebug (QString("vym tmpDir=%1").arg(tmpVymDir) );
 
 	// Create direcctory for clipboard
 	clipboardDir=tmpVymDir+"/clipboard";
-	clipboardFile="part.xml";
+	clipboardFile="map.xml";
 	QDir d(clipboardDir);
 	d.mkdir (clipboardDir,true);
 	makeSubDirs (clipboardDir);
@@ -239,7 +241,7 @@
 
 void Main::statusMessage(const QString &s)
 {
-	statusBar()->message (s);
+	statusBar()->message( s);
 }
 
 void Main::closeEvent (QCloseEvent* )
@@ -3321,7 +3323,7 @@
 
 void Main::windowToggleNoteEditor()
 {
-	if (textEditor->showWithMain() )
+	if (textEditor->isVisible() )
 		windowHideNoteEditor();
 	else
 		windowShowNoteEditor();
diff -r 6ced87c58cea -r f3465a5f0dc4 mapeditor.cpp
--- a/mapeditor.cpp	Thu Jun 14 10:21:40 2007 +0000
+++ b/mapeditor.cpp	Thu Jun 14 10:21:41 2007 +0000
@@ -133,6 +133,10 @@
 	// Create temporary files
 	makeTmpDirs();
 
+	curStep=0;
+	redosAvail=0;
+	undosAvail=0;
+
 	setAcceptDrops (true);	
 
 	mapCenter->reposition();	//	for positioning heading
@@ -314,9 +318,6 @@
 
 	if (debug) cout << "ME::saveState() for  "<<mapName.ascii()<<endl;
 	
-	int undosAvail=undoSet.readNumEntry ("/history/undosAvail",0);
-	int redosAvail=undoSet.readNumEntry ("/history/redosAvail",0);
-	int curStep=undoSet.readNumEntry ("/history/curStep",0);
 	// Find out current undo directory
 	if (undosAvail<stepsTotal) undosAvail++;
 	curStep++;
@@ -587,6 +588,18 @@
 			QColor c=parser.parColor (ok,0);
 			if (ok) colorSubtree (c);
 		}	
+	} else if (com=="copy")
+	{
+		if (xelection.isEmpty())
+		{
+			parser.setError (Aborted,"Nothing selected");
+		} else if (! selb )
+		{				  
+			parser.setError (Aborted,"Type of selection is not a branch");
+		} else if (parser.checkParamCount(0))
+		{	
+			//FIXME missing action for copy
+		}	
 	} else if (com=="cut")
 	{
 		if (xelection.isEmpty())
@@ -777,6 +790,8 @@
 				if (ok) moveRel (x,y);
 			}
 		}	
+	} else if (com=="nop")
+	{
 	} else if (com=="paste")
 	{
 		if (xelection.isEmpty() )
@@ -785,9 +800,76 @@
 		} else if (! selb )
 		{				  
 			parser.setError (Aborted,"Type of selection is not a branch");
-		} else if (parser.checkParamCount(0))
+		} else if (parser.checkParamCount(1))
 		{	
-			paste();
+			x=parser.parInt (ok,0);
+			if (ok) pasteNoSave(x);
+		}	
+	/////////////////////////////////////////////////////////////////////
+	} else if (com=="qa")
+	{
+		if (xelection.isEmpty() )
+		{
+			parser.setError (Aborted,"Nothing selected");
+		} else if (! selb )
+		{				  
+			parser.setError (Aborted,"Type of selection is not a branch");
+		} else if (parser.checkParamCount(4))
+		{	
+			QString c,u;
+			c=parser.parString (ok,0);
+			if (!ok)
+			{
+				parser.setError (Aborted,"No comment given");
+			} else
+			{
+				s=parser.parString (ok,1);
+				if (!ok)
+				{
+					parser.setError (Aborted,"First parameter is not a string");
+				} else
+				{
+					t=parser.parString (ok,2);
+					if (!ok)
+					{
+						parser.setError (Aborted,"Condition is not a string");
+					} else
+					{
+						u=parser.parString (ok,3);
+						if (!ok)
+						{
+							parser.setError (Aborted,"Third parameter is not a string");
+						} else
+						{
+							if (s!="heading")
+							{
+								parser.setError (Aborted,"Unknown type: "+s);
+							} else
+							{
+								if (! (t=="eq") ) 
+								{
+									parser.setError (Aborted,"Unknown operator: "+t);
+								} else
+								{
+									if (! selb    )
+									{
+										parser.setError (Aborted,"Type of selection is not a branch");
+									} else
+									{
+										if (selb->getHeading() == u)
+										{
+											cout << "PASSED: " << c.ascii() << endl;
+										} else
+										{
+											cout << "FAILED: " << c.ascii() << endl;
+										}
+									}
+								}
+							}
+						} 
+					} 
+				} 
+			}
 		}	
 	} else if (com=="saveImage")
 	{
@@ -1613,23 +1695,18 @@
 	LinkableMapObj *sel=xelection.single();
 	if (sel)
 	{
-		// write to directory
-		QString saveFile=saveToDir (clipboardDir,clipboardFile+"-",true,QPointF(),sel ); // FIXME check FIO
-		QFile file;
-
-		file.setName ( clipboardDir + "/"+clipboardFile);
-		if ( !file.open( QIODevice::WriteOnly ) )
+		if (redosAvail == 0)
 		{
-			// This should neverever happen
-			QMessageBox::critical (0,tr("Critical Export Error"),tr("MapEditor::exportXML couldn't open %1").arg(file.name()));
-			return;
-		}	
-
-		// Write it finally, and write in UTF8, no matter what 
-		QTextStream ts( &file );
-		ts.setEncoding (QTextStream::UnicodeUTF8);
-		ts << saveFile;
-		file.close();
+			// Copy to history
+			QString s=sel->getSelectString();
+			saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy to clipboard",sel  );
+			curClipboard=curStep;
+		}
+
+		// Copy also to global clipboard, because we are at last step in history
+		QString bakMapName=QDir::convertSeparators (QString("history-%1").arg(curStep));
+		QString bakMapDir=QDir::convertSeparators (tmpMapDir +"/"+bakMapName);
+		copyDir (bakMapDir,clipboardDir );
 
 		clipboardEmpty=false;
 		updateActions();
@@ -1638,10 +1715,6 @@
 
 void MapEditor::redo()
 {
-	// Restore variables
-	int curStep=undoSet.readNumEntry (QString("/history/curStep"));
-	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
-	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
 	// Can we undo at all?
 	if (redosAvail<1) return;
 
@@ -1722,14 +1795,11 @@
 
 void MapEditor::undo()
 {
-	// Restore variables
-	int curStep=undoSet.readNumEntry (QString("/history/curStep"));
-	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
-	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
-
 	// Can we undo at all?
 	if (undosAvail<1) return;
 
+	mainWindow->statusMessage (tr("Autosave disabled during undo."));
+
 	bool blockSaveStateOrg=blockSaveState;
 	blockSaveState=true;
 	
@@ -1902,11 +1972,19 @@
 	}		
 }
 
-void MapEditor::pasteNoSave()
+void MapEditor::pasteNoSave(const int &n)
 {
 	bool old=blockSaveState;
 	blockSaveState=true;
-	load (clipboardDir+"/"+clipboardFile,ImportAdd);
+	if (redosAvail > 0 || n!=0)
+	{
+		// Use the "historical" buffer
+		QString bakMapName=QDir::convertSeparators (QString("history-%1").arg(n));
+		QString bakMapDir=QDir::convertSeparators (tmpMapDir +"/"+bakMapName);
+		load (bakMapDir+"/"+clipboardFile,ImportAdd);
+	} else
+		// Use the global buffer
+		load (clipboardDir+"/"+clipboardFile,ImportAdd);
 	blockSaveState=old;
 }
 
@@ -1918,10 +1996,10 @@
 		saveStateChangingPart(
 			sel,
 			sel,
-			"paste ()",
+			QString ("paste (%1)").arg(curClipboard),
 			QString("Paste to %1").arg( getName(sel))
 		);
-		pasteNoSave();
+		pasteNoSave(0);
 		mapCenter->reposition();
 	}
 }
@@ -3759,7 +3837,8 @@
 
 void MapEditor::testFunction()
 {
-	// This is the playground
+	mainWindow->statusMessage("Biiiiiiiiiiiiiiing!");
+	return;
 
 	BranchObj *bo=xelection.getBranch();
 	if (bo) animObjList.append( bo );
diff -r 6ced87c58cea -r f3465a5f0dc4 mapeditor.h
--- a/mapeditor.h	Thu Jun 14 10:21:40 2007 +0000
+++ b/mapeditor.h	Thu Jun 14 10:21:41 2007 +0000
@@ -68,13 +68,13 @@
 	*/	
     void saveState(const SaveMode& savemode, const QString &undoSelection, const QString &undoCommand, const QString &redoSelection, const QString &redoCommand, const QString &comment, LinkableMapObj *saveSelection);
 	/*! Overloaded for convenience */
-    void saveStateChangingPart(LinkableMapObj *undoSelection, LinkableMapObj* redoSelection, const QString &rendoCommand, const QString &comment);
+    void saveStateChangingPart(LinkableMapObj *undoSelection, LinkableMapObj* redoSelection, const QString &redoCommand, const QString &comment);
 	/*! Overloaded for convenience */
     void saveStateRemovingPart(LinkableMapObj *redoSelection, const QString &comment);
 	/*! Overloaded for convenience */
-    void saveState(LinkableMapObj *undoSelection, const QString &undoCommand, LinkableMapObj *redoSelection, const QString &rendoCommand, const QString &comment); 
+    void saveState(LinkableMapObj *undoSelection, const QString &undoCommand, LinkableMapObj *redoSelection, const QString &redoCommand, const QString &comment); 
 	/*! Overloaded for convenience */
-    void saveState(const QString &undoSelection, const QString &undoCommand, const QString &redoSelection, const QString &rendoCommand, const QString &comment) ;
+    void saveState(const QString &undoSelection, const QString &undoCommand, const QString &redoSelection, const QString &redoCommand, const QString &comment) ;
 
 public:	
 
@@ -144,7 +144,7 @@
 private:	
     void addMapReplaceInt(const QString & undoSel, const QString & path);
     void addMapInsertInt (const QString & path, int pos);
-    void pasteNoSave();		// paste clipboard to branch
+    void pasteNoSave(const int &n);		//!< paste clipboard to branch
 public:	
     void paste();		//!< Paste clipboard to branch and backup
     void cut();			//!< Cut to clipboard (and copy)
@@ -365,8 +365,12 @@
 
 	QString tmpMapDir;			// tmp directory with undo history
 	QString histPath;			// Path to history file
+	SimpleSettings undoSet;		// undo/redo commands, saved in histPath
 	int stepsTotal;				// total number of steps (undos+redos) 
-	SimpleSettings undoSet;		// undo/redo commands
+	int curStep;				// Current step in history (ring buffer)
+	int curClipboard;			// number of history step, which is the current clipboard
+	int redosAvail;				// Available number of redo steps
+	int undosAvail;				// Available number of undo steps
 	bool blockReposition;		// block while load or undo
 	bool blockSaveState;		// block while load or undo