src/model.h

Sun, 19 Jan 2020 14:25:43 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 19 Jan 2020 14:25:43 +0200
changeset 24
1a0faaaceb84
parent 21
0133e565e072
child 26
3a9e761e4faa
permissions
-rw-r--r--

added license

/*
 *  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>
	T* append(Args&&... args);
	void append(ModelObjectPointer&& object);
	template<typename T, typename... Args>
	T* 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>
T* Model::append(Args&&... args)
{
	emit layoutAboutToBeChanged();
	this->body.push_back(std::make_unique<T>(args...));
	T* pointer = static_cast<T*>(this->body.back().get());
	this->objectsById[pointer->id] = pointer;
	emit objectAdded(pointer->id, this->body.size() - 1);
	emit layoutChanged();
	return pointer;
}

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

mercurial