src/documentmanager.cpp

changeset 200
ca23936b455b
parent 195
6e79c1cb83e6
child 201
5d201ee4a9c3
--- a/src/documentmanager.cpp	Wed May 25 20:36:34 2022 +0300
+++ b/src/documentmanager.cpp	Mon Jun 06 22:01:22 2022 +0300
@@ -21,8 +21,6 @@
 #include <QFileInfo>
 #include <QSaveFile>
 #include "documentmanager.h"
-#include "modeleditor.h"
-#include "linetypes/subfilereference.h"
 #include "parser.h"
 
 /**
@@ -43,7 +41,7 @@
 	const ModelId modelId{++this->modelIdCounter};
 	const QString name = makeNewModelName();
 	this->openModels[modelId] = ModelInfo{
-		.model = std::make_unique<Model>(),
+		.model = std::make_unique<Model>(this),
 		.id = modelId,
 		.opentype = OpenType::ManuallyOpened,
 	};
@@ -400,6 +398,16 @@
 	return referencedFilePath;
 }
 
+template<typename T>
+void iterate(const Model& model, std::function<void(const T&)> fn)
+{
+	for (int i = 0; i < model.size(); ++i) {
+		if (std::holds_alternative<T>(model[i])) {
+			fn(std::get<T>(model[i]));
+		}
+	}
+}
+
 void DocumentManager::loadDependenciesForModel(
 	const ModelId modelId,
 	const QString &path,
@@ -418,9 +426,8 @@
 	}
 	ModelInfo& modelInfo = this->openModels[modelId];
 	modelInfo.dependencies.clear();
-	for (int i = 0; i < modelInfo.model->size(); i += 1)
-	{
-		const QString referenceName = (*modelInfo.model)[i]->getProperty(ldraw::Property::ReferenceName).toString();
+	iterate<Colored<SubfileReference>>(*modelInfo.model, [&](const SubfileReference& ref) {
+		const QString referenceName = ref.name;
 		if (not referenceName.isEmpty()
 			and modelInfo.dependencies.count(referenceName) == 0
 			and not failedToOpen.contains(referenceName))
@@ -458,5 +465,5 @@
 				bag.missing.append(referenceName);
 			}
 		}
-	}
+	});
 }

mercurial