src/model.h

changeset 151
e628fc2e0c72
parent 150
b6cbba6e29a1
child 152
03f8e6d42e13
--- a/src/model.h	Thu Mar 03 11:42:52 2022 +0200
+++ b/src/model.h	Thu Mar 03 21:13:16 2022 +0200
@@ -43,10 +43,8 @@
 	EditContext edit();
 	int rowCount(const QModelIndex&) const override;
 	QVariant data(const QModelIndex& index, int role) const override;
-	QVariant getHeaderProperty(const HeaderProperty property);
-	QVariant getObjectProperty(const int index, const ldraw::Property property) const;
-	template<ldraw::Property Property>
-	ldraw::PropertyType<Property> getObjectProperty(const ldraw::id_t id) const;
+	ldraw::Object* findObjectById(const ldraw::id_t id);
+	const ldraw::Object* findObjectById(const ldraw::id_t id) const;
 	QModelIndex find(ldraw::id_t id) const;
 	ldraw::id_t idAt(const QModelIndex& index) const;
 	template<typename R>
@@ -59,10 +57,8 @@
 	};
 	template<typename R>
 	Get2Result<R> get2(ldraw::Id<R> id) const;
-	template<typename R, typename Fn>
-	void apply(Fn f) const;
-	void save(QIODevice *device) const;
 	ldraw::Object* operator[](int index);
+	const ldraw::Object* operator[](int index) const;
 Q_SIGNALS:
 	void objectAdded(int position);
 	void objectModified(int position);
@@ -75,16 +71,9 @@
 	template<typename T, typename... Args>
 	ldraw::Id<T> insert(std::size_t position, Args&&... args);
 	void remove(int position);
-	ldraw::Object* objectAt(const QModelIndex& index);
-	const ldraw::Object* objectAt(const QModelIndex& index) const;
-	template<typename T>
-	T* objectAt(ldraw::Id<T> id);
-	template<typename T>
-	const T* objectAt(ldraw::Id<T> id) 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;
 	/**
@@ -94,6 +83,7 @@
 };
 
 void makeUnofficial(Model& model);
+void save(const Model& model, QIODevice *device);
 
 /**
  * @brief Calls the specified function to all matching objects in the model
@@ -101,11 +91,12 @@
  * @param fn Function to call.
  */
 template<typename R, typename Fn>
-void Model::apply(Fn f) const
+void applyToModel(const Model& model, Fn&& f)
 {
-	for (const ModelObjectPointer& object : this->body)
+	for (int i = 0; i < model.size(); i += 1)
 	{
-		const R* subobject = dynamic_cast<const R*>(object.get());
+		const ldraw::Object* object = model[i];
+		const R* subobject = dynamic_cast<const R*>(object);
 		if (subobject != nullptr)
 		{
 			f(subobject);
@@ -151,7 +142,7 @@
 	result.index = this->find(id);
 	if (result.index.isValid())
 	{
-		result.object = static_cast<const R*>(this->objectAt(result.index));
+		result.object = static_cast<const R*>((*this)[result.index.row()]);
 	}
 	else
 	{
@@ -159,38 +150,3 @@
 	}
 	return result;
 }
-
-/**
- * @brief Gets an object pointer by id. Used by the editing context to actually modify objects.
- * @param id
- * @return object pointer
- */
-template<typename T>
-T* Model::objectAt(ldraw::Id<T> 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->find(id)));
-}
-
-/**
- * @brief Gets an object property by id
- * @tparam Property Property to obtain
- * @param id ID of object
- * @returns property or default value
- */
-template<ldraw::Property Property>
-ldraw::PropertyType<Property> Model::getObjectProperty(const ldraw::id_t id) const
-{
-	const ldraw::Object* const object = this->objectAt(id);
-	ldraw::PropertyType<Property> result;
-	if (object != nullptr)
-	{
-		result = object->getProperty<Property>();
-	}
-	return result;
-}

mercurial