Thu, 15 Feb 2018 12:18:57 +0200
converted highlighting to mvc
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/lddocument.h | file | annotate | diff | comparison | revisions | |
src/model.cpp | file | annotate | diff | comparison | revisions | |
src/model.h | file | annotate | diff | comparison | revisions |
--- a/src/glcompiler.cpp Thu Feb 15 11:34:04 2018 +0200 +++ b/src/glcompiler.cpp Thu Feb 15 12:18:57 2018 +0200 @@ -89,7 +89,12 @@ this, SLOT(handleDataChange(QModelIndex, QModelIndex)) ); - // connect(renderer, SIGNAL(objectHighlightingChanged(LDObject*)), this, SLOT(compileObject(LDObject*))); + connect( + renderer, + SIGNAL(objectHighlightingChanged(QModelIndex, QModelIndex)), + this, + SLOT(handleObjectHighlightingChanged(QModelIndex, QModelIndex)) + ); connect(m_window, SIGNAL(gridChanged()), this, SLOT(recompile())); for (QModelIndex index : renderer->model()->indices()) @@ -206,7 +211,7 @@ if (this->selectionModel and this->selectionModel->isSelected(polygonOwnerIndex)) blendAlpha = 1.0; - else if (polygonOwner == m_renderer->objectAtCursor()) + else if (polygonOwnerIndex == m_renderer->objectAtCursor()) blendAlpha = 0.5; if (blendAlpha != 0.0) @@ -499,7 +504,7 @@ void GLCompiler::handleRowInsertion(const QModelIndex&, int first, int last) { for (int row = first; row <= last; row += 1) - compileObject(m_renderer->model()->index(row)); + m_staged.insert(m_renderer->model()->index(row)); } void GLCompiler::handleRowRemoval(const QModelIndex&, int first, int last) @@ -511,7 +516,15 @@ 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)); + m_staged.insert(m_renderer->model()->index(row)); +} + +void GLCompiler::handleObjectHighlightingChanged( + const QModelIndex& oldIndex, + const QModelIndex& newIndex +) { + m_staged.insert(oldIndex); + m_staged.insert(newIndex); } void GLCompiler::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
--- a/src/glcompiler.h Thu Feb 15 11:34:04 2018 +0200 +++ b/src/glcompiler.h Thu Feb 15 12:18:57 2018 +0200 @@ -83,6 +83,7 @@ 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 handleObjectHighlightingChanged(const QModelIndex& oldIndex, const QModelIndex& newIndex); void clearSelectionModel(); };
--- a/src/glrenderer.cpp Thu Feb 15 11:34:04 2018 +0200 +++ b/src/glrenderer.cpp Thu Feb 15 12:18:57 2018 +0200 @@ -71,7 +71,6 @@ m_toolTipTimer->setSingleShot (true); setAcceptDrops (true); connect (m_toolTipTimer, SIGNAL (timeout()), this, SLOT (showCameraIconTooltip())); - connect(model, SIGNAL(aboutToRemoveObject(LDObject*)), this, SLOT(removeObject(LDObject*))); resetAllAngles(); m_needZoomToFit = true; @@ -191,7 +190,7 @@ /* * Returns the object currently highlighted by the cursor. */ -LDObject* GLRenderer::objectAtCursor() const +QPersistentModelIndex GLRenderer::objectAtCursor() const { return m_objectAtCursor; } @@ -798,14 +797,6 @@ } } -// ============================================================================= -// -void GLRenderer::removeObject(LDObject* object) -{ - if (m_objectAtCursor == object) - m_objectAtCursor = nullptr; -} - /* * Returns an image containing the current render of the scene. */ @@ -936,18 +927,13 @@ // void GLRenderer::highlightCursorObject() { - if (not m_config->highlightObjectBelowCursor() and objectAtCursor() == nullptr) + if (not m_config->highlightObjectBelowCursor() and not objectAtCursor().isValid()) return; - LDObject* newObject = nullptr; - LDObject* oldObject = objectAtCursor(); - qint32 newIndex; + QModelIndex newIndex; + QModelIndex oldIndex = m_objectAtCursor; - if (m_isCameraMoving or not m_config->highlightObjectBelowCursor()) - { - newIndex = 0; - } - else + if (not m_isCameraMoving and m_config->highlightObjectBelowCursor()) { setPicking (true); drawGLScene(); @@ -955,21 +941,16 @@ unsigned char pixel[4]; glReadPixels (m_mousePosition.x(), height() - m_mousePosition.y(), 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixel[0]); - newIndex = pixel[0] * 0x10000 | pixel[1] * 0x100 | pixel[2]; + qint32 id = pixel[0] * 0x10000 | pixel[1] * 0x100 | pixel[2]; + + if (id != 0) + newIndex = model()->indexFromId(id); } - if (newIndex != (oldObject ? oldObject->id() : 0)) + if (newIndex != oldIndex) { - if (newIndex != 0) - newObject = LDObject::fromID (newIndex); - - m_objectAtCursor = newObject; - - if (oldObject) - emit objectHighlightingChanged(oldObject); - - if (newObject) - emit objectHighlightingChanged(newObject); + m_objectAtCursor = newIndex; + emit objectHighlightingChanged(oldIndex, newIndex); } update();
--- a/src/glrenderer.h Thu Feb 15 11:34:04 2018 +0200 +++ b/src/glrenderer.h Thu Feb 15 12:18:57 2018 +0200 @@ -63,7 +63,7 @@ const Model* model() const; QPoint const& mousePosition() const; QPointF const& mousePositionF() const; - LDObject* objectAtCursor() const; + QPersistentModelIndex objectAtCursor() const; QSet<LDObject*> pick(const QRect& range); LDObject* pick(int mouseX, int mouseY); void resetAllAngles(); @@ -84,7 +84,7 @@ static const GLRotationMatrix ldrawToGLAdapterMatrix; signals: - void objectHighlightingChanged(LDObject* object); + void objectHighlightingChanged(const QModelIndex& oldIndex, const QModelIndex& newIndex); protected: void initializeGL(); @@ -118,7 +118,7 @@ private: const Model* const m_model; class GLCompiler* m_compiler; - LDObject* m_objectAtCursor = nullptr; + QPersistentModelIndex m_objectAtCursor; CameraIcon m_cameraIcons[7]; QTimer* m_toolTipTimer; Qt::MouseButtons m_lastButtons; @@ -151,7 +151,6 @@ void initializeLighting(); void initGLData(); void needZoomToFit(); - Q_SLOT void removeObject(LDObject* object); void setPicking(bool picking); Q_SLOT void showCameraIconTooltip(); void zoomToFit();
--- a/src/lddocument.h Thu Feb 15 11:34:04 2018 +0200 +++ b/src/lddocument.h Thu Feb 15 12:18:57 2018 +0200 @@ -62,7 +62,7 @@ void inlineContents(Model& model, bool deep, bool renderinline); QList<LDPolygon> inlinePolygons(); const QSet<Vertex>& inlineVertices(); - void insertObject (int pos, LDObject* obj) __attribute__((deprecated)); + void insertObject (int pos, LDObject* obj); bool isFrozen() const; bool isSafeToClose(); QString name() const;
--- a/src/model.cpp Thu Feb 15 11:34:04 2018 +0200 +++ b/src/model.cpp Thu Feb 15 12:18:57 2018 +0200 @@ -638,6 +638,17 @@ return nullptr; } +QModelIndex Model::indexFromId(qint32 id) const +{ + for (int row = 0; row < this->size(); ++row) + { + if (this->objects()[row]->id() == id) + return index(row); + } + + return {}; +} + int countof(Model& model) { return model.size();
--- a/src/model.h Thu Feb 15 11:34:04 2018 +0200 +++ b/src/model.h Thu Feb 15 12:18:57 2018 +0200 @@ -113,6 +113,7 @@ LDObject* replaceWithFromString(LDObject* object, QString line); IndexGenerator indices() const; LDObject* lookup(const QModelIndex& index) const; + QModelIndex indexFromId(qint32 id) const; int rowCount(const QModelIndex& parent) const override; QVariant data(const QModelIndex& index, int role) const override;