- made history ignore file loading and undo/redo calls instead of having hard-coded ignore on the actions, this will probably save a lot of hair

Wed, 18 Dec 2013 23:38:45 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Wed, 18 Dec 2013 23:38:45 +0200
changeset 563
10939452bf86
parent 562
c4ff45e98551
child 564
79b23e02dcf1

- made history ignore file loading and undo/redo calls instead of having hard-coded ignore on the actions, this will probably save a lot of hair

src/document.cc file | annotate | diff | comparison | revisions
src/gui.cc file | annotate | diff | comparison | revisions
src/history.cc file | annotate | diff | comparison | revisions
--- a/src/document.cc	Wed Dec 18 20:43:50 2013 +0200
+++ b/src/document.cc	Wed Dec 18 23:38:45 2013 +0200
@@ -415,12 +415,18 @@
 	LDDocument* load = new LDDocument;
 	load->setName (path);
 
+	// Don't take the file loading as actual edits to the file
+	load->getHistory()->setIgnoring (true);
+
 	int numWarnings;
 	bool ok;
 	QList<LDObject*> objs = loadFileContents (f, &numWarnings, &ok);
 
 	if (!ok)
+	{	delete f;
+		delete load;
 		return null;
+	}
 
 	for (LDObject* obj : objs)
 		load->addObject (obj);
@@ -434,6 +440,7 @@
 		log (QObject::tr ("File %1 parsed successfully (%2 errors)."), path, numWarnings);
 	}
 
+	load->getHistory()->setIgnoring (false);
 	return load;
 }
 
@@ -445,19 +452,18 @@
 
 	// If we have unsaved changes, warn and give the option of saving.
 	if (hasUnsavedChanges())
-	{	str message = fmt ("There are unsaved changes to %1. Should it be saved?",
-						   (getName().length() > 0) ? getName() : "<anonymous>");
+	{	str message = fmt (tr ("There are unsaved changes to %1. Should it be saved?"),
+			(getName().length() > 0) ? getName() : tr ("<anonymous>"));
 
-		int button = msgbox::question (g_win, "Unsaved Changes", message,
-									   (msgbox::Yes | msgbox::No | msgbox::Cancel), msgbox::Cancel);
+		int button = msgbox::question (g_win, tr ("Unsaved Changes"), message,
+			(msgbox::Yes | msgbox::No | msgbox::Cancel), msgbox::Cancel);
 
 		switch (button)
 		{	case msgbox::Yes:
-
-				// If we don't have a file path yet, we have to ask the user for one.
+			{	// If we don't have a file path yet, we have to ask the user for one.
 				if (getName().length() == 0)
-				{	str newpath = QFileDialog::getSaveFileName (g_win, "Save As",
-								  getCurrentDocument()->getName(), "LDraw files (*.dat *.ldr)");
+				{	str newpath = QFileDialog::getSaveFileName (g_win, tr ("Save As"),
+						getCurrentDocument()->getName(), tr ("LDraw files (*.dat *.ldr)"));
 
 					if (newpath.length() == 0)
 						return false;
@@ -466,16 +472,15 @@
 				}
 
 				if (!save())
-				{	message = fmt (QObject::tr ("Failed to save %1: %2\nDo you still want to close?"),
-								   getName(), strerror (errno));
+				{	message = fmt (tr ("Failed to save %1 (%2)\nDo you still want to close?"),
+						getName(), strerror (errno));
 
-					if (msgbox::critical (g_win, "Save Failure", message,
-										  (msgbox::Yes | msgbox::No), msgbox::No) == msgbox::No)
+					if (msgbox::critical (g_win, tr ("Save Failure"), message,
+						(msgbox::Yes | msgbox::No), msgbox::No) == msgbox::No)
 					{	return false;
 					}
 				}
-
-				break;
+			} break;
 
 			case msgbox::Cancel:
 				return false;
@@ -494,7 +499,7 @@
 {	// Remove all loaded files and the objects they contain
 	QList<LDDocument*> files = g_loadedFiles;
 
-for (LDDocument * file : files)
+	for (LDDocument* file : files)
 		delete file;
 }
 
@@ -517,7 +522,7 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 void addRecentFile (str path)
-{	alias rfiles = io_recentfiles.value;
+{	auto& rfiles = io_recentfiles.value;
 	int idx = rfiles.indexOf (path);
 
 	// If this file already is in the list, pop it out.
@@ -924,12 +929,14 @@
 // =============================================================================
 // -----------------------------------------------------------------------------
 void LDDocument::setObject (int idx, LDObject* obj)
-{	assert (idx < m_Objects.size());
+{	assert (idx >= 0 && idx < m_Objects.size());
 
 	// Mark this change to history
-	str oldcode = getObject (idx)->raw();
-	str newcode = obj->raw();
-	*m_History << new EditHistory (idx, oldcode, newcode);
+	if (!m_History->isIgnoring())
+	{	str oldcode = getObject (idx)->raw();
+		str newcode = obj->raw();
+		*m_History << new EditHistory (idx, oldcode, newcode);
+	}
 
 	obj->setFile (this);
 	m_Objects[idx] = obj;
--- a/src/gui.cc	Wed Dec 18 20:43:50 2013 +0200
+++ b/src/gui.cc	Wed Dec 18 23:38:45 2013 +0200
@@ -863,11 +863,7 @@
 	f->getListItem()->setIcon (f->hasUnsavedChanges() ? getIcon ("file-save") : QIcon());
 }
 
-void ForgeWindow::beginAction (QAction* act)
-{	// Open the history so we can record the edits done during this action.
-	if (act == ui->actionUndo || act == ui->actionRedo || act == ui->actionOpen)
-		getCurrentDocument()->getHistory()->setIgnoring (true);
-}
+void ForgeWindow::beginAction (QAction* act) {}
 
 void ForgeWindow::endAction()
 {	// Close the history now.
@@ -876,9 +872,6 @@
 	// Update the list item of the current file - we may need to draw an icon
 	// now that marks it as having unsaved changes.
 	updateDocumentListItem (getCurrentDocument());
-
-	// We're done with the action, the history should stop ignoring now.
-	getCurrentDocument()->getHistory()->setIgnoring (false);
 }
 
 // =============================================================================
--- a/src/history.cc	Wed Dec 18 20:43:50 2013 +0200
+++ b/src/history.cc	Wed Dec 18 23:38:45 2013 +0200
@@ -36,6 +36,9 @@
 {	if (m_changesets.isEmpty() || getPosition() == -1)
 		return;
 
+	// Don't take the changes done here as actual edits to the document
+	setIgnoring (true);
+
 	const Changeset& set = getChangeset (getPosition());
 	g_fullRefresh = false;
 
@@ -54,6 +57,7 @@
 
 	g_win->updateActions();
 	dlog ("Position is now %1", getPosition());
+	setIgnoring (false);
 }
 
 // =============================================================================
@@ -62,6 +66,7 @@
 {	if (getPosition() == m_changesets.size())
 		return;
 
+	setIgnoring (true);
 	const Changeset& set = getChangeset (getPosition() + 1);
 	g_fullRefresh = false;
 
@@ -78,6 +83,7 @@
 
 	g_win->updateActions();
 	dlog ("Position is now %1", getPosition());
+	setIgnoring (false);
 }
 
 // =============================================================================

mercurial