src/documentmanager.h

Fri, 01 Jul 2022 16:46:43 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Fri, 01 Jul 2022 16:46:43 +0300
changeset 312
2637134bc37c
parent 264
76a025db4948
child 333
07e65a4c6611
permissions
-rw-r--r--

Fix right click to delete not really working properly
Instead of removing the point that had been added, it would remove
the point that is being drawn, which would cause it to overwrite the
previous point using the new point, causing a bit of a delay

/*
 *  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