ImagingTools Core SDK
COpenGLWidget.h
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ImtCore-Commercial
2#pragma once
3
4
5// Qt includes
6#include <QtGui/QOpenGLFunctions>
7#include <QtCore/QLine>
8#include <QtCore/QTimer>
9#include <QtCore/QVariantAnimation>
10#if QT_VERSION < 0x060000
11#include <QtWidgets/QOpenGLWidget>
12#else
13#include <QtOpenGLWidgets/QOpenGLWidget>
14#endif
15// ACF includes
16#include <istd/istd.h>
17
18// ImtCore includes
19#include <imt3dview/CScene3d.h>
20
21
22namespace imt3dgui
23{
24
25class ISceneEventHandler;
26
27class COpenGLWidget: public QOpenGLWidget, protected QOpenGLFunctions
28{
29 Q_OBJECT
30
31public:
32 enum ProjectionMode
33 {
34 PM_PERSPECTIVE,
35 PM_ORTHO
36 };
37
38 enum ViewDirection
39 {
40 VD_DEFAULT = 0,
41 VD_RIGHT,
42 VD_FRONT,
43 VD_TOP,
44 VD_LEFT,
45 VD_BOTTOM,
46 VD_BACK
47 };
48
49 enum ViewMode
50 {
51 VM_VIEW,
52 VM_SELECTION,
53 };
54
55 enum SelectionMode
56 {
57 SM_POINT,
58 SM_BOX,
59 SM_CIRCLE
60 };
61
62 enum RotationMode
63 {
64 RTM_FREE,
65 RTM_AROUND_X,
66 RTM_AROUND_Y,
67 RTM_AROUND_Z
68 };
69
70 enum RenderHint
71 {
72 RH_ANTIALIASING = 0x1,
73 RH_CULLFACE = 0x2,
74 RH_BLEND = 0x4
75 };
76
77 I_DECLARE_FLAGS(RenderHints, RH_ANTIALIASING, RH_CULLFACE, RH_BLEND);
78
79 COpenGLWidget(QWidget* parentPtr = nullptr);
80 ~COpenGLWidget();
81
82 void SetCamera(imt3dview::IScene3dCamera* cameraPtr);
83 void SetSceneEventHandler(ISceneEventHandler* handlerPtr);
84 void UnsetSceneEventHandler();
85 imt3dview::IScene3d* GetScene();
86 void ZoomIn();
87 void ZoomOut();
88 void ShowGrid(bool show);
89 void ShowAxis(bool show);
90 void ShowRuler(bool show);
91 bool GetRenderHint(RenderHint renderHint) const;
92 void SetRenderHint(RenderHint renderHint, bool on = true);
93 void SetCameraView(ViewDirection viewDirection, bool animated = true);
94 void SetViewMode(ViewMode viewMode);
95 void SetProjectionMode(ProjectionMode projectionMode);
96 void SetSelectionMode(SelectionMode selectionMode);
97 void SetRotationMode(RotationMode rotationMode);
98 void ClearSelection();
99 void AllSelection();
100 void InvertSelection();
101 void DeleteSelection();
102 void SetBackgroundColor(const QColor& backgroundColor);
103
104protected:
105 // reimplemented (QOpenGLWidget)
106 virtual void initializeGL() override;
107 virtual void resizeGL(int w, int h) override;
108 virtual void paintGL() override;
109
110 // reimplemented (QWidget)
111 virtual void mousePressEvent(QMouseEvent *e) override;
112 virtual void mouseReleaseEvent(QMouseEvent* event) override;
113 virtual void mouseMoveEvent(QMouseEvent *e) override;
114 virtual void closeEvent(QCloseEvent* event) override;
115 virtual void wheelEvent(QWheelEvent* event) override;
116 virtual void keyPressEvent(QKeyEvent* e) override;
117
118private Q_SLOTS:
119 void OnZoomIn();
120 void OnZoomOut();
121 void OnInternalTimer();
122 void OnCameraRotationAnimation(const QVariant& value);
123 void OnCameraPositionAnimation(const QVariant& value);
124
125private:
126 void PaintGl();
127 void Paint(QPainter& painter);
128 void PaintSelection(QPainter& painter);
129 void MousePressView(QMouseEvent& e);
130 void MousePressSelection(QMouseEvent& e);
131 void MouseMoveView(QMouseEvent& e);
132 void MouseMoveSelection(QMouseEvent& e);
133 void SetGlFlags();
134 void SetGlUniformValues();
135 QMatrix4x4 GetProjectionMatrix() const;
136 static void GetFovRect(float aspectRatio, float nearPlane, float& width, float& height);
137
138private:
139 QPoint m_mouseClickPosition;
140 QPoint m_prevMousePosition;
141 QRect m_selectionRect;
142 imt3dgui::ISceneEventHandler* m_eventHandlerPtr;
143 imt3dview::CScene3d m_scene;
144 QTimer m_timer;
145 QVariantAnimation m_cameraRotationAnimation;
146 QVariantAnimation m_cameraPositionAnimation;
147 int m_renderHints;
148 ViewMode m_viewMode;
149 SelectionMode m_selectionMode;
150 RotationMode m_rotationMode;
151 QOpenGLShaderProgram* m_programPtr;
152 imt3dview::IScene3dCamera* m_cameraPtr;
153 QColor m_backgroundColor;
154 ProjectionMode m_projectionMode = PM_PERSPECTIVE;
155
156 static const float s_verticalAngle;
157 static const float s_nearPlane;
158 static const float s_farPlane;
159 static const QVector3D s_lightPosition;
160 static const QVector3D s_lightColor;
161};
162
163
164} // namespace imt3dgui
165
166