Skip to content

Commit

Permalink
Add VKWidget files
Browse files Browse the repository at this point in the history
  • Loading branch information
ksuprynowicz committed Jan 25, 2025
1 parent 1744878 commit cff6ae8
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 0 deletions.
158 changes: 158 additions & 0 deletions libraries/vk/src/vk/VKWidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//
// Created by ksuprynowicz on 25.01.25.
//

//
//
// Created by Bradley Austin Davis on 2015/12/03
// Derived from interface/src/GLCanvas.cpp created by Stephen Birarda on 8/14/13.
// Copyright 2013-2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//

#include "VKWidget.h"

#include "Config.h"

#include <mutex>

#include <QtGlobal>
#include <QtCore/QMimeData>
#include <QtCore/QUrl>
#include <QtCore/QCoreApplication>

#include <QtGui/QKeyEvent>
#include <QtGui/QPaintEngine>
#include <QtGui/QWindow>

#include "Context.h"

class GLPaintEngine : public QPaintEngine {
bool begin(QPaintDevice *pdev) override { return true; }
bool end() override { return true; }
void updateState(const QPaintEngineState &state) override { }
void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr) override { }
Type type() const override { return OpenGL2; }
};

VKWidget::VKWidget() {
setAttribute(Qt::WA_AcceptTouchEvents);
setAttribute(Qt::WA_NativeWindow);
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_NoSystemBackground);
setAttribute(Qt::WA_InputMethodEnabled);
setAutoFillBackground(false);
grabGesture(Qt::PinchGesture);
setAcceptDrops(true);
_paintEngine = new GLPaintEngine();
}

VKWidget::~VKWidget() {
delete _paintEngine;
_paintEngine = nullptr;
}

int VKWidget::getDeviceWidth() const {
return width() * (windowHandle() ? (float)windowHandle()->devicePixelRatio() : 1.0f);
}

int VKWidget::getDeviceHeight() const {
return height() * (windowHandle() ? (float)windowHandle()->devicePixelRatio() : 1.0f);
}

void VKWidget::createContext(QOpenGLContext* shareContext) {
_context = new gl::Context();
_context->setWindow(windowHandle());
_context->create(shareContext);
_context->makeCurrent();
_context->clear();
_context->doneCurrent();
}

void VKWidget::swapBuffers() {
_context->swapBuffers();
}

bool VKWidget::makeCurrent() {
vks::Context::makeCurrent(_context->qglContext(), windowHandle());
return _context->makeCurrent();
}

QOpenGLContext* VKWidget::qglContext() {
return _context->qglContext();
}

void VKWidget::doneCurrent() {
_context->doneCurrent();
}

QVariant VKWidget::inputMethodQuery(Qt::InputMethodQuery query) const {
// TODO: for now we just use top left corner for an IME popup location, but in the future its position could be calculated
// for a given entry field.
if (query == Qt::ImCursorRectangle) {
int x = 50;
int y = 50;
return QRect(x, y, 10, 10);
}
return QWidget::inputMethodQuery(query);
}

bool VKWidget::event(QEvent* event) {
switch (event->type()) {
case QEvent::MouseMove:
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
case QEvent::MouseButtonDblClick:
case QEvent::KeyPress:
case QEvent::KeyRelease:
case QEvent::FocusIn:
case QEvent::FocusOut:
case QEvent::Resize:
case QEvent::TouchBegin:
case QEvent::TouchEnd:
case QEvent::TouchUpdate:
case QEvent::Gesture:
case QEvent::Wheel:
case QEvent::DragEnter:
case QEvent::Drop:
if (QCoreApplication::sendEvent(QCoreApplication::instance(), event)) {
return true;
}
break;
case QEvent::InputMethod:
if (QCoreApplication::sendEvent(QCoreApplication::instance(), event)) {
return true;
}
break;
case QEvent::InputMethodQuery:
if (QCoreApplication::sendEvent(QCoreApplication::instance(), event)) {
return true;
}
break;

default:
break;
}
return QWidget::event(event);
}

bool VKWidget::nativeEvent(const QByteArray &eventType, void *message, long *result) {
#ifdef Q_OS_WIN32
MSG* win32message = static_cast<MSG*>(message);
switch (win32message->message) {
case WM_ERASEBKGND:
*result = 1L;
return TRUE;

default:
break;
}
#endif
return QWidget::nativeEvent(eventType, message, result);
}

QPaintEngine* VKWidget::paintEngine() const {
return _paintEngine;
}
48 changes: 48 additions & 0 deletions libraries/vk/src/vk/VKWidget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Created by ksuprynowicz on 25.01.25.
//

//
//
// Created by Bradley Austin Davis on 2015/12/03
// Derived from interface/src/GLCanvas.cpp created by Stephen Birarda on 8/14/13.
// Copyright 2013-2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//


#pragma once

#include <QtWidgets/QWidget>

#include "Context.h"

/// customized canvas that simply forwards requests/events to the singleton application
class VKWidget : public QWidget {
Q_OBJECT

public:
VKWidget();
~VKWidget();
int getDeviceWidth() const;
int getDeviceHeight() const;
QSize getDeviceSize() const { return QSize(getDeviceWidth(), getDeviceHeight()); }
QPaintEngine* paintEngine() const override;
void createContext(QOpenGLContext* shareContext = nullptr);
bool makeCurrent();
void doneCurrent();
void swapBuffers();
vks::Context* context() { return _context; }
virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;

protected:
virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result) override;
virtual bool event(QEvent* event) override;
vks::Context* _context { nullptr };

private:
QPaintEngine* _paintEngine { nullptr };
bool _vsyncSupported { false };
};

0 comments on commit cff6ae8

Please sign in to comment.