Radium Engine  1.5.0
WindowQt.hpp
1 // This file si picked and slightly adapted from
2 // globjects/source/examples/qtexample/WindowQt.h
3 // so that update in globjects is easy
4 // Radium Viewer derive this class
5 
6 #pragma once
7 #include <Gui/RaGui.hpp>
8 
9 #include <QScopedPointer>
10 #include <QWindow>
11 
12 #include <glbinding/ProcAddress.h>
13 
14 #include <atomic>
15 
16 class QSurfaceFormat;
17 class QOpenGLContext;
18 
19 namespace Ra {
20 namespace Gui {
21 
27 class RA_GUI_API WindowQt : public QWindow
28 {
29  Q_OBJECT
30  private:
31  class ScopedGLContext;
32 
33  public:
34  explicit WindowQt( QScreen* screen );
35  virtual ~WindowQt();
36 
37  void resizeEvent( QResizeEvent* event ) override;
38  void showEvent( QShowEvent* event ) override;
39  void exposeEvent( QExposeEvent* event ) override;
40  // bool event( QEvent* event ) override;
41 
42  virtual void enterEvent( QEvent* event );
43  virtual void leaveEvent( QEvent* event );
44 
53  void makeCurrent();
54 
62  void doneCurrent();
63 
82  inline ScopedGLContext activateScopedContext();
83 
85  QOpenGLContext* context();
86 
87  // note when updating from globjets
88  // updateGL done by base app rendering loop
89 
90  bool isOpenGlInitialized() const { return m_glInitialized.load(); }
91 
92  signals:
94  void dpiChanged();
95 
96  public slots:
98  void cleanupGL();
99  void screenChanged();
100 
101  private slots:
102  void physicalDpiChanged( qreal dpi );
103 
104  protected:
105  // OpenglContext used with this widget
106  std::unique_ptr<QOpenGLContext> m_context;
107 
108  bool m_updatePending;
109  std::atomic_bool m_glInitialized;
110 
111  void initialize();
112  void resizeInternal( QResizeEvent* event );
113 
114  //
115  // OpenGL related methods
116  // Not inherited, defined here in the same way QOpenGLWidget define them.
117  //
119  virtual bool initializeGL();
121  virtual void deinitializeGL();
123  virtual void resizeGL( QResizeEvent* event );
124 
125  // note when updating from globjets
126  // paintGL done by base app rendering loop
127 
128  protected:
129  static glbinding::ProcAddress getProcAddress( const char* name );
130 
131  private:
134  using QWindow::create;
135 
136  static WindowQt* s_getProcAddressHelper;
137  int m_contextActivationCount { 0 };
138 
139  QMetaObject::Connection m_screenObserver;
140 };
141 
142 class WindowQt::ScopedGLContext
143 {
144  public:
145  explicit ScopedGLContext( WindowQt* window ) : m_window( window ) { window->makeCurrent(); }
146  ~ScopedGLContext() { m_window->doneCurrent(); }
147  ScopedGLContext( const ScopedGLContext& ) = delete;
148  ScopedGLContext& operator=( ScopedGLContext const& ) = delete;
149 
150  private:
151  WindowQt* m_window;
152 };
153 
154 inline WindowQt::ScopedGLContext WindowQt::activateScopedContext() {
155  return ScopedGLContext { this };
156 }
157 
158 } // namespace Gui
159 } // namespace Ra
Base class for OpenGL widgets, compatble with Qt and globjects/glbindings.
Definition: WindowQt.hpp:28
void dpiChanged()
Emitted when physical device changes. Useful for tracking devicePixelRatio() change.
ScopedGLContext activateScopedContext()
Definition: WindowQt.hpp:154
void makeCurrent()
Definition: WindowQt.cpp:80
Definition: Cage.cpp:3