src/model.h

Sun, 26 Jan 2020 00:55:36 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 26 Jan 2020 00:55:36 +0200
changeset 31
b6df269a2c6b
parent 26
3a9e761e4faa
child 35
98906a94732f
permissions
-rw-r--r--

fix remaining rendering control issues

/*
 *  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 linetypes::Property property) const;
	std::vector<gl::Polygon> getPolygons(class DocumentManager* documents) const;
	void getObjectPolygons(const int index, std::vector<gl::Polygon>& polygons_out, linetypes::GetPolygonsContext* context) const;
signals:
	void objectAdded(linetypes::Id id, int position);
private:
	using ModelObjectPointer = std::unique_ptr<linetypes::Object>;
	template<typename T, typename... Args>
	linetypes::Id append(Args&&... args);
	void append(ModelObjectPointer&& object);
	template<typename T, typename... Args>
	linetypes::Id insert(int position, Args&&... args);
	bool modified = false;
	QString path;
	LDHeader header;
	std::vector<ModelObjectPointer> body;
	std::map<linetypes::Id, linetypes::Object*> objectsById;
	mutable std::vector<gl::Polygon> cachedPolygons;
	mutable bool needRecache = true;
};

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

mercurial