Added history management for auto-bordering (and mass-addition in general)

Wed, 10 Apr 2013 04:34:19 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Wed, 10 Apr 2013 04:34:19 +0300
changeset 92
586d294ca83f
parent 91
b4dda6348e7e
child 93
92682e6369e9

Added history management for auto-bordering (and mass-addition in general)

gui.cpp file | annotate | diff | comparison | revisions
gui_editactions.cpp file | annotate | diff | comparison | revisions
history.cpp file | annotate | diff | comparison | revisions
history.h file | annotate | diff | comparison | revisions
--- a/gui.cpp	Wed Apr 10 03:47:17 2013 +0300
+++ b/gui.cpp	Wed Apr 10 04:34:19 2013 +0300
@@ -25,6 +25,7 @@
 #include "config.h"
 #include "misc.h"
 #include "colors.h"
+#include "history.h"
 #include "config.h"
 
 EXTERN_ACTION (newFile)
@@ -124,6 +125,8 @@
 	
 	for (QAction* act : qaDisabledActions)
 		act->setEnabled (false);
+	
+	History::updateActions ();
 }
 
 // =============================================================================
--- a/gui_editactions.cpp	Wed Apr 10 03:47:17 2013 +0300
+++ b/gui_editactions.cpp	Wed Apr 10 04:34:19 2013 +0300
@@ -246,6 +246,9 @@
 {
 	vector<LDObject*> objs = g_ForgeWindow->getSelectedObjects ();
 	
+	vector<ulong> ulaIndices;
+	vector<LDObject*> paObjs;
+	
 	for (LDObject* obj : objs) {
 		if (obj->getType() != OBJ_Quad && obj->getType() != OBJ_Triangle)
 			continue;
@@ -271,11 +274,17 @@
 		}
 		
 		for (short i = 0; i < dNumLines; ++i) {
+			ulong idx = obj->getIndex (g_CurrentFile) + i + 1;
+			
 			lines[i]->dColor = dEdgeColor;
-			g_CurrentFile->addObject (lines[i]);
+			g_CurrentFile->objects.insert (g_CurrentFile->objects.begin() + idx, lines[i]);
+			
+			ulaIndices.push_back (idx);
+			paObjs.push_back (lines[i]->clone ());
 		}
 	}
 	
+	History::addEntry (new AdditionHistory (ulaIndices, paObjs));
 	g_ForgeWindow->refresh ();
 }
 
--- a/history.cpp	Wed Apr 10 03:47:17 2013 +0300
+++ b/history.cpp	Wed Apr 10 04:34:19 2013 +0300
@@ -22,6 +22,9 @@
 #include "misc.h"
 #include "gui.h"
 
+EXTERN_ACTION (undo)
+EXTERN_ACTION (redo)
+
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
@@ -41,6 +44,8 @@
 		
 		entries.push_back (entry);
 		lPos++;
+		
+		updateActions ();
 	}
 	
 	// =========================================================================
@@ -49,6 +54,7 @@
 			return; // nothing to undo
 		
 		entries[lPos--]->undo ();
+		updateActions ();
 	}
 	
 	// =========================================================================
@@ -57,6 +63,13 @@
 			return; // nothing to redo;
 		
 		entries[++lPos]->redo ();
+		updateActions ();
+	}
+	
+	// =========================================================================
+	void updateActions () {
+		ACTION_NAME (undo)->setEnabled (lPos > -1);
+		ACTION_NAME (redo)->setEnabled (lPos < (long) entries.size () - 1);
 	}
 }
 
@@ -155,4 +168,35 @@
 	g_ForgeWindow->buildObjList ();
 }
 
-ListMoveHistory::~ListMoveHistory() {}
\ No newline at end of file
+ListMoveHistory::~ListMoveHistory() {}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+AdditionHistory::~AdditionHistory () {
+	for (LDObject* pObj : paObjs)
+		delete pObj;
+}
+
+void AdditionHistory::undo () {
+	for (ulong i = 0; i < paObjs.size(); ++i) {
+		ulong idx = ulaIndices[ulaIndices.size() - i - 1];
+		LDObject* obj = g_CurrentFile->objects[idx];
+		
+		g_CurrentFile->forgetObject (obj);
+		delete obj;
+	}
+	
+	g_ForgeWindow->refresh ();
+}
+
+void AdditionHistory::redo () {
+	for (ulong i = 0; i < paObjs.size(); ++i) {
+		ulong idx = ulaIndices[i];
+		LDObject* obj = paObjs[i]->clone ();
+		
+		g_CurrentFile->objects.insert (g_CurrentFile->objects.begin() + idx, obj);
+	}
+	
+	g_ForgeWindow->refresh ();
+}
\ No newline at end of file
--- a/history.h	Wed Apr 10 03:47:17 2013 +0300
+++ b/history.h	Wed Apr 10 04:34:19 2013 +0300
@@ -98,12 +98,27 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
+class AdditionHistory : public HistoryEntry {
+public:
+	IMPLEMENT_HISTORY_TYPE (Addition)
+	
+	std::vector<ulong> ulaIndices;
+	std::vector<LDObject*> paObjs;
+	
+	AdditionHistory (std::vector<ulong> ulaIndices, std::vector<LDObject*> paObjs) :
+		ulaIndices (ulaIndices), paObjs (paObjs) {}
+};
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
 namespace History {
 	extern std::vector<HistoryEntry*> entries;
 	
 	void addEntry (HistoryEntry* entry);
 	void undo ();
 	void redo ();
+	void updateActions ();
 };
 
 #endif // HISTORY_H
\ No newline at end of file

mercurial