136 lines
4.1 KiB
C++
136 lines
4.1 KiB
C++
#ifndef VIEWPORTWIDGET_H
|
|
#define VIEWPORTWIDGET_H
|
|
|
|
#include <QOpenGLWidget>
|
|
#include <QOpenGLFunctions>
|
|
#include <QMatrix4x4>
|
|
#include <QPoint>
|
|
#include <QVector3D>
|
|
#include <QRect>
|
|
#include <gp_Pnt.hxx>
|
|
#include <QMap>
|
|
#include <QOpenGLShaderProgram>
|
|
#include <QOpenGLVertexArrayObject>
|
|
#include <QOpenGLBuffer>
|
|
|
|
class QSvgRenderer;
|
|
class ViewCube;
|
|
class SketchGrid;
|
|
class Document;
|
|
class FeatureBrowser;
|
|
class SketchFeature;
|
|
class Camera;
|
|
class SketchTool;
|
|
class Snapping;
|
|
|
|
class ViewportWidget : public QOpenGLWidget, protected QOpenGLFunctions
|
|
{
|
|
Q_OBJECT
|
|
|
|
public:
|
|
enum class SketchPlane {
|
|
NONE,
|
|
XY,
|
|
XZ,
|
|
YZ
|
|
};
|
|
|
|
explicit ViewportWidget(QWidget *parent = nullptr);
|
|
~ViewportWidget();
|
|
|
|
void setDocument(Document* document);
|
|
|
|
QVector3D project(const QVector3D& worldCoord, const QMatrix4x4& modelView, const QMatrix4x4& projection, const QRect& viewport);
|
|
QVector3D unproject(const QPoint& screenPos, SketchPlane plane);
|
|
QOpenGLShaderProgram* shaderProgram() { return m_shaderProgram; }
|
|
QOpenGLBuffer& vbo() { return m_vbo; }
|
|
int colorLoc() const { return m_colorLoc; }
|
|
Camera* camera() const { return m_camera; }
|
|
Document* document() const { return m_document; }
|
|
SketchPlane currentPlane() const { return m_currentPlane; }
|
|
const QPoint& currentMousePos() const { return m_currentMousePos; }
|
|
bool isSnappingOrigin() const;
|
|
bool isSnappingVertex() const;
|
|
const gp_Pnt& snapVertex() const;
|
|
int activeTool() const { return m_activeTool; }
|
|
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();
|
|
|
|
public slots:
|
|
void onSketchModeStarted(SketchPlane plane);
|
|
void onSketchModeEnded();
|
|
void onPlaneSelectionModeStarted();
|
|
void onActiveToolChanged(int tool);
|
|
|
|
signals:
|
|
void lineAdded(const gp_Pnt& start, const gp_Pnt& end);
|
|
void rectangleAdded(const gp_Pnt& corner1, const gp_Pnt& corner2);
|
|
void circleAdded(const gp_Pnt& center, double radius);
|
|
void planeSelected(SketchPlane plane);
|
|
void toolDeactivated();
|
|
|
|
private slots:
|
|
void onRestoreStateAnimationFinished();
|
|
|
|
protected:
|
|
void initializeGL() override;
|
|
void paintGL() override;
|
|
void resizeGL(int w, int h) override;
|
|
|
|
void mousePressEvent(QMouseEvent *event) override;
|
|
void mouseReleaseEvent(QMouseEvent *event) override;
|
|
void mouseMoveEvent(QMouseEvent *event) override;
|
|
void wheelEvent(QWheelEvent *event) override;
|
|
void keyPressEvent(QKeyEvent *event) override;
|
|
bool focusNextPrevChild(bool next) override;
|
|
|
|
private:
|
|
void initShaders();
|
|
void drawSketch(const SketchFeature* sketch);
|
|
void drawSelectionPlanes();
|
|
ViewportWidget::SketchPlane checkPlaneSelection(const QPoint& screenPos);
|
|
|
|
QMatrix4x4 projection;
|
|
QOpenGLShaderProgram* m_shaderProgram = nullptr;
|
|
QOpenGLShaderProgram* m_litShaderProgram = nullptr;
|
|
QOpenGLVertexArrayObject m_vao;
|
|
QOpenGLBuffer m_vbo;
|
|
|
|
// Shader uniform locations
|
|
int m_projMatrixLoc = -1;
|
|
int m_mvMatrixLoc = -1;
|
|
int m_colorLoc = -1;
|
|
|
|
// Lit shader uniform locations
|
|
int m_litProjMatrixLoc = -1;
|
|
int m_litMvMatrixLoc = -1;
|
|
int m_litNormalMatrixLoc = -1;
|
|
Camera* m_camera = nullptr;
|
|
ViewCube* m_viewCube;
|
|
SketchGrid* m_sketchGrid = nullptr;
|
|
FeatureBrowser* m_featureBrowser = nullptr;
|
|
Document* m_document = nullptr;
|
|
SketchPlane m_currentPlane = SketchPlane::NONE;
|
|
|
|
bool m_isSelectingPlane = false;
|
|
SketchPlane m_highlightedPlane = SketchPlane::NONE;
|
|
|
|
int m_activeTool = 0;
|
|
SketchTool* m_activeSketchTool = nullptr;
|
|
QMap<int, SketchTool*> m_sketchTools;
|
|
QPoint m_currentMousePos;
|
|
Snapping* m_snapping = nullptr;
|
|
bool m_isSnappingHorizontal = false;
|
|
bool m_isSnappingVertical = false;
|
|
|
|
QMap<int, QSvgRenderer*> m_toolIcons;
|
|
QSvgRenderer* m_cursorRenderer = nullptr;
|
|
};
|
|
|
|
#endif // VIEWPORTWIDGET_H
|