From 5d4eab23419304c6955c13deaca312f29d2498c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=8B=D1=87=D0=BA=D0=BE=D0=B2=20=D0=90=D0=BD=D0=B4?= =?UTF-8?q?=D1=80=D0=B5=D0=B9?= Date: Mon, 27 May 2019 08:50:09 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/libs@537 a8b55f48-bf90-11e4-a774-851b48703e85 --- qglview/glwidget.cpp | 15 +++++++ qglview/glwidget.h | 26 ++++++++++++ qglview/openglwindow.cpp | 89 ++++++++++++++++++++++++++++++++++++++++ qglview/openglwindow.h | 39 ++++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 qglview/glwidget.cpp create mode 100644 qglview/glwidget.h create mode 100644 qglview/openglwindow.cpp create mode 100644 qglview/openglwindow.h diff --git a/qglview/glwidget.cpp b/qglview/glwidget.cpp new file mode 100644 index 0000000..364af9b --- /dev/null +++ b/qglview/glwidget.cpp @@ -0,0 +1,15 @@ +#include "glwidget.h" +#include "qglview.h" +#include + + +GLWidget::GLWidget(QWidget *parent) : QWidget(parent) +{ + view_ = new QGLView(); + view_->setFlag(Qt::FramelessWindowHint); + container = QWidget::createWindowContainer(view_, this); + lay = new QVBoxLayout(this); + lay->addWidget(container); + lay->setContentsMargins(0, 0, 0, 0); + lay->setSpacing(0); +} diff --git a/qglview/glwidget.h b/qglview/glwidget.h new file mode 100644 index 0000000..a1e6aa9 --- /dev/null +++ b/qglview/glwidget.h @@ -0,0 +1,26 @@ +#ifndef GLWIDGET_H +#define GLWIDGET_H + +#include + + +class QGLView; + +class GLWidget : public QWidget +{ + Q_OBJECT +public: + explicit GLWidget(QWidget *parent = nullptr); + QGLView * view() {return view_;} + +public slots: + +private: + QWidget * container; + QGLView * view_; + QLayout * lay; + +signals: +}; + +#endif // GLWIDGET_H diff --git a/qglview/openglwindow.cpp b/qglview/openglwindow.cpp new file mode 100644 index 0000000..a2813a8 --- /dev/null +++ b/qglview/openglwindow.cpp @@ -0,0 +1,89 @@ +#include "openglwindow.h" +#include +#include +#include +#include + + +OpenGLWindow::OpenGLWindow(QWindow *parent) + : QWindow(parent) + , m_animating(false) + , m_context(0) + , m_device(0) +{ + setFlag(Qt::FramelessWindowHint); + setSurfaceType(QWindow::OpenGLSurface); +} + + +OpenGLWindow::~OpenGLWindow() { + delete m_device; +} + + +void OpenGLWindow::render(QPainter *painter) { +} + + +void OpenGLWindow::initialize() { +} + + +void OpenGLWindow::render() { +// if (!m_device) m_device = new QOpenGLPaintDevice; +// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); +// m_device->setSize(size() * devicePixelRatio()); +// m_device->setDevicePixelRatio(devicePixelRatio()); +// QPainter painter(m_device); +// render(&painter); +} + + +void OpenGLWindow::renderLater() { + requestUpdate(); +} + + +bool OpenGLWindow::event(QEvent *event) { + switch (event->type()) { + case QEvent::UpdateRequest: + renderNow(); + return true; + default: + return QWindow::event(event); + } +} + + +void OpenGLWindow::exposeEvent(QExposeEvent *event) { + if (isExposed()) renderNow(); +} + + +void OpenGLWindow::renderNow() { + if (!isExposed()) + return; + bool needsInitialize = false; + if (!m_context) { + m_context = new QOpenGLContext(this); + m_context->setFormat(requestedFormat()); + m_context->create(); + needsInitialize = true; + } + m_context->makeCurrent(this); + if (needsInitialize) { + initializeOpenGLFunctions(); + initialize(); + } + render(); + m_context->swapBuffers(this); + if (m_animating) + renderLater(); +} + + +void OpenGLWindow::setAnimating(bool animating) { + m_animating = animating; + if (animating) renderLater(); +} + diff --git a/qglview/openglwindow.h b/qglview/openglwindow.h new file mode 100644 index 0000000..19ae683 --- /dev/null +++ b/qglview/openglwindow.h @@ -0,0 +1,39 @@ +#include +#include + +class QPainter; +class QOpenGLContext; +class QOpenGLPaintDevice; + + +class OpenGLWindow : public QWindow, protected QOpenGLFunctions +{ + Q_OBJECT +public: + explicit OpenGLWindow(QWindow *parent = 0); + ~OpenGLWindow(); + + virtual void render(QPainter *painter); + virtual void render(); + + virtual void initialize(); + + void setAnimating(bool animating); + + QOpenGLContext * context() {return m_context;} + +public slots: + void renderLater(); + void renderNow(); + +protected: + bool event(QEvent *event) override; + + void exposeEvent(QExposeEvent *event) override; + +private: + bool m_animating; + QOpenGLContext *m_context; + QOpenGLPaintDevice *m_device; +}; +