src/model.h

Thu, 03 Mar 2022 11:42:52 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 03 Mar 2022 11:42:52 +0200
changeset 150
b6cbba6e29a1
parent 148
e1ced2523cad
child 151
e628fc2e0c72
permissions
-rw-r--r--

extract polygon cache out of Model

/*
 *  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 <QAbstractListModel>
#include <memory>
#include "main.h"
#include "header.h"
#include "linetypes/object.h"
#include "linetypes/metacommand.h"
#include "gl/common.h"

enum class HeaderProperty
{
	Name
};

class Model : public QAbstractListModel
{
	Q_OBJECT
public:
	class EditContext;
	Model(QObject* parent = nullptr);
	Model(const Model&) = delete;

	int size() const;
	ldraw::id_t at(int index) const;
	EditContext edit();
	int rowCount(const QModelIndex&) const override;
	QVariant data(const QModelIndex& index, int role) const override;
	QVariant getHeaderProperty(const HeaderProperty property);
	QVariant getObjectProperty(const int index, const ldraw::Property property) const;
	template<ldraw::Property Property>
	ldraw::PropertyType<Property> getObjectProperty(const ldraw::id_t id) const;
	QModelIndex find(ldraw::id_t id) const;
	ldraw::id_t idAt(const QModelIndex& index) const;
	template<typename R>
	const R* get(ldraw::Id<R> id) const;
	template<typename R>
	struct Get2Result
	{
		QModelIndex index;
		const R* object;
	};
	template<typename R>
	Get2Result<R> get2(ldraw::Id<R> id) const;
	template<typename R, typename Fn>
	void apply(Fn f) const;
	void save(QIODevice *device) const;
	ldraw::Object* operator[](int index);
Q_SIGNALS:
	void objectAdded(int position);
	void objectModified(int position);
	void objectRemoved(ldraw::id_t id);
private:
	using ModelObjectPointer = std::unique_ptr<ldraw::Object>;
	template<typename T, typename... Args>
	ldraw::Id<T> append(Args&&... args);
	void append(ModelObjectPointer&& object);
	template<typename T, typename... Args>
	ldraw::Id<T> insert(std::size_t position, Args&&... args);
	void remove(int position);
	ldraw::Object* objectAt(const QModelIndex& index);
	const ldraw::Object* objectAt(const QModelIndex& index) const;
	template<typename T>
	T* objectAt(ldraw::Id<T> id);
	template<typename T>
	const T* objectAt(ldraw::Id<T> id) const;
	void editFinished();
	void objectModified(ldraw::id_t id);
	bool modified = false;
	LDHeader header;
	std::vector<ModelObjectPointer> body;
	std::map<ldraw::id_t, ldraw::Object*> objectsById;
	/**
	 * @brief Amount of model edit contexts active
	 */
	int editCounter = 0;
};

void makeUnofficial(Model& model);

/**
 * @brief Calls the specified function to all matching objects in the model
 * @tparam R Type of LDraw line type object to filter by
 * @param fn Function to call.
 */
template<typename R, typename Fn>
void Model::apply(Fn f) const
{
	for (const ModelObjectPointer& object : this->body)
	{
		const R* subobject = dynamic_cast<const R*>(object.get());
		if (subobject != nullptr)
		{
			f(subobject);
		}
	}
}

template<typename T, typename... Args>
ldraw::Id<T> Model::append(Args&&... args)
{
	const int position = static_cast<int>(this->body.size());
	Q_EMIT beginInsertRows({}, position, position);
	this->body.push_back(std::make_unique<T>(args...));
	ldraw::Object* pointer = this->body.back().get();
	this->objectsById[pointer->id] = pointer;
	Q_EMIT objectAdded(static_cast<int>(this->body.size() - 1));
	Q_EMIT endInsertRows();
	return ldraw::Id<T>{pointer->id.value};
}

template<typename T, typename... Args>
ldraw::Id<T> Model::insert(const std::size_t position, Args&&... args)
{
	Q_EMIT beginInsertRows({}, position, position);
	this->body.insert(std::begin(this->body) + position, std::make_unique<T>(args...));
	ldraw::Object* pointer = this->body[position].get();
	this->objectsById[pointer->id] = pointer;
	Q_EMIT objectAdded(static_cast<int>(position));
	Q_EMIT endInsertRows();
	return ldraw::Id<T>{pointer->id.value};
}

template<typename R>
const R* Model::get(ldraw::Id<R> id) const
{
	return this->get2(id).object;
}

template<typename R>
Model::Get2Result<R> Model::get2(const ldraw::Id<R> id) const
{
	Get2Result<R> result;
	result.index = this->find(id);
	if (result.index.isValid())
	{
		result.object = static_cast<const R*>(this->objectAt(result.index));
	}
	else
	{
		result.object = nullptr;
	}
	return result;
}

/**
 * @brief Gets an object pointer by id. Used by the editing context to actually modify objects.
 * @param id
 * @return object pointer
 */
template<typename T>
T* Model::objectAt(ldraw::Id<T> id)
{
	return static_cast<T*>(this->objectAt(this->find(id)));
}

template<typename T>
const T* Model::objectAt(ldraw::Id<T> id) const
{
	return static_cast<const T*>(this->objectAt(this->find(id)));
}

/**
 * @brief Gets an object property by id
 * @tparam Property Property to obtain
 * @param id ID of object
 * @returns property or default value
 */
template<ldraw::Property Property>
ldraw::PropertyType<Property> Model::getObjectProperty(const ldraw::id_t id) const
{
	const ldraw::Object* const object = this->objectAt(id);
	ldraw::PropertyType<Property> result;
	if (object != nullptr)
	{
		result = object->getProperty<Property>();
	}
	return result;
}

mercurial