LibrePCB Developers Documentation
Loading...
Searching...
No Matches
slintopenglview.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_SLINTOPENGLVIEW_H
21#define LIBREPCB_EDITOR_SLINTOPENGLVIEW_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "openglobject.h"
27
28#include <QtCore>
29#include <QtOpenGL>
30
31#include <slint.h>
32
33/*******************************************************************************
34 * Namespace / Forward Declarations
35 ******************************************************************************/
36namespace librepcb {
37namespace editor {
38
39/*******************************************************************************
40 * Struct OpenGlProjection
41 ******************************************************************************/
42
44 qreal fov;
45 QPointF center;
46 QMatrix4x4 transform;
47
48 OpenGlProjection() noexcept : fov(15), center(), transform() {}
49 OpenGlProjection(const OpenGlProjection& other) noexcept
50 : fov(other.fov), center(other.center), transform(other.transform) {}
51 OpenGlProjection(qreal fov, const QPointF& center,
52 const QMatrix4x4& transform) noexcept
55 qreal factor) const noexcept {
56 return OpenGlProjection(fov + delta.fov * factor,
57 center + delta.center * factor,
58 transform + delta.transform * factor);
59 }
60 bool operator==(const OpenGlProjection& rhs) const noexcept {
61 return (fov == rhs.fov) && (center == rhs.center) &&
62 (transform == rhs.transform);
63 }
64 bool operator!=(const OpenGlProjection& rhs) const noexcept {
65 return !(*this == rhs);
66 }
67 OpenGlProjection operator-(const OpenGlProjection& rhs) const noexcept {
68 return OpenGlProjection(fov - rhs.fov, center - rhs.center,
69 transform - rhs.transform);
70 }
72 fov = rhs.fov;
73 center = rhs.center;
74 transform = rhs.transform;
75 return *this;
76 }
77};
78
79/*******************************************************************************
80 * Class SlintOpenGlView
81 ******************************************************************************/
82
86class SlintOpenGlView final : public QObject, protected QOpenGLFunctions {
87 Q_OBJECT
88
89public:
90 // Constructors / Destructor
91 explicit SlintOpenGlView(
92 const OpenGlProjection& projection = OpenGlProjection(),
93 QObject* parent = nullptr) noexcept;
94 SlintOpenGlView(const SlintOpenGlView& other) = delete;
95 virtual ~SlintOpenGlView() noexcept;
96
97 // Getters
98 bool isPanning() const noexcept;
99 const QStringList& getOpenGlErrors() const noexcept { return mErrors; }
100 const OpenGlProjection& getProjection() const noexcept { return mProjection; }
101 const QHash<OpenGlObject::Type, float>& getAlpha() const noexcept {
102 return mAlpha;
103 }
104
105 // Setters
106 void setBackgroundColor(QColor color) noexcept;
107
108 // General Methods
109 void addObject(std::shared_ptr<OpenGlObject> obj) noexcept;
110 void removeObject(std::shared_ptr<OpenGlObject> obj) noexcept;
111 void setObjects(const QVector<std::shared_ptr<OpenGlObject>>& objs) noexcept;
112 void setAlpha(const QHash<OpenGlObject::Type, float>& alpha) noexcept;
113 slint::Image render(float width, float height) noexcept;
114 bool pointerEvent(const QPointF& pos,
115 slint::private_api::PointerEvent e) noexcept;
116 bool scrollEvent(const QPointF& pos,
117 slint::private_api::PointerScrollEvent e) noexcept;
118 void zoomIn() noexcept;
119 void zoomOut() noexcept;
120 void zoomAll() noexcept;
121
122 // Operator Overloadings
123 SlintOpenGlView& operator=(const SlintOpenGlView& rhs) = delete;
124
125signals:
128
129private: // Methods
130 void initializeGl() noexcept;
131 void zoom(const QPointF& center, qreal factor) noexcept;
132 void smoothTo(const OpenGlProjection& projection) noexcept;
133 bool applyOpenGlProjection(const OpenGlProjection& projection) noexcept;
134 QPointF toNormalizedPos(const QPointF& pos) const noexcept;
135 QPointF toModelPos(const QPointF& pos) const noexcept;
136
137private: // Data
138 std::unique_ptr<QOffscreenSurface> mSurface;
139 std::unique_ptr<QOpenGLContext> mContext;
140 std::unique_ptr<QOpenGLShaderProgram> mProgram;
141 std::unique_ptr<QOpenGLFramebufferObject> mFbo;
142 QStringList mErrors;
143 QSizeF mViewSize;
145
146 // State
148 QHash<OpenGlObject::Type, float> mAlpha;
152 QSet<slint::private_api::PointerEventButton> mPressedMouseButtons;
153
154 // Transform Animation
157 QScopedPointer<QVariantAnimation> mAnimation;
158
159 // Content
160 QVector<std::shared_ptr<OpenGlObject>> mObjects;
161
162 // Static Variables
163 static constexpr qreal sCameraPosZ = 5.0;
164};
165
166/*******************************************************************************
167 * End of File
168 ******************************************************************************/
169
170} // namespace editor
171} // namespace librepcb
172
173#endif
Represents one 3D object in an OpenGL 3D model.
Definition openglobject.h:43
The SlintOpenGlView class.
Definition slintopenglview.h:86
QVector< std::shared_ptr< OpenGlObject > > mObjects
Definition slintopenglview.h:160
static constexpr qreal sCameraPosZ
Definition slintopenglview.h:163
slint::Image render(float width, float height) noexcept
Definition slintopenglview.cpp:135
void zoomAll() noexcept
Definition slintopenglview.cpp:278
std::unique_ptr< QOpenGLShaderProgram > mProgram
Definition slintopenglview.h:140
bool applyOpenGlProjection(const OpenGlProjection &projection) noexcept
Definition slintopenglview.cpp:364
void setObjects(const QVector< std::shared_ptr< OpenGlObject > > &objs) noexcept
Definition slintopenglview.cpp:121
void addObject(std::shared_ptr< OpenGlObject > obj) noexcept
Definition slintopenglview.cpp:111
void initializeGl() noexcept
Definition slintopenglview.cpp:297
void removeObject(std::shared_ptr< OpenGlObject > obj) noexcept
Definition slintopenglview.cpp:116
virtual ~SlintOpenGlView() noexcept
Definition slintopenglview.cpp:82
QSizeF mViewSize
Definition slintopenglview.h:143
void smoothTo(const OpenGlProjection &projection) noexcept
Definition slintopenglview.cpp:354
QHash< OpenGlObject::Type, float > mAlpha
Definition slintopenglview.h:148
void zoomOut() noexcept
Definition slintopenglview.cpp:273
SlintOpenGlView(const SlintOpenGlView &other)=delete
OpenGlProjection mProjection
Definition slintopenglview.h:147
QPointF mMousePressPosition
Definition slintopenglview.h:149
QMatrix4x4 mMousePressTransform
Definition slintopenglview.h:150
void zoomIn() noexcept
Definition slintopenglview.cpp:268
const OpenGlProjection & getProjection() const noexcept
Definition slintopenglview.h:100
QScopedPointer< QVariantAnimation > mAnimation
Definition slintopenglview.h:157
bool isPanning() const noexcept
Definition slintopenglview.cpp:89
OpenGlProjection mAnimationDataDelta
Definition slintopenglview.h:156
void setBackgroundColor(QColor color) noexcept
Definition slintopenglview.cpp:99
QStringList mErrors
Definition slintopenglview.h:142
QSet< slint::private_api::PointerEventButton > mPressedMouseButtons
Definition slintopenglview.h:152
OpenGlProjection mAnimationDataStart
Definition slintopenglview.h:155
const QStringList & getOpenGlErrors() const noexcept
Definition slintopenglview.h:99
QPointF mMousePressCenter
Definition slintopenglview.h:151
std::unique_ptr< QOpenGLFramebufferObject > mFbo
Definition slintopenglview.h:141
const QHash< OpenGlObject::Type, float > & getAlpha() const noexcept
Definition slintopenglview.h:101
void setAlpha(const QHash< OpenGlObject::Type, float > &alpha) noexcept
Definition slintopenglview.cpp:127
bool scrollEvent(const QPointF &pos, slint::private_api::PointerScrollEvent e) noexcept
Definition slintopenglview.cpp:262
QColor mBackgroundColor
Definition slintopenglview.h:144
bool pointerEvent(const QPointF &pos, slint::private_api::PointerEvent e) noexcept
Definition slintopenglview.cpp:209
std::unique_ptr< QOpenGLContext > mContext
Definition slintopenglview.h:139
std::unique_ptr< QOffscreenSurface > mSurface
Definition slintopenglview.h:138
void zoom(const QPointF &center, qreal factor) noexcept
Definition slintopenglview.cpp:343
QPointF toModelPos(const QPointF &pos) const noexcept
Definition slintopenglview.cpp:381
QPointF toNormalizedPos(const QPointF &pos) const noexcept
Definition slintopenglview.cpp:374
Definition occmodel.cpp:77
Definition uuid.h:186
Definition slintopenglview.h:43
OpenGlProjection & operator=(const OpenGlProjection &rhs) noexcept
Definition slintopenglview.h:71
OpenGlProjection(qreal fov, const QPointF &center, const QMatrix4x4 &transform) noexcept
Definition slintopenglview.h:51
bool operator==(const OpenGlProjection &rhs) const noexcept
Definition slintopenglview.h:60
OpenGlProjection operator-(const OpenGlProjection &rhs) const noexcept
Definition slintopenglview.h:67
OpenGlProjection interpolated(const OpenGlProjection &delta, qreal factor) const noexcept
Definition slintopenglview.h:54
bool operator!=(const OpenGlProjection &rhs) const noexcept
Definition slintopenglview.h:64
OpenGlProjection(const OpenGlProjection &other) noexcept
Definition slintopenglview.h:49
qreal fov
Definition slintopenglview.h:44
OpenGlProjection() noexcept
Definition slintopenglview.h:48
QMatrix4x4 transform
Definition slintopenglview.h:46
QPointF center
Definition slintopenglview.h:45