src/model.h

Sun, 29 Aug 2021 21:28:08 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 29 Aug 2021 21:28:08 +0300
changeset 126
a7c720aff97c
parent 117
121a40d5e34c
child 133
e39326ee48dc
permissions
-rw-r--r--

moved ObjectEditor under SelectTool

/*
 *  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 "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;
	EditContext edit();
	int rowCount(const QModelIndex&) const override;
	QVariant data(const QModelIndex& index, int role) const override;
	QVariant getHeaderProperty(const HeaderProperty property);
	const QString& getName() const;
	QVariant getObjectProperty(const int index, const ldraw::Property property) const;
	std::vector<gl::Polygon> getPolygons(class DocumentManager* documents) const;
	QModelIndex lookup(ldraw::id_t id) const;
	ldraw::id_t resolve(const QModelIndex& index) const;
	template<typename R>
	ldraw::Id<R> checkType(ldraw::id_t id) 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);
	template<typename R, typename Fn>
	void apply(Fn f) const;
Q_SIGNALS:
	void objectAdded(ldraw::id_t id, int position);
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)
	{
		return static_cast<T*>(this->objectAt(this->lookup(id)));
	}
	template<typename T>
	const T* objectAt(ldraw::Id<T> id) const
	{
		return static_cast<const T*>(this->objectAt(this->lookup(id)));
	}
	void getObjectPolygons(
		const int index,
		std::vector<gl::Polygon>& polygons_out,
		ldraw::GetPolygonsContext* context) const;
	bool modified = false;
	QString path;
	LDHeader header;
	std::vector<ModelObjectPointer> body;
	std::map<ldraw::id_t, ldraw::Object*> objectsById;
	mutable std::vector<gl::Polygon> cachedPolygons;
	mutable bool needRecache = true;
};

/**
 * \brief Call @c f for all objects of type \c R
 */
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);
		}
	}
}

/**
 * \brief Call @c f for all objects of type \c R
 */
template<typename R, typename Fn>
void Model::apply(Fn f)
{
	for (ModelObjectPointer& object : this->body)
	{
		R* subobject = dynamic_cast<R*>(object.get());
		if (subobject != nullptr)
		{
			f(subobject);
		}
	}
}

/**
 * \brief Checks type of object behind id
 * Checks whether the specified id refers to an object of the specified type.
 * \returns id casted to subclass if appropriate, null id otherwise
 */
template<typename R>
ldraw::Id<R> Model::checkType(ldraw::id_t id) const
{
	if (dynamic_cast<const R*>(this->objectAt(this->lookup(id))) != nullptr)
	{
		return ldraw::Id<R>{id.value};
	}
	else
	{
		return ldraw::NULL_ID;
	}
}

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(pointer->id, static_cast<int>(this->body.size() - 1));
	Q_EMIT endInsertRows();
	this->needRecache = true;
	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(pointer->id, static_cast<int>(position));
	Q_EMIT endInsertRows();
	this->needRecache = true;
	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->lookup(id);
	if (result.index.isValid())
	{
		result.object = static_cast<const R*>(this->objectAt(result.index));
	}
	else
	{
		result.object = nullptr;
	}
	return result;
}

mercurial