|
| 1 | +// |
| 2 | +// Created by ksuprynowicz on 25.01.25. |
| 3 | +// |
| 4 | + |
| 5 | +// |
| 6 | +// |
| 7 | +// Created by Bradley Austin Davis on 2015/12/03 |
| 8 | +// Derived from interface/src/GLCanvas.cpp created by Stephen Birarda on 8/14/13. |
| 9 | +// Copyright 2013-2015 High Fidelity, Inc. |
| 10 | +// |
| 11 | +// Distributed under the Apache License, Version 2.0. |
| 12 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| 13 | +// |
| 14 | + |
| 15 | +#include "VKWidget.h" |
| 16 | + |
| 17 | +#include "Config.h" |
| 18 | + |
| 19 | +#include <mutex> |
| 20 | + |
| 21 | +#include <QtGlobal> |
| 22 | +#include <QtCore/QMimeData> |
| 23 | +#include <QtCore/QUrl> |
| 24 | +#include <QtCore/QCoreApplication> |
| 25 | + |
| 26 | +#include <QtGui/QKeyEvent> |
| 27 | +#include <QtGui/QPaintEngine> |
| 28 | +#include <QtGui/QWindow> |
| 29 | + |
| 30 | +#include "Context.h" |
| 31 | + |
| 32 | +class GLPaintEngine : public QPaintEngine { |
| 33 | + bool begin(QPaintDevice *pdev) override { return true; } |
| 34 | + bool end() override { return true; } |
| 35 | + void updateState(const QPaintEngineState &state) override { } |
| 36 | + void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr) override { } |
| 37 | + Type type() const override { return OpenGL2; } |
| 38 | +}; |
| 39 | + |
| 40 | +VKWidget::VKWidget() { |
| 41 | + setAttribute(Qt::WA_AcceptTouchEvents); |
| 42 | + setAttribute(Qt::WA_NativeWindow); |
| 43 | + setAttribute(Qt::WA_PaintOnScreen); |
| 44 | + setAttribute(Qt::WA_NoSystemBackground); |
| 45 | + setAttribute(Qt::WA_InputMethodEnabled); |
| 46 | + setAutoFillBackground(false); |
| 47 | + grabGesture(Qt::PinchGesture); |
| 48 | + setAcceptDrops(true); |
| 49 | + _paintEngine = new GLPaintEngine(); |
| 50 | +} |
| 51 | + |
| 52 | +VKWidget::~VKWidget() { |
| 53 | + delete _paintEngine; |
| 54 | + _paintEngine = nullptr; |
| 55 | +} |
| 56 | + |
| 57 | +int VKWidget::getDeviceWidth() const { |
| 58 | + return width() * (windowHandle() ? (float)windowHandle()->devicePixelRatio() : 1.0f); |
| 59 | +} |
| 60 | + |
| 61 | +int VKWidget::getDeviceHeight() const { |
| 62 | + return height() * (windowHandle() ? (float)windowHandle()->devicePixelRatio() : 1.0f); |
| 63 | +} |
| 64 | + |
| 65 | +void VKWidget::createContext(QOpenGLContext* shareContext) { |
| 66 | + _context = new gl::Context(); |
| 67 | + _context->setWindow(windowHandle()); |
| 68 | + _context->create(shareContext); |
| 69 | + _context->makeCurrent(); |
| 70 | + _context->clear(); |
| 71 | + _context->doneCurrent(); |
| 72 | +} |
| 73 | + |
| 74 | +void VKWidget::swapBuffers() { |
| 75 | + _context->swapBuffers(); |
| 76 | +} |
| 77 | + |
| 78 | +bool VKWidget::makeCurrent() { |
| 79 | + vks::Context::makeCurrent(_context->qglContext(), windowHandle()); |
| 80 | + return _context->makeCurrent(); |
| 81 | +} |
| 82 | + |
| 83 | +QOpenGLContext* VKWidget::qglContext() { |
| 84 | + return _context->qglContext(); |
| 85 | +} |
| 86 | + |
| 87 | +void VKWidget::doneCurrent() { |
| 88 | + _context->doneCurrent(); |
| 89 | +} |
| 90 | + |
| 91 | +QVariant VKWidget::inputMethodQuery(Qt::InputMethodQuery query) const { |
| 92 | + // TODO: for now we just use top left corner for an IME popup location, but in the future its position could be calculated |
| 93 | + // for a given entry field. |
| 94 | + if (query == Qt::ImCursorRectangle) { |
| 95 | + int x = 50; |
| 96 | + int y = 50; |
| 97 | + return QRect(x, y, 10, 10); |
| 98 | + } |
| 99 | + return QWidget::inputMethodQuery(query); |
| 100 | +} |
| 101 | + |
| 102 | +bool VKWidget::event(QEvent* event) { |
| 103 | + switch (event->type()) { |
| 104 | + case QEvent::MouseMove: |
| 105 | + case QEvent::MouseButtonPress: |
| 106 | + case QEvent::MouseButtonRelease: |
| 107 | + case QEvent::MouseButtonDblClick: |
| 108 | + case QEvent::KeyPress: |
| 109 | + case QEvent::KeyRelease: |
| 110 | + case QEvent::FocusIn: |
| 111 | + case QEvent::FocusOut: |
| 112 | + case QEvent::Resize: |
| 113 | + case QEvent::TouchBegin: |
| 114 | + case QEvent::TouchEnd: |
| 115 | + case QEvent::TouchUpdate: |
| 116 | + case QEvent::Gesture: |
| 117 | + case QEvent::Wheel: |
| 118 | + case QEvent::DragEnter: |
| 119 | + case QEvent::Drop: |
| 120 | + if (QCoreApplication::sendEvent(QCoreApplication::instance(), event)) { |
| 121 | + return true; |
| 122 | + } |
| 123 | + break; |
| 124 | + case QEvent::InputMethod: |
| 125 | + if (QCoreApplication::sendEvent(QCoreApplication::instance(), event)) { |
| 126 | + return true; |
| 127 | + } |
| 128 | + break; |
| 129 | + case QEvent::InputMethodQuery: |
| 130 | + if (QCoreApplication::sendEvent(QCoreApplication::instance(), event)) { |
| 131 | + return true; |
| 132 | + } |
| 133 | + break; |
| 134 | + |
| 135 | + default: |
| 136 | + break; |
| 137 | + } |
| 138 | + return QWidget::event(event); |
| 139 | +} |
| 140 | + |
| 141 | +bool VKWidget::nativeEvent(const QByteArray &eventType, void *message, long *result) { |
| 142 | +#ifdef Q_OS_WIN32 |
| 143 | + MSG* win32message = static_cast<MSG*>(message); |
| 144 | + switch (win32message->message) { |
| 145 | + case WM_ERASEBKGND: |
| 146 | + *result = 1L; |
| 147 | + return TRUE; |
| 148 | + |
| 149 | + default: |
| 150 | + break; |
| 151 | + } |
| 152 | +#endif |
| 153 | + return QWidget::nativeEvent(eventType, message, result); |
| 154 | +} |
| 155 | + |
| 156 | +QPaintEngine* VKWidget::paintEngine() const { |
| 157 | + return _paintEngine; |
| 158 | +} |
0 commit comments