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 OpenGlProjection operator-(const OpenGlProjection& rhs) const noexcept {
65 return OpenGlProjection(fov - rhs.fov, center - rhs.center,
66 transform - rhs.transform);
67 }
69 fov = rhs.fov;
70 center = rhs.center;
71 transform = rhs.transform;
72 return *this;
73 }
74};
75
76/*******************************************************************************
77 * Class SlintOpenGlView
78 ******************************************************************************/
79
83class SlintOpenGlView final : public QObject, protected QOpenGLFunctions {
84 Q_OBJECT
85
86public:
87 // Constructors / Destructor
88 explicit SlintOpenGlView(
89 const OpenGlProjection& projection = OpenGlProjection(),
90 QObject* parent = nullptr) noexcept;
91 SlintOpenGlView(const SlintOpenGlView& other) = delete;
92 virtual ~SlintOpenGlView() noexcept;
93
94 // Getters
95 bool isPanning() const noexcept;
96 const QStringList& getOpenGlErrors() const noexcept { return mErrors; }
97 const OpenGlProjection& getProjection() const noexcept { return mProjection; }
98 const QHash<OpenGlObject::Type, float>& getAlpha() const noexcept {
99 return mAlpha;
100 }
101 static QColor getBackgroundColor() noexcept { return QColor(230, 242, 255); }
102
103 // General Methods
104 void addObject(std::shared_ptr<OpenGlObject> obj) noexcept;
105 void removeObject(std::shared_ptr<OpenGlObject> obj) noexcept;
106 void setObjects(const QVector<std::shared_ptr<OpenGlObject>>& objs) noexcept;
107 void setAlpha(const QHash<OpenGlObject::Type, float>& alpha) noexcept;
108 slint::Image render(float width, float height) noexcept;
109 bool pointerEvent(const QPointF& pos,
110 slint::private_api::PointerEvent e) noexcept;
111 bool scrollEvent(const QPointF& pos,
112 slint::private_api::PointerScrollEvent e) noexcept;
113 void zoomIn() noexcept;
114 void zoomOut() noexcept;
115 void zoomAll() noexcept;
116
117 // Operator Overloadings
118 SlintOpenGlView& operator=(const SlintOpenGlView& rhs) = delete;
119
120signals:
123
124private: // Methods
125 void initializeGl() noexcept;
126 void zoom(const QPointF& center, qreal factor) noexcept;
127 void smoothTo(const OpenGlProjection& projection) noexcept;
128 bool applyOpenGlProjection(const OpenGlProjection& projection) noexcept;
129 QPointF toNormalizedPos(const QPointF& pos) const noexcept;
130 QPointF toModelPos(const QPointF& pos) const noexcept;
131
132private: // Data
133 std::unique_ptr<QOffscreenSurface> mSurface;
134 std::unique_ptr<QOpenGLContext> mContext;
135 std::unique_ptr<QOpenGLShaderProgram> mProgram;
136 std::unique_ptr<QOpenGLFramebufferObject> mFbo;
137 QStringList mErrors;
138 QSizeF mViewSize;
139
140 // State
142 QHash<OpenGlObject::Type, float> mAlpha;
146 QSet<slint::private_api::PointerEventButton> mPressedMouseButtons;
147
148 // Transform Animation
151 QScopedPointer<QVariantAnimation> mAnimation;
152
153 // Content
154 QVector<std::shared_ptr<OpenGlObject>> mObjects;
155
156 // Static Variables
157 static constexpr qreal sCameraPosZ = 5.0;
158};
159
160/*******************************************************************************
161 * End of File
162 ******************************************************************************/
163
164} // namespace editor
165} // namespace librepcb
166
167#endif
Represents one 3D object in an OpenGL 3D model.
Definition openglobject.h:43
The SlintOpenGlView class.
Definition slintopenglview.h:83
QVector< std::shared_ptr< OpenGlObject > > mObjects
Definition slintopenglview.h:154
static constexpr qreal sCameraPosZ
Definition slintopenglview.h:157
slint::Image render(float width, float height) noexcept
Definition slintopenglview.cpp:121
void zoomAll() noexcept
Definition slintopenglview.cpp:260
std::unique_ptr< QOpenGLShaderProgram > mProgram
Definition slintopenglview.h:135
bool applyOpenGlProjection(const OpenGlProjection &projection) noexcept
Definition slintopenglview.cpp:339
void setObjects(const QVector< std::shared_ptr< OpenGlObject > > &objs) noexcept
Definition slintopenglview.cpp:107
void addObject(std::shared_ptr< OpenGlObject > obj) noexcept
Definition slintopenglview.cpp:97
void initializeGl() noexcept
Definition slintopenglview.cpp:268
void removeObject(std::shared_ptr< OpenGlObject > obj) noexcept
Definition slintopenglview.cpp:102
virtual ~SlintOpenGlView() noexcept
Definition slintopenglview.cpp:80
QSizeF mViewSize
Definition slintopenglview.h:138
void smoothTo(const OpenGlProjection &projection) noexcept
Definition slintopenglview.cpp:329
QHash< OpenGlObject::Type, float > mAlpha
Definition slintopenglview.h:142
void zoomOut() noexcept
Definition slintopenglview.cpp:255
SlintOpenGlView(const SlintOpenGlView &other)=delete
OpenGlProjection mProjection
Definition slintopenglview.h:141
static QColor getBackgroundColor() noexcept
Definition slintopenglview.h:101
QPointF mMousePressPosition
Definition slintopenglview.h:143
QMatrix4x4 mMousePressTransform
Definition slintopenglview.h:144
void zoomIn() noexcept
Definition slintopenglview.cpp:250
const OpenGlProjection & getProjection() const noexcept
Definition slintopenglview.h:97
QScopedPointer< QVariantAnimation > mAnimation
Definition slintopenglview.h:151
bool isPanning() const noexcept
Definition slintopenglview.cpp:87
OpenGlProjection mAnimationDataDelta
Definition slintopenglview.h:150
QStringList mErrors
Definition slintopenglview.h:137
QSet< slint::private_api::PointerEventButton > mPressedMouseButtons
Definition slintopenglview.h:146
OpenGlProjection mAnimationDataStart
Definition slintopenglview.h:149
const QStringList & getOpenGlErrors() const noexcept
Definition slintopenglview.h:96
QPointF mMousePressCenter
Definition slintopenglview.h:145
std::unique_ptr< QOpenGLFramebufferObject > mFbo
Definition slintopenglview.h:136
const QHash< OpenGlObject::Type, float > & getAlpha() const noexcept
Definition slintopenglview.h:98
void setAlpha(const QHash< OpenGlObject::Type, float > &alpha) noexcept
Definition slintopenglview.cpp:113
bool scrollEvent(const QPointF &pos, slint::private_api::PointerScrollEvent e) noexcept
Definition slintopenglview.cpp:244
bool pointerEvent(const QPointF &pos, slint::private_api::PointerEvent e) noexcept
Definition slintopenglview.cpp:191
std::unique_ptr< QOpenGLContext > mContext
Definition slintopenglview.h:134
std::unique_ptr< QOffscreenSurface > mSurface
Definition slintopenglview.h:133
void zoom(const QPointF &center, qreal factor) noexcept
Definition slintopenglview.cpp:318
QPointF toModelPos(const QPointF &pos) const noexcept
Definition slintopenglview.cpp:356
QPointF toNormalizedPos(const QPointF &pos) const noexcept
Definition slintopenglview.cpp:349
Definition occmodel.cpp:77
Definition uuid.h:186
Definition slintopenglview.h:43
OpenGlProjection & operator=(const OpenGlProjection &rhs) noexcept
Definition slintopenglview.h:68
OpenGlProjection(qreal fov, const QPointF &center, const QMatrix4x4 &transform) noexcept
Definition slintopenglview.h:51
OpenGlProjection operator-(const OpenGlProjection &rhs) const noexcept
Definition slintopenglview.h:64
OpenGlProjection interpolated(const OpenGlProjection &delta, qreal factor) const noexcept
Definition slintopenglview.h:54
bool operator!=(const OpenGlProjection &rhs) const noexcept
Definition slintopenglview.h:60
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