src/model.cpp

Sun, 19 Jan 2020 14:25:43 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 19 Jan 2020 14:25:43 +0200
changeset 24
1a0faaaceb84
parent 21
0133e565e072
child 35
98906a94732f
permissions
-rw-r--r--

added license

/*
 *  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/>.
 */

#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