diff --git a/src/SketchGrid.cpp b/src/SketchGrid.cpp index 44c1ce3..6c8b0bf 100644 --- a/src/SketchGrid.cpp +++ b/src/SketchGrid.cpp @@ -1,10 +1,12 @@ #include "SketchGrid.h" +#include "ViewportWidget.h" #include #include #include +#include #include -SketchGrid::SketchGrid() +SketchGrid::SketchGrid(ViewportWidget* viewport) : m_viewport(viewport) { } @@ -123,3 +125,36 @@ void SketchGrid::drawAxes(SketchPlane plane, QOpenGLShaderProgram* shaderProgram m_vbo.allocate(vertices.constData(), vertices.size() * sizeof(GLfloat)); glDrawArrays(GL_POINTS, 0, 1); } + +void SketchGrid::paintAxisLabels(QPainter& painter, SketchGrid::SketchPlane plane, const QMatrix4x4& modelView, const QMatrix4x4& projection) +{ + painter.setPen(Qt::white); + painter.setFont(QFont("Arial", 10)); + + const int range = 50; + const int step = 5; + + auto drawLabelsForAxis = [&](int axis_idx) { + for (int i = -range; i <= range; i += step) { + if (i == 0) continue; + QVector3D worldCoord; + worldCoord[axis_idx] = i; + + QVector3D screenPos = m_viewport->project(worldCoord, modelView, projection, m_viewport->rect()); + if (screenPos.z() < 1.0f) { // Not clipped + painter.drawText(screenPos.toPoint(), QString::number(i)); + } + } + }; + + if (plane == SketchGrid::XY) { + drawLabelsForAxis(0); // X + drawLabelsForAxis(2); // Y + } else if (plane == SketchGrid::XZ) { + drawLabelsForAxis(0); // X + drawLabelsForAxis(1); // Z + } else if (plane == SketchGrid::YZ) { + drawLabelsForAxis(1); // Y + drawLabelsForAxis(2); // Z + } +} diff --git a/src/SketchGrid.h b/src/SketchGrid.h index 2de391c..fc2f44e 100644 --- a/src/SketchGrid.h +++ b/src/SketchGrid.h @@ -7,6 +7,8 @@ #include class QOpenGLShaderProgram; +class QPainter; +class ViewportWidget; class SketchGrid : protected QOpenGLFunctions { @@ -17,11 +19,12 @@ public: YZ = 3 }; - SketchGrid(); + explicit SketchGrid(ViewportWidget* viewport); ~SketchGrid(); void initializeGL(); void paintGL(SketchPlane plane, QOpenGLShaderProgram* shaderProgram, int colorLoc); + void paintAxisLabels(QPainter& painter, SketchPlane plane, const QMatrix4x4& modelView, const QMatrix4x4& projection); private: void drawGridLines(SketchPlane plane, QOpenGLShaderProgram* shaderProgram, int colorLoc); @@ -29,6 +32,7 @@ private: QOpenGLVertexArrayObject m_vao; QOpenGLBuffer m_vbo; + ViewportWidget* m_viewport = nullptr; }; #endif // SKETCHGRID_H diff --git a/src/ViewportWidget.cpp b/src/ViewportWidget.cpp index f305a40..6c232c5 100644 --- a/src/ViewportWidget.cpp +++ b/src/ViewportWidget.cpp @@ -46,7 +46,7 @@ ViewportWidget::ViewportWidget(QWidget *parent) connect(m_camera, &Camera::cameraChanged, this, QOverload<>::of(&QWidget::update)); connect(m_camera, &Camera::restoreStateAnimationFinished, this, &ViewportWidget::onRestoreStateAnimationFinished); m_viewCube = new ViewCube(); - m_sketchGrid = new SketchGrid(); + m_sketchGrid = new SketchGrid(this); m_featureBrowser = new FeatureBrowser(); setMouseTracking(true); setFocusPolicy(Qt::StrongFocus); @@ -171,7 +171,7 @@ void ViewportWidget::paintGL() QPainter painter(this); if (m_currentPlane != SketchPlane::NONE) { - drawAxisLabels(painter, model, projection); + m_sketchGrid->paintAxisLabels(painter, static_cast(m_currentPlane), model, projection); } m_featureBrowser->paint(painter, width(), height()); @@ -353,39 +353,6 @@ QVector3D ViewportWidget::project(const QVector3D& worldCoord, const QMatrix4x4& return QVector3D(winX, winY, (ndc.z() + 1.0) / 2.0); } -void ViewportWidget::drawAxisLabels(QPainter& painter, const QMatrix4x4& modelView, const QMatrix4x4& projection) -{ - painter.setPen(Qt::white); - painter.setFont(QFont("Arial", 10)); - - const int range = 50; - const int step = 5; - - auto drawLabelsForAxis = [&](int axis_idx) { - for (int i = -range; i <= range; i += step) { - if (i == 0) continue; - QVector3D worldCoord; - worldCoord[axis_idx] = i; - - QVector3D screenPos = project(worldCoord, modelView, projection, rect()); - if (screenPos.z() < 1.0f) { // Not clipped - painter.drawText(screenPos.toPoint(), QString::number(i)); - } - } - }; - - if (m_currentPlane == SketchPlane::XY) { - drawLabelsForAxis(0); // X - drawLabelsForAxis(2); // Y - } else if (m_currentPlane == SketchPlane::XZ) { - drawLabelsForAxis(0); // X - drawLabelsForAxis(1); // Z - } else if (m_currentPlane == SketchPlane::YZ) { - drawLabelsForAxis(1); // Y - drawLabelsForAxis(2); // Z - } -} - void ViewportWidget::onActiveToolChanged(int tool) { if (m_activeSketchTool) { diff --git a/src/ViewportWidget.h b/src/ViewportWidget.h index c0fd68b..3bbdefb 100644 --- a/src/ViewportWidget.h +++ b/src/ViewportWidget.h @@ -89,7 +89,6 @@ protected: private: void initShaders(); - void drawAxisLabels(QPainter& painter, const QMatrix4x4& modelView, const QMatrix4x4& projection); void drawSketch(const SketchFeature* sketch); void drawSelectionPlanes(); ViewportWidget::SketchPlane checkPlaneSelection(const QPoint& screenPos);