Added history management for cut and paste, copy doesn't alter the object list by itself so it doesn't touch history

Wed, 10 Apr 2013 14:10:58 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Wed, 10 Apr 2013 14:10:58 +0300
changeset 94
a9e67f6e610e
parent 93
92682e6369e9
child 95
0a8ad4e3e7c1

Added history management for cut and paste, copy doesn't alter the object list by itself so it doesn't touch history

file.cpp file | annotate | diff | comparison | revisions
file.h file | annotate | diff | comparison | revisions
gui.cpp file | annotate | diff | comparison | revisions
gui.h file | annotate | diff | comparison | revisions
gui_editactions.cpp file | annotate | diff | comparison | revisions
history.cpp file | annotate | diff | comparison | revisions
--- a/file.cpp	Wed Apr 10 13:24:35 2013 +0300
+++ b/file.cpp	Wed Apr 10 14:10:58 2013 +0300
@@ -468,14 +468,15 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-void OpenFile::addObject (LDObject* obj) {
+ulong OpenFile::addObject (LDObject* obj) {
 	if (this != g_CurrentFile) {
 		objects.insert (objects.end (), obj);
-		return;
+		return objects.size() - 1;
 	}
 	
 	const ulong ulSpot = g_ForgeWindow->getInsertionPoint ();
 	objects.insert (objects.begin() + ulSpot, obj);
+	return ulSpot;
 }
 
 // =============================================================================
--- a/file.h	Wed Apr 10 13:24:35 2013 +0300
+++ b/file.h	Wed Apr 10 14:10:58 2013 +0300
@@ -42,7 +42,7 @@
 	bool save (str zPath = "");
 	
 	// Adds an object to this file at the appropriate location.
-	void addObject (LDObject* obj);
+	ulong addObject (LDObject* obj);
 	
 	// Deletes the given object from the object chain.
 	void forgetObject (LDObject* obj);
--- a/gui.cpp	Wed Apr 10 13:24:35 2013 +0300
+++ b/gui.cpp	Wed Apr 10 14:10:58 2013 +0300
@@ -298,11 +298,16 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
-void ForgeWindow::deleteSelection () {
+void ForgeWindow::deleteSelection (vector<ulong>* ulapIndices, std::vector<LDObject*>* papObjects) {
 	vector<LDObject*> objs = getSelectedObjects ();
 	
 	// Delete the objects that were being selected
 	for (LDObject* obj : objs) {
+		if (papObjects && ulapIndices) {
+			papObjects->push_back (obj->clone ());
+			ulapIndices->push_back (obj->getIndex (g_CurrentFile));
+		}
+		
 		g_CurrentFile->forgetObject (obj);
 		delete obj;
 	}
--- a/gui.h	Wed Apr 10 13:24:35 2013 +0300
+++ b/gui.h	Wed Apr 10 14:10:58 2013 +0300
@@ -110,7 +110,7 @@
 	void refresh ();
 	std::vector<LDObject*> getSelectedObjects ();
 	ulong getInsertionPoint ();
-	void deleteSelection ();
+	void deleteSelection (vector<ulong>* ulapIndices, std::vector<LDObject*>* papObjects);
 	void updateToolBars ();
 
 private:
--- a/gui_editactions.cpp	Wed Apr 10 13:24:35 2013 +0300
+++ b/gui_editactions.cpp	Wed Apr 10 14:10:58 2013 +0300
@@ -54,10 +54,14 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 ACTION (cut, "Cut", "cut", "Cut the current selection to clipboard.", CTRL (X)) {
+	vector<ulong> ulaIndices;
+	vector<LDObject*> copies;
+	
 	if (!copyToClipboard ())
 		return;
 	
-	g_ForgeWindow->deleteSelection ();
+	g_ForgeWindow->deleteSelection (&ulaIndices, &copies);
+	History::addEntry (new DeleteHistory (ulaIndices, copies));
 }
 
 // =============================================================================
@@ -71,9 +75,17 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 ACTION (paste, "Paste", "paste", "Paste clipboard contents.", CTRL (V)) {
-	for (LDObject* obj : g_Clipboard)
-		g_CurrentFile->addObject (obj->clone ());
+	vector<ulong> ulaIndices;
+	vector<LDObject*> paCopies;
 	
+	for (LDObject* obj : g_Clipboard) {
+		ulong idx = g_CurrentFile->addObject (obj->clone ());
+		
+		ulaIndices.push_back (idx);
+		paCopies.push_back (obj->clone ());
+	}
+	
+	History::addEntry (new AdditionHistory (ulaIndices, paCopies));
 	g_ForgeWindow->refresh ();
 }
 
@@ -81,24 +93,13 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
 ACTION (del, "Delete", "delete", "Delete the selection", KEY (Delete)) {
-	vector<LDObject*> sel = g_ForgeWindow->getSelectedObjects ();
-	
-	if (sel.size() == 0)
-		return;
-	
 	vector<ulong> ulaIndices;
 	vector<LDObject*> copies;
 	
-	for (LDObject* obj : sel) {
-		copies.insert (copies.begin(), obj->clone ());
-		ulaIndices.insert (ulaIndices.begin(), obj->getIndex (g_CurrentFile));
-		
-		g_CurrentFile->forgetObject (obj);
-		delete obj;
-	}
+	g_ForgeWindow->deleteSelection (&ulaIndices, &copies);
 	
-	History::addEntry (new DeleteHistory (ulaIndices, copies));
-	g_ForgeWindow->refresh ();
+	if (copies.size ())
+		History::addEntry (new DeleteHistory (ulaIndices, copies));
 }
 
 // =============================================================================
--- a/history.cpp	Wed Apr 10 13:24:35 2013 +0300
+++ b/history.cpp	Wed Apr 10 14:10:58 2013 +0300
@@ -78,8 +78,9 @@
 // =============================================================================
 void DeleteHistory::undo () {
 	for (ulong i = 0; i < cache.size(); ++i) {
-		LDObject* obj = cache[i]->clone ();
-		g_CurrentFile->objects.insert (g_CurrentFile->objects.begin() + indices[i], obj);
+		ulong idx = cache.size() - i - 1;
+		LDObject* obj = cache[idx]->clone ();
+		g_CurrentFile->objects.insert (g_CurrentFile->objects.begin() + indices[idx], obj);
 	}
 	
 	g_ForgeWindow->refresh ();

mercurial