src/model.cpp

Thu, 03 Mar 2022 21:13:16 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 03 Mar 2022 21:13:16 +0200
changeset 151
e628fc2e0c72
parent 150
b6cbba6e29a1
child 152
03f8e6d42e13
permissions
-rw-r--r--

Clean up Model

/*
 *  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 <QFile>
#include <QFileInfo>
#include <QFont>
#include <QSaveFile>
#include "model.h"
#include "modeleditcontext.h"
#include "documentmanager.h"

/**
 * @brief Constructs a model
 * @param parent QObject parent to pass forward
 */
Model::Model(QObject *parent) :
	QAbstractListModel{parent}
{
}

/**
 * @returns the amount of elements in the model
 */
int Model::size() const
{
	return static_cast<int>(this->body.size());
}

/**
 * @brief Looks up the object ID at the specified index. If out of bounds, returns NULL_ID.
 * @param index Index of object to look up
 * @return object ID
 */
ldraw::id_t Model::at(int index) const
{
	if (index >= 0 and index < this->size())
	{
		return this->body[index]->id;
	}
	else
	{
		return ldraw::NULL_ID;
	}
}

/**
 * @brief Obtains an editing context for this model.
 * Editing contexts are used to perform modifications to the model.
 * @return editing context
 */
Model::EditContext Model::edit()
{
	this->editCounter += 1;
	return {*this};
}

/**
 * @brief @overload QAbstractListModel::rowCount
 * @return size
 */
int Model::rowCount(const QModelIndex&) const
{
	return this->size();
}

/**
 * @brief @overload QAbstractListModel::data
 * @param index
 * @param role
 * @return QVariant
 */
QVariant Model::data(const QModelIndex& index, int role) const
{
	const ldraw::Object* object = (*this)[index.row()];
	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 {};
	}
}

/**
 * @brief Finds the position of the specified object in the model
 * @param id Object id to look for
 * @return model index
 */
QModelIndex Model::find(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 {};
}

/**
 * @brief Gets an object id by position in the model
 * @param index Position of the object in the model
 * @return id
 */
ldraw::id_t Model::idAt(const QModelIndex& index) const
{
	return (*this)[index.row()]->id;
}

#if 0
/**
 * @brief Sets the path to the model
 * @param path New path to use
 */
void Model::setPath(const QString &path)
{
	this->storedPath = path;
	this->header.name = QFileInfo{path}.fileName();
	// Update the "Name: 1234.dat" comment
	if (this->body.size() >= 2)
	{
		const ldraw::id_t id = this->body[1]->id;
		if (this->isA<ldraw::MetaCommand>(id))
		{
			const QString& textBody = this->body[1]->getProperty<ldraw::Property::Text>();
			if (textBody.startsWith("Name: "))
			{
				auto editor = this->edit();
				editor.setObjectProperty<ldraw::Property::Text>(id, "Name: " + this->header.name);
			}
		}
	}
}
#endif

/**
 * @brief Called by the editing context to signal to the model that editing is done.
 */
void Model::editFinished()
{
	this->editCounter -= 1;
}

/**
 * @brief Called by the editing context to indicate that the specified object has been modified.
 * @param id ID of the object that has been modified
 */
void Model::objectModified(ldraw::id_t id)
{
	const QModelIndex index = this->find(id);
	Q_EMIT this->dataChanged(index, index);
}

/**
 * @brief Adds the given object into the model.
 * @param object r-value reference to the object
 */
void Model::append(ModelObjectPointer&& object)
{
	const int position = static_cast<int>(this->body.size());
	Q_EMIT this->beginInsertRows({}, position, position);
	this->body.push_back(std::move(object));
	Q_EMIT this->endInsertRows();
	Q_EMIT this->objectAdded(position);
}

/**
 * @brief Removes the object at the specified position
 * @param position
 */
void Model::remove(int position)
{
	if (position >= 0 and position < signed_cast(this->body.size()))
	{
		Q_EMIT this->beginRemoveRows({}, position, position);
		const ldraw::id_t id = this->body[position]->id;
		this->body.erase(std::begin(this->body) + position);
		Q_EMIT this->endRemoveRows();
		Q_EMIT this->objectRemoved(id);
	}
}

/**
 * @brief Gets the object pointer at the specified position
 * @param index Position of the object
 * @returns object pointer
 */
ldraw::Object* Model::operator[](int index)
{
	if (index >= 0 and index < this->size())
	{
		return this->body[index].get();
	}
	else
	{
		throw std::out_of_range{"index out of range"};
	}
}

/**
 * @brief Gets the object pointer at the specified position
 * @param index Position of the object
 * @returns object pointer
 */
const ldraw::Object* Model::operator[](int index) const
{
	if (index >= 0 and index < this->size())
	{
		return this->body[index].get();
	}
	else
	{
		throw std::out_of_range{"index out of range"};
	}
}

/**
 * @brief Gets an object pointer by id. Used by the editing context to actually modify objects.
 * @param id
 * @return object pointer
 */
ldraw::Object* Model::findObjectById(const ldraw::id_t id)
{
	const QModelIndex index = this->find(id);
	if (index.isValid())
	{
		return (*this)[index.row()];
	}
	else
	{
		return nullptr;
	}
}

const ldraw::Object* Model::findObjectById(const ldraw::id_t id) const
{
	const QModelIndex index = this->find(id);
	if (index.isValid())
	{
		return (*this)[index.row()];
	}
	else
	{
		return nullptr;
	}
}

/**
 * @brief Attempts the save the model
 */
void save(const Model &model, QIODevice *device)
{
	QTextStream out{device};
	applyToModel<ldraw::Object>(model, [&](const ldraw::Object* object) {
		out << object->toLDrawCode() << "\r\n";
	});
}

/**
 * @brief Modifies the !LDRAW_ORG line so that it becomes unofficial
 */
void makeUnofficial(Model& model)
{
	if (model.size() >= 4)
	{
		const ldraw::Object* ldrawOrgLine = model[3];
		if (isA<ldraw::MetaCommand>(ldrawOrgLine))
		{
			const QString& body = ldrawOrgLine->getProperty<ldraw::Property::Text>();
			if (body.startsWith("!LDRAW_ORG ") and not body.startsWith("!LDRAW_ORG Unofficial_"))
			{
				// Add Unofficial_ to part type
				QStringList tokens = body.split(" ");
				tokens[1] = "Unofficial_" + tokens[1];
				// Remove the UPDATE tag if it's there
				if (tokens.size() >= 4 && tokens[2] == "UPDATE")
				{
					tokens.removeAt(3);
					tokens.removeAt(2);
				}
				Model::EditContext editor = model.edit();
				editor.setObjectProperty<ldraw::Property::Text>(ldrawOrgLine->id, tokens.join(" "));
			}
		}
	}
}

mercurial