1 #include "WindowQt.hpp"
3 #include <QApplication>
5 #include <QOpenGLContext>
6 #include <QResizeEvent>
8 #include <QSurfaceFormat>
10 #include <Core/Utils/Log.hpp>
12 using namespace Ra::Core::Utils;
17 WindowQt* WindowQt::s_getProcAddressHelper =
nullptr;
19 QSurfaceFormat defaultFormat() {
20 QSurfaceFormat format;
21 format.setProfile( QSurfaceFormat::CoreProfile );
23 format.setOption( QSurfaceFormat::DebugContext );
28 WindowQt::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 );
52 WindowQt::~WindowQt() {
56 void 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 );
66 void WindowQt::physicalDpiChanged( qreal ) {
70 QSize s { size().width(), size().height() };
71 QResizeEvent patchEvent { s, s };
72 resizeInternal( &patchEvent );
76 QOpenGLContext* WindowQt::context() {
77 return m_context.get();
80 void WindowQt::makeCurrent() {
81 if ( QOpenGLContext::currentContext() != m_context.get() ) {
82 m_context->makeCurrent(
this );
84 m_contextActivationCount = 0;
86 else { ++m_contextActivationCount; }
89 void WindowQt::doneCurrent() {
90 if ( m_contextActivationCount == 0 ) { m_context->doneCurrent(); }
91 else { --m_contextActivationCount; }
94 void WindowQt::resizeEvent( QResizeEvent* event ) {
95 resizeInternal( event );
98 void WindowQt::exposeEvent( QExposeEvent* ) {
102 void WindowQt::initialize() {
103 if ( !m_glInitialized.load() ) {
110 void WindowQt::showEvent( QShowEvent* ) {
114 void 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 );
154 bool WindowQt::initializeGL() {
156 m_glInitialized =
false;
157 return m_glInitialized;
160 void WindowQt::cleanupGL() {
161 if ( m_glInitialized.load() ) {
170 void WindowQt::deinitializeGL() {}
172 void WindowQt::resizeGL( QResizeEvent* ) {}
176 void WindowQt::enterEvent( QEvent* ) {}
178 void WindowQt::leaveEvent( QEvent* ) {}
180 glbinding::ProcAddress WindowQt::getProcAddress(
const char* name ) {
181 if ( !s_getProcAddressHelper || name ==
nullptr ) {
return nullptr; }
183 const auto symbol = std::string( name );
185 #if ( QT_VERSION >= QT_VERSION_CHECK( 5, 4, 0 ) )
186 const auto qtSymbol = QByteArray::fromStdString( symbol );
188 const auto qtSymbol = QByteArray::fromRawData( symbol.c_str(), symbol.size() );
190 return s_getProcAddressHelper->m_context->getProcAddress( qtSymbol );