src/linetypes/object.h

Tue, 28 Sep 2021 23:07:23 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 28 Sep 2021 23:07:23 +0300
changeset 145
4dea24d3eda0
parent 141
185eb297dc1e
child 148
e1ced2523cad
permissions
-rw-r--r--

Use QSaveFile to save the file more safely

#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
	};
	/**
	 * @brief Enumerates different object types
	 */
	enum class Type
	{
		Empty,
		Comment,
		MetaCommand,
		ErrorLine,
		SubfileReference,
		EdgeLine,
		ConditionalEdge,
		Triangle,
		Quadrilateral,
	};
	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>
	PropertyType<property> getProperty() 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;
	virtual QDataStream& serialize(QDataStream& stream) const;
	virtual QDataStream& deserialize(QDataStream& stream);
	virtual Type typeIdentifier() const = 0;
	virtual QString toLDrawCode() const = 0;

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;
	}
}

template<ldraw::Property property>
ldraw::PropertyType<property> ldraw::Object::getProperty() const
{
	return this->getProperty(property).value<ldraw::PropertyType<property>>();
}

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

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

mercurial