src/documentmanager.h

Sat, 08 Apr 2023 12:55:11 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sat, 08 Apr 2023 12:55:11 +0300
changeset 343
4a82990affd5
parent 338
719b909a7d2b
permissions
-rw-r--r--

Fix BFC formatting not working due to being evaluated after comment format

/*
 *  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<QTextDocument> 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();
	QTextDocument* findDependencyByName(const ModelId modelId, const QString& name);
	QTextDocument* 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 QTextDocument* 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 QTextDocument* model);
	void updateDependencies(ModelInfo* model);
	void prune();
	void makePolygonCacheForModel(const ModelId modelId);
};

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

mercurial