src/documentmanager.h

Sun, 03 Jul 2022 13:44:11 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 03 Jul 2022 13:44:11 +0300
changeset 319
9727e545b0bc
parent 264
76a025db4948
child 333
07e65a4c6611
permissions
-rw-r--r--

Extract the triangulation and triangle merging code into a new source file and clean it up somewhat

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2020 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once
#include "src/libraries.h"
#include "src/model.h"
#include "src/polygoncache.h"

enum OpenType
{
	//! \brief Document was opened manually by the user
	ManuallyOpened,
	//! \brief Document was opened automatically in order to resolve subfile references
	AutomaticallyOpened,
};

class DocumentManager : public QObject
{
	Q_OBJECT
public:
	struct ModelInfo
	{
		std::unique_ptr<Model> model;
		ModelId id;
		QString path;
		OpenType opentype;
		std::map<QString, ModelId> dependencies = {};
		PolygonCache polygonCache = {};
		QObject* payload;
	};
	using MissingDependencies = std::map<QString, QString>;
	DocumentManager(QObject* parent = nullptr);
	auto begin() const { return this->openModels.begin(); }
	auto end() const { return this->openModels.end(); }
	ModelId newModel();
	Model* findDependencyByName(const ModelId modelId, const QString& name);
	Model* getModelById(ModelId modelId);
	std::optional<ModelId> openModel(const QString& path, QTextStream& errorStream, const OpenType openType);
	std::map<QString, QString> loadDependenciesForAllModels(const LibrariesModel &libraries);
	void closeDocument(const ModelId modelId);
	const QString* modelPath(ModelId modelId) const;
	void setModelPath(
			const ModelId modelId,
			const QString& newPath,
			const LibrariesModel &libraries,
			QTextStream &errorStream);
	bool saveModel(const ModelId modelId, QTextStream& errors);
	std::optional<ModelId> findIdForModel(const Model* model) const;
	PolygonCache* getPolygonCacheForModel(ModelId modelId);
	const ModelInfo* find(ModelId modelId) const;
	void setModelPayload(ModelId modelId, QObject* object);
	template<typename T>
	T* findPayload(ModelId modelId) const
	{
		const ModelInfo* info = this->find(modelId);
		return info ? qobject_cast<T*>(info->payload) : nullptr;
	}
Q_SIGNALS:
	void message(const Message& message);
private:
	int modelIdCounter = 0;
	std::map<ModelId, ModelInfo> openModels;
	void collectReferences(QSet<QString> &referenced, const QString& name, const Model* model);
	void updateDependencies(ModelInfo* model);
	void prune();
	void makePolygonCacheForModel(const ModelId modelId);
};

QString errorStringFromMissingDependencies(const DocumentManager::MissingDependencies& missing);
QString pathToName(const QFileInfo& path);

mercurial