Fixed undo/redo of copy/paste
authorinsilmaril
Thu, 14 Jun 2007 10:21:41 +0000
changeset 502f3465a5f0dc4
parent 501 6ced87c58cea
child 503 48ca4c364eab
Fixed undo/redo of copy/paste
file.cpp
file.h
highlighter.cpp
mainwindow.cpp
mapeditor.cpp
mapeditor.h
     1.1 --- a/file.cpp	Thu Jun 14 10:21:40 2007 +0000
     1.2 +++ b/file.cpp	Thu Jun 14 10:21:41 2007 +0000
     1.3 @@ -126,6 +126,51 @@
     1.4  		qWarning ("removeDir("+d.path()+") failed!");
     1.5  }		
     1.6  
     1.7 +void copyDir (QDir src, QDir dst)
     1.8 +{
     1.9 +	system ("cp -r "+src.path()+"/* "+dst.path());
    1.10 +
    1.11 +	/*
    1.12 +	ErrorCode err=success;
    1.13 +
    1.14 +	Process *cpProc=new Process ();
    1.15 +	QStringList args;
    1.16 +	cpProc->setWorkingDirectory (src.path());
    1.17 +	args <<"-r";
    1.18 +	args <<src.path();
    1.19 +	args <<dst.path();
    1.20 +
    1.21 +	cpProc->start ("cp",args);
    1.22 +	if (!cpProc->waitForStarted() )
    1.23 +	{	
    1.24 +		// zip could not be started
    1.25 +		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
    1.26 +					   QObject::tr("Couldn't start zip to compress data."));
    1.27 +		err=aborted;
    1.28 +	} else
    1.29 +	{
    1.30 +		// zip could be started
    1.31 +		cpProc->waitForFinished();
    1.32 +		if (cpProc->exitStatus()!=QProcess::NormalExit )
    1.33 +		{
    1.34 +			QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
    1.35 +						   QObject::tr("cp didn't exit normally")+
    1.36 +						   "\n" + cpProc->getErrout());
    1.37 +			err=aborted;
    1.38 +		} else
    1.39 +		{
    1.40 +			if (cpProc->exitCode()>0)
    1.41 +			{
    1.42 +				QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
    1.43 +						   QString("cp exit code:  %1").arg(cpProc->exitCode() )+
    1.44 +						   "\n" + cpProc->getErrout() );
    1.45 +				err=aborted;
    1.46 +			}
    1.47 +		}
    1.48 +	}	// cp could be started
    1.49 +	*/
    1.50 +}
    1.51 +
    1.52  void makeSubDirs (const QString &s)
    1.53  {
    1.54  	QDir d(s);
     2.1 --- a/file.h	Thu Jun 14 10:21:40 2007 +0000
     2.2 +++ b/file.h	Thu Jun 14 10:21:41 2007 +0000
     2.3 @@ -13,6 +13,7 @@
     2.4  QString convertToRel (const QString &,const QString &);
     2.5  QString makeUniqueDir (bool &,QString);
     2.6  void removeDir(QDir);
     2.7 +void copyDir (QDir src,QDir dst);
     2.8  void makeSubDirs (const QString &);
     2.9  ErrorCode zipDir (const QDir &,const QString&);
    2.10  ErrorCode unzipDir (const QDir &,const QString&);
     3.1 --- a/highlighter.cpp	Thu Jun 14 10:21:40 2007 +0000
     3.2 +++ b/highlighter.cpp	Thu Jun 14 10:21:41 2007 +0000
     3.3 @@ -42,10 +42,11 @@
     3.4  					<< "\\baddMapReplace\\b"
     3.5                      << "\\bcolorBranch\\b" 
     3.6  					<< "\\bcolorSubtree\\b"
     3.7 +					<< "\\bcopy\\b"
     3.8                      << "\\bcut\\b" 
     3.9  					<< "\\bdelete\\b" 
    3.10 -					<< "\\bdeletepKeepChilds\\b" 
    3.11 -					<< "\\bdeletepChilds\\b"
    3.12 +					<< "\\bdeleteKeepChilds\\b" 
    3.13 +					<< "\\bdeleteChilds\\b"
    3.14  					<< "\\bimportDir\\b"
    3.15  					<< "\\blinkTo\\b" 
    3.16  					<< "\\bloadImage\\b"
    3.17 @@ -53,7 +54,9 @@
    3.18  					<< "\\bmoveBranchDown\\b"
    3.19  					<< "\\bmove\\b" 
    3.20  					<< "\\bmoveRel\\b"
    3.21 +					<< "\\bnop\\b"
    3.22  					<< "\\bpaste\\b" 
    3.23 +					<< "\\bqa\\b" 
    3.24  					<< "\\bsaveImage\\b" 
    3.25  					<< "\\bscroll\\b" 
    3.26  					<< "\\bselect\\b" 
     4.1 --- a/mainwindow.cpp	Thu Jun 14 10:21:40 2007 +0000
     4.2 +++ b/mainwindow.cpp	Thu Jun 14 10:21:41 2007 +0000
     4.3 @@ -33,6 +33,7 @@
     4.4  extern QString vymName;
     4.5  extern QString vymVersion;
     4.6  extern QString vymBuildDate;
     4.7 +extern bool debug;
     4.8  
     4.9  QMenu* branchContextMenu;
    4.10  QMenu* branchAddContextMenu;
    4.11 @@ -83,10 +84,11 @@
    4.12  		qWarning ("Mainwindow: Could not create temporary directory, failed to start vym");
    4.13  		exit (1);
    4.14  	}
    4.15 +	if (debug) qDebug (QString("vym tmpDir=%1").arg(tmpVymDir) );
    4.16  
    4.17  	// Create direcctory for clipboard
    4.18  	clipboardDir=tmpVymDir+"/clipboard";
    4.19 -	clipboardFile="part.xml";
    4.20 +	clipboardFile="map.xml";
    4.21  	QDir d(clipboardDir);
    4.22  	d.mkdir (clipboardDir,true);
    4.23  	makeSubDirs (clipboardDir);
    4.24 @@ -239,7 +241,7 @@
    4.25  
    4.26  void Main::statusMessage(const QString &s)
    4.27  {
    4.28 -	statusBar()->message (s);
    4.29 +	statusBar()->message( s);
    4.30  }
    4.31  
    4.32  void Main::closeEvent (QCloseEvent* )
    4.33 @@ -3321,7 +3323,7 @@
    4.34  
    4.35  void Main::windowToggleNoteEditor()
    4.36  {
    4.37 -	if (textEditor->showWithMain() )
    4.38 +	if (textEditor->isVisible() )
    4.39  		windowHideNoteEditor();
    4.40  	else
    4.41  		windowShowNoteEditor();
     5.1 --- a/mapeditor.cpp	Thu Jun 14 10:21:40 2007 +0000
     5.2 +++ b/mapeditor.cpp	Thu Jun 14 10:21:41 2007 +0000
     5.3 @@ -133,6 +133,10 @@
     5.4  	// Create temporary files
     5.5  	makeTmpDirs();
     5.6  
     5.7 +	curStep=0;
     5.8 +	redosAvail=0;
     5.9 +	undosAvail=0;
    5.10 +
    5.11  	setAcceptDrops (true);	
    5.12  
    5.13  	mapCenter->reposition();	//	for positioning heading
    5.14 @@ -314,9 +318,6 @@
    5.15  
    5.16  	if (debug) cout << "ME::saveState() for  "<<mapName.ascii()<<endl;
    5.17  	
    5.18 -	int undosAvail=undoSet.readNumEntry ("/history/undosAvail",0);
    5.19 -	int redosAvail=undoSet.readNumEntry ("/history/redosAvail",0);
    5.20 -	int curStep=undoSet.readNumEntry ("/history/curStep",0);
    5.21  	// Find out current undo directory
    5.22  	if (undosAvail<stepsTotal) undosAvail++;
    5.23  	curStep++;
    5.24 @@ -587,6 +588,18 @@
    5.25  			QColor c=parser.parColor (ok,0);
    5.26  			if (ok) colorSubtree (c);
    5.27  		}	
    5.28 +	} else if (com=="copy")
    5.29 +	{
    5.30 +		if (xelection.isEmpty())
    5.31 +		{
    5.32 +			parser.setError (Aborted,"Nothing selected");
    5.33 +		} else if (! selb )
    5.34 +		{				  
    5.35 +			parser.setError (Aborted,"Type of selection is not a branch");
    5.36 +		} else if (parser.checkParamCount(0))
    5.37 +		{	
    5.38 +			//FIXME missing action for copy
    5.39 +		}	
    5.40  	} else if (com=="cut")
    5.41  	{
    5.42  		if (xelection.isEmpty())
    5.43 @@ -777,6 +790,8 @@
    5.44  				if (ok) moveRel (x,y);
    5.45  			}
    5.46  		}	
    5.47 +	} else if (com=="nop")
    5.48 +	{
    5.49  	} else if (com=="paste")
    5.50  	{
    5.51  		if (xelection.isEmpty() )
    5.52 @@ -785,9 +800,76 @@
    5.53  		} else if (! selb )
    5.54  		{				  
    5.55  			parser.setError (Aborted,"Type of selection is not a branch");
    5.56 -		} else if (parser.checkParamCount(0))
    5.57 +		} else if (parser.checkParamCount(1))
    5.58  		{	
    5.59 -			paste();
    5.60 +			x=parser.parInt (ok,0);
    5.61 +			if (ok) pasteNoSave(x);
    5.62 +		}	
    5.63 +	/////////////////////////////////////////////////////////////////////
    5.64 +	} else if (com=="qa")
    5.65 +	{
    5.66 +		if (xelection.isEmpty() )
    5.67 +		{
    5.68 +			parser.setError (Aborted,"Nothing selected");
    5.69 +		} else if (! selb )
    5.70 +		{				  
    5.71 +			parser.setError (Aborted,"Type of selection is not a branch");
    5.72 +		} else if (parser.checkParamCount(4))
    5.73 +		{	
    5.74 +			QString c,u;
    5.75 +			c=parser.parString (ok,0);
    5.76 +			if (!ok)
    5.77 +			{
    5.78 +				parser.setError (Aborted,"No comment given");
    5.79 +			} else
    5.80 +			{
    5.81 +				s=parser.parString (ok,1);
    5.82 +				if (!ok)
    5.83 +				{
    5.84 +					parser.setError (Aborted,"First parameter is not a string");
    5.85 +				} else
    5.86 +				{
    5.87 +					t=parser.parString (ok,2);
    5.88 +					if (!ok)
    5.89 +					{
    5.90 +						parser.setError (Aborted,"Condition is not a string");
    5.91 +					} else
    5.92 +					{
    5.93 +						u=parser.parString (ok,3);
    5.94 +						if (!ok)
    5.95 +						{
    5.96 +							parser.setError (Aborted,"Third parameter is not a string");
    5.97 +						} else
    5.98 +						{
    5.99 +							if (s!="heading")
   5.100 +							{
   5.101 +								parser.setError (Aborted,"Unknown type: "+s);
   5.102 +							} else
   5.103 +							{
   5.104 +								if (! (t=="eq") ) 
   5.105 +								{
   5.106 +									parser.setError (Aborted,"Unknown operator: "+t);
   5.107 +								} else
   5.108 +								{
   5.109 +									if (! selb    )
   5.110 +									{
   5.111 +										parser.setError (Aborted,"Type of selection is not a branch");
   5.112 +									} else
   5.113 +									{
   5.114 +										if (selb->getHeading() == u)
   5.115 +										{
   5.116 +											cout << "PASSED: " << c.ascii() << endl;
   5.117 +										} else
   5.118 +										{
   5.119 +											cout << "FAILED: " << c.ascii() << endl;
   5.120 +										}
   5.121 +									}
   5.122 +								}
   5.123 +							}
   5.124 +						} 
   5.125 +					} 
   5.126 +				} 
   5.127 +			}
   5.128  		}	
   5.129  	} else if (com=="saveImage")
   5.130  	{
   5.131 @@ -1613,23 +1695,18 @@
   5.132  	LinkableMapObj *sel=xelection.single();
   5.133  	if (sel)
   5.134  	{
   5.135 -		// write to directory
   5.136 -		QString saveFile=saveToDir (clipboardDir,clipboardFile+"-",true,QPointF(),sel ); // FIXME check FIO
   5.137 -		QFile file;
   5.138 -
   5.139 -		file.setName ( clipboardDir + "/"+clipboardFile);
   5.140 -		if ( !file.open( QIODevice::WriteOnly ) )
   5.141 +		if (redosAvail == 0)
   5.142  		{
   5.143 -			// This should neverever happen
   5.144 -			QMessageBox::critical (0,tr("Critical Export Error"),tr("MapEditor::exportXML couldn't open %1").arg(file.name()));
   5.145 -			return;
   5.146 -		}	
   5.147 -
   5.148 -		// Write it finally, and write in UTF8, no matter what 
   5.149 -		QTextStream ts( &file );
   5.150 -		ts.setEncoding (QTextStream::UnicodeUTF8);
   5.151 -		ts << saveFile;
   5.152 -		file.close();
   5.153 +			// Copy to history
   5.154 +			QString s=sel->getSelectString();
   5.155 +			saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy to clipboard",sel  );
   5.156 +			curClipboard=curStep;
   5.157 +		}
   5.158 +
   5.159 +		// Copy also to global clipboard, because we are at last step in history
   5.160 +		QString bakMapName=QDir::convertSeparators (QString("history-%1").arg(curStep));
   5.161 +		QString bakMapDir=QDir::convertSeparators (tmpMapDir +"/"+bakMapName);
   5.162 +		copyDir (bakMapDir,clipboardDir );
   5.163  
   5.164  		clipboardEmpty=false;
   5.165  		updateActions();
   5.166 @@ -1638,10 +1715,6 @@
   5.167  
   5.168  void MapEditor::redo()
   5.169  {
   5.170 -	// Restore variables
   5.171 -	int curStep=undoSet.readNumEntry (QString("/history/curStep"));
   5.172 -	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
   5.173 -	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
   5.174  	// Can we undo at all?
   5.175  	if (redosAvail<1) return;
   5.176  
   5.177 @@ -1722,14 +1795,11 @@
   5.178  
   5.179  void MapEditor::undo()
   5.180  {
   5.181 -	// Restore variables
   5.182 -	int curStep=undoSet.readNumEntry (QString("/history/curStep"));
   5.183 -	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
   5.184 -	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
   5.185 -
   5.186  	// Can we undo at all?
   5.187  	if (undosAvail<1) return;
   5.188  
   5.189 +	mainWindow->statusMessage (tr("Autosave disabled during undo."));
   5.190 +
   5.191  	bool blockSaveStateOrg=blockSaveState;
   5.192  	blockSaveState=true;
   5.193  	
   5.194 @@ -1902,11 +1972,19 @@
   5.195  	}		
   5.196  }
   5.197  
   5.198 -void MapEditor::pasteNoSave()
   5.199 +void MapEditor::pasteNoSave(const int &n)
   5.200  {
   5.201  	bool old=blockSaveState;
   5.202  	blockSaveState=true;
   5.203 -	load (clipboardDir+"/"+clipboardFile,ImportAdd);
   5.204 +	if (redosAvail > 0 || n!=0)
   5.205 +	{
   5.206 +		// Use the "historical" buffer
   5.207 +		QString bakMapName=QDir::convertSeparators (QString("history-%1").arg(n));
   5.208 +		QString bakMapDir=QDir::convertSeparators (tmpMapDir +"/"+bakMapName);
   5.209 +		load (bakMapDir+"/"+clipboardFile,ImportAdd);
   5.210 +	} else
   5.211 +		// Use the global buffer
   5.212 +		load (clipboardDir+"/"+clipboardFile,ImportAdd);
   5.213  	blockSaveState=old;
   5.214  }
   5.215  
   5.216 @@ -1918,10 +1996,10 @@
   5.217  		saveStateChangingPart(
   5.218  			sel,
   5.219  			sel,
   5.220 -			"paste ()",
   5.221 +			QString ("paste (%1)").arg(curClipboard),
   5.222  			QString("Paste to %1").arg( getName(sel))
   5.223  		);
   5.224 -		pasteNoSave();
   5.225 +		pasteNoSave(0);
   5.226  		mapCenter->reposition();
   5.227  	}
   5.228  }
   5.229 @@ -3759,7 +3837,8 @@
   5.230  
   5.231  void MapEditor::testFunction()
   5.232  {
   5.233 -	// This is the playground
   5.234 +	mainWindow->statusMessage("Biiiiiiiiiiiiiiing!");
   5.235 +	return;
   5.236  
   5.237  	BranchObj *bo=xelection.getBranch();
   5.238  	if (bo) animObjList.append( bo );
     6.1 --- a/mapeditor.h	Thu Jun 14 10:21:40 2007 +0000
     6.2 +++ b/mapeditor.h	Thu Jun 14 10:21:41 2007 +0000
     6.3 @@ -68,13 +68,13 @@
     6.4  	*/	
     6.5      void saveState(const SaveMode& savemode, const QString &undoSelection, const QString &undoCommand, const QString &redoSelection, const QString &redoCommand, const QString &comment, LinkableMapObj *saveSelection);
     6.6  	/*! Overloaded for convenience */
     6.7 -    void saveStateChangingPart(LinkableMapObj *undoSelection, LinkableMapObj* redoSelection, const QString &rendoCommand, const QString &comment);
     6.8 +    void saveStateChangingPart(LinkableMapObj *undoSelection, LinkableMapObj* redoSelection, const QString &redoCommand, const QString &comment);
     6.9  	/*! Overloaded for convenience */
    6.10      void saveStateRemovingPart(LinkableMapObj *redoSelection, const QString &comment);
    6.11  	/*! Overloaded for convenience */
    6.12 -    void saveState(LinkableMapObj *undoSelection, const QString &undoCommand, LinkableMapObj *redoSelection, const QString &rendoCommand, const QString &comment); 
    6.13 +    void saveState(LinkableMapObj *undoSelection, const QString &undoCommand, LinkableMapObj *redoSelection, const QString &redoCommand, const QString &comment); 
    6.14  	/*! Overloaded for convenience */
    6.15 -    void saveState(const QString &undoSelection, const QString &undoCommand, const QString &redoSelection, const QString &rendoCommand, const QString &comment) ;
    6.16 +    void saveState(const QString &undoSelection, const QString &undoCommand, const QString &redoSelection, const QString &redoCommand, const QString &comment) ;
    6.17  
    6.18  public:	
    6.19  
    6.20 @@ -144,7 +144,7 @@
    6.21  private:	
    6.22      void addMapReplaceInt(const QString & undoSel, const QString & path);
    6.23      void addMapInsertInt (const QString & path, int pos);
    6.24 -    void pasteNoSave();		// paste clipboard to branch
    6.25 +    void pasteNoSave(const int &n);		//!< paste clipboard to branch
    6.26  public:	
    6.27      void paste();		//!< Paste clipboard to branch and backup
    6.28      void cut();			//!< Cut to clipboard (and copy)
    6.29 @@ -365,8 +365,12 @@
    6.30  
    6.31  	QString tmpMapDir;			// tmp directory with undo history
    6.32  	QString histPath;			// Path to history file
    6.33 +	SimpleSettings undoSet;		// undo/redo commands, saved in histPath
    6.34  	int stepsTotal;				// total number of steps (undos+redos) 
    6.35 -	SimpleSettings undoSet;		// undo/redo commands
    6.36 +	int curStep;				// Current step in history (ring buffer)
    6.37 +	int curClipboard;			// number of history step, which is the current clipboard
    6.38 +	int redosAvail;				// Available number of redo steps
    6.39 +	int undosAvail;				// Available number of undo steps
    6.40  	bool blockReposition;		// block while load or undo
    6.41  	bool blockSaveState;		// block while load or undo
    6.42