src/model.cpp

Thu, 13 Feb 2020 15:25:01 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 13 Feb 2020 15:25:01 +0200
changeset 54
a4055f67b9c7
parent 51
1a9eac27698d
child 73
97df974b5ed5
permissions
-rw-r--r--

made the grid look nicer

/*
 *  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 static_cast<int>(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 ldraw::Object* object = this->objectAt(index);
	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;
	}
	return {};
}

QVariant Model::getObjectProperty(const int index, const ldraw::Property property) const
{
	const ldraw::Object* object = this->body[unsigned_cast(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;
}

QModelIndex Model::lookup(ldraw::Id id) const
{
	// FIXME: This linear search will probably cause performance issues
	for (std::size_t i = 0; i < this->body.size(); i += 1)
	{
		if (this->body[i]->id == id)
		{
			return this->index(static_cast<int>(i));
		}
	}
	return {};
}

ldraw::Id Model::resolve(const QModelIndex& index) const
{
	return this->objectAt(index)->id;
}

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

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

ldraw::Object* Model::objectAt(const QModelIndex& index)
{
	return this->body[unsigned_cast(index.row())].get();
}

const ldraw::Object* Model::objectAt(const QModelIndex& index) const
{
	return this->body[unsigned_cast(index.row())].get();
}

mercurial