more work on mvc

Thu, 15 Feb 2018 10:24:39 +0200

author
Santeri Piippo
date
Thu, 15 Feb 2018 10:24:39 +0200
changeset 1245
338d66111168
parent 1244
68e126e8c629
child 1246
0054d13ed614

more work on mvc

src/glShared.h file | annotate | diff | comparison | revisions
src/glcompiler.cpp file | annotate | diff | comparison | revisions
src/glcompiler.h file | annotate | diff | comparison | revisions
src/model.cpp file | annotate | diff | comparison | revisions
src/model.h file | annotate | diff | comparison | revisions
--- a/src/glShared.h	Wed Feb 14 15:17:30 2018 +0200
+++ b/src/glShared.h	Thu Feb 15 10:24:39 2018 +0200
@@ -47,6 +47,8 @@
 	}
 };
 
+Q_DECLARE_METATYPE(LDPolygon)
+
 enum class VboClass
 {
 	Lines,
--- a/src/glcompiler.cpp	Wed Feb 14 15:17:30 2018 +0200
+++ b/src/glcompiler.cpp	Thu Feb 15 10:24:39 2018 +0200
@@ -71,10 +71,25 @@
 	HierarchyElement (renderer),
 	m_renderer (renderer)
 {
-	connect(renderer->model(), SIGNAL(objectAdded(LDObject*)), this, SLOT(compileObject(LDObject*)));
-	connect(renderer->model(), SIGNAL(objectModified(LDObject*)), this, SLOT(compileObject(LDObject*)));
-	connect(renderer->model(), SIGNAL(aboutToRemoveObject(LDObject*)), this, SLOT(forgetObject(LDObject*)), Qt::DirectConnection);
-	connect(renderer, SIGNAL(objectHighlightingChanged(LDObject*)), this, SLOT(compileObject(LDObject*)));
+	connect(
+		renderer->model(),
+		SIGNAL(rowsInserted(QModelIndex, int, int)),
+		this,
+		SLOT(handleRowInsertion(QModelIndex, int, int))
+	);
+	connect(
+		renderer->model(),
+		SIGNAL(rowsRemoved(QModelIndex, int, int)),
+		this,
+		SLOT(handleRowRemoval(QModelIndex, int, int))
+	);
+	connect(
+		renderer->model(),
+		SIGNAL(dataChanged(QModelIndex, QModelIndex, QVector<int>)),
+		this,
+		SLOT(handleDataChange(QModelIndex, QModelIndex))
+	);
+	// connect(renderer, SIGNAL(objectHighlightingChanged(LDObject*)), this, SLOT(compileObject(LDObject*)));
 	connect(m_window, SIGNAL(gridChanged()), this, SLOT(recompile()));
 
 	for (QModelIndex index : renderer->model()->indices())
@@ -231,7 +246,7 @@
 /*
  * Stages the given object for compilation.
  */
-void GLCompiler::stageForCompilation(QModelIndex index)
+void GLCompiler::stageForCompilation(const QModelIndex& index)
 {
 	m_staged.insert(index);
 }
@@ -239,16 +254,11 @@
 /*
  * Removes an object from the set of objects to be compiled.
  */
-void GLCompiler::unstage(QModelIndex index)
+void GLCompiler::unstage(const QModelIndex& index)
 {
 	m_staged.remove(index);
 }
 
-LDObject* GLCompiler::resolveObject(const QModelIndex& index)
-{
-	return m_renderer->model()->data(index, Model::ObjectRole).value<LDObject*>();
-}
-
 /*
  * Compiles all staged objects.
  */
@@ -283,7 +293,7 @@
 			}
 			else
 			{
-				LDObject* object = resolveObject(iterator.key());
+				LDObject* object = m_renderer->model()->lookup(iterator.key());
 				if (not object->isHidden())
 					vbodata += iterator->data[vbonum];
 
@@ -329,7 +339,7 @@
  */
 void GLCompiler::compileObject(QModelIndex index)
 {
-	LDObject* object = resolveObject(index);
+	LDObject* object = m_renderer->model()->lookup(index);
 
 	if (object == nullptr)
 		return;
@@ -478,3 +488,21 @@
 	for (QModelIndex index : m_renderer->model()->indices())
 		compileObject(index);
 }
+
+void GLCompiler::handleRowInsertion(const QModelIndex&, int first, int last)
+{
+	for (int row = first; row <= last; row += 1)
+		compileObject(m_renderer->model()->index(row));
+}
+
+void GLCompiler::handleRowRemoval(const QModelIndex&, int first, int last)
+{
+	for (int row = first; row <= last; row += 1)
+		forgetObject(m_renderer->model()->index(row));
+}
+
+void GLCompiler::handleDataChange(const QModelIndex& topLeft, const QModelIndex& bottomRight)
+{
+	for (int row = topLeft.row(); row <= bottomRight.row(); row += 1)
+		compileObject(m_renderer->model()->index(row));
+}
--- a/src/glcompiler.h	Wed Feb 14 15:17:30 2018 +0200
+++ b/src/glcompiler.h	Thu Feb 15 10:24:39 2018 +0200
@@ -56,9 +56,8 @@
 	Q_SLOT void recompile();
 	void dropObjectInfo (const QModelIndex &index);
 	Q_SLOT void forgetObject(QModelIndex index);
-	void stageForCompilation(QModelIndex index);
-	void unstage (QModelIndex index);
-	LDObject* resolveObject(const QModelIndex& index);
+	void stageForCompilation(const QModelIndex &index);
+	void unstage (const QModelIndex &index);
 
 	QMap<QPersistentModelIndex, ObjectVboData> m_objectInfo;
 	QSet<QPersistentModelIndex> m_staged; // Objects that need to be compiled
@@ -66,6 +65,11 @@
 	bool m_vboChanged[NumVbos] = {true};
 	int m_vboSizes[NumVbos] = {0};
 	GLRenderer* m_renderer;
+
+private slots:
+	void handleRowInsertion(const QModelIndex&, int first, int last);
+	void handleRowRemoval(const QModelIndex&, int first, int last);
+	void handleDataChange(const QModelIndex& topLeft, const QModelIndex &bottomRight);
 };
 
 #define CHECK_GL_ERROR() { checkGLError(this, __FILE__, __LINE__); }
--- a/src/model.cpp	Wed Feb 14 15:17:30 2018 +0200
+++ b/src/model.cpp	Thu Feb 15 10:24:39 2018 +0200
@@ -69,10 +69,12 @@
 		object->model()->withdraw(object);
 
 	// TODO: check that the object isn't in the vector once there's a cheap way to do so!
+	beginInsertRows({}, position, position);
 	_objects.insert(position, object);
 	_needsTriangleRecount = true;
 	object->setDocument(this);
 	emit objectAdded(object);
+	endInsertRows();
 }
 
 /*
@@ -255,10 +257,12 @@
  */
 LDObject* Model::withdrawAt(int position)
 {
+	beginRemoveRows({}, position, position);
 	LDObject* object = _objects[position];
 	emit aboutToRemoveObject(object);
 	_objects.removeAt(position);
 	_needsTriangleRecount = true;
+	endRemoveRows();
 	return object;
 }
 
@@ -595,14 +599,15 @@
 			return {};
 		}
 
-	case ObjectRole:
-		return {qMetaTypeId<LDObject*>(), object};
+	case ObjectIdRole:
+		return object->id();
 
 	default:
 		return {};
 	}
 }
 
+/*
 bool Model::removeRows(int row, int count, const QModelIndex& parent)
 {
 	if (row >= 0 and row < size() and count <= size() - row)
@@ -620,6 +625,18 @@
 		return false;
 	}
 }
+*/
+
+/*
+ * Looks up an object by the given index.
+ */
+LDObject* Model::lookup(const QModelIndex &index) const
+{
+	if (index.row() >= 0 and index.row() < size())
+		return this->objects()[index.row()];
+	else
+		return nullptr;
+}
 
 int countof(Model& model)
 {
--- a/src/model.h	Wed Feb 14 15:17:30 2018 +0200
+++ b/src/model.h	Thu Feb 15 10:24:39 2018 +0200
@@ -79,7 +79,7 @@
 public:
 	enum
 	{
-		ObjectRole = Qt::UserRole
+		ObjectIdRole = Qt::UserRole,
 	};
 
 	Model(class DocumentManager* manager);
@@ -112,10 +112,11 @@
 	LDObject* addFromString(QString line);
 	LDObject* replaceWithFromString(LDObject* object, QString line);
 	IndexGenerator indices() const;
+	LDObject* lookup(const QModelIndex& index) const;
 
 	int rowCount(const QModelIndex& parent) const override;
 	QVariant data(const QModelIndex& index, int role) const override;
-	bool removeRows(int row, int count, const QModelIndex& ) override;
+	// bool removeRows(int row, int count, const QModelIndex& ) override;
 
 signals:
 	void objectAdded(LDObject* object);

mercurial