src/documentmanager.h

Sat, 11 Jun 2022 15:20:24 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sat, 11 Jun 2022 15:20:24 +0300
changeset 213
ee5758ddb6d2
parent 212
27259810da6d
child 214
8e1fe64ce4e3
permissions
-rw-r--r--

Rewrite prune to use graphs rather than O(n²) searches

/*
 *  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 "libraries.h"
#include "model.h"
#include "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:
	struct ModelInfo
	{
		std::unique_ptr<Model> model;
		ModelId id;
		QString path;
		OpenType opentype;
		std::map<QString, ModelId> dependencies = {};
		PolygonCache polygonCache;
	};
	using MissingDependencies = std::map<QString, QString>;
	DocumentManager();
	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 LibraryManager &libraries);
	void closeDocument(const ModelId modelId);
	const QString* modelPath(ModelId modelId) const;
	void setModelPath(
			const ModelId modelId,
			const QString& newPath,
			const LibraryManager &libraries,
			QTextStream &errorStream);
	bool saveModel(const ModelId modelId, QTextStream& errors);
	std::optional<ModelId> findIdForModel(const Model* model) const;
	PolygonCache* getPolygonCacheForModel(ModelId modelId);
	const ModelInfo* infoForModel(ModelId modelId) const;
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