Examples: remove OpenGL code paths from painting examples

The extra code for using the OpenGL paint engine is significant enough
to be distracting from what the examples are supposed to show. If we
want to show how to use QPainter on an OpenGL widget, then we can make
dedicated and documented examples for that, in the OpenGL category. And
we have such examples in the Qt OpenGL module anyway.

As is, the examples feel more like manual tests of the OpenGL paint
engine; if we need more coverage there, then we can add it there.

Change-Id: I7b56ea6d08c02cd0a1050ab03991656a0538498d
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
This commit is contained in:
Volker Hilsheimer 2023-10-24 17:41:51 +02:00
parent 333650c596
commit 7c59c5ed13
15 changed files with 24 additions and 491 deletions

View File

@ -4,7 +4,7 @@
qt_internal_add_example(basicdrawing)
qt_internal_add_example(concentriccircles)
qt_internal_add_example(affine)
# qt_internal_add_example(composition) # FIXME: Seems buggy wrt. usesOpenGL function
qt_internal_add_example(composition)
qt_internal_add_example(deform)
qt_internal_add_example(gradients)
qt_internal_add_example(pathstroke)

View File

@ -777,12 +777,6 @@ XFormWidget::XFormWidget(QWidget *parent)
QPushButton *showSourceButton = new QPushButton(mainGroup);
showSourceButton->setText(tr("Show Source"));
#if QT_CONFIG(opengl)
QPushButton *enableOpenGLButton = new QPushButton(mainGroup);
enableOpenGLButton->setText(tr("Use OpenGL"));
enableOpenGLButton->setCheckable(true);
enableOpenGLButton->setChecked(view->usesOpenGL());
#endif
QPushButton *whatsThisButton = new QPushButton(mainGroup);
whatsThisButton->setText(tr("What's This?"));
whatsThisButton->setCheckable(true);
@ -812,9 +806,6 @@ XFormWidget::XFormWidget(QWidget *parent)
mainGroupLayout->addWidget(resetButton);
mainGroupLayout->addWidget(animateButton);
mainGroupLayout->addWidget(showSourceButton);
#if QT_CONFIG(opengl)
mainGroupLayout->addWidget(enableOpenGLButton);
#endif
mainGroupLayout->addWidget(whatsThisButton);
mainGroup->setLayout(mainGroupLayout);
@ -852,9 +843,6 @@ XFormWidget::XFormWidget(QWidget *parent)
connect(view, &XFormView::descriptionEnabledChanged, view->hoverPoints(), &HoverPoints::setDisabled);
connect(view, &XFormView::descriptionEnabledChanged, whatsThisButton, &QPushButton::setChecked);
connect(showSourceButton, &QPushButton::clicked, view, &XFormView::showSource);
#if QT_CONFIG(opengl)
connect(enableOpenGLButton, &QPushButton::clicked, view, &XFormView::enableOpenGL);
#endif
view->loadSourceFile(":res/affine/xform.cpp");
view->loadDescription(":res/affine/xform.html");

View File

@ -10,11 +10,6 @@
#include <QMouseEvent>
#include <qmath.h>
#if QT_CONFIG(opengl)
#include <QOpenGLFunctions>
#include <QOpenGLWindow>
#endif
const int animationInterval = 15; // update every 16 ms = ~60FPS
CompositionWidget::CompositionWidget(QWidget *parent)
@ -94,12 +89,6 @@ CompositionWidget::CompositionWidget(QWidget *parent)
QPushButton *showSourceButton = new QPushButton(mainGroup);
showSourceButton->setText(tr("Show Source"));
#if QT_CONFIG(opengl)
QPushButton *enableOpenGLButton = new QPushButton(mainGroup);
enableOpenGLButton->setText(tr("Use OpenGL"));
enableOpenGLButton->setCheckable(true);
enableOpenGLButton->setChecked(view->usesOpenGL());
#endif
QPushButton *whatsThisButton = new QPushButton(mainGroup);
whatsThisButton->setText(tr("What's This?"));
whatsThisButton->setCheckable(true);
@ -121,9 +110,6 @@ CompositionWidget::CompositionWidget(QWidget *parent)
mainGroupLayout->addWidget(animateButton);
mainGroupLayout->addWidget(whatsThisButton);
mainGroupLayout->addWidget(showSourceButton);
#if QT_CONFIG(opengl)
mainGroupLayout->addWidget(enableOpenGLButton);
#endif
QGridLayout *modesLayout = new QGridLayout(modesGroup);
modesLayout->addWidget(rbClear, 0, 0);
@ -165,9 +151,6 @@ CompositionWidget::CompositionWidget(QWidget *parent)
connect(whatsThisButton, &QAbstractButton::clicked, view, &ArthurFrame::setDescriptionEnabled);
connect(view, &ArthurFrame::descriptionEnabledChanged, whatsThisButton, &QAbstractButton::setChecked);
connect(showSourceButton, &QAbstractButton::clicked, view, &ArthurFrame::showSource);
#if QT_CONFIG(opengl)
connect(enableOpenGLButton, &QAbstractButton::clicked, view, &ArthurFrame::enableOpenGL);
#endif
connect(animateButton, &QAbstractButton::toggled, view, &CompositionRenderer::setAnimationEnabled);
circleColorSlider->setValue(270);
@ -217,10 +200,6 @@ CompositionRenderer::CompositionRenderer(QWidget *parent)
m_circle_pos = QPoint(200, 100);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
#if QT_CONFIG(opengl)
m_pbuffer_size = 1024;
m_base_tex = 0;
#endif
}
CompositionRenderer::~CompositionRenderer()
@ -313,89 +292,25 @@ void CompositionRenderer::drawSource(QPainter &p)
void CompositionRenderer::paint(QPainter *painter)
{
#if QT_CONFIG(opengl)
if (usesOpenGL() && glWindow()->isValid()) {
auto *funcs = QOpenGLContext::currentContext()->functions();
if (m_buffer.size() != size()) {
m_buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
m_base_buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
if (!m_blitter.isCreated())
m_blitter.create();
m_base_buffer.fill(0);
int new_pbuf_size = m_pbuffer_size;
while (size().width() > new_pbuf_size || size().height() > new_pbuf_size)
new_pbuf_size *= 2;
QPainter p(&m_base_buffer);
while (size().width() < new_pbuf_size/2 && size().height() < new_pbuf_size/2)
new_pbuf_size /= 2;
if (!m_fbo || new_pbuf_size != m_pbuffer_size) {
m_fbo.reset(new QFboPaintDevice(QSize(new_pbuf_size, new_pbuf_size), false, false));
m_pbuffer_size = new_pbuf_size;
}
if (size() != m_previous_size) {
m_previous_size = size();
QPainter p(m_fbo.get());
p.setCompositionMode(QPainter::CompositionMode_Source);
p.fillRect(QRect(QPoint(0, 0), size()), Qt::transparent);
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
drawBase(p);
p.end();
if (m_base_tex)
funcs->glDeleteTextures(1, &m_base_tex);
m_base_tex = m_fbo->takeTexture();
}
painter->beginNativePainting();
uint compositingTex;
{
QPainter p(m_fbo.get());
p.beginNativePainting();
m_blitter.bind();
const QRect targetRect(QPoint(0, 0), m_fbo->size());
const QMatrix4x4 target = QOpenGLTextureBlitter::targetTransform(targetRect, QRect(QPoint(0, 0), m_fbo->size()));
m_blitter.blit(m_base_tex, target, QOpenGLTextureBlitter::OriginBottomLeft);
m_blitter.release();
p.endNativePainting();
drawSource(p);
p.end();
compositingTex = m_fbo->texture();
}
painter->endNativePainting();
painter->beginNativePainting();
funcs->glEnable(GL_BLEND);
funcs->glBlendEquation(GL_FUNC_ADD);
funcs->glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
m_blitter.bind();
const QRect targetRect(QPoint(0, 0), m_fbo->size());
const QMatrix4x4 target = QOpenGLTextureBlitter::targetTransform(targetRect, QRect(QPoint(0, 0), size()));
m_blitter.blit(compositingTex, target, QOpenGLTextureBlitter::OriginBottomLeft);
m_blitter.release();
painter->endNativePainting();
} else
#endif
{
// using a QImage
if (m_buffer.size() != size()) {
m_buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
m_base_buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
m_base_buffer.fill(0);
QPainter p(&m_base_buffer);
drawBase(p);
}
memcpy(m_buffer.bits(), m_base_buffer.bits(), m_buffer.sizeInBytes());
{
QPainter p(&m_buffer);
drawSource(p);
}
painter->drawImage(0, 0, m_buffer);
drawBase(p);
}
memcpy(m_buffer.bits(), m_base_buffer.bits(), m_buffer.sizeInBytes());
{
QPainter p(&m_buffer);
drawSource(p);
}
painter->drawImage(0, 0, m_buffer);
}
void CompositionRenderer::mousePressEvent(QMouseEvent *e)
@ -443,12 +358,6 @@ void CompositionRenderer::setCirclePos(const QPointF &pos)
const QRect oldRect = rectangle_around(m_circle_pos).toAlignedRect();
m_circle_pos = pos;
const QRect newRect = rectangle_around(m_circle_pos).toAlignedRect();
#if QT_CONFIG(opengl)
if (usesOpenGL()) {
update();
return;
}
#endif
update(oldRect | newRect);
}

View File

@ -6,11 +6,6 @@
#include "arthurwidgets.h"
#if QT_CONFIG(opengl)
#include "fbopaintdevice.h"
#include <QOpenGLTextureBlitter>
#endif
#include <QPainter>
#include <QEvent>
@ -143,14 +138,6 @@ private:
ObjectType m_current_object;
bool m_animation_enabled;
int m_animationTimer;
#if QT_CONFIG(opengl)
std::unique_ptr<QFboPaintDevice> m_fbo;
int m_pbuffer_size; // width==height==size of pbuffer
uint m_base_tex;
QSize m_previous_size;
QOpenGLTextureBlitter m_blitter;
#endif
};
#endif // COMPOSITION_H

View File

@ -61,13 +61,6 @@ void PathDeformControls::layoutForDesktop()
QPushButton *showSourceButton = new QPushButton(mainGroup);
showSourceButton->setText(tr("Show Source"));
#if QT_CONFIG(opengl)
QPushButton *enableOpenGLButton = new QPushButton(mainGroup);
enableOpenGLButton->setText(tr("Use OpenGL"));
enableOpenGLButton->setCheckable(true);
enableOpenGLButton->setChecked(m_renderer->usesOpenGL());
#endif
QPushButton *whatsThisButton = new QPushButton(mainGroup);
whatsThisButton->setText(tr("What's This?"));
whatsThisButton->setCheckable(true);
@ -82,9 +75,6 @@ void PathDeformControls::layoutForDesktop()
mainGroupLayout->addWidget(textGroup);
mainGroupLayout->addWidget(animateButton);
mainGroupLayout->addStretch(1);
#if QT_CONFIG(opengl)
mainGroupLayout->addWidget(enableOpenGLButton);
#endif
mainGroupLayout->addWidget(showSourceButton);
mainGroupLayout->addWidget(whatsThisButton);
@ -108,9 +98,6 @@ void PathDeformControls::layoutForDesktop()
connect(deformSlider, &QAbstractSlider::valueChanged, m_renderer, &PathDeformRenderer::setIntensity);
connect(fontSizeSlider, &QAbstractSlider::valueChanged, m_renderer, &PathDeformRenderer::setFontSize);
connect(animateButton, &QAbstractButton::clicked, m_renderer, &PathDeformRenderer::setAnimated);
#if QT_CONFIG(opengl)
connect(enableOpenGLButton, &QAbstractButton::clicked, m_renderer, &ArthurFrame::enableOpenGL);
#endif
connect(textInput, &QLineEdit::textChanged, m_renderer, &PathDeformRenderer::setText);
connect(m_renderer, &ArthurFrame::descriptionEnabledChanged,
@ -151,13 +138,6 @@ void PathDeformControls::layoutForSmallScreen()
QPushButton *animateButton = new QPushButton(tr("Animated"), mainGroup);
animateButton->setCheckable(true);
#if QT_CONFIG(opengl)
QPushButton *enableOpenGLButton = new QPushButton(mainGroup);
enableOpenGLButton->setText(tr("Use OpenGL"));
enableOpenGLButton->setCheckable(mainGroup);
enableOpenGLButton->setChecked(m_renderer->usesOpenGL());
#endif
QPushButton *quitButton = new QPushButton(tr("Quit"), mainGroup);
QPushButton *okButton = new QPushButton(tr("OK"), mainGroup);
@ -171,9 +151,6 @@ void PathDeformControls::layoutForSmallScreen()
mainGroupLayout->addWidget(fontSizeLabel, 2, 0, Qt::AlignRight);
mainGroupLayout->addWidget(fontSizeSlider, 2, 1);
mainGroupLayout->addWidget(animateButton, 3,0, 1,2);
#if QT_CONFIG(opengl)
mainGroupLayout->addWidget(enableOpenGLButton, 4,0, 1,2);
#endif
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(mainGroup);
@ -187,10 +164,6 @@ void PathDeformControls::layoutForSmallScreen()
connect(deformSlider, &QAbstractSlider::valueChanged, m_renderer, &PathDeformRenderer::setIntensity);
connect(fontSizeSlider, &QAbstractSlider::valueChanged, m_renderer, &PathDeformRenderer::setFontSize);
connect(animateButton, &QAbstractButton::clicked, m_renderer, &PathDeformRenderer::setAnimated);
#if QT_CONFIG(opengl)
connect(enableOpenGLButton, &QAbstractButton::clicked, m_renderer, &ArthurFrame::enableOpenGL);
#endif
animateButton->animateClick();
deformSlider->setValue(80);
@ -380,7 +353,6 @@ void PathDeformRenderer::setAnimated(bool animated)
void PathDeformRenderer::timerEvent(QTimerEvent *e)
{
if (e->timerId() == m_repaintTimer.timerId()) {
if (QLineF(QPointF(0,0), m_direction).length() > 1)
@ -414,22 +386,9 @@ void PathDeformRenderer::timerEvent(QTimerEvent *e)
m_pos.setY(height() - m_radius);
}
#if QT_CONFIG(opengl)
if (usesOpenGL()) {
update();
} else
#endif
{
QRect rectAfter = circle_bounds(m_pos, m_radius, m_fontSize);
update(rectAfter | rectBefore);
}
QRect rectAfter = circle_bounds(m_pos, m_radius, m_fontSize);
update(rectAfter | rectBefore);
}
// else if (e->timerId() == m_fpsTimer.timerId()) {
// printf("fps: %d\n", m_fpsCounter);
// emit frameRate(m_fpsCounter);
// m_fpsCounter = 0;
// }
}
void PathDeformRenderer::mousePressEvent(QMouseEvent *e)
@ -478,15 +437,8 @@ void PathDeformRenderer::mouseMoveEvent(QMouseEvent *e)
m_direction = (m_direction + dir) / 2;
}
m_pos = e->position().toPoint() + m_offset;
#if QT_CONFIG(opengl)
if (usesOpenGL()) {
update();
} else
#endif
{
QRect rectAfter = circle_bounds(m_pos, m_radius, m_fontSize);
update(rectBefore | rectAfter);
}
QRect rectAfter = circle_bounds(m_pos, m_radius, m_fontSize);
update(rectBefore | rectAfter);
}
}
@ -570,27 +522,13 @@ void PathDeformRenderer::setRadius(int radius)
qreal max = qMax(m_radius, (qreal)radius);
m_radius = radius;
generateLensPixmap();
if (!m_animated || m_radius < max) {
#if QT_CONFIG(opengl)
if (usesOpenGL()){
update();
return;
}
#endif
if (!m_animated || m_radius < max)
update(circle_bounds(m_pos, max, m_fontSize));
}
}
void PathDeformRenderer::setIntensity(int intensity)
{
m_intensity = intensity;
if (!m_animated) {
#if QT_CONFIG(opengl)
if (usesOpenGL()) {
update();
return;
}
#endif
if (!m_animated)
update(circle_bounds(m_pos, m_radius, m_fontSize));
}
}

View File

@ -273,12 +273,6 @@ GradientWidget::GradientWidget(QWidget *parent)
QPushButton *showSourceButton = new QPushButton(mainGroup);
showSourceButton->setText(tr("Show Source"));
#if QT_CONFIG(opengl)
QPushButton *enableOpenGLButton = new QPushButton(mainGroup);
enableOpenGLButton->setText(tr("Use OpenGL"));
enableOpenGLButton->setCheckable(true);
enableOpenGLButton->setChecked(m_renderer->usesOpenGL());
#endif
QPushButton *whatsThisButton = new QPushButton(mainGroup);
whatsThisButton->setText(tr("What's This?"));
whatsThisButton->setCheckable(true);
@ -292,9 +286,6 @@ GradientWidget::GradientWidget(QWidget *parent)
mainGroupLayout->addWidget(defaultsGroup);
mainGroupLayout->addStretch(1);
mainGroupLayout->addWidget(showSourceButton);
#if QT_CONFIG(opengl)
mainGroupLayout->addWidget(enableOpenGLButton);
#endif
mainGroupLayout->addWidget(whatsThisButton);
QVBoxLayout *editorGroupLayout = new QVBoxLayout(editorGroup);
@ -370,11 +361,6 @@ GradientWidget::GradientWidget(QWidget *parent)
connect(showSourceButton, &QPushButton::clicked,
m_renderer, &GradientRenderer::showSource);
#if QT_CONFIG(opengl)
connect(enableOpenGLButton, QOverload<bool>::of(&QPushButton::clicked),
m_renderer, &ArthurFrame::enableOpenGL);
#endif
connect(whatsThisButton, QOverload<bool>::of(&QPushButton::clicked),
m_renderer, &ArthurFrame::setDescriptionEnabled);
connect(whatsThisButton, QOverload<bool>::of(&QPushButton::clicked),

View File

@ -177,12 +177,6 @@ void PathStrokeControls::layoutForDesktop()
QPushButton *showSourceButton = new QPushButton(mainGroup);
showSourceButton->setText(tr("Show Source"));
#if QT_CONFIG(opengl)
QPushButton *enableOpenGLButton = new QPushButton(mainGroup);
enableOpenGLButton->setText(tr("Use OpenGL"));
enableOpenGLButton->setCheckable(true);
enableOpenGLButton->setChecked(m_renderer->usesOpenGL());
#endif
QPushButton *whatsThisButton = new QPushButton(mainGroup);
whatsThisButton->setText(tr("What's This?"));
whatsThisButton->setCheckable(true);
@ -206,9 +200,6 @@ void PathStrokeControls::layoutForDesktop()
mainGroupLayout->addWidget(animated);
mainGroupLayout->addStretch(1);
mainGroupLayout->addWidget(showSourceButton);
#if QT_CONFIG(opengl)
mainGroupLayout->addWidget(enableOpenGLButton);
#endif
mainGroupLayout->addWidget(whatsThisButton);
@ -221,10 +212,6 @@ void PathStrokeControls::layoutForDesktop()
connect(showSourceButton, &QAbstractButton::clicked,
m_renderer, &ArthurFrame::showSource);
#if QT_CONFIG(opengl)
connect(enableOpenGLButton, &QAbstractButton::clicked,
m_renderer, &ArthurFrame::enableOpenGL);
#endif
connect(whatsThisButton, &QAbstractButton::clicked,
m_renderer, &ArthurFrame::setDescriptionEnabled);
connect(m_renderer, &ArthurFrame::descriptionEnabledChanged,
@ -259,13 +246,6 @@ void PathStrokeControls::layoutForSmallScreens()
penWidth->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
penWidth->setRange(0, 500);
#if QT_CONFIG(opengl)
QPushButton *enableOpenGLButton = new QPushButton(this);
enableOpenGLButton->setText(tr("Use OpenGL"));
enableOpenGLButton->setCheckable(true);
enableOpenGLButton->setChecked(m_renderer->usesOpenGL());
#endif
// Layouts:
QHBoxLayout *penWidthLayout = new QHBoxLayout;
penWidthLayout->addWidget(penWidthLabel, 0, Qt::AlignRight);
@ -274,9 +254,6 @@ void PathStrokeControls::layoutForSmallScreens()
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addWidget(m_capGroup);
leftLayout->addWidget(m_joinGroup);
#if QT_CONFIG(opengl)
leftLayout->addWidget(enableOpenGLButton);
#endif
leftLayout->addLayout(penWidthLayout);
QVBoxLayout *rightLayout = new QVBoxLayout;
@ -297,10 +274,6 @@ void PathStrokeControls::layoutForSmallScreens()
mainLayout->addWidget(quitBtn, 2, 1, Qt::AlignHCenter | Qt::AlignTop);
mainLayout->addWidget(okBtn, 2, 2, Qt::AlignHCenter | Qt::AlignTop);
#if QT_CONFIG(opengl)
connect(enableOpenGLButton, &QAbstractButton::clicked, m_renderer, &ArthurFrame::enableOpenGL);
#endif
connect(penWidth, &QAbstractSlider::valueChanged, m_renderer, &PathStrokeRenderer::setPenWidth);
connect(quitBtn, &QAbstractButton::clicked, this, &PathStrokeControls::emitQuitSignal);
connect(okBtn, &QAbstractButton::clicked, this, &PathStrokeControls::emitOkSignal);

View File

@ -15,15 +15,3 @@ set_target_properties(painting_shared PROPERTIES UNITY_BUILD OFF)
target_link_libraries(painting_shared PUBLIC Qt6::Widgets)
target_include_directories(painting_shared PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
## Scopes:
#####################################################################
if (TARGET Qt6::OpenGL OR QT_FEATURE_opengles2)
target_compile_definitions(painting_shared PRIVATE QT_OPENGL_SUPPORT)
target_link_libraries(painting_shared PUBLIC
Qt6::OpenGL
)
qt6_wrap_cpp(moc_files_gl fbopaintdevice.h) # no automoc for OBJECT libs
target_sources(painting_shared PRIVATE fbopaintdevice.cpp fbopaintdevice.h ${moc_files_gl})
endif()

View File

@ -14,11 +14,6 @@
#include <QBoxLayout>
#include <QRegularExpression>
#include <QOffscreenSurface>
#include <QOpenGLContext>
#if QT_CONFIG(opengl)
#include <QtOpenGL/QOpenGLPaintDevice>
#include <QtOpenGL/QOpenGLWindow>
#endif
extern QPixmap cached(const QString &img);
@ -34,61 +29,13 @@ ArthurFrame::ArthurFrame(QWidget *parent)
pt.end();
}
#if QT_CONFIG(opengl)
void ArthurFrame::enableOpenGL(bool use_opengl)
{
if (m_use_opengl == use_opengl)
return;
m_use_opengl = use_opengl;
if (!m_glWindow && use_opengl) {
createGlWindow();
QApplication::postEvent(this, new QResizeEvent(size(), size()));
}
if (use_opengl) {
m_glWidget->show();
} else {
if (m_glWidget)
m_glWidget->hide();
}
update();
}
void ArthurFrame::createGlWindow()
{
Q_ASSERT(m_use_opengl);
m_glWindow = new QOpenGLWindow();
QSurfaceFormat f = QSurfaceFormat::defaultFormat();
f.setSamples(4);
f.setAlphaBufferSize(8);
f.setStencilBufferSize(8);
m_glWindow->setFormat(f);
m_glWindow->setFlags(Qt::WindowTransparentForInput);
m_glWindow->resize(width(), height());
m_glWidget = QWidget::createWindowContainer(m_glWindow, this);
// create() must be called after createWindowContainer() otherwise
// an incorrect offsetting of the position will occur.
m_glWindow->create();
}
#endif
void ArthurFrame::paintEvent(QPaintEvent *e)
{
static QImage *static_image = nullptr;
QPainter painter;
if (preferImage()
#if QT_CONFIG(opengl)
&& !m_use_opengl
#endif
) {
if (preferImage()) {
if (!static_image || static_image->size() != size()) {
delete static_image;
static_image = new QImage(size(), QImage::Format_RGB32);
@ -103,18 +50,7 @@ void ArthurFrame::paintEvent(QPaintEvent *e)
painter.fillRect(0, height() - o, o, o, bg);
painter.fillRect(width() - o, height() - o, o, o, bg);
} else {
#if QT_CONFIG(opengl)
if (m_use_opengl && m_glWindow->isValid()) {
m_glWindow->makeCurrent();
painter.begin(m_glWindow);
painter.fillRect(QRectF(0, 0, m_glWindow->width(), m_glWindow->height()), palette().color(backgroundRole()));
} else {
painter.begin(this);
}
#else
painter.begin(this);
#endif
}
painter.setClipRect(e->rect());
@ -158,27 +94,15 @@ void ArthurFrame::paintEvent(QPaintEvent *e)
painter.setBrush(Qt::NoBrush);
painter.drawPath(clipPath);
if (preferImage()
#if QT_CONFIG(opengl)
&& !m_use_opengl
#endif
) {
if (preferImage()) {
painter.end();
painter.begin(this);
painter.drawImage(e->rect(), *static_image, e->rect());
}
#if QT_CONFIG(opengl)
if (m_use_opengl)
m_glWindow->update();
#endif
}
void ArthurFrame::resizeEvent(QResizeEvent *e)
{
#if QT_CONFIG(opengl)
if (m_glWidget)
m_glWidget->setGeometry(0, 0, e->size().width(), e->size().height());
#endif
QWidget::resizeEvent(e);
}

View File

@ -9,7 +9,6 @@
#include <QPushButton>
#include <QGroupBox>
QT_FORWARD_DECLARE_CLASS(QOpenGLWindow)
QT_FORWARD_DECLARE_CLASS(QTextDocument)
QT_FORWARD_DECLARE_CLASS(QTextEdit)
QT_FORWARD_DECLARE_CLASS(QVBoxLayout)
@ -30,20 +29,12 @@ public:
void loadSourceFile(const QString &fileName);
bool preferImage() const { return m_preferImage; }
#if QT_CONFIG(opengl)
QOpenGLWindow *glWindow() const { return m_glWindow; }
#endif
public slots:
void setPreferImage(bool pi) { m_preferImage = pi; }
void setDescriptionEnabled(bool enabled);
void showSource();
#if QT_CONFIG(opengl)
void enableOpenGL(bool use_opengl);
bool usesOpenGL() { return m_use_opengl; }
#endif
signals:
void descriptionEnabledChanged(bool);
@ -51,12 +42,6 @@ protected:
void paintEvent(QPaintEvent *) override;
void resizeEvent(QResizeEvent *) override;
#if QT_CONFIG(opengl)
virtual void createGlWindow();
QOpenGLWindow *m_glWindow = nullptr;
QWidget *m_glWidget = nullptr;
bool m_use_opengl = false;
#endif
QPixmap m_tile;
bool m_showDoc = false;

View File

@ -1,75 +0,0 @@
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "fbopaintdevice.h"
#include <QOffscreenSurface>
#include <QOpenGLFunctions>
QFboPaintDevice::QFboPaintDevice(const QSize &size, bool flipped, bool clearOnInit,
QOpenGLFramebufferObject::Attachment attachment)
: QOpenGLPaintDevice(size)
{
QOpenGLFramebufferObjectFormat format;
format.setAttachment(attachment);
format.setSamples(4);
m_framebufferObject = new QOpenGLFramebufferObject(size, format);
QOffscreenSurface *surface = new QOffscreenSurface();
surface->create();
m_surface = surface;
setPaintFlipped(flipped);
if (clearOnInit) {
m_framebufferObject->bind();
context()->functions()->glClearColor(0, 0, 0, 0);
context()->functions()->glClear(GL_COLOR_BUFFER_BIT);
}
m_resolvedFbo = new QOpenGLFramebufferObject(m_framebufferObject->size(), m_framebufferObject->attachment());
}
QFboPaintDevice::~QFboPaintDevice()
{
delete m_framebufferObject;
delete m_resolvedFbo;
delete m_surface;
}
void QFboPaintDevice::ensureActiveTarget()
{
if (QOpenGLContext::currentContext() != context())
context()->makeCurrent(m_surface);
m_framebufferObject->bind();
}
GLuint QFboPaintDevice::texture()
{
m_resolvedFbo->bind(); // to get the backing texture recreated if it was taken (in takeTexture) previously
QOpenGLFramebufferObject::blitFramebuffer(m_resolvedFbo, m_framebufferObject);
return m_resolvedFbo->texture();
}
GLuint QFboPaintDevice::takeTexture()
{
m_resolvedFbo->bind(); // to get the backing texture recreated if it was taken (in takeTexture) previously
// We have multisamples so we can't just forward takeTexture(), have to resolve first.
QOpenGLFramebufferObject::blitFramebuffer(m_resolvedFbo, m_framebufferObject);
return m_resolvedFbo->takeTexture();
}
QImage QFboPaintDevice::toImage() const
{
QOpenGLContext *currentContext = QOpenGLContext::currentContext();
QSurface *currentSurface = currentContext ? currentContext->surface() : nullptr;
context()->makeCurrent(m_surface);
QImage image = m_framebufferObject->toImage(!paintFlipped());
if (currentContext)
currentContext->makeCurrent(currentSurface);
else
context()->doneCurrent();
return image;
}

View File

@ -1,46 +0,0 @@
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef QFBOPAINTDEVICE_H
#define QFBOPAINTDEVICE_H
#ifndef QT_NO_OPENGL
#include <QImage>
#include <QOpenGLFramebufferObject>
#include <QOpenGLPaintDevice>
#include <QSurface>
class QFboPaintDevice : public QOpenGLPaintDevice {
public:
QFboPaintDevice(const QSize &size, bool flipped = false, bool clearOnInit = true,
QOpenGLFramebufferObject::Attachment = QOpenGLFramebufferObject::CombinedDepthStencil);
~QFboPaintDevice();
// QOpenGLPaintDevice:
void ensureActiveTarget() override;
bool isValid() const { return m_framebufferObject->isValid(); }
GLuint handle() const { return m_framebufferObject->handle(); }
GLuint texture();
GLuint takeTexture();
QImage toImage() const;
bool bind() { return m_framebufferObject->bind(); }
bool release() { return m_framebufferObject->release(); }
QSize size() const { return m_framebufferObject->size(); }
QOpenGLFramebufferObject* framebufferObject() { return m_framebufferObject; }
const QOpenGLFramebufferObject* framebufferObject() const { return m_framebufferObject; }
static bool isSupported() { return QOpenGLFramebufferObject::hasOpenGLFramebufferObjects(); }
private:
QOpenGLFramebufferObject *m_framebufferObject;
QOpenGLFramebufferObject *m_resolvedFbo;
QSurface *m_surface;
};
#endif // QT_NO_OPENGL
#endif // QFBOPAINTDEVICE_H

View File

@ -6,10 +6,6 @@
#include <algorithm>
#if QT_CONFIG(opengl)
#include <QtOpenGL/QOpenGLWindow>
#endif
HoverPoints::HoverPoints(QWidget *widget, PointShape shape)
: QObject(widget),
m_widget(widget),
@ -230,17 +226,7 @@ bool HoverPoints::eventFilter(QObject *object, QEvent *event)
void HoverPoints::paintPoints()
{
QPainter p;
#if QT_CONFIG(opengl)
ArthurFrame *af = qobject_cast<ArthurFrame *>(m_widget);
if (af && af->usesOpenGL() && af->glWindow()->isValid()) {
af->glWindow()->makeCurrent();
p.begin(af->glWindow());
} else {
p.begin(m_widget);
}
#else
p.begin(m_widget);
#endif
p.setRenderHint(QPainter::Antialiasing);

View File

@ -1,11 +1,5 @@
INCLUDEPATH += $$PWD
qtConfig(opengl) {
QT += opengl
SOURCES += $$PWD/fbopaintdevice.cpp
HEADERS += $$PWD/fbopaintdevice.h
}
SOURCES += \
$$PWD/arthurstyle.cpp\
$$PWD/arthurwidgets.cpp \

View File

@ -9,8 +9,4 @@ if(NOT TARGET Qt::Widgets)
find_package(Qt6 REQUIRED COMPONENTS Widgets)
endif()
if(NOT TARGET Qt::OpenGL)
find_package(Qt6 OPTIONAL_COMPONENTS OpenGL)
endif()
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}" painting_shared)