src/model.cpp

Sun, 19 Jan 2020 02:54:48 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 19 Jan 2020 02:54:48 +0200
changeset 22
6da867fa5429
parent 21
0133e565e072
child 24
1a0faaaceb84
permissions
-rw-r--r--

commit work on GL rendering

#include <QBrush>
#include <QFont>
#include "model.h"
#include "modeleditcontext.h"

Model::Model(QObject* parent) :
	QAbstractListModel{parent} {}

int Model::size() const
{
	return this->body.size();
}

Model::EditContext Model::edit()
{
	return {*this};
}

int Model::rowCount(const QModelIndex&) const
{
	return size();
}

QVariant Model::data(const QModelIndex& index, int role) const
{
	const int row = index.row();
	linetypes::Object* object = this->body[row].get();
	switch(role)
	{
	case Qt::DisplayRole:
		return object->textRepresentation();
	case Qt::ForegroundRole:
		return object->textRepresentationForeground();
	case Qt::BackgroundRole:
		return object->textRepresentationBackground();
	case Qt::FontRole:
		return object->textRepresentationFont();
	default:
		return {};
	}
}

QVariant Model::getHeaderProperty(const HeaderProperty property)
{
	switch (property)
	{
	case HeaderProperty::Name:
		return header.name;
	default:
		return {};
	}
}

QVariant Model::getObjectProperty(const int index, const linetypes::Property property) const
{
	const linetypes::Object* object = this->body[index].get();
	return object->getProperty(property);
}

std::vector<gl::Polygon> Model::getPolygons(DocumentManager* documents) const
{
	if (this->needRecache)
	{
		this->cachedPolygons.clear();
		linetypes::GetPolygonsContext context{documents};
		for (int i = 0; i < this->size(); i += 1)
		{
			this->getObjectPolygons(i, this->cachedPolygons, &context);
		}
		this->needRecache = false;
	}
	return this->cachedPolygons;
}

void Model::getObjectPolygons(
	const int index,
	std::vector<gl::Polygon>& polygons_out,
	linetypes::GetPolygonsContext* context) const
{
	const linetypes::Object* object = this->body[index].get();
	object->getPolygons(polygons_out, context);
}

void Model::append(ModelObjectPointer&& object)
{
	this->body.push_back(std::move(object));
}

mercurial