refactor: Move axis label drawing logic from ViewportWidget to SketchGrid

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2026-02-17 12:14:25 -07:00
parent a0dbc537cf
commit d274b4f59f
4 changed files with 43 additions and 38 deletions

View File

@@ -1,10 +1,12 @@
#include "SketchGrid.h" #include "SketchGrid.h"
#include "ViewportWidget.h"
#include <QOpenGLContext> #include <QOpenGLContext>
#include <QOpenGLExtraFunctions> #include <QOpenGLExtraFunctions>
#include <QOpenGLShaderProgram> #include <QOpenGLShaderProgram>
#include <QPainter>
#include <QVector> #include <QVector>
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)); m_vbo.allocate(vertices.constData(), vertices.size() * sizeof(GLfloat));
glDrawArrays(GL_POINTS, 0, 1); 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
}
}

View File

@@ -7,6 +7,8 @@
#include <QOpenGLBuffer> #include <QOpenGLBuffer>
class QOpenGLShaderProgram; class QOpenGLShaderProgram;
class QPainter;
class ViewportWidget;
class SketchGrid : protected QOpenGLFunctions class SketchGrid : protected QOpenGLFunctions
{ {
@@ -17,11 +19,12 @@ public:
YZ = 3 YZ = 3
}; };
SketchGrid(); explicit SketchGrid(ViewportWidget* viewport);
~SketchGrid(); ~SketchGrid();
void initializeGL(); void initializeGL();
void paintGL(SketchPlane plane, QOpenGLShaderProgram* shaderProgram, int colorLoc); void paintGL(SketchPlane plane, QOpenGLShaderProgram* shaderProgram, int colorLoc);
void paintAxisLabels(QPainter& painter, SketchPlane plane, const QMatrix4x4& modelView, const QMatrix4x4& projection);
private: private:
void drawGridLines(SketchPlane plane, QOpenGLShaderProgram* shaderProgram, int colorLoc); void drawGridLines(SketchPlane plane, QOpenGLShaderProgram* shaderProgram, int colorLoc);
@@ -29,6 +32,7 @@ private:
QOpenGLVertexArrayObject m_vao; QOpenGLVertexArrayObject m_vao;
QOpenGLBuffer m_vbo; QOpenGLBuffer m_vbo;
ViewportWidget* m_viewport = nullptr;
}; };
#endif // SKETCHGRID_H #endif // SKETCHGRID_H

View File

@@ -46,7 +46,7 @@ ViewportWidget::ViewportWidget(QWidget *parent)
connect(m_camera, &Camera::cameraChanged, this, QOverload<>::of(&QWidget::update)); connect(m_camera, &Camera::cameraChanged, this, QOverload<>::of(&QWidget::update));
connect(m_camera, &Camera::restoreStateAnimationFinished, this, &ViewportWidget::onRestoreStateAnimationFinished); connect(m_camera, &Camera::restoreStateAnimationFinished, this, &ViewportWidget::onRestoreStateAnimationFinished);
m_viewCube = new ViewCube(); m_viewCube = new ViewCube();
m_sketchGrid = new SketchGrid(); m_sketchGrid = new SketchGrid(this);
m_featureBrowser = new FeatureBrowser(); m_featureBrowser = new FeatureBrowser();
setMouseTracking(true); setMouseTracking(true);
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
@@ -171,7 +171,7 @@ void ViewportWidget::paintGL()
QPainter painter(this); QPainter painter(this);
if (m_currentPlane != SketchPlane::NONE) { if (m_currentPlane != SketchPlane::NONE) {
drawAxisLabels(painter, model, projection); m_sketchGrid->paintAxisLabels(painter, static_cast<SketchGrid::SketchPlane>(m_currentPlane), model, projection);
} }
m_featureBrowser->paint(painter, width(), height()); 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); 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) void ViewportWidget::onActiveToolChanged(int tool)
{ {
if (m_activeSketchTool) { if (m_activeSketchTool) {

View File

@@ -89,7 +89,6 @@ protected:
private: private:
void initShaders(); void initShaders();
void drawAxisLabels(QPainter& painter, const QMatrix4x4& modelView, const QMatrix4x4& projection);
void drawSketch(const SketchFeature* sketch); void drawSketch(const SketchFeature* sketch);
void drawSelectionPlanes(); void drawSelectionPlanes();
ViewportWidget::SketchPlane checkPlaneSelection(const QPoint& screenPos); ViewportWidget::SketchPlane checkPlaneSelection(const QPoint& screenPos);