From 35cad7436760d630d20cf18e8444c51519595de2 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Tue, 17 Feb 2026 10:57:02 -0700 Subject: [PATCH] fix: Move line tool specific logic from ViewportWidget to LineTool Co-authored-by: aider (gemini/gemini-2.5-pro) --- src/LineTool.cpp | 34 ++++++++++++++++++++++++++++++- src/ViewportWidget.cpp | 45 +++++++++++------------------------------- src/ViewportWidget.h | 2 ++ 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/src/LineTool.cpp b/src/LineTool.cpp index ca118c1..8feaedc 100644 --- a/src/LineTool.cpp +++ b/src/LineTool.cpp @@ -153,7 +153,39 @@ void LineTool::mousePressEvent(QMouseEvent *event) void LineTool::mouseMoveEvent(QMouseEvent *event) { - // To be implemented + bool oldIsSnappingHorizontal = m_viewport->isSnappingHorizontal(); + bool oldIsSnappingVertical = m_viewport->isSnappingVertical(); + m_viewport->setSnappingHorizontal(false); + m_viewport->setSnappingVertical(false); + + if (m_isDefiningLine && !m_viewport->isSnappingOrigin() && !m_viewport->isSnappingVertex()) { + QVector3D worldPos = m_viewport->unproject(m_viewport->currentMousePos(), m_viewport->currentPlane()); + QVector3D startPos(m_firstLinePoint.X(), m_firstLinePoint.Y(), m_firstLinePoint.Z()); + QVector3D delta = worldPos - startPos; + + if (delta.length() > 1e-6) { + const double snapAngleThreshold = qDegreesToRadians(2.0); + double angle = 0; + + if (m_viewport->currentPlane() == ViewportWidget::SketchPlane::XY) { + angle = atan2(delta.z(), delta.x()); + } else if (m_viewport->currentPlane() == ViewportWidget::SketchPlane::XZ) { + angle = atan2(delta.y(), delta.x()); + } else if (m_viewport->currentPlane() == ViewportWidget::SketchPlane::YZ) { + angle = atan2(delta.z(), delta.y()); + } + + if (qAbs(sin(angle)) < sin(snapAngleThreshold)) { + m_viewport->setSnappingHorizontal(true); + } else if (qAbs(cos(angle)) < sin(snapAngleThreshold)) { + m_viewport->setSnappingVertical(true); + } + } + } + + if (oldIsSnappingHorizontal != m_viewport->isSnappingHorizontal() || oldIsSnappingVertical != m_viewport->isSnappingVertical()) { + m_viewport->update(); + } } void LineTool::keyPressEvent(QKeyEvent *event) diff --git a/src/ViewportWidget.cpp b/src/ViewportWidget.cpp index d1e1095..bfc6b5a 100644 --- a/src/ViewportWidget.cpp +++ b/src/ViewportWidget.cpp @@ -385,38 +385,8 @@ void ViewportWidget::mouseMoveEvent(QMouseEvent *event) update(); } - bool oldIsSnappingHorizontal = m_isSnappingHorizontal; - bool oldIsSnappingVertical = m_isSnappingVertical; - m_isSnappingHorizontal = false; - m_isSnappingVertical = false; - - if (m_isDefiningLine && !m_isSnappingOrigin && !m_isSnappingVertex) { - QVector3D worldPos = unproject(m_currentMousePos, m_currentPlane); - QVector3D startPos(m_firstLinePoint.X(), m_firstLinePoint.Y(), m_firstLinePoint.Z()); - QVector3D delta = worldPos - startPos; - - if (delta.length() > 1e-6) { - const double snapAngleThreshold = qDegreesToRadians(2.0); - double angle = 0; - - if (m_currentPlane == SketchPlane::XY) { - angle = atan2(delta.z(), delta.x()); - } else if (m_currentPlane == SketchPlane::XZ) { - angle = atan2(delta.y(), delta.x()); - } else if (m_currentPlane == SketchPlane::YZ) { - angle = atan2(delta.z(), delta.y()); - } - - if (qAbs(sin(angle)) < sin(snapAngleThreshold)) { - m_isSnappingHorizontal = true; - } else if (qAbs(cos(angle)) < sin(snapAngleThreshold)) { - m_isSnappingVertical = true; - } - } - } - - if (oldIsSnappingHorizontal != m_isSnappingHorizontal || oldIsSnappingVertical != m_isSnappingVertical) { - update(); + if (m_activeSketchTool) { + m_activeSketchTool->mouseMoveEvent(event); } if (event->buttons() & Qt::MiddleButton) { @@ -465,6 +435,16 @@ void ViewportWidget::deactivateActiveTool() emit toolDeactivated(); } +void ViewportWidget::setSnappingHorizontal(bool snapping) +{ + m_isSnappingHorizontal = snapping; +} + +void ViewportWidget::setSnappingVertical(bool snapping) +{ + m_isSnappingVertical = snapping; +} + bool ViewportWidget::focusNextPrevChild(bool next) { if (m_activeTool != static_cast(ApplicationController::ToolType::None)) { @@ -650,7 +630,6 @@ void ViewportWidget::onActiveToolChanged(int tool) } m_activeTool = tool; - m_isDefiningLine = false; if (m_sketchTools.contains(tool)) { m_activeSketchTool = m_sketchTools.value(tool); diff --git a/src/ViewportWidget.h b/src/ViewportWidget.h index 2848157..900082f 100644 --- a/src/ViewportWidget.h +++ b/src/ViewportWidget.h @@ -52,6 +52,8 @@ public: const gp_Pnt& snapVertex() const { return m_snapVertex; } bool isSnappingHorizontal() const { return m_isSnappingHorizontal; } bool isSnappingVertical() const { return m_isSnappingVertical; } + void setSnappingHorizontal(bool snapping); + void setSnappingVertical(bool snapping); void addLine(const gp_Pnt& start, const gp_Pnt& end); void deactivateActiveTool();