Added inlining history management

Thu, 11 Apr 2013 02:26:14 +0300

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Thu, 11 Apr 2013 02:26:14 +0300
changeset 98
5dcc551f260a
parent 97
52bcca21579e
child 99
920d51fec412

Added inlining history management

file.h file | annotate | diff | comparison | revisions
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
zz_historyDialog.cpp file | annotate | diff | comparison | revisions
--- a/file.h	Thu Apr 11 01:35:30 2013 +0300
+++ b/file.h	Thu Apr 11 02:26:14 2013 +0300
@@ -46,6 +46,10 @@
 	
 	// Deletes the given object from the object chain.
 	void forgetObject (LDObject* obj);
+	
+	LDObject* object (size_t uPos) const {
+		return objects[uPos];
+	}
 };
 
 // Close all current loaded files and start off blank.
--- a/gui.cpp	Thu Apr 11 01:35:30 2013 +0300
+++ b/gui.cpp	Thu Apr 11 02:26:14 2013 +0300
@@ -176,7 +176,6 @@
 	ADD_MENU_ITEM (Edit, moveDown)			// Move Down
 	qEditMenu->addSeparator ();				// -----
 	ADD_MENU_ITEM (Edit, setColor)			// Set Color
-	qEditMenu->addSeparator ();				// -----
 	ADD_MENU_ITEM (Edit, inlineContents)	// Inline
 	ADD_MENU_ITEM (Edit, deepInline)		// Deep Inline
 	ADD_MENU_ITEM (Edit, splitQuads)		// Split Quads
@@ -250,6 +249,7 @@
 	initSingleToolBar ("Objects");
 	ADD_TOOLBAR_ITEM (setColor)
 	ADD_TOOLBAR_ITEM (inlineContents)
+	ADD_TOOLBAR_ITEM (deepInline)
 	ADD_TOOLBAR_ITEM (splitQuads)
 	ADD_TOOLBAR_ITEM (setContents)
 	ADD_TOOLBAR_ITEM (makeBorders)
--- a/gui_editactions.cpp	Thu Apr 11 01:35:30 2013 +0300
+++ b/gui_editactions.cpp	Thu Apr 11 02:26:14 2013 +0300
@@ -109,6 +109,18 @@
 static void doInline (bool bDeep) {
 	vector<LDObject*> sel = g_ForgeWindow->getSelectedObjects ();
 	
+	// History stuff
+	vector<LDSubfile*> paRefs;
+	vector<ulong> ulaRefIndices, ulaBitIndices;
+	
+	for (LDObject* obj : sel) {
+		if (obj->getType() != OBJ_Subfile)
+			continue;
+		
+		ulaRefIndices.push_back (obj->getIndex (g_CurrentFile));
+		paRefs.push_back (static_cast<LDSubfile*> (obj)->clone ());
+	}
+	
 	for (LDObject* obj : sel) {
 		// Obviously, only subfiles can be inlined.
 		if (obj->getType() != OBJ_Subfile)
@@ -126,14 +138,17 @@
 		vector<LDObject*> objs = ref->inlineContents (bDeep, true);
 		
 		// Merge in the inlined objects
-		for (LDObject* inlineobj : objs)
+		for (LDObject* inlineobj : objs) {
+			ulaBitIndices.push_back (idx);
 			g_CurrentFile->objects.insert (g_CurrentFile->objects.begin() + idx++, inlineobj);
+		}
 		
 		// Delete the subfile now as it's been inlined.
 		g_CurrentFile->forgetObject (ref);
 		delete ref;
 	}
 	
+	History::addEntry (new InlineHistory (ulaBitIndices, ulaRefIndices, paRefs, bDeep));
 	g_ForgeWindow->refresh ();
 }
 
--- a/history.cpp	Thu Apr 11 01:35:30 2013 +0300
+++ b/history.cpp	Thu Apr 11 02:26:14 2013 +0300
@@ -256,4 +256,45 @@
 	}
 	
 	g_ForgeWindow->refresh ();
+}
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
+void InlineHistory::undo () {
+	for (long i = ulaBitIndices.size() - 1; i >= 0; --i) {
+		LDObject* obj = g_CurrentFile->objects [ulaBitIndices[i]];
+		g_CurrentFile->forgetObject (obj);
+		delete obj;
+	}
+	
+	for (ulong i = 0; i < ulaRefIndices.size(); ++i) {
+		LDSubfile* obj = paRefs[i]->clone ();
+		g_CurrentFile->objects.insert (g_CurrentFile->objects.begin() + ulaRefIndices[i], obj);
+	}
+	
+	g_ForgeWindow->refresh ();
+}
+
+void InlineHistory::redo () {
+	for (long i = ulaRefIndices.size() - 1; i >= 0; --i) {
+		ulong idx = ulaRefIndices[i];
+		
+		assert (g_CurrentFile->object (idx)->getType () == OBJ_Subfile);
+		LDSubfile* ref = static_cast<LDSubfile*> (g_CurrentFile->object (idx));
+		vector<LDObject*> objs = ref->inlineContents (bDeep, false);
+		
+		for (LDObject* obj : objs)
+			g_CurrentFile->objects.insert (g_CurrentFile->objects.begin() + idx++, obj);
+		
+		g_CurrentFile->forgetObject (ref);
+		delete ref;
+	}
+	
+	g_ForgeWindow->refresh ();
+}
+
+InlineHistory::~InlineHistory () {
+	for (LDSubfile* ref : paRefs)
+		delete ref;
 }
\ No newline at end of file
--- a/history.h	Thu Apr 11 01:35:30 2013 +0300
+++ b/history.h	Thu Apr 11 02:26:14 2013 +0300
@@ -36,6 +36,7 @@
 	HISTORY_ListMove,
 	HISTORY_Add,
 	HISTORY_QuadSplit,
+	HISTORY_Inline,
 };
 
 // =============================================================================
@@ -151,6 +152,22 @@
 // =============================================================================
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 // =============================================================================
+class InlineHistory : public HistoryEntry {
+public:
+	IMPLEMENT_HISTORY_TYPE (Inline)
+	
+	const std::vector<ulong> ulaBitIndices, ulaRefIndices;
+	const std::vector<LDSubfile*> paRefs;
+	const bool bDeep;
+	
+	InlineHistory (const std::vector<ulong> ulaBitIndices, const std::vector<ulong> ulaRefIndices,
+		const std::vector<LDSubfile*> paRefs, const bool bDeep) :
+		ulaBitIndices (ulaBitIndices), ulaRefIndices (ulaRefIndices), paRefs (paRefs), bDeep (bDeep) {}
+};
+
+// =============================================================================
+// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+// =============================================================================
 namespace History {
 	extern std::vector<HistoryEntry*> entries;
 	
--- a/zz_historyDialog.cpp	Thu Apr 11 01:35:30 2013 +0300
+++ b/zz_historyDialog.cpp	Thu Apr 11 02:26:14 2013 +0300
@@ -54,6 +54,7 @@
 	qButtonLayout->addStretch ();
 	
 	QGridLayout* qLayout = new QGridLayout;
+	qLayout->setColumnStretch (0, 1);
 	qLayout->addWidget (qHistoryList, 0, 0, 2, 1);
 	qLayout->addLayout (qButtonLayout, 0, 1);
 	qLayout->addWidget (qButtons, 1, 1);
@@ -190,6 +191,19 @@
 			}
 			break;
 		
+		case HISTORY_Inline:
+			{
+				InlineHistory* subentry = static_cast<InlineHistory*> (entry);
+				
+				zText.format ("%s %lu subfiles:\n%lu resultants",
+					(subentry->bDeep) ? "Deep-inlined" : "Inlined",
+					(ulong) subentry->paRefs.size(),
+					(ulong) subentry->ulaBitIndices.size());
+				
+				qEntryIcon = getIcon (subentry->bDeep ? "inline-deep" : "inline");
+			}
+			break;
+		
 		default:
 			zText = "???";
 			break;

mercurial