src/model.cpp

Sun, 16 May 2021 22:41:00 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 16 May 2021 22:41:00 +0300
changeset 103
94719518e310
parent 86
4bec0525ef1b
child 111
1f42c03fafca
permissions
-rw-r--r--

update

/*
 *  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}
{
	connect(this, &Model::dataChanged, [&](){ this->needRecache = true; });
}

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_t 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_t 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)
{
	const int position = static_cast<int>(this->body.size());
	emit beginInsertRows({}, position, position);
	this->body.push_back(std::move(object));
	emit endInsertRows();
}

void Model::remove(int position)
{
	if (position >= 0 and position < signed_cast(this->body.size()))
	{
		emit beginRemoveRows({}, position, position);
		this->body.erase(std::begin(this->body) + position);
		emit endRemoveRows();
	}
}

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