src/model.h

Fri, 31 Jan 2020 00:25:35 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 31 Jan 2020 00:25:35 +0200
changeset 36
bbb901b97404
parent 35
98906a94732f
child 51
1a9eac27698d
permissions
-rw-r--r--

added render style storage

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

#pragma once
#include <QAbstractListModel>
#include <memory>
#include "main.h"
#include "header.h"
#include "linetypes/object.h"
#include "gl/common.h"

enum class HeaderProperty
{
	Name
};

class Model : public QAbstractListModel
{
	Q_OBJECT
public:
	class EditContext;
	Model(QObject* parent = nullptr);
	Model(const Model&) = delete;
	int size() const;
	EditContext edit();
	int rowCount(const QModelIndex&) const override;
	QVariant data(const QModelIndex& index, int role) const override;
	QVariant getHeaderProperty(const HeaderProperty property);
	const QString& getName() const;
	QVariant getObjectProperty(const int index, const ldraw::Property property) const;
	std::vector<gl::Polygon> getPolygons(class DocumentManager* documents) const;
	void getObjectPolygons(const int index, std::vector<gl::Polygon>& polygons_out, ldraw::GetPolygonsContext* context) const;
signals:
	void objectAdded(ldraw::Id id, int position);
private:
	using ModelObjectPointer = std::unique_ptr<ldraw::Object>;
	template<typename T, typename... Args>
	ldraw::Id append(Args&&... args);
	void append(ModelObjectPointer&& object);
	template<typename T, typename... Args>
	ldraw::Id insert(int position, Args&&... args);
	bool modified = false;
	QString path;
	LDHeader header;
	std::vector<ModelObjectPointer> body;
	std::map<ldraw::Id, ldraw::Object*> objectsById;
	mutable std::vector<gl::Polygon> cachedPolygons;
	mutable bool needRecache = true;
};

template<typename T, typename... Args>
ldraw::Id Model::append(Args&&... args)
{
	emit layoutAboutToBeChanged();
	this->body.push_back(std::make_unique<T>(args...));
	ldraw::Object* pointer = this->body.back().get();
	this->objectsById[pointer->id] = pointer;
	emit objectAdded(pointer->id, this->body.size() - 1);
	emit layoutChanged();
	return pointer->id;
}

template<typename T, typename... Args>
ldraw::Id Model::insert(int position, Args&&... args)
{
	emit layoutAboutToBeChanged();
	this->body.insert(position, std::make_unique<T>(args...));
	ldraw::Object* pointer = this->body[position].get();
	this->objectsById[pointer->id] = pointer;
	emit objectAdded(pointer->id, position);
	emit layoutChanged();
	return pointer->id;
}

mercurial