src/model.h

Thu, 09 Jun 2022 13:32:55 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Thu, 09 Jun 2022 13:32:55 +0300
changeset 210
232e7634cc8a
parent 206
654661eab7f3
child 211
b27b90fb993f
permissions
-rw-r--r--

more refactoring, dosn't build yet

/*
 *  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 "basics.h"
#include "colors.h"

struct SubfileReference
{
	QString name;
	glm::mat4 transformation;
	bool inverted = false;
};

template<typename T>
struct Colored : T
{
	ldraw::Color color;
};

struct Comment
{
	QString text;
};

struct ParseError
{
	QString code;
};

struct Empty {};

template<typename T, typename R>
struct transfer_reference
{
	using type = std::remove_reference_t<R>;
};

template<typename T, typename R>
struct transfer_reference<T&, R>
{
	using type = std::remove_reference_t<R>&;
};

template<typename T, typename R>
struct transfer_reference<T&&, R>
{
	using type = std::remove_reference_t<R>&&;
};

template<typename T, typename R>
using transfer_reference_t = typename transfer_reference<T, R>::type;
static_assert(std::is_same_v<transfer_reference_t<int, char>, char>);
static_assert(std::is_same_v<transfer_reference_t<int&, char>, char&>);
static_assert(std::is_same_v<transfer_reference_t<int&&, char>, char&&>);

using ModelElement = std::variant<
	Colored<SubfileReference>,
	Colored<LineSegment>,
	Colored<Triangle>,
	Colored<Quadrilateral>,
	Colored<ConditionalEdge>,
	Comment,
	Empty,
	ParseError>;

using PolygonElement = Colored<std::variant<
	LineSegment,
	Triangle,
	Quadrilateral,
	ConditionalEdge>>;

template<typename T>
struct remove_color { using type = T; };

template<typename T>
struct remove_color<Colored<T>> { using type = T; };

template<typename T>
struct remove_color<Colored<T>&> { using type = T&; };

template<typename T>
struct remove_color<Colored<T>&&> { using type = T&&; };

template<typename T>
using remove_color_t = typename remove_color<T>::type;

template<typename T>
constexpr remove_color_t<T&&> extract_colored(T&& x)
{
	return static_cast<remove_color_t<T&&>>(x);
}

template<typename Ret, typename T>
constexpr auto visitPolygon(
	std::function<Ret(transfer_reference_t<T&&, LineSegment>)> f1,
	std::function<Ret(transfer_reference_t<T&&, Triangle>)> f2,
	std::function<Ret(transfer_reference_t<T&&, Quadrilateral>)> f3,
	std::function<Ret(transfer_reference_t<T&&, ConditionalEdge>)> f4,
	T&& element)
{
	return std::visit(overloaded{f1, f2, f3, f4}, extract_colored(element));
}

template<typename T, typename Fn>
constexpr void visitPoints(T&& element, Fn func)
{
	visitPolygon<void>(
		[&func](transfer_reference_t<T&&, LineSegment> edge)
		{
			func(edge.p1);
			func(edge.p2);
		},
		[&func](transfer_reference_t<T&&, Triangle>& tri)
		{
			func(tri.p1);
			func(tri.p2);
			func(tri.p3);
		},
		[&func](transfer_reference_t<T&&, Quadrilateral>& quad)
		{
			func(quad.p1);
			func(quad.p2);
			func(quad.p3);
			func(quad.p4);
		},
		[&func](transfer_reference_t<T&&, ConditionalEdge>& cedge)
		{
			func(cedge.p1);
			func(cedge.p2);
			func(cedge.c1);
			func(cedge.c2);
		},
		element);
}

QString modelElementToString(const ModelElement& element);
struct ModelId
{
	std::int32_t value;
	constexpr auto operator<=>(const ModelId& other) const = default;
};

constexpr int qHash(ModelId id)
{
	return qHash(id.value);
}

class Model : public QAbstractListModel
{
	Q_OBJECT
	struct Entry {
		ModelElement data;
		ModelId id;
	};
	std::vector<Entry> body;
	std::map<ModelId, int> positions;
	ModelId runningId = {1};
public:
	Model(QObject* parent);
	virtual ~Model();
	ModelId append(const ModelElement& value);
	const ModelElement& at(int position) const;
	ModelId idAt(int position) const;
	void assignAt(int position, const ModelElement& element);
	std::optional<int> find(ModelId id) const;
	void remove(int index);
	int rowCount(const QModelIndex&) const override;
	QVariant data(const QModelIndex& index, int role) const override;
	const ModelElement& operator[](int index) const;
	int size() const;
	auto operator[](int index) {
		struct {
			Model& model;
			int index;
			operator const ModelElement&() {
				return model.at(index);
			}
			auto& operator=(const ModelElement& newData) {
				model.assignAt(index, newData);
				return *this;
			}
			const auto* operator&() {
				return &(this->operator const ModelElement&());
			}
		} result{*this, index};
		return result;
	}
};

void save(const Model& model, QIODevice *device);
void updateHeaderNameField(Model& model, const QString &name);

mercurial