src/model.cpp

Sat, 01 Feb 2020 17:20:10 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 01 Feb 2020 17:20:10 +0200
changeset 40
30cb5e836736
parent 35
98906a94732f
child 51
1a9eac27698d
permissions
-rw-r--r--

added configurable background color

/*
 *  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();
	ldraw::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 ldraw::Property property) const
{
	const ldraw::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();
		ldraw::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,
	ldraw::GetPolygonsContext* context) const
{
	const ldraw::Object* object = this->body[index].get();
	object->getPolygons(polygons_out, context);
}

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

mercurial