src/modeleditcontext.h

Sun, 29 Aug 2021 20:05:24 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 29 Aug 2021 20:05:24 +0300
changeset 124
f9f308c8e0c5
parent 89
7abaf1d64719
child 149
008989bc7d6e
child 151
e628fc2e0c72
permissions
-rw-r--r--

esc with draw mode now clears the polygon

/*
 *  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 "model.h"

class Model::EditContext
{
public:
	~EditContext();
	template<typename T, typename... Args>
	ldraw::Id<T> append(Args&&... args);
	ldraw::id_t append(std::unique_ptr<ldraw::Object>&& object);
	template<typename T, typename... Args>
	ldraw::Id<T> insert(int position, Args&&... args);
	void remove(int position);
	template<ldraw::Property property>
	void setObjectProperty(ldraw::id_t id, const ldraw::PropertyType<property>& value);
	auto setObjectProperty(ldraw::id_t id, ldraw::Property property, const QVariant& value)
		-> ldraw::Object::SetPropertyResult;
	void setObjectPoint(ldraw::id_t id, int pointId, const glm::vec3& value);
	void invertObject(ldraw::id_t id);
	Model& model();
private:
	EditContext(Model& model);
	friend class Model;
	QSet<ldraw::id_t> modifiedObjects;
	Model& storedModel;
};

template<ldraw::Property Property>
void Model::EditContext::setObjectProperty(const ldraw::id_t id, const ldraw::PropertyType<Property>& value)
{
	ldraw::Object* object = this->model().objectAt(id);
	if (object != nullptr)
	{
		object->setProperty<Property>(value);
		modifiedObjects.insert(id);
	}
}

template<typename T, typename... Args>
ldraw::Id<T> Model::EditContext::append(Args&&... args)
{
	return this->storedModel.append<T>(args...);
}

template<typename T, typename... Args>
ldraw::Id<T> Model::EditContext::insert(int position, Args&&... args)
{
	return this->storedModel.insert<T>(position, args...);
}

namespace ldraw
{
	/// Determines how quadrilaterals are split into triangles
	enum class Diagonal
	{
		Diagonal_13,
		Diagonal_24
	};

	// Splits the specified quadrilateral into triangles.
	// If it is not a quadrilateral then no action is performed
	auto splitQuadrilateral(Model::EditContext& editor,
		quadrilateralid_t quadrilateral_id,
		Diagonal splitType = Diagonal::Diagonal_13
	) -> std::optional<std::pair<triangleid_t, triangleid_t>>;
}

mercurial