The current document pointer may no longer be null.

Sat, 04 Feb 2017 14:24:16 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 04 Feb 2017 14:24:16 +0200
changeset 1090
ed73c4f48ca4
parent 1089
97a539057536
child 1091
4a754362f660

The current document pointer may no longer be null.

src/documentmanager.cpp file | annotate | diff | comparison | revisions
src/ldDocument.cpp file | annotate | diff | comparison | revisions
src/ldDocument.h file | annotate | diff | comparison | revisions
src/ldObject.cpp file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/mainwindow.h file | annotate | diff | comparison | revisions
src/primitives.cpp file | annotate | diff | comparison | revisions
src/toolsets/algorithmtoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/filetoolset.cpp file | annotate | diff | comparison | revisions
--- a/src/documentmanager.cpp	Fri Feb 03 10:51:08 2017 +0200
+++ b/src/documentmanager.cpp	Sat Feb 04 14:24:16 2017 +0200
@@ -119,7 +119,7 @@
 		return;
 	}
 
-	file->openForEditing();
+	m_window->openDocumentForEditing(file);
 	m_window->closeInitialDocument();
 	m_window->changeDocument (file);
 	m_window->doFullRefresh();
--- a/src/ldDocument.cpp	Fri Feb 03 10:51:08 2017 +0200
+++ b/src/ldDocument.cpp	Sat Feb 04 14:24:16 2017 +0200
@@ -31,7 +31,7 @@
     Model {parent},
 	HierarchyElement (parent),
 	m_history (new EditHistory (this)),
-	m_flags(IsCache | VerticesOutdated | NeedsVertexMerge | NeedsRecache),
+    m_flags(IsFrozen | VerticesOutdated | NeedsVertexMerge | NeedsRecache),
 	m_savePosition(-1),
     m_tabIndex(-1),
 	m_gldata (new LDGLData),
@@ -104,23 +104,17 @@
 	m_defaultName = value;
 }
 
-void LDDocument::openForEditing()
+void LDDocument::setFrozen(bool value)
 {
-	if (isCache())
-	{
-		m_flags &= ~IsCache;
-		print ("Opened %1", name());
-
-		// Cache files are not compiled by the GL renderer. Now that this file is open for editing, it needs to be
-		// compiled.
-		m_window->renderer()->compiler()->compileDocument (this);
-		m_window->updateDocumentList();
-	}
+	if (value)
+		m_flags |= IsFrozen;
+	else
+		m_flags &= ~IsFrozen;
 }
 
-bool LDDocument::isCache() const
+bool LDDocument::isFrozen() const
 {
-	return !!(m_flags & IsCache);
+	return !!(m_flags & IsFrozen);
 }
 
 void LDDocument::addHistoryStep()
@@ -150,9 +144,9 @@
 
 void LDDocument::close()
 {
-	if (not isCache())
+	if (not isFrozen())
 	{
-		m_flags |= IsCache;
+		m_flags |= IsFrozen;
 		print ("Closed %1", name());
 		m_window->updateDocumentList();
 
@@ -229,7 +223,7 @@
 //
 bool LDDocument::save (QString path, int64* sizeptr)
 {
-	if (isCache())
+	if (isFrozen())
 		return false;
 
 	if (path.isEmpty())
@@ -335,7 +329,7 @@
 	connect(obj, SIGNAL(codeChanged(int,QString,QString)), this, SLOT(objectChanged(int,QString,QString)));
 
 #ifdef DEBUG
-	if (not isCache())
+	if (not isFrozen())
 		dprint ("Inserted object #%1 (%2) at %3\n", obj->id(), obj->typeName(), pos);
 #endif
 }
@@ -367,7 +361,7 @@
 {
 	LDObject* object = getObject(position);
 
-	if (not isCache() and not checkFlag(IsBeingDestroyed))
+	if (not isFrozen() and not checkFlag(IsBeingDestroyed))
 	{
 		history()->add(new DelHistoryEntry {position, object});
 		m_objectVertices.remove(object);
@@ -381,7 +375,7 @@
 //
 bool LDDocument::hasUnsavedChanges() const
 {
-	return not isCache() and history()->position() != savePosition();
+	return not isFrozen() and history()->position() != savePosition();
 }
 
 // =============================================================================
--- a/src/ldDocument.h	Fri Feb 03 10:51:08 2017 +0200
+++ b/src/ldDocument.h	Sat Feb 04 14:24:16 2017 +0200
@@ -44,7 +44,7 @@
 public:
 	enum Flag
 	{
-		IsCache = (1 << 0),
+		IsFrozen = (1 << 0), // Document may not be modified
 		VerticesOutdated = (1 << 1),
 		NeedsVertexMerge = (1 << 2),
 		IsBeingDestroyed = (1 << 3),
@@ -76,12 +76,11 @@
 	QList<LDPolygon> inlinePolygons();
 	const QSet<Vertex>& inlineVertices();
 	void insertObject (int pos, LDObject* obj);
-	bool isCache() const;
+	bool isFrozen() const;
 	bool isSafeToClose();
 	QString name() const;
 	void needVertexMerge();
 	void objectRemoved(LDObject* object, int index);
-	void openForEditing();
 	const QList<LDPolygon>& polygonData() const;
 	void recountTriangles();
 	void redo();
@@ -91,6 +90,7 @@
 	bool save (QString path = "", int64* sizeptr = nullptr);
 	long savePosition() const;
 	void setDefaultName (QString value);
+	void setFrozen(bool value);
 	void setFullPath (QString value);
 	void setName (QString value);
 	void setSavePosition (long value);
--- a/src/ldObject.cpp	Fri Feb 03 10:51:08 2017 +0200
+++ b/src/ldObject.cpp	Sat Feb 04 14:24:16 2017 +0200
@@ -950,7 +950,7 @@
 	// If it's an immediate subfile reference (i.e. this subfile is in an opened document), we need to pre-compile the
 	// GL polygons for the document if they don't exist already.
 	if (newReferee and
-		newReferee->isCache() == false and
+		newReferee->isFrozen() == false and
 		newReferee->polygonData().isEmpty())
 	{
 		newReferee->initializeCachedData();
--- a/src/mainwindow.cpp	Fri Feb 03 10:51:08 2017 +0200
+++ b/src/mainwindow.cpp	Sat Feb 04 14:24:16 2017 +0200
@@ -732,7 +732,7 @@
 //
 bool MainWindow::save (LDDocument* doc, bool saveAs)
 {
-	if (doc->isCache())
+	if (doc->isFrozen())
 		return false;
 
 	QString path = doc->fullPath();
@@ -843,7 +843,7 @@
 
 	for (LDDocument* document : m_documents->allDocuments())
 	{
-		if (not document->isCache())
+		if (not document->isFrozen())
 		{
 			// Add an item to the list for this file and store the tab index
 			// in the document so we can find documents by tab index.
@@ -901,7 +901,7 @@
 	// Find the file pointer of the item that was selected.
 	for (LDDocument* document : m_documents->allDocuments())
 	{
-		if (not document->isCache() and document->tabIndex() == tabIndex)
+		if (not document->isFrozen() and document->tabIndex() == tabIndex)
 		{
 			switchee = document;
 			break;
@@ -1141,11 +1141,24 @@
 	connect (document->history(), SIGNAL (stepAdded()), this, SLOT (updateActions()));
 
 	if (not cache)
-		document->openForEditing();
+		openDocumentForEditing(document);
 
 	return document;
 }
 
+void MainWindow::openDocumentForEditing(LDDocument* document)
+{
+	if (document->isFrozen())
+	{
+		document->setFrozen(false);
+		print ("Opened %1", document->name());
+
+		// Cache files are not compiled by the GL renderer. Now that this file is open for editing, it needs to be compiled.
+		m_renderer->compiler()->compileDocument(document);
+		updateDocumentList();
+	}
+}
+
 // ---------------------------------------------------------------------------------------------------------------------
 //
 LDDocument* MainWindow::currentDocument()
@@ -1160,7 +1173,7 @@
 void MainWindow::changeDocument (LDDocument* document)
 {
 	// Implicit files were loaded for caching purposes and may never be switched to.
-	if (document and document->isCache())
+	if (document and document->isFrozen())
 		return;
 
 	m_currentDocument = document;
@@ -1210,7 +1223,7 @@
 	// Find a replacement document to use
 	for (LDDocument* doc : m_documents->allDocuments())
 	{
-		if (doc != old and not doc->isCache())
+		if (doc != old and not doc->isFrozen())
 		{
 			changeDocument (doc);
 			break;
--- a/src/mainwindow.h	Fri Feb 03 10:51:08 2017 +0200
+++ b/src/mainwindow.h	Sat Feb 04 14:24:16 2017 +0200
@@ -92,6 +92,7 @@
 	void loadShortcuts();
 	MathFunctions* mathFunctions() const;
 	LDDocument* newDocument (bool cache = false);
+	void openDocumentForEditing(LDDocument* document);
 	PrimitiveManager* primitives();
 	GLRenderer* renderer();
 	void refresh();
--- a/src/primitives.cpp	Fri Feb 03 10:51:08 2017 +0200
+++ b/src/primitives.cpp	Sat Feb 04 14:24:16 2017 +0200
@@ -471,7 +471,7 @@
 		author = format("%1 [%2]", m_config->defaultName(), m_config->defaultUser());
 	}
 
-	document->openForEditing();
+	document->setFrozen(false);
 	document->history()->setIgnoring(false);
 	document->emplace<LDComment>(description);
 	document->emplace<LDComment>(format("Name: %1", fileName));
@@ -496,10 +496,13 @@
 	QString name = model.makeFileName();
 	LDDocument* document = m_window->documents()->getDocumentByName (name);
 
-	if (document)
-		return document;
-	else
-		return generatePrimitive(model);
+	if (not document)
+	{
+		document = generatePrimitive(model);
+		m_window->openDocumentForEditing(document);
+	}
+
+	return document;
 }
 
 /*
--- a/src/toolsets/algorithmtoolset.cpp	Fri Feb 03 10:51:08 2017 +0200
+++ b/src/toolsets/algorithmtoolset.cpp	Sat Feb 04 14:24:16 2017 +0200
@@ -530,7 +530,6 @@
 
 	// Create the new subfile document
 	LDDocument* subfile = m_window->newDocument();
-	subfile->openForEditing();
 	subfile->setFullPath(fullsubname);
 	subfile->setName(LDDocument::shortenName(fullsubname));
 
--- a/src/toolsets/filetoolset.cpp	Fri Feb 03 10:51:08 2017 +0200
+++ b/src/toolsets/filetoolset.cpp	Sat Feb 04 14:24:16 2017 +0200
@@ -179,8 +179,8 @@
 	{
 		LDSubfileReference* reference = dynamic_cast<LDSubfileReference*>(object);
 
-		if (reference and reference->fileInfo()->isCache())
-			reference->fileInfo()->openForEditing();
+		if (reference and reference->fileInfo()->isFrozen())
+			m_window->openDocumentForEditing(reference->fileInfo());
 	}
 }
 
@@ -203,7 +203,7 @@
 	if (dialog->exec())
 	{
 		LDDocument* primitive = primitives()->generatePrimitive(dialog->primitiveModel());
-		primitive->openForEditing();
+		m_window->openDocumentForEditing(primitive);
 		m_window->save(primitive, false);
 	}
 }

mercurial