5#include <QOpenGLContext>
8#include <QSurfaceFormat>
10#include <Core/Utils/Log.hpp>
12using namespace Ra::Core::Utils;
17WindowQt* WindowQt::s_getProcAddressHelper =
nullptr;
19QSurfaceFormat defaultFormat() {
20 QSurfaceFormat format;
21 format.setProfile( QSurfaceFormat::CoreProfile );
23 format.setOption( QSurfaceFormat::DebugContext );
28WindowQt::WindowQt( QScreen* screen ) :
29 QWindow( screen ), m_context( nullptr ), m_updatePending( false ), m_glInitialized( false ) {
31 setSurfaceType( QWindow::OpenGLSurface );
32 if ( !s_getProcAddressHelper ) { s_getProcAddressHelper =
this; }
37 m_context = std::make_unique<QOpenGLContext>(
this );
38 m_context->setFormat( QSurfaceFormat::defaultFormat() );
40 if ( !m_context->create() ) {
41 LOG( logINFO ) <<
"Could not create OpenGL context.";
45 m_screenObserver = connect(
46 this->screen(), &QScreen::physicalDotsPerInchChanged,
this, &WindowQt::physicalDpiChanged );
47 connect(
this, &QWindow::screenChanged,
this, &WindowQt::screenChanged );
52WindowQt::~WindowQt() {
56void WindowQt::screenChanged() {
57 disconnect( m_screenObserver );
58 m_screenObserver = connect(
59 this->screen(), &QScreen::physicalDotsPerInchChanged,
this, &WindowQt::physicalDpiChanged );
61 QSize s { size().width(), size().height() };
62 QResizeEvent patchEvent { s, s };
63 resizeInternal( &patchEvent );
66void WindowQt::physicalDpiChanged( qreal ) {
70 QSize s { size().width(), size().height() };
71 QResizeEvent patchEvent { s, s };
72 resizeInternal( &patchEvent );
76QOpenGLContext* WindowQt::context() {
77 return m_context.get();
80void WindowQt::makeCurrent() {
81 if ( QOpenGLContext::currentContext() != m_context.get() ) {
82 m_context->makeCurrent(
this );
84 m_contextActivationCount = 0;
86 else { ++m_contextActivationCount; }
89void WindowQt::doneCurrent() {
90 if ( m_contextActivationCount == 0 ) { m_context->doneCurrent(); }
91 else { --m_contextActivationCount; }
94void WindowQt::resizeEvent( QResizeEvent* event ) {
95 resizeInternal( event );
98void WindowQt::exposeEvent( QExposeEvent* ) {
102void WindowQt::initialize() {
103 if ( !m_glInitialized.load() ) {
110void WindowQt::showEvent( QShowEvent* ) {
114void WindowQt::resizeInternal( QResizeEvent* event ) {
117 if ( event->size().width() < minimumSize().width() ||
118 event->size().height() < minimumSize().height() ) {
119 QSize size {
std::max( event->size().width(), minimumSize().width() ),
120 std::max( event->size().height(), minimumSize().height() ) };
121 QResizeEvent* patchEvent =
new QResizeEvent( size, event->oldSize() );
123 QWindow::resize( size );
154bool WindowQt::initializeGL() {
156 m_glInitialized =
false;
157 return m_glInitialized;
160void WindowQt::cleanupGL() {
161 if ( m_glInitialized.load() ) {
170void WindowQt::deinitializeGL() {}
172void WindowQt::resizeGL( QResizeEvent* ) {}
176void WindowQt::enterEvent( QEvent* ) {}
178void WindowQt::leaveEvent( QEvent* ) {}
180glbinding::ProcAddress WindowQt::getProcAddress(
const char* name ) {
181 if ( !s_getProcAddressHelper || name ==
nullptr ) {
return nullptr; }
185 const auto qtSymbol = QByteArray::fromStdString( symbol );
187 return s_getProcAddressHelper->m_context->getProcAddress( qtSymbol );
Base class for OpenGL widgets, compatble with Qt and globjects/glbindings.
hepler function to manage enum as underlying types in VariableSet