src/model.h

Sun, 19 Jan 2020 13:53:07 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 19 Jan 2020 13:53:07 +0200
changeset 23
3387a84ddaba
parent 21
0133e565e072
child 24
1a0faaaceb84
permissions
-rw-r--r--

fixed a pile of nonsense that caused subfiles to go haywire

#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 linetypes::Property property) const;
	std::vector<gl::Polygon> getPolygons(class DocumentManager* documents) const;
	void getObjectPolygons(const int index, std::vector<gl::Polygon>& polygons_out, linetypes::GetPolygonsContext* context) const;
signals:
	void objectAdded(linetypes::Id id, int position);
private:
	using ModelObjectPointer = std::unique_ptr<linetypes::Object>;
	template<typename T, typename... Args>
	T* append(Args&&... args);
	void append(ModelObjectPointer&& object);
	template<typename T, typename... Args>
	T* insert(int position, Args&&... args);
	bool modified = false;
	QString path;
	LDHeader header;
	std::vector<ModelObjectPointer> body;
	std::map<linetypes::Id, linetypes::Object*> objectsById;
	mutable std::vector<gl::Polygon> cachedPolygons;
	mutable bool needRecache = true;
};

template<typename T, typename... Args>
T* Model::append(Args&&... args)
{
	emit layoutAboutToBeChanged();
	this->body.push_back(std::make_unique<T>(args...));
	T* pointer = static_cast<T*>(this->body.back().get());
	this->objectsById[pointer->id] = pointer;
	emit objectAdded(pointer->id, this->body.size() - 1);
	emit layoutChanged();
	return pointer;
}

template<typename T, typename... Args>
T* Model::insert(int position, Args&&... args)
{
	emit layoutAboutToBeChanged();
	this->body.insert(position, std::make_unique<T>(args...));
	T* pointer = static_cast<T*>(this->body[position]);
	this->objectsById[pointer->id] = pointer;
	emit objectAdded(pointer->id, position);
	emit layoutChanged();
	return pointer;
}

mercurial