LDSubfileReference now contains the name of the subfile and not a pointer anymore. Some methods now require a DocumentManager* for context on resolving the name.

Sat, 03 Mar 2018 17:47:40 +0200

author
Santeri Piippo
date
Sat, 03 Mar 2018 17:47:40 +0200
changeset 1263
0256edecda54
parent 1262
f6b253c17643
child 1264
5f00dfde4901

LDSubfileReference now contains the name of the subfile and not a pointer anymore. Some methods now require a DocumentManager* for context on resolving the name.

src/canvas.cpp file | annotate | diff | comparison | revisions
src/dialogs/configdialog.cpp file | annotate | diff | comparison | revisions
src/documentmanager.cpp file | annotate | diff | comparison | revisions
src/editmodes/circleMode.cpp file | annotate | diff | comparison | revisions
src/glcompiler.cpp file | annotate | diff | comparison | revisions
src/lddocument.cpp file | annotate | diff | comparison | revisions
src/lddocument.h file | annotate | diff | comparison | revisions
src/linetypes/modelobject.cpp file | annotate | diff | comparison | revisions
src/linetypes/modelobject.h file | annotate | diff | comparison | revisions
src/linetypes/quadrilateral.cpp file | annotate | diff | comparison | revisions
src/linetypes/quadrilateral.h file | annotate | diff | comparison | revisions
src/linetypes/triangle.cpp file | annotate | diff | comparison | revisions
src/linetypes/triangle.h file | annotate | diff | comparison | revisions
src/model.cpp file | annotate | diff | comparison | revisions
src/partdownloader.cpp file | annotate | diff | comparison | revisions
src/toolsets/algorithmtoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/basictoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/filetoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/viewtoolset.cpp file | annotate | diff | comparison | revisions
--- a/src/canvas.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/canvas.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -257,8 +257,11 @@
 		PrimitiveTreeItem* item = static_cast<PrimitiveTreeItem*> (m_window->getPrimitivesTree()->currentItem());
 		QString primitiveName = item->primitive()->name;
 		int position = m_window->suggestInsertPoint();
-		LDSubfileReference* reference = currentDocument()->emplaceAt<LDSubfileReference>(position);
-		reference->setFileInfo(m_documents->getDocumentByName(primitiveName));
+		currentDocument()->emplaceAt<LDSubfileReference>(
+			position,
+			primitiveName,
+			Matrix {},
+			Vertex {});
 		m_window->select(currentDocument()->index(position));
 		update();
 		event->acceptProposedAction();
--- a/src/dialogs/configdialog.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/dialogs/configdialog.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -298,7 +298,6 @@
 	}
 
 	m_window->syncSettings();
-	currentDocument()->reloadAllSubfiles();
 	m_documents->loadLogoedStuds();
 	m_window->renderer()->setBackground();
 	m_window->doFullRefresh();
--- a/src/documentmanager.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/documentmanager.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -149,7 +149,6 @@
 
 		dl.exec();
 		dl.checkIfFinished();
-		file->reloadAllSubfiles();
 	}
 }
 
--- a/src/editmodes/circleMode.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/editmodes/circleMode.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -105,7 +105,7 @@
 			primitiveModel.ringNumber = component.num;
 			primitiveFile = primitives()->getPrimitive(primitiveModel);
 			Matrix matrix = Matrix::fromRotationMatrix(renderer()->currentCamera().transformationMatrix(component.scale));
-			model.emplace<LDSubfileReference>(primitiveFile, matrix, m_drawedVerts.first());
+			model.emplace<LDSubfileReference>(primitiveFile->name(), matrix, m_drawedVerts.first());
 		}
 	}
 	else
@@ -149,7 +149,7 @@
 	}
 
 	if (circleOrDisc and primitiveFile)
-		model.emplace<LDSubfileReference>(primitiveFile, transform, m_drawedVerts.first());
+		model.emplace<LDSubfileReference>(primitiveFile->name(), transform, m_drawedVerts.first());
 
 	finishDraw (model);
 }
--- a/src/glcompiler.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/glcompiler.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -376,7 +376,7 @@
 	case LDObjectType::SubfileReference:
 		{
 			LDSubfileReference* subfileReference = static_cast<LDSubfileReference*>(object);
-			auto data = subfileReference->inlinePolygons();
+			auto data = subfileReference->inlinePolygons(m_documents);
 
 			for (LDPolygon& poly : data)
 			{
--- a/src/lddocument.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/lddocument.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -259,35 +259,6 @@
 
 // =============================================================================
 //
-void LDDocument::reloadAllSubfiles()
-{
-	print ("Reloading subfiles of %1", getDisplayName());
-
-	// Go through all objects in the current file and reload the subfiles
-	for (LDObject* obj : objects())
-	{
-		if (obj->type() == LDObjectType::SubfileReference)
-		{
-			LDSubfileReference* reference = static_cast<LDSubfileReference*> (obj);
-			LDDocument* fileInfo = m_documents->getDocumentByName (reference->fileInfo()->name());
-
-			if (fileInfo)
-				reference->setFileInfo (fileInfo);
-			else
-				emplaceReplacement<LDError>(reference, reference->asText(), format("Could not open %1", reference->fileInfo()->name()));
-		}
-
-		// Reparse gibberish files. It could be that they are invalid because
-		// of loading errors. Circumstances may be different now.
-		if (obj->type() == LDObjectType::Error)
-			replaceWithFromString(obj, static_cast<LDError*> (obj)->contents());
-	}
-
-	m_needsRecache = true;
-}
-
-// =============================================================================
-//
 void LDDocument::insertObject (int pos, LDObject* obj)
 {
 	Model::insertObject(pos, obj);
@@ -358,7 +329,7 @@
 			if (obj->type() == LDObjectType::SubfileReference)
 			{
 				print ("Warning: unable to inline %1 into %2",
-					static_cast<LDSubfileReference*> (obj)->fileInfo()->getDisplayName(),
+					static_cast<LDSubfileReference*> (obj)->referenceName(),
 					getDisplayName());
 				continue;
 			}
@@ -390,7 +361,7 @@
 			else
 				iterator->clear();
 
-			object->getVertices (*iterator);
+			object->getVertices(documentManager(), *iterator);
 		}
 
 		m_vertices.clear();
--- a/src/lddocument.h	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/lddocument.h	Sat Mar 03 17:47:40 2018 +0200
@@ -68,7 +68,6 @@
 	void recountTriangles();
 	void redo();
 	void redoVertices();
-	void reloadAllSubfiles();
 	bool save (QString path = "", qint64* sizeptr = nullptr);
 	long savePosition() const;
 	void setDefaultName (QString value);
--- a/src/linetypes/modelobject.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/linetypes/modelobject.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -81,7 +81,7 @@
 	QString val = format ("1 %1 %2 ", color(), position());
 	val += transformationMatrix().toString();
 	val += ' ';
-	val += fileInfo()->name();
+	val += m_referenceName;
 	return val;
 }
 
@@ -110,14 +110,14 @@
 	return format ("0 BFC %1", statementToString());
 }
 
-int LDObject::triangleCount() const
+int LDObject::triangleCount(DocumentManager*) const
 {
 	return 0;
 }
 
-int LDSubfileReference::triangleCount() const
+int LDSubfileReference::triangleCount(DocumentManager* context) const
 {
-	return fileInfo()->triangleCount();
+	return fileInfo(context)->triangleCount();
 }
 
 int LDObject::numVertices() const
@@ -190,7 +190,7 @@
 void LDSubfileReference::inlineContents(DocumentManager* context, Model& model, bool deep, bool render)
 {
 	Model inlined {context};
-	fileInfo()->inlineContents(inlined, deep, render);
+	fileInfo(context)->inlineContents(inlined, deep, render);
 
 	// Transform the objects
 	for (LDObject* object : inlined)
@@ -246,9 +246,9 @@
 
 // =============================================================================
 //
-QList<LDPolygon> LDSubfileReference::inlinePolygons()
+QList<LDPolygon> LDSubfileReference::inlinePolygons(DocumentManager* context)
 {
-	QList<LDPolygon> data = fileInfo()->inlinePolygons();
+	QList<LDPolygon> data = fileInfo(context)->inlinePolygons();
 
 	for (LDPolygon& entry : data)
 	{
@@ -508,41 +508,36 @@
 	return result;
 }
 
-LDSubfileReference::LDSubfileReference(LDDocument* reference, const Matrix& transformationMatrix,
-                                       const Vertex& position, Model* model) :
-    LDMatrixObject {transformationMatrix, position, model},
-    m_fileInfo {reference} {}
+LDSubfileReference::LDSubfileReference(
+	QString referenceName,
+	const Matrix& transformationMatrix,
+	const Vertex& position,
+	Model* model
+) :
+	LDMatrixObject {transformationMatrix, position, model},
+	m_referenceName {referenceName} {}
 
 // =============================================================================
 //
-LDDocument* LDSubfileReference::fileInfo() const
+LDDocument* LDSubfileReference::fileInfo(DocumentManager* context) const
 {
-	return m_fileInfo;
+	return context->getDocumentByName(m_referenceName);
 }
 
-void LDSubfileReference::setFileInfo (LDDocument* newReferee)
+QString LDSubfileReference::referenceName() const
 {
-	changeProperty(&m_fileInfo, newReferee);
-
-	// 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->isFrozen() == false and
-		newReferee->polygonData().isEmpty())
-	{
-		newReferee->initializeCachedData();
-	}
+	return m_referenceName;
 }
 
-void LDObject::getVertices (QSet<Vertex>& verts) const
+void LDObject::getVertices(DocumentManager* context, QSet<Vertex>& verts) const
 {
 	for (int i = 0; i < numVertices(); ++i)
 		verts.insert(vertex(i));
 }
 
-void LDSubfileReference::getVertices (QSet<Vertex>& verts) const
+void LDSubfileReference::getVertices (DocumentManager* context, QSet<Vertex>& verts) const
 {
-	verts.unite(fileInfo()->inlineVertices());
+	verts.unite(fileInfo(context)->inlineVertices());
 }
 
 QString LDObject::objectListText() const
@@ -574,7 +569,7 @@
 
 QString LDSubfileReference::objectListText() const
 {
-	QString result = format ("%1 %2, (", fileInfo()->getDisplayName(), position().toString(true));
+	QString result = format ("%1 %2, (", referenceName(), position().toString(true));
 
 	for (int i = 0; i < 9; ++i)
 		result += format("%1%2", transformationMatrix().value(i), (i != 8) ? " " : "");
--- a/src/linetypes/modelobject.h	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/linetypes/modelobject.h	Sat Mar 03 17:47:40 2018 +0200
@@ -25,6 +25,7 @@
 
 class Model;
 class LDDocument;
+class DocumentManager;
 
 /*
  * Object type codes.
@@ -64,7 +65,7 @@
 	virtual LDColor defaultColor() const; // What color does the object default to?
 	Model* model() const;
 	LDPolygon* getPolygon();
-	virtual void getVertices (QSet<Vertex>& verts) const;
+	virtual void getVertices (DocumentManager *context, QSet<Vertex>& verts) const;
 	virtual bool hasMatrix() const; // Does this object have a matrix and position? (see LDMatrixObject)
 	qint32 id() const;
 	virtual bool isColored() const;
@@ -81,7 +82,7 @@
 	void setVertex (int i, const Vertex& vert);
 	bool isInverted() const;
 	void setInverted(bool value);
-	virtual int triangleCount() const;
+	virtual int triangleCount(DocumentManager* context) const;
 	virtual LDObjectType type() const = 0;
 	virtual QString typeName() const = 0;
 	const Vertex& vertex (int i) const;
@@ -235,23 +236,23 @@
 	}
 
 	virtual QString asText() const override;
-	LDDocument* fileInfo() const;
-	virtual void getVertices (QSet<Vertex>& verts) const override;
-	void inlineContents(class DocumentManager* context, Model& model, bool deep, bool render);
-	QList<LDPolygon> inlinePolygons();
+	LDDocument* fileInfo(DocumentManager *context) const;
+	virtual void getVertices(DocumentManager *context, QSet<Vertex>& verts) const override;
+	void inlineContents(DocumentManager* context, Model& model, bool deep, bool render);
+	QList<LDPolygon> inlinePolygons(DocumentManager* context);
 	QString objectListText() const override;
-	void setFileInfo (LDDocument* fileInfo);
-	int triangleCount() const override;
+	QString referenceName() const;
+	int triangleCount(DocumentManager *context) const override;
 	bool hasMatrix() const override { return true; }
 	QString typeName() const override { return "subfilereference"; }
 
 protected:
 	friend class Model;
-	LDSubfileReference (Model* model);
-	LDSubfileReference(LDDocument* reference, const Matrix& transformationMatrix, const Vertex& position, Model* model = nullptr);
+	LDSubfileReference(Model* model);
+	LDSubfileReference(QString referenceName, const Matrix& transformationMatrix, const Vertex& position, Model* model = nullptr);
 
 private:
-	LDDocument* m_fileInfo;
+	QString m_referenceName;
 };
 
 /*
--- a/src/linetypes/quadrilateral.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/linetypes/quadrilateral.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -50,7 +50,7 @@
 	return result;
 }
 
-int LDQuadrilateral::triangleCount() const
+int LDQuadrilateral::triangleCount(DocumentManager*) const
 {
 	return 2;
 }
--- a/src/linetypes/quadrilateral.h	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/linetypes/quadrilateral.h	Sat Mar 03 17:47:40 2018 +0200
@@ -29,7 +29,7 @@
 
 	QString asText() const override;
 	int numVertices() const override;
-	int triangleCount() const override;
+	int triangleCount(DocumentManager*) const override;
 	LDObjectType type() const override;
 	QString typeName() const override;
 
--- a/src/linetypes/triangle.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/linetypes/triangle.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -29,7 +29,7 @@
 	setVertex(2, v3);
 }
 
-int LDTriangle::triangleCount() const
+int LDTriangle::triangleCount(DocumentManager*) const
 {
 	return 1;
 }
--- a/src/linetypes/triangle.h	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/linetypes/triangle.h	Sat Mar 03 17:47:40 2018 +0200
@@ -33,7 +33,7 @@
 	}
 
 	virtual QString asText() const override;
-	int triangleCount() const override;
+	int triangleCount(DocumentManager*) const override;
 	int numVertices() const override { return 3; }
 	QString typeName() const override { return "triangle"; }
 
--- a/src/model.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/model.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -186,7 +186,7 @@
 		_triangleCount = 0;
 
 		for (LDObject* object : objects())
-			_triangleCount += object->triangleCount();
+			_triangleCount += object->triangleCount(documentManager());
 
 		_needsTriangleRecount = false;
 	}
@@ -439,16 +439,6 @@
 			    // Subfile
 			    CheckTokenCount (tokens, 15);
 				CheckTokenNumbers (tokens, 1, 13);
-				LDDocument* document = _manager->getDocumentByName(tokens[14]);
-
-				// If we cannot open the file, mark it an error. Note we cannot use LDParseError
-				// here because the error object needs the document reference.
-				if (not document)
-				{
-					LDError* obj = emplaceAt<LDError>(position, line, format ("Could not open %1", tokens[14]));
-					obj->setFileReferenced (tokens[14]);
-					return obj;
-				}
 
 				Vertex referncePosition = ParseVertex (tokens, 2);  // 2 - 4
 				Matrix transform;
@@ -456,7 +446,7 @@
 				for (int i = 0; i < 9; ++i)
 					transform.value(i) = tokens[i + 5].toDouble(); // 5 - 13
 
-				LDSubfileReference* obj = emplaceAt<LDSubfileReference>(position, document, transform, referncePosition);
+				LDSubfileReference* obj = emplaceAt<LDSubfileReference>(position, tokens[14], transform, referncePosition);
 				obj->setColor (StringToNumber (tokens[1]));
 				return obj;
 		    }
--- a/src/partdownloader.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/partdownloader.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -255,9 +255,6 @@
 	if (primaryFile())
 		emit primaryFileDownloaded();
 
-	for (LDDocument* file : _files)
-		file->reloadAllSubfiles();
-
 	if (m_config->autoCloseDownloadDialog() and not failed)
 	{
 		// Close automatically if desired.
--- a/src/toolsets/algorithmtoolset.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/toolsets/algorithmtoolset.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -556,7 +556,7 @@
 			currentDocument()->remove(object);
 
 		// Add a reference to the new subfile to where the selection was
-		currentDocument()->emplaceAt<LDSubfileReference>(referencePosition, subfile, Matrix::identity, Vertex {0, 0, 0});
+		currentDocument()->emplaceAt<LDSubfileReference>(referencePosition, subfile->name(), Matrix::identity, Vertex {0, 0, 0});
 
 		// Refresh stuff
 		m_window->updateDocumentList();
--- a/src/toolsets/basictoolset.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/toolsets/basictoolset.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -237,7 +237,7 @@
 			int axisSet = (1 << X) | (1 << Y) | (1 << Z);
 			Model model {currentDocument()->documentManager()};
 			LDSubfileReference* reference = static_cast<LDSubfileReference*>(obj);
-			reference->fileInfo()->inlineContents(model, true, false);
+			reference->fileInfo(m_documents)->inlineContents(model, true, false);
 
 			for (LDObject* subobj : model.objects())
 			{
--- a/src/toolsets/filetoolset.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/toolsets/filetoolset.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -175,9 +175,10 @@
 	for (LDObject* object : selectedObjects())
 	{
 		LDSubfileReference* reference = dynamic_cast<LDSubfileReference*>(object);
+		LDDocument* referenceDocument = reference ? reference->fileInfo(m_documents) : nullptr;
 
-		if (reference and reference->fileInfo()->isFrozen())
-			m_window->openDocumentForEditing(reference->fileInfo());
+		if (referenceDocument and referenceDocument->isFrozen())
+			m_window->openDocumentForEditing(referenceDocument);
 	}
 }
 
--- a/src/toolsets/viewtoolset.cpp	Sat Mar 03 17:28:27 2018 +0200
+++ b/src/toolsets/viewtoolset.cpp	Sat Mar 03 17:47:40 2018 +0200
@@ -80,7 +80,7 @@
 		types << obj->type();
 
 		if (obj->type() == LDObjectType::SubfileReference)
-			subfilenames << static_cast<LDSubfileReference*> (obj)->fileInfo()->name();
+			subfilenames << static_cast<LDSubfileReference*>(obj)->fileInfo(m_documents)->name();
 	}
 
 	QItemSelection selection;
@@ -95,7 +95,7 @@
 
 		// For subfiles, type check is not enough, we check the name of the document as well.
 		if (type == LDObjectType::SubfileReference
-			and not subfilenames.contains (static_cast<LDSubfileReference*> (obj)->fileInfo()->name()))
+			and not subfilenames.contains(static_cast<LDSubfileReference*>(obj)->fileInfo(m_documents)->name()))
 		{
 			continue;
 		}

mercurial