src/model.h

changeset 150
b6cbba6e29a1
parent 148
e1ced2523cad
child 151
e628fc2e0c72
--- a/src/model.h	Tue Nov 02 15:43:57 2021 +0200
+++ b/src/model.h	Thu Mar 03 11:42:52 2022 +0200
@@ -37,23 +37,18 @@
 	class EditContext;
 	Model(QObject* parent = nullptr);
 	Model(const Model&) = delete;
+
 	int size() const;
 	ldraw::id_t at(int index) 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;
 	template<ldraw::Property Property>
 	ldraw::PropertyType<Property> getObjectProperty(const ldraw::id_t id) const;
-	std::vector<gl::Polygon> getPolygons(class DocumentManager* documents) const;
-	QModelIndex lookup(ldraw::id_t id) const;
-	ldraw::id_t resolve(const QModelIndex& index) const;
-	template<typename R>
-	ldraw::Id<R> checkType(ldraw::id_t id) const;
-	template<typename R>
-	bool isA(ldraw::id_t id) const;
+	QModelIndex find(ldraw::id_t id) const;
+	ldraw::id_t idAt(const QModelIndex& index) const;
 	template<typename R>
 	const R* get(ldraw::Id<R> id) const;
 	template<typename R>
@@ -67,10 +62,11 @@
 	template<typename R, typename Fn>
 	void apply(Fn f) const;
 	void save(QIODevice *device) const;
-	void makeUnofficial();
+	ldraw::Object* operator[](int index);
 Q_SIGNALS:
-	void objectAdded(ldraw::id_t id, int position);
-	void objectModified(ldraw::id_t id, int position);
+	void objectAdded(int position);
+	void objectModified(int position);
+	void objectRemoved(ldraw::id_t id);
 private:
 	using ModelObjectPointer = std::unique_ptr<ldraw::Object>;
 	template<typename T, typename... Args>
@@ -85,38 +81,19 @@
 	T* objectAt(ldraw::Id<T> id);
 	template<typename T>
 	const T* objectAt(ldraw::Id<T> id) const;
-	void getObjectPolygons(
-		const int index,
-		std::vector<gl::Polygon>& polygons_out,
-		ldraw::GetPolygonsContext* context) const;
 	void editFinished();
 	void objectModified(ldraw::id_t id);
 	bool modified = false;
 	LDHeader header;
 	std::vector<ModelObjectPointer> body;
 	std::map<ldraw::id_t, ldraw::Object*> objectsById;
-	mutable std::vector<gl::Polygon> cachedPolygons;
-	mutable bool needRecache = true;
 	/**
 	 * @brief Amount of model edit contexts active
 	 */
 	int editCounter = 0;
 };
 
-/**
- * @brief Checks whether the id is exactly of the specified type
- * @tparam R Type of LDraw line type object to test for
- * @param id Id of object to test
- * @returns whether the type of the object specified by @c id is the same type as R. Returns false if it is a subclass.
- */
-template<typename R>
-bool Model::isA(ldraw::id_t id) const
-{
-	const ldraw::Object* object = this->objectAt(this->lookup(id));
-	const std::type_info& a = typeid(*object);
-	const std::type_info& b = typeid(R);
-	return a == b;
-}
+void makeUnofficial(Model& model);
 
 /**
  * @brief Calls the specified function to all matching objects in the model
@@ -136,24 +113,6 @@
 	}
 }
 
-/**
- * \brief Checks type of object behind id
- * Checks whether the specified id refers to an object of the specified type.
- * \returns id casted to subclass if appropriate, null id otherwise
- */
-template<typename R>
-ldraw::Id<R> Model::checkType(ldraw::id_t id) const
-{
-	if (dynamic_cast<const R*>(this->objectAt(this->lookup(id))) != nullptr)
-	{
-		return ldraw::Id<R>{id.value};
-	}
-	else
-	{
-		return ldraw::NULL_ID;
-	}
-}
-
 template<typename T, typename... Args>
 ldraw::Id<T> Model::append(Args&&... args)
 {
@@ -162,9 +121,8 @@
 	this->body.push_back(std::make_unique<T>(args...));
 	ldraw::Object* pointer = this->body.back().get();
 	this->objectsById[pointer->id] = pointer;
-	Q_EMIT objectAdded(pointer->id, static_cast<int>(this->body.size() - 1));
+	Q_EMIT objectAdded(static_cast<int>(this->body.size() - 1));
 	Q_EMIT endInsertRows();
-	this->needRecache = true;
 	return ldraw::Id<T>{pointer->id.value};
 }
 
@@ -175,9 +133,8 @@
 	this->body.insert(std::begin(this->body) + position, std::make_unique<T>(args...));
 	ldraw::Object* pointer = this->body[position].get();
 	this->objectsById[pointer->id] = pointer;
-	Q_EMIT objectAdded(pointer->id, static_cast<int>(position));
+	Q_EMIT objectAdded(static_cast<int>(position));
 	Q_EMIT endInsertRows();
-	this->needRecache = true;
 	return ldraw::Id<T>{pointer->id.value};
 }
 
@@ -191,7 +148,7 @@
 Model::Get2Result<R> Model::get2(const ldraw::Id<R> id) const
 {
 	Get2Result<R> result;
-	result.index = this->lookup(id);
+	result.index = this->find(id);
 	if (result.index.isValid())
 	{
 		result.object = static_cast<const R*>(this->objectAt(result.index));
@@ -211,13 +168,13 @@
 template<typename T>
 T* Model::objectAt(ldraw::Id<T> id)
 {
-	return static_cast<T*>(this->objectAt(this->lookup(id)));
+	return static_cast<T*>(this->objectAt(this->find(id)));
 }
 
 template<typename T>
 const T* Model::objectAt(ldraw::Id<T> id) const
 {
-	return static_cast<const T*>(this->objectAt(this->lookup(id)));
+	return static_cast<const T*>(this->objectAt(this->find(id)));
 }
 
 /**

mercurial