src/documentmanager.cpp

changeset 12
fe67489523b5
parent 8
44679e468ba9
child 13
6e838748867b
--- a/src/documentmanager.cpp	Sun Nov 03 13:18:55 2019 +0200
+++ b/src/documentmanager.cpp	Sun Nov 03 17:57:21 2019 +0200
@@ -94,3 +94,72 @@
 	untitledNameCounter += 1;
 	return "untitled-" + QString::number(untitledNameCounter);
 }
+
+void DocumentManager::loadDependenciesForModel(
+	const QString& modelName,
+	const LibraryManager& libraries,
+	QTextStream& errorStream)
+{
+	QStringList missing;
+	QStringList processed;
+	loadDependenciesForModel(modelName, libraries, missing, processed, errorStream);
+	if (not missing.empty())
+	{
+		missing.sort(Qt::CaseInsensitive);
+		errorStream << utility::format(
+			"The following files could not be opened: %1",
+			missing.join(", "));
+	}
+}
+
+void DocumentManager::loadDependenciesForModel(
+	const QString& modelName,
+	const LibraryManager& libraries,
+	QStringList& missing,
+	QStringList& processed,
+	QTextStream& errorStream)
+{
+	struct LoadingError
+	{
+		QString message;
+	};
+	processed.append(modelName);
+	Model* model = this->findModelByName(modelName);
+	for (int i = 0; i < model->size(); i += 1)
+	{
+		const QString referenceName = model->getObjectProperty(i, modelobjects::Property::ReferenceName).toString();
+		if (not referenceName.isEmpty()
+			and openModels.find(referenceName) == std::end(openModels)
+			and not missing.contains(referenceName))
+		{
+			try
+			{
+				const QString path = libraries.findFile(referenceName);
+				if (path.isEmpty())
+				{
+					throw LoadingError{utility::format("'%1' was not found.", referenceName)};
+				}
+				QString errorString;
+				QTextStream localErrorStream{&errorString};
+				QString resultName = this->openModel(path, localErrorStream);
+				if (resultName.isEmpty())
+				{
+					throw LoadingError{utility::format(
+						"could not load '%1': %2",
+						path,
+						errorString)};
+				}
+				if (not processed.contains(referenceName))
+				{
+					loadDependenciesForModel(referenceName, libraries, missing, processed, errorStream);
+				}
+			}
+			catch(const LoadingError& error)
+			{
+				errorStream << error.message << "\n";
+				missing.append(referenceName);
+				processed.append(referenceName);
+			}
+		}
+	}
+}

mercurial