src/linetypes/object.h

Tue, 21 Sep 2021 19:58:06 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 21 Sep 2021 19:58:06 +0300
changeset 133
e39326ee48dc
parent 89
7abaf1d64719
child 132
488d0ba6070b
permissions
-rw-r--r--

Begin work on edit history

#pragma once
#include <QPointF>
#include <QString>
#include <QStringView>
#include "main.h"
#include "colors.h"
#include "gl/common.h"
#include "linetypes/propertygenerics.h"

class Model;

namespace ldraw
{
	struct GetPolygonsContext;
	class Object;
	class ColoredObject;
	class Empty;
	class UnhandledProperty;
}

class DocumentManager;

struct ldraw::GetPolygonsContext
{
	::DocumentManager* documents;
};

class ldraw::Object
{
public:
	enum class SetPropertyResult
	{
		Success = 0,
		PropertyNotHandled
	};
	friend bool handled(SetPropertyResult result)
	{
		return result == SetPropertyResult::Success;
	}
	class BadPointIndex : public std::exception
	{
	};
	Object();
	Object(const Object&) = delete;
	virtual ~Object();
	const id_t id;
	virtual bool hasColor() const;
	virtual QVariant getProperty(Property id) const;
	template<ldraw::Property property>
	SetPropertyResult setProperty(const PropertyType<property>& value);
	SetPropertyResult setProperty(const PropertyKeyValue& pair);
	virtual QString textRepresentation() const = 0;
	virtual QBrush textRepresentationForeground() const;
	virtual QBrush textRepresentationBackground() const;
	virtual QFont textRepresentationFont() const;
	virtual void getPolygons(std::vector<gl::Polygon>& polygons, GetPolygonsContext* context) const;
	virtual void invert() {}
	virtual int numPoints() const { return 0; }
	virtual const glm::vec3& getPoint(int index) const;
protected:
	template<Property property, typename Function>
	void handle(SetPropertyResult* result, const PropertyKeyValue& pair, Function function);
	virtual void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair);
};

template<ldraw::Property property>
ldraw::Object::SetPropertyResult ldraw::Object::setProperty(const ldraw::PropertyType<property>& value)
{
	SetPropertyResult result = SetPropertyResult::PropertyNotHandled;
	this->setProperty(&result, PropertyKeyValue{property, QVariant::fromValue(value)});
	return result;
}

template<ldraw::Property property, typename Function>
void ldraw::Object::handle(SetPropertyResult* result, const PropertyKeyValue& pair, Function function)
{
	if (pair.key == property)
	{
		function(pair.value.value<ldraw::PropertyType<property>>());
		*result = SetPropertyResult::Success;
	}
}

class ldraw::ColoredObject : public Object
{
public:
	ColoredObject(const Color colorIndex = ldraw::mainColor);
	bool hasColor() const override final;
	QVariant getProperty(Property id) const override;
	Color colorIndex = ldraw::mainColor;
protected:
	void setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) override;
};

/**
 * @brief Represents an empty line.
 */
class ldraw::Empty : public Object
{
	QString textRepresentation() const override;
};

mercurial