used mvc selection models

Thu, 15 Feb 2018 11:34:04 +0200

author
Santeri Piippo
date
Thu, 15 Feb 2018 11:34:04 +0200
changeset 1246
0054d13ed614
parent 1245
338d66111168
child 1247
7e1ce2fc066b

used mvc selection models

src/glcompiler.cpp file | annotate | diff | comparison | revisions
src/glcompiler.h file | annotate | diff | comparison | revisions
src/glrenderer.cpp file | annotate | diff | comparison | revisions
src/glrenderer.h file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/mainwindow.h file | annotate | diff | comparison | revisions
--- a/src/glcompiler.cpp	Thu Feb 15 10:24:39 2018 +0200
+++ b/src/glcompiler.cpp	Thu Feb 15 11:34:04 2018 +0200
@@ -137,9 +137,13 @@
  * - polygonOwner is the LDObject from which the polygon originated.
  * - subclass provides context for the polygon.
  */
-QColor GLCompiler::getColorForPolygon(LDPolygon& polygon, LDObject* polygonOwner, VboSubclass subclass)
-{
+QColor GLCompiler::getColorForPolygon(
+	LDPolygon& polygon,
+	const QModelIndex& polygonOwnerIndex,
+	VboSubclass subclass
+) {
 	QColor color;
+	LDObject* polygonOwner = m_renderer->model()->lookup(polygonOwnerIndex);
 
 	switch (subclass)
 	{
@@ -200,7 +204,7 @@
 		// We may wish to apply blending on the color to indicate selection or highlight.
 		double blendAlpha = 0.0;
 
-		if (polygonOwner->isSelected())
+		if (this->selectionModel and this->selectionModel->isSelected(polygonOwnerIndex))
 			blendAlpha = 1.0;
 		else if (polygonOwner == m_renderer->objectAtCursor())
 			blendAlpha = 0.5;
@@ -337,7 +341,7 @@
 /*
  * Compiles a single object.
  */
-void GLCompiler::compileObject(QModelIndex index)
+void GLCompiler::compileObject(const QModelIndex& index)
 {
 	LDObject* object = m_renderer->model()->lookup(index);
 
@@ -358,7 +362,7 @@
 		{
 			LDPolygon* poly = object->getPolygon();
 			poly->id = object->id();
-			compilePolygon (*poly, object, info);
+			compilePolygon (*poly, index, info);
 			delete poly;
 			break;
 		}
@@ -372,7 +376,7 @@
 			for (LDPolygon& poly : data)
 			{
 				poly.id = object->id();
-				compilePolygon (poly, object, info);
+				compilePolygon (poly, index, info);
 			}
 			break;
 		}
@@ -383,7 +387,7 @@
 			for (LDPolygon& polygon : curve->rasterizePolygons(grid()->bezierCurveSegments()))
 			{
 				polygon.id = object->id();
-				compilePolygon (polygon, object, info);
+				compilePolygon (polygon, index, info);
 			}
 		}
 		break;
@@ -399,8 +403,11 @@
 /*
  * Inserts a single polygon into VBOs.
  */
-void GLCompiler::compilePolygon(LDPolygon& poly, LDObject* polygonOwner, ObjectVboData& objectInfo)
-{
+void GLCompiler::compilePolygon(
+	LDPolygon& poly,
+	const QModelIndex& polygonOwnerIndex,
+	ObjectVboData& objectInfo
+) {
 	VboClass surface;
 	int vertexCount;
 
@@ -429,7 +436,7 @@
 	{
 		const int vbonum = vboNumber (surface, complement);
 		QVector<GLfloat>& vbodata = objectInfo.data[vbonum];
-		const QColor color = getColorForPolygon (poly, polygonOwner, complement);
+		const QColor color = getColorForPolygon (poly, polygonOwnerIndex, complement);
 
 		for (int vert = 0; vert < vertexCount; ++vert)
 		{
@@ -506,3 +513,43 @@
 	for (int row = topLeft.row(); row <= bottomRight.row(); row += 1)
 		compileObject(m_renderer->model()->index(row));
 }
+
+void GLCompiler::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
+{
+	for (const QModelIndex& index : selected.indexes())
+		m_staged.insert(index);
+
+	for (const QModelIndex& index : deselected.indexes())
+		m_staged.insert(index);
+
+	m_renderer->update();
+}
+
+void GLCompiler::setSelectionModel(QItemSelectionModel* selectionModel)
+{
+	if (this->selectionModel)
+		disconnect(this->selectionModel, 0, 0, 0);
+
+	this->selectionModel = selectionModel;
+
+	if (this->selectionModel)
+	{
+		connect(
+			this->selectionModel,
+			SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
+			this,
+			SLOT(selectionChanged(const QItemSelection&, const QItemSelection&))
+		);
+		connect(
+			this->selectionModel,
+			SIGNAL(destroyed(QObject*)),
+			this,
+			SLOT(clearSelectionModel())
+		);
+	}
+}
+
+void GLCompiler::clearSelectionModel()
+{
+	this->setSelectionModel(nullptr);
+}
--- a/src/glcompiler.h	Thu Feb 15 10:24:39 2018 +0200
+++ b/src/glcompiler.h	Thu Feb 15 11:34:04 2018 +0200
@@ -38,9 +38,13 @@
 	void prepareVBO (int vbonum);
 	GLuint vbo (int vbonum) const;
 	int vboSize (int vbonum) const;
+	void setSelectionModel(QItemSelectionModel* selectionModel);
 
 	static int vboNumber (VboClass surface, VboSubclass complement);
 
+public slots:
+	void selectionChanged(const QItemSelection& selected, const QItemSelection& deselected);
+
 private:
 	struct ObjectVboData
 	{
@@ -48,9 +52,17 @@
 	};
 
 	void compileStaged();
-	void compilePolygon (LDPolygon& poly, LDObject* polygonOwner, ObjectVboData& objectInfo);
-	Q_SLOT void compileObject (QModelIndex index);
-	QColor getColorForPolygon (LDPolygon& poly, LDObject* topobj, VboSubclass complement);
+	void compilePolygon(
+		LDPolygon& poly,
+		const QModelIndex& polygonOwnerIndex,
+		ObjectVboData& objectInfo
+	);
+	Q_SLOT void compileObject (const QModelIndex &index);
+	QColor getColorForPolygon(
+		LDPolygon& poly,
+		const QModelIndex& polygonOwnerIndex,
+		VboSubclass complement
+	);
 	QColor indexColorForID (qint32 id) const;
 	void needMerge();
 	Q_SLOT void recompile();
@@ -65,11 +77,13 @@
 	bool m_vboChanged[NumVbos] = {true};
 	int m_vboSizes[NumVbos] = {0};
 	GLRenderer* m_renderer;
+	QItemSelectionModel* selectionModel = nullptr;
 
 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);
+	void clearSelectionModel();
 };
 
 #define CHECK_GL_ERROR() { checkGLError(this, __FILE__, __LINE__); }
--- a/src/glrenderer.cpp	Thu Feb 15 10:24:39 2018 +0200
+++ b/src/glrenderer.cpp	Thu Feb 15 11:34:04 2018 +0200
@@ -1036,7 +1036,7 @@
  */
 void GLRenderer::drawFixedCameraBackdrop() {}
 
-void GLRenderer::setSelection(const QItemSelection& selection)
+void GLRenderer::setSelectionModel(QItemSelectionModel* selectionModel)
 {
-	this->m_selectedItems = selection;
+	this->m_compiler->setSelectionModel(selectionModel);
 }
--- a/src/glrenderer.h	Thu Feb 15 10:24:39 2018 +0200
+++ b/src/glrenderer.h	Thu Feb 15 11:34:04 2018 +0200
@@ -72,7 +72,7 @@
 	void setBackground();
 	void setCamera(Camera cam);
 	QPen textPen() const;
-	void setSelection(const QItemSelection& selection);
+	void setSelectionModel(QItemSelectionModel* selectionModel);
 
 	static const QPen thinBorderPen;
 	static const GLRotationMatrix topCameraMatrix;
@@ -141,7 +141,6 @@
 	QColor m_backgroundColor;
 	GLuint m_axesVbo;
 	GLuint m_axesColorVbo;
-	QItemSelection m_selectedItems;
 
 	void calcCameraIcons();
 	void drawGLScene();
--- a/src/mainwindow.cpp	Thu Feb 15 10:24:39 2018 +0200
+++ b/src/mainwindow.cpp	Thu Feb 15 11:34:04 2018 +0200
@@ -74,10 +74,6 @@
 	connect (m_tabs, SIGNAL (currentChanged(int)), this, SLOT (tabSelected()));
 	connect (m_tabs, SIGNAL (tabCloseRequested (int)), this, SLOT (closeTab (int)));
 	connect(m_documents, SIGNAL(documentClosed(LDDocument*)), this, SLOT(documentClosed(LDDocument*)));
-	connect(
-		ui.objectList->selectionModel(), SIGNAL(selectionChanged()),
-		this, SLOT(selectionChanged())
-	);
 
 	if (m_primitives->activeScanner())
 		connect (m_primitives->activeScanner(), SIGNAL (workDone()), this, SLOT (updatePrimitives()));
@@ -973,6 +969,17 @@
 		updateTitle();
 		print ("Changed document to %1", document->getDisplayName());
 		ui.objectList->setModel(document);
+		QItemSelectionModel* selection = m_selections.value(document);
+
+		if (selection == nullptr)
+		{
+			m_selections[document] = ui.objectList->selectionModel();
+			renderer->setSelectionModel(m_selections[document]);
+		}
+		else
+		{
+			ui.objectList->setSelectionModel(selection);
+		}
 	}
 }
 
@@ -985,9 +992,7 @@
 
 	if (not renderer)
 	{
-		print("MainWindow: Couldn't find a renderer for %1, creating one now", document->getDisplayName());
 		renderer = new Canvas {document, this};
-		print("Created renderer: %1", renderer);
 		m_renderers[document] = renderer;
 		ui.rendererStack->addWidget(renderer);
 		connect(m_messageLog, SIGNAL(changed()), renderer, SLOT(update()));
--- a/src/mainwindow.h	Thu Feb 15 10:24:39 2018 +0200
+++ b/src/mainwindow.h	Thu Feb 15 11:34:04 2018 +0200
@@ -146,6 +146,7 @@
 	class GuiUtilities* m_guiUtilities;
 	MessageManager* m_messageLog = nullptr;
 	QMap<LDDocument*, Canvas*> m_renderers;
+	QMap<LDDocument*, QItemSelectionModel*> m_selections;
 	PrimitiveManager* m_primitives;
 	Grid* m_grid;
 	MathFunctions* m_mathFunctions;

mercurial