LibrePCB Developers Documentation
openglview.h
Go to the documentation of this file.
1/*
2 * LibrePCB - Professional EDA for everyone!
3 * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4 * https://librepcb.org/
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef LIBREPCB_EDITOR_OPENGLVIEW_H
21#define LIBREPCB_EDITOR_OPENGLVIEW_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include <QtCore>
27#include <QtOpenGL>
28#include <QtWidgets>
29
30#if QT_VERSION_MAJOR >= 6
31#include <QtOpenGLWidgets>
32#endif
33
34#include <memory>
35
36/*******************************************************************************
37 * Namespace / Forward Declarations
38 ******************************************************************************/
39namespace librepcb {
40namespace editor {
41
42class OpenGlObject;
43class WaitingSpinnerWidget;
44
45/*******************************************************************************
46 * Class OpenGlView
47 ******************************************************************************/
48
52class OpenGlView final : public QOpenGLWidget, protected QOpenGLFunctions {
53 Q_OBJECT
54
55public:
56 // Constructors / Destructor
57 explicit OpenGlView(QWidget* parent = nullptr) noexcept;
58 OpenGlView(const OpenGlView& other) = delete;
59 virtual ~OpenGlView() noexcept;
60
61 // Getters
62 qint64 getIdleTimeMs() const noexcept { return mIdleTimeMs; }
63
64 // General Methods
65 void addObject(std::shared_ptr<OpenGlObject> obj) noexcept;
66 void removeObject(std::shared_ptr<OpenGlObject> obj) noexcept;
67 void setObjects(const QVector<std::shared_ptr<OpenGlObject>>& objs) noexcept;
68 void zoomIn() noexcept;
69 void zoomOut() noexcept;
70 void zoomAll() noexcept;
71 void startSpinning() noexcept;
72 void stopSpinning(QString errorMsg) noexcept;
73
74 // Operator Overloadings
75 OpenGlView& operator=(const OpenGlView& rhs) = delete;
76
77protected:
78 void mousePressEvent(QMouseEvent* e) override;
79 void mouseMoveEvent(QMouseEvent* e) override;
80 void wheelEvent(QWheelEvent* e) override;
81 void smoothTo(qreal fov, const QPointF& center,
82 const QMatrix4x4& transform) noexcept;
83 void initializeGL() override;
84 void resizeGL(int w, int h) override;
85 void paintGL() override;
86
87private:
88 void zoom(const QPointF& center, qreal factor) noexcept;
89 QPointF toNormalizedPos(const QPointF& pos) const noexcept;
90 QPointF toModelPos(const QPointF& pos) const noexcept;
91
92private:
93 QScopedPointer<QVBoxLayout> mLayout;
94 QScopedPointer<QLabel> mErrorLabel;
96 QOpenGLShaderProgram mProgram;
100 QMatrix4x4 mTransform;
106
107 // Transform Animation
109 qreal fov;
110 QPointF center;
111 QMatrix4x4 transform;
112 };
115 QScopedPointer<QVariantAnimation> mAnimation;
116
117 // Content
118 QVector<std::shared_ptr<OpenGlObject>> mObjects;
119
120 // Static Variables
121 static constexpr qreal sCameraPosZ = 5.0;
122 static constexpr qreal sInitialFov = 15.0;
123 static constexpr qreal sZoomStepFactor = 1.3;
124};
125
126/*******************************************************************************
127 * End of File
128 ******************************************************************************/
129
130} // namespace editor
131} // namespace librepcb
132
133#endif
OpenGL 3D viewer widget.
Definition: openglview.h:52
QVector< std::shared_ptr< OpenGlObject > > mObjects
Definition: openglview.h:118
static constexpr qreal sCameraPosZ
Definition: openglview.h:121
void zoomAll() noexcept
Definition: openglview.cpp:137
QOpenGLShaderProgram mProgram
Definition: openglview.h:96
OpenGlView(const OpenGlView &other)=delete
void resizeGL(int w, int h) override
Definition: openglview.cpp:269
void setObjects(const QVector< std::shared_ptr< OpenGlObject > > &objs) noexcept
Definition: openglview.cpp:123
void addObject(std::shared_ptr< OpenGlObject > obj) noexcept
Definition: openglview.cpp:113
qreal mProjectionAspectRatio
Definition: openglview.h:97
void removeObject(std::shared_ptr< OpenGlObject > obj) noexcept
Definition: openglview.cpp:118
void mousePressEvent(QMouseEvent *e) override
Definition: openglview.cpp:160
virtual ~OpenGlView() noexcept
Definition: openglview.cpp:103
void mouseMoveEvent(QMouseEvent *e) override
Definition: openglview.cpp:167
TransformData mAnimationDataDelta
Definition: openglview.h:114
void zoomOut() noexcept
Definition: openglview.cpp:133
QMatrix4x4 mTransform
Definition: openglview.h:100
void paintGL() override
Definition: openglview.cpp:273
OpenGlView(QWidget *parent=nullptr) noexcept
Definition: openglview.cpp:59
void smoothTo(qreal fov, const QPointF &center, const QMatrix4x4 &transform) noexcept
Definition: openglview.cpp:216
void wheelEvent(QWheelEvent *e) override
Definition: openglview.cpp:204
void stopSpinning(QString errorMsg) noexcept
Definition: openglview.cpp:146
bool mInitialized
Definition: openglview.h:95
QMatrix4x4 mMousePressTransform
Definition: openglview.h:102
qint64 mIdleTimeMs
Definition: openglview.h:104
void zoomIn() noexcept
Definition: openglview.cpp:129
QScopedPointer< WaitingSpinnerWidget > mWaitingSpinner
Definition: openglview.h:105
QPoint mMousePressPosition
Definition: openglview.h:101
void startSpinning() noexcept
Definition: openglview.cpp:142
QScopedPointer< QLabel > mErrorLabel
Definition: openglview.h:94
QScopedPointer< QVariantAnimation > mAnimation
Definition: openglview.h:115
static constexpr qreal sZoomStepFactor
Definition: openglview.h:123
void initializeGL() override
Definition: openglview.cpp:235
QPointF mMousePressCenter
Definition: openglview.h:103
QPointF mProjectionCenter
Definition: openglview.h:99
QScopedPointer< QVBoxLayout > mLayout
Definition: openglview.h:93
TransformData mAnimationDataStart
Definition: openglview.h:113
qreal mProjectionFov
Definition: openglview.h:98
qint64 getIdleTimeMs() const noexcept
Definition: openglview.h:62
void zoom(const QPointF &center, qreal factor) noexcept
Definition: openglview.cpp:301
QPointF toModelPos(const QPointF &pos) const noexcept
Definition: openglview.cpp:320
QPointF toNormalizedPos(const QPointF &pos) const noexcept
Definition: openglview.cpp:314
static constexpr qreal sInitialFov
Definition: openglview.h:122
A widget drawing a rotating spinner to indicate an ongoing operation.
Definition: waitingspinnerwidget.h:52
Definition: occmodel.cpp:77
qreal fov
Definition: openglview.h:109
QMatrix4x4 transform
Definition: openglview.h:111
QPointF center
Definition: openglview.h:110