diff options
Diffstat (limited to 'examples/widgets/painting')
30 files changed, 586 insertions, 458 deletions
diff --git a/examples/widgets/painting/affine/affine.pro b/examples/widgets/painting/affine/affine.pro index 0ad869183c..0af18b82ca 100644 --- a/examples/widgets/painting/affine/affine.pro +++ b/examples/widgets/painting/affine/affine.pro @@ -1,10 +1,6 @@ SOURCES += main.cpp xform.cpp HEADERS += xform.h -qtHaveModule(opengl) { - DEFINES += QT_OPENGL_SUPPORT - QT += opengl -} QT += widgets SHARED_FOLDER = ../shared diff --git a/examples/widgets/painting/affine/main.cpp b/examples/widgets/painting/affine/main.cpp index a820c784f0..6ce8efe482 100644 --- a/examples/widgets/painting/affine/main.cpp +++ b/examples/widgets/painting/affine/main.cpp @@ -58,12 +58,12 @@ int main(int argc, char **argv) QApplication app(argc, argv); - XFormWidget xformWidget(0); - QStyle *arthurStyle = new ArthurStyle(); + XFormWidget xformWidget(nullptr); + QStyle *arthurStyle = new ArthurStyle; xformWidget.setStyle(arthurStyle); - QList<QWidget *> widgets = xformWidget.findChildren<QWidget *>(); - foreach (QWidget *w, widgets) { + const QList<QWidget *> widgets = xformWidget.findChildren<QWidget *>(); + for (QWidget *w : widgets) { w->setStyle(arthurStyle); w->setAttribute(Qt::WA_AcceptTouchEvents); } diff --git a/examples/widgets/painting/affine/xform.cpp b/examples/widgets/painting/affine/xform.cpp index 20f6eb3c6d..482e0f3268 100644 --- a/examples/widgets/painting/affine/xform.cpp +++ b/examples/widgets/painting/affine/xform.cpp @@ -160,10 +160,7 @@ void XFormView::updateCtrlPoints(const QPolygonF &points) ctrlPoints = points; QLineF line(ctrlPoints.at(0), ctrlPoints.at(1)); - m_rotation = line.angle(QLineF(0, 0, 1, 0)); - if (line.dy() < 0) - m_rotation = 360 - m_rotation; - + m_rotation = 360 - QLineF(0, 0, 1, 0).angleTo(line); if (trans.isNull()) emit rotationChanged(int(m_rotation*10)); } @@ -830,13 +827,11 @@ XFormWidget::XFormWidget(QWidget *parent) QPushButton *showSourceButton = new QPushButton(mainGroup); showSourceButton->setText(tr("Show Source")); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) QPushButton *enableOpenGLButton = new QPushButton(mainGroup); enableOpenGLButton->setText(tr("Use OpenGL")); enableOpenGLButton->setCheckable(true); enableOpenGLButton->setChecked(view->usesOpenGL()); - if (!QGLFormat::hasOpenGL()) - enableOpenGLButton->hide(); #endif QPushButton *whatsThisButton = new QPushButton(mainGroup); whatsThisButton->setText(tr("What's This?")); @@ -871,7 +866,7 @@ XFormWidget::XFormWidget(QWidget *parent) mainGroupLayout->addWidget(resetButton); mainGroupLayout->addWidget(animateButton); mainGroupLayout->addWidget(showSourceButton); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) mainGroupLayout->addWidget(enableOpenGLButton); #endif mainGroupLayout->addWidget(whatsThisButton); @@ -897,7 +892,7 @@ 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); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) connect(enableOpenGLButton, &QPushButton::clicked, view, &XFormView::enableOpenGL); #endif view->loadSourceFile(":res/affine/xform.cpp"); diff --git a/examples/widgets/painting/basicdrawing/window.cpp b/examples/widgets/painting/basicdrawing/window.cpp index c80237e914..65f6971d13 100644 --- a/examples/widgets/painting/basicdrawing/window.cpp +++ b/examples/widgets/painting/basicdrawing/window.cpp @@ -157,22 +157,22 @@ Window::Window() //! [7] //! [8] - connect(shapeComboBox, SIGNAL(activated(int)), - this, SLOT(shapeChanged())); - connect(penWidthSpinBox, SIGNAL(valueChanged(int)), - this, SLOT(penChanged())); - connect(penStyleComboBox, SIGNAL(activated(int)), - this, SLOT(penChanged())); - connect(penCapComboBox, SIGNAL(activated(int)), - this, SLOT(penChanged())); - connect(penJoinComboBox, SIGNAL(activated(int)), - this, SLOT(penChanged())); - connect(brushStyleComboBox, SIGNAL(activated(int)), - this, SLOT(brushChanged())); - connect(antialiasingCheckBox, SIGNAL(toggled(bool)), - renderArea, SLOT(setAntialiased(bool))); - connect(transformationsCheckBox, SIGNAL(toggled(bool)), - renderArea, SLOT(setTransformed(bool))); + connect(shapeComboBox, QOverload<int>::of(&QComboBox::activated), + this, &Window::shapeChanged); + connect(penWidthSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), + this, &Window::penChanged); + connect(penStyleComboBox, QOverload<int>::of(&QComboBox::activated), + this, &Window::penChanged); + connect(penCapComboBox, QOverload<int>::of(&QComboBox::activated), + this, &Window::penChanged); + connect(penJoinComboBox, QOverload<int>::of(&QComboBox::activated), + this, &Window::penChanged); + connect(brushStyleComboBox, QOverload<int>::of(&QComboBox::activated), + this, &Window::brushChanged); + connect(antialiasingCheckBox, &QAbstractButton::toggled, + renderArea, &RenderArea::setAntialiased); + connect(transformationsCheckBox, &QAbstractButton::toggled, + renderArea, &RenderArea::setTransformed); //! [8] //! [9] diff --git a/examples/widgets/painting/composition/composition.cpp b/examples/widgets/painting/composition/composition.cpp index 0b57d3c7d3..9bd71735a0 100644 --- a/examples/widgets/painting/composition/composition.cpp +++ b/examples/widgets/painting/composition/composition.cpp @@ -57,6 +57,11 @@ #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) @@ -71,79 +76,76 @@ CompositionWidget::CompositionWidget(QWidget *parent) modesGroup->setTitle(tr("Mode")); rbClear = new QRadioButton(tr("Clear"), modesGroup); - connect(rbClear, SIGNAL(clicked()), view, SLOT(setClearMode())); + connect(rbClear, &QAbstractButton::clicked, view, &CompositionRenderer::setClearMode); rbSource = new QRadioButton(tr("Source"), modesGroup); - connect(rbSource, SIGNAL(clicked()), view, SLOT(setSourceMode())); + connect(rbSource, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceMode); rbDest = new QRadioButton(tr("Destination"), modesGroup); - connect(rbDest, SIGNAL(clicked()), view, SLOT(setDestMode())); + connect(rbDest, &QAbstractButton::clicked, view, &CompositionRenderer::setDestMode); rbSourceOver = new QRadioButton(tr("Source Over"), modesGroup); - connect(rbSourceOver, SIGNAL(clicked()), view, SLOT(setSourceOverMode())); + connect(rbSourceOver, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceOverMode); rbDestOver = new QRadioButton(tr("Destination Over"), modesGroup); - connect(rbDestOver, SIGNAL(clicked()), view, SLOT(setDestOverMode())); + connect(rbDestOver, &QAbstractButton::clicked, view, &CompositionRenderer::setDestOverMode); rbSourceIn = new QRadioButton(tr("Source In"), modesGroup); - connect(rbSourceIn, SIGNAL(clicked()), view, SLOT(setSourceInMode())); + connect(rbSourceIn, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceInMode); rbDestIn = new QRadioButton(tr("Dest In"), modesGroup); - connect(rbDestIn, SIGNAL(clicked()), view, SLOT(setDestInMode())); + connect(rbDestIn, &QAbstractButton::clicked, view, &CompositionRenderer::setDestInMode); rbSourceOut = new QRadioButton(tr("Source Out"), modesGroup); - connect(rbSourceOut, SIGNAL(clicked()), view, SLOT(setSourceOutMode())); + connect(rbSourceOut, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceOutMode); rbDestOut = new QRadioButton(tr("Dest Out"), modesGroup); - connect(rbDestOut, SIGNAL(clicked()), view, SLOT(setDestOutMode())); + connect(rbDestOut, &QAbstractButton::clicked, view, &CompositionRenderer::setDestOutMode); rbSourceAtop = new QRadioButton(tr("Source Atop"), modesGroup); - connect(rbSourceAtop, SIGNAL(clicked()), view, SLOT(setSourceAtopMode())); + connect(rbSourceAtop, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceAtopMode); rbDestAtop = new QRadioButton(tr("Dest Atop"), modesGroup); - connect(rbDestAtop, SIGNAL(clicked()), view, SLOT(setDestAtopMode())); + connect(rbDestAtop, &QAbstractButton::clicked, view, &CompositionRenderer::setDestAtopMode); rbXor = new QRadioButton(tr("Xor"), modesGroup); - connect(rbXor, SIGNAL(clicked()), view, SLOT(setXorMode())); + connect(rbXor, &QAbstractButton::clicked, view, &CompositionRenderer::setXorMode); rbPlus = new QRadioButton(tr("Plus"), modesGroup); - connect(rbPlus, SIGNAL(clicked()), view, SLOT(setPlusMode())); + connect(rbPlus, &QAbstractButton::clicked, view, &CompositionRenderer::setPlusMode); rbMultiply = new QRadioButton(tr("Multiply"), modesGroup); - connect(rbMultiply, SIGNAL(clicked()), view, SLOT(setMultiplyMode())); + connect(rbMultiply, &QAbstractButton::clicked, view, &CompositionRenderer::setMultiplyMode); rbScreen = new QRadioButton(tr("Screen"), modesGroup); - connect(rbScreen, SIGNAL(clicked()), view, SLOT(setScreenMode())); + connect(rbScreen, &QAbstractButton::clicked, view, &CompositionRenderer::setScreenMode); rbOverlay = new QRadioButton(tr("Overlay"), modesGroup); - connect(rbOverlay, SIGNAL(clicked()), view, SLOT(setOverlayMode())); + connect(rbOverlay, &QAbstractButton::clicked, view, &CompositionRenderer::setOverlayMode); rbDarken = new QRadioButton(tr("Darken"), modesGroup); - connect(rbDarken, SIGNAL(clicked()), view, SLOT(setDarkenMode())); + connect(rbDarken, &QAbstractButton::clicked, view, &CompositionRenderer::setDarkenMode); rbLighten = new QRadioButton(tr("Lighten"), modesGroup); - connect(rbLighten, SIGNAL(clicked()), view, SLOT(setLightenMode())); + connect(rbLighten, &QAbstractButton::clicked, view, &CompositionRenderer::setLightenMode); rbColorDodge = new QRadioButton(tr("Color Dodge"), modesGroup); - connect(rbColorDodge, SIGNAL(clicked()), view, SLOT(setColorDodgeMode())); + connect(rbColorDodge, &QAbstractButton::clicked, view, &CompositionRenderer::setColorDodgeMode); rbColorBurn = new QRadioButton(tr("Color Burn"), modesGroup); - connect(rbColorBurn, SIGNAL(clicked()), view, SLOT(setColorBurnMode())); + connect(rbColorBurn, &QAbstractButton::clicked, view, &CompositionRenderer::setColorBurnMode); rbHardLight = new QRadioButton(tr("Hard Light"), modesGroup); - connect(rbHardLight, SIGNAL(clicked()), view, SLOT(setHardLightMode())); + connect(rbHardLight, &QAbstractButton::clicked, view, &CompositionRenderer::setHardLightMode); rbSoftLight = new QRadioButton(tr("Soft Light"), modesGroup); - connect(rbSoftLight, SIGNAL(clicked()), view, SLOT(setSoftLightMode())); + connect(rbSoftLight, &QAbstractButton::clicked, view, &CompositionRenderer::setSoftLightMode); rbDifference = new QRadioButton(tr("Difference"), modesGroup); - connect(rbDifference, SIGNAL(clicked()), view, SLOT(setDifferenceMode())); + connect(rbDifference, &QAbstractButton::clicked, view, &CompositionRenderer::setDifferenceMode); rbExclusion = new QRadioButton(tr("Exclusion"), modesGroup); - connect(rbExclusion, SIGNAL(clicked()), view, SLOT(setExclusionMode())); + connect(rbExclusion, &QAbstractButton::clicked, view, &CompositionRenderer::setExclusionMode); QGroupBox *circleColorGroup = new QGroupBox(mainGroup); circleColorGroup->setTitle(tr("Circle color")); QSlider *circleColorSlider = new QSlider(Qt::Horizontal, circleColorGroup); circleColorSlider->setRange(0, 359); circleColorSlider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); - connect(circleColorSlider, SIGNAL(valueChanged(int)), view, SLOT(setCircleColor(int))); + connect(circleColorSlider, &QAbstractSlider::valueChanged, view, &CompositionRenderer::setCircleColor); QGroupBox *circleAlphaGroup = new QGroupBox(mainGroup); circleAlphaGroup->setTitle(tr("Circle alpha")); QSlider *circleAlphaSlider = new QSlider(Qt::Horizontal, circleAlphaGroup); circleAlphaSlider->setRange(0, 255); circleAlphaSlider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); - connect(circleAlphaSlider, SIGNAL(valueChanged(int)), view, SLOT(setCircleAlpha(int))); + connect(circleAlphaSlider, &QAbstractSlider::valueChanged, view, &CompositionRenderer::setCircleAlpha); QPushButton *showSourceButton = new QPushButton(mainGroup); showSourceButton->setText(tr("Show Source")); -#if defined(USE_OPENGL) && !defined(QT_OPENGL_ES) +#if QT_CONFIG(opengl) QPushButton *enableOpenGLButton = new QPushButton(mainGroup); enableOpenGLButton->setText(tr("Use OpenGL")); enableOpenGLButton->setCheckable(true); enableOpenGLButton->setChecked(view->usesOpenGL()); - - if (!QGLFormat::hasOpenGL() || !QGLPixelBuffer::hasOpenGLPbuffers()) - enableOpenGLButton->hide(); #endif QPushButton *whatsThisButton = new QPushButton(mainGroup); whatsThisButton->setText(tr("What's This?")); @@ -166,7 +168,7 @@ CompositionWidget::CompositionWidget(QWidget *parent) mainGroupLayout->addWidget(animateButton); mainGroupLayout->addWidget(whatsThisButton); mainGroupLayout->addWidget(showSourceButton); -#if defined(USE_OPENGL) && !defined(QT_OPENGL_ES) +#if QT_CONFIG(opengl) mainGroupLayout->addWidget(enableOpenGLButton); #endif @@ -207,13 +209,13 @@ CompositionWidget::CompositionWidget(QWidget *parent) view->loadDescription(":res/composition/composition.html"); view->loadSourceFile(":res/composition/composition.cpp"); - connect(whatsThisButton, SIGNAL(clicked(bool)), view, SLOT(setDescriptionEnabled(bool))); - connect(view, SIGNAL(descriptionEnabledChanged(bool)), whatsThisButton, SLOT(setChecked(bool))); - connect(showSourceButton, SIGNAL(clicked()), view, SLOT(showSource())); -#if defined(USE_OPENGL) && !defined(QT_OPENGL_ES) - connect(enableOpenGLButton, SIGNAL(clicked(bool)), view, SLOT(enableOpenGL(bool))); + 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, SIGNAL(toggled(bool)), view, SLOT(setAnimationEnabled(bool))); + connect(animateButton, &QAbstractButton::toggled, view, &CompositionRenderer::setAnimationEnabled); circleColorSlider->setValue(270); circleAlphaSlider->setValue(200); @@ -258,8 +260,7 @@ CompositionRenderer::CompositionRenderer(QWidget *parent) m_circle_pos = QPoint(200, 100); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); -#ifdef USE_OPENGL - m_pbuffer = 0; +#if QT_CONFIG(opengl) m_pbuffer_size = 1024; #endif } @@ -340,9 +341,9 @@ void CompositionRenderer::drawSource(QPainter &p) QRectF circle_rect = rectangle_around(m_circle_pos); QColor color = QColor::fromHsvF(m_circle_hue / 360.0, 1, 1, m_circle_alpha / 255.0); QLinearGradient circle_gradient(circle_rect.topLeft(), circle_rect.bottomRight()); - circle_gradient.setColorAt(0, color.light()); + circle_gradient.setColorAt(0, color.lighter()); circle_gradient.setColorAt(0.5, color); - circle_gradient.setColorAt(1, color.dark()); + circle_gradient.setColorAt(1, color.darker()); p.setBrush(circle_gradient); p.drawEllipse(circle_rect); @@ -350,105 +351,62 @@ void CompositionRenderer::drawSource(QPainter &p) void CompositionRenderer::paint(QPainter *painter) { -#if defined(USE_OPENGL) && !defined(QT_OPENGL_ES) - if (usesOpenGL()) { +#if QT_CONFIG(opengl) + if (usesOpenGL() && glWindow()->isValid()) { + + if (!m_blitter.isCreated()) + m_blitter.create(); int new_pbuf_size = m_pbuffer_size; - if (size().width() > m_pbuffer_size || size().height() > m_pbuffer_size) + while (size().width() > new_pbuf_size || size().height() > new_pbuf_size) new_pbuf_size *= 2; - if (size().width() < m_pbuffer_size/2 && size().height() < m_pbuffer_size/2) + while (size().width() < new_pbuf_size/2 && size().height() < new_pbuf_size/2) new_pbuf_size /= 2; - if (!m_pbuffer || new_pbuf_size != m_pbuffer_size) { - if (m_pbuffer) { - m_pbuffer->deleteTexture(m_base_tex); - m_pbuffer->deleteTexture(m_compositing_tex); - delete m_pbuffer; - } - - m_pbuffer = new QGLPixelBuffer(QSize(new_pbuf_size, new_pbuf_size), QGLFormat::defaultFormat(), glWidget()); - m_pbuffer->makeCurrent(); - m_base_tex = m_pbuffer->generateDynamicTexture(); - m_compositing_tex = m_pbuffer->generateDynamicTexture(); + 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_pbuffer); + QPainter p(m_fbo.data()); p.setCompositionMode(QPainter::CompositionMode_Source); - p.fillRect(QRect(0, 0, m_pbuffer->width(), m_pbuffer->height()), Qt::transparent); + p.fillRect(QRect(QPoint(0, 0), size()), Qt::transparent); + p.setCompositionMode(QPainter::CompositionMode_SourceOver); drawBase(p); p.end(); - m_pbuffer->updateDynamicTexture(m_base_tex); + m_base_tex = m_fbo->takeTexture(); } - qreal x_fraction = width()/float(m_pbuffer->width()); - qreal y_fraction = height()/float(m_pbuffer->height()); - + painter->beginNativePainting(); { - QPainter p(m_pbuffer); - p.setCompositionMode(QPainter::CompositionMode_Source); - p.fillRect(QRect(0, 0, m_pbuffer->width(), m_pbuffer->height()), Qt::transparent); - - p.save(); // Needed when using the GL1 engine - p.beginNativePainting(); // Needed when using the GL2 engine - - glBindTexture(GL_TEXTURE_2D, m_base_tex); - glEnable(GL_TEXTURE_2D); - glColor4f(1.,1.,1.,1.); - - glBegin(GL_QUADS); - { - glTexCoord2f(0, 1.0); - glVertex2f(0, 0); - - glTexCoord2f(x_fraction, 1.0); - glVertex2f(width(), 0); - - glTexCoord2f(x_fraction, 1.0-y_fraction); - glVertex2f(width(), height()); - - glTexCoord2f(0, 1.0-y_fraction); - glVertex2f(0, height()); - } - glEnd(); - - glDisable(GL_TEXTURE_2D); - - p.endNativePainting(); // Needed when using the GL2 engine - p.restore(); // Needed when using the GL1 engine - + QPainter p(m_fbo.data()); + 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(); - m_pbuffer->updateDynamicTexture(m_compositing_tex); - } - - painter->beginNativePainting(); // Needed when using the GL2 engine - glWidget()->makeCurrent(); // Needed when using the GL1 engine - glBindTexture(GL_TEXTURE_2D, m_compositing_tex); - glEnable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - glColor4f(1.,1.,1.,1.); - glBegin(GL_QUADS); - { - glTexCoord2f(0, 1.0); - glVertex2f(0, 0); - - glTexCoord2f(x_fraction, 1.0); - glVertex2f(width(), 0); - - glTexCoord2f(x_fraction, 1.0-y_fraction); - glVertex2f(width(), height()); - - glTexCoord2f(0, 1.0-y_fraction); - glVertex2f(0, height()); + m_compositing_tex = m_fbo->takeTexture(); } - glEnd(); - glDisable(GL_TEXTURE_2D); - painter->endNativePainting(); // Needed when using the GL2 engine + painter->endNativePainting(); + + painter->beginNativePainting(); + auto *funcs = QOpenGLContext::currentContext()->functions(); + 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(m_compositing_tex, target, QOpenGLTextureBlitter::OriginBottomLeft); + m_blitter.release(); + painter->endNativePainting(); } else #endif { @@ -520,7 +478,7 @@ 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 defined(USE_OPENGL) && !defined(QT_OPENGL_ES) +#if QT_CONFIG(opengl) if (usesOpenGL()) { update(); return; diff --git a/examples/widgets/painting/composition/composition.h b/examples/widgets/painting/composition/composition.h index 49853f503b..19150e2024 100644 --- a/examples/widgets/painting/composition/composition.h +++ b/examples/widgets/painting/composition/composition.h @@ -53,6 +53,11 @@ #include "arthurwidgets.h" +#if QT_CONFIG(opengl) +#include "fbopaintdevice.h" +#include <QOpenGLTextureBlitter> +#endif + #include <QPainter> #include <QEvent> @@ -61,10 +66,6 @@ class QPushButton; class QRadioButton; QT_END_NAMESPACE -#ifdef QT_OPENGL_SUPPORT -#include <QtOpenGL> -#endif - class CompositionWidget : public QWidget { Q_OBJECT @@ -186,12 +187,13 @@ private: bool m_animation_enabled; int m_animationTimer; -#ifdef QT_OPENGL_SUPPORT - QGLPixelBuffer *m_pbuffer; - GLuint m_base_tex; - GLuint m_compositing_tex; +#if QT_CONFIG(opengl) + QScopedPointer<QFboPaintDevice> m_fbo; int m_pbuffer_size; // width==height==size of pbuffer + uint m_base_tex; + uint m_compositing_tex; QSize m_previous_size; + QOpenGLTextureBlitter m_blitter; #endif }; diff --git a/examples/widgets/painting/composition/composition.pro b/examples/widgets/painting/composition/composition.pro index 5fdbe4a5a2..15d8c6e67b 100644 --- a/examples/widgets/painting/composition/composition.pro +++ b/examples/widgets/painting/composition/composition.pro @@ -6,10 +6,6 @@ SHARED_FOLDER = ../shared include($$SHARED_FOLDER/shared.pri) RESOURCES += composition.qrc -qtHaveModule(opengl):!qtConfig(dynamicgl) { - DEFINES += USE_OPENGL - QT += opengl -} QT += widgets # install diff --git a/examples/widgets/painting/composition/main.cpp b/examples/widgets/painting/composition/main.cpp index 1a5dfd3e5e..2eaeaba2c5 100644 --- a/examples/widgets/painting/composition/main.cpp +++ b/examples/widgets/painting/composition/main.cpp @@ -51,22 +51,17 @@ #include "composition.h" #include <QApplication> -#ifdef QT_OPENGL_SUPPORT -#include <QtOpenGL> -#endif int main(int argc, char *argv[]) { - // Q_INIT_RESOURCE(deform); - QApplication app(argc, argv); - CompositionWidget compWidget(0); + CompositionWidget compWidget(nullptr); QStyle *arthurStyle = new ArthurStyle(); compWidget.setStyle(arthurStyle); - QList<QWidget *> widgets = compWidget.findChildren<QWidget *>(); - foreach (QWidget *w, widgets) + const QList<QWidget *> widgets = compWidget.findChildren<QWidget *>(); + for (QWidget *w : widgets) w->setStyle(arthurStyle); compWidget.show(); diff --git a/examples/widgets/painting/concentriccircles/window.cpp b/examples/widgets/painting/concentriccircles/window.cpp index 0f65dc2285..45258e0bc4 100644 --- a/examples/widgets/painting/concentriccircles/window.cpp +++ b/examples/widgets/painting/concentriccircles/window.cpp @@ -77,8 +77,8 @@ Window::Window() circleWidgets[i][j]->setAntialiased(j != 0); circleWidgets[i][j]->setFloatBased(i != 0); - connect(timer, SIGNAL(timeout()), - circleWidgets[i][j], SLOT(nextAnimationFrame())); + connect(timer, &QTimer::timeout, + circleWidgets[i][j], &CircleWidget::nextAnimationFrame); layout->addWidget(circleWidgets[i][j], i + 1, j + 1); } diff --git a/examples/widgets/painting/deform/deform.pro b/examples/widgets/painting/deform/deform.pro index 6409aaed96..498ec57686 100644 --- a/examples/widgets/painting/deform/deform.pro +++ b/examples/widgets/painting/deform/deform.pro @@ -7,10 +7,6 @@ include($$SHARED_FOLDER/shared.pri) RESOURCES += deform.qrc -qtHaveModule(opengl) { - DEFINES += QT_OPENGL_SUPPORT - QT += opengl -} QT += widgets # install diff --git a/examples/widgets/painting/deform/main.cpp b/examples/widgets/painting/deform/main.cpp index 85e83207c0..28e3d6823d 100644 --- a/examples/widgets/painting/deform/main.cpp +++ b/examples/widgets/painting/deform/main.cpp @@ -60,12 +60,12 @@ int main(int argc, char **argv) bool smallScreen = QApplication::arguments().contains("-small-screen"); - PathDeformWidget deformWidget(0, smallScreen); + PathDeformWidget deformWidget(nullptr, smallScreen); - QStyle *arthurStyle = new ArthurStyle(); + QStyle *arthurStyle = new ArthurStyle; deformWidget.setStyle(arthurStyle); - QList<QWidget *> widgets = deformWidget.findChildren<QWidget *>(); - foreach (QWidget *w, widgets) + const QList<QWidget *> widgets = deformWidget.findChildren<QWidget *>(); + for (QWidget *w : widgets) w->setStyle(arthurStyle); if (smallScreen) diff --git a/examples/widgets/painting/deform/pathdeform.cpp b/examples/widgets/painting/deform/pathdeform.cpp index 805804716f..64e81f8cab 100644 --- a/examples/widgets/painting/deform/pathdeform.cpp +++ b/examples/widgets/painting/deform/pathdeform.cpp @@ -109,13 +109,11 @@ void PathDeformControls::layoutForDesktop() QPushButton *showSourceButton = new QPushButton(mainGroup); showSourceButton->setText(tr("Show Source")); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) QPushButton *enableOpenGLButton = new QPushButton(mainGroup); enableOpenGLButton->setText(tr("Use OpenGL")); enableOpenGLButton->setCheckable(true); enableOpenGLButton->setChecked(m_renderer->usesOpenGL()); - if (!QGLFormat::hasOpenGL()) - enableOpenGLButton->hide(); #endif QPushButton *whatsThisButton = new QPushButton(mainGroup); @@ -132,7 +130,7 @@ void PathDeformControls::layoutForDesktop() mainGroupLayout->addWidget(textGroup); mainGroupLayout->addWidget(animateButton); mainGroupLayout->addStretch(1); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) mainGroupLayout->addWidget(enableOpenGLButton); #endif mainGroupLayout->addWidget(showSourceButton); @@ -152,21 +150,21 @@ void PathDeformControls::layoutForDesktop() QVBoxLayout * mainLayout = new QVBoxLayout(this); mainLayout->addWidget(mainGroup); - mainLayout->setMargin(0); - - connect(radiusSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setRadius(int))); - connect(deformSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setIntensity(int))); - connect(fontSizeSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setFontSize(int))); - connect(animateButton, SIGNAL(clicked(bool)), m_renderer, SLOT(setAnimated(bool))); -#ifdef QT_OPENGL_SUPPORT - connect(enableOpenGLButton, SIGNAL(clicked(bool)), m_renderer, SLOT(enableOpenGL(bool))); + mainLayout->setContentsMargins(QMargins()); + + connect(radiusSlider, &QAbstractSlider::valueChanged, m_renderer, &PathDeformRenderer::setRadius); + 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, SIGNAL(textChanged(QString)), m_renderer, SLOT(setText(QString))); - connect(m_renderer, SIGNAL(descriptionEnabledChanged(bool)), - whatsThisButton, SLOT(setChecked(bool))); - connect(whatsThisButton, SIGNAL(clicked(bool)), m_renderer, SLOT(setDescriptionEnabled(bool))); - connect(showSourceButton, SIGNAL(clicked()), m_renderer, SLOT(showSource())); + connect(textInput, &QLineEdit::textChanged, m_renderer, &PathDeformRenderer::setText); + connect(m_renderer, &ArthurFrame::descriptionEnabledChanged, + whatsThisButton, &QAbstractButton::setChecked); + connect(whatsThisButton, &QAbstractButton::clicked, m_renderer, &ArthurFrame::setDescriptionEnabled); + connect(showSourceButton, &QAbstractButton::clicked, m_renderer, &ArthurFrame::showSource); animateButton->animateClick(); deformSlider->setValue(80); @@ -201,13 +199,11 @@ void PathDeformControls::layoutForSmallScreen() QPushButton *animateButton = new QPushButton(tr("Animated"), mainGroup); animateButton->setCheckable(true); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) QPushButton *enableOpenGLButton = new QPushButton(mainGroup); enableOpenGLButton->setText(tr("Use OpenGL")); enableOpenGLButton->setCheckable(mainGroup); enableOpenGLButton->setChecked(m_renderer->usesOpenGL()); - if (!QGLFormat::hasOpenGL()) - enableOpenGLButton->hide(); #endif QPushButton *quitButton = new QPushButton(tr("Quit"), mainGroup); @@ -215,7 +211,7 @@ void PathDeformControls::layoutForSmallScreen() QGridLayout *mainGroupLayout = new QGridLayout(mainGroup); - mainGroupLayout->setMargin(0); + mainGroupLayout->setContentsMargins(QMargins()); mainGroupLayout->addWidget(radiusLabel, 0, 0, Qt::AlignRight); mainGroupLayout->addWidget(radiusSlider, 0, 1); mainGroupLayout->addWidget(deformLabel, 1, 0, Qt::AlignRight); @@ -223,7 +219,7 @@ void PathDeformControls::layoutForSmallScreen() mainGroupLayout->addWidget(fontSizeLabel, 2, 0, Qt::AlignRight); mainGroupLayout->addWidget(fontSizeSlider, 2, 1); mainGroupLayout->addWidget(animateButton, 3,0, 1,2); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) mainGroupLayout->addWidget(enableOpenGLButton, 4,0, 1,2); #endif @@ -233,14 +229,14 @@ void PathDeformControls::layoutForSmallScreen() mainLayout->addWidget(okButton); mainLayout->addWidget(quitButton); - connect(quitButton, SIGNAL(clicked()), this, SIGNAL(quitPressed())); - connect(okButton, SIGNAL(clicked()), this, SIGNAL(okPressed())); - connect(radiusSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setRadius(int))); - connect(deformSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setIntensity(int))); - connect(fontSizeSlider, SIGNAL(valueChanged(int)), m_renderer, SLOT(setFontSize(int))); - connect(animateButton, SIGNAL(clicked(bool)), m_renderer, SLOT(setAnimated(bool))); -#ifdef QT_OPENGL_SUPPORT - connect(enableOpenGLButton, SIGNAL(clicked(bool)), m_renderer, SLOT(enableOpenGL(bool))); + connect(quitButton, &QAbstractButton::clicked, this, &PathDeformControls::quitPressed); + connect(okButton, &QAbstractButton::clicked, this, &PathDeformControls::okPressed); + connect(radiusSlider, &QAbstractSlider::valueChanged, m_renderer, &PathDeformRenderer::setRadius); + 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 @@ -276,9 +272,12 @@ PathDeformWidget::PathDeformWidget(QWidget *parent, bool smallScreen) m_renderer->loadDescription(":res/deform/pathdeform.html"); m_renderer->setDescriptionEnabled(false); - connect(m_renderer, SIGNAL(clicked()), this, SLOT(showControls())); - connect(m_controls, SIGNAL(okPressed()), this, SLOT(hideControls())); - connect(m_controls, SIGNAL(quitPressed()), QCoreApplication::instance(), SLOT(quit())); + connect(m_renderer, &PathDeformRenderer::clicked, + this, &PathDeformWidget::showControls); + connect(m_controls, &PathDeformControls::okPressed, + this, &PathDeformWidget::hideControls); + connect(m_controls, &PathDeformControls::quitPressed, + qApp, &QCoreApplication::quit); } @@ -292,16 +291,16 @@ void PathDeformWidget::hideControls() m_controls->hide(); } -void PathDeformWidget::setStyle( QStyle * style ) +void PathDeformWidget::setStyle(QStyle *style) { QWidget::setStyle(style); - if (m_controls == 0) + if (!m_controls) return; m_controls->setStyle(style); - QList<QWidget *> widgets = m_controls->findChildren<QWidget *>(); - foreach (QWidget *w, widgets) + const QList<QWidget *> widgets = m_controls->findChildren<QWidget *>(); + for (QWidget *w : widgets) w->setStyle(style); } @@ -463,7 +462,7 @@ void PathDeformRenderer::timerEvent(QTimerEvent *e) m_pos.setY(height() - m_radius); } -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) if (usesOpenGL()) { update(); } else @@ -527,7 +526,7 @@ void PathDeformRenderer::mouseMoveEvent(QMouseEvent *e) m_direction = (m_direction + dir) / 2; } m_pos = e->pos() + m_offset; -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) if (usesOpenGL()) { update(); } else @@ -620,7 +619,7 @@ void PathDeformRenderer::setRadius(int radius) m_radius = radius; generateLensPixmap(); if (!m_animated || m_radius < max) { -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) if (usesOpenGL()){ update(); return; @@ -634,7 +633,7 @@ void PathDeformRenderer::setIntensity(int intensity) { m_intensity = intensity; if (!m_animated) { -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) if (usesOpenGL()) { update(); return; diff --git a/examples/widgets/painting/deform/pathdeform.h b/examples/widgets/painting/deform/pathdeform.h index 68908045b9..b7c7386e2a 100644 --- a/examples/widgets/painting/deform/pathdeform.h +++ b/examples/widgets/painting/deform/pathdeform.h @@ -135,7 +135,7 @@ signals: void okPressed(); void quitPressed(); private: - PathDeformRenderer* m_renderer; + PathDeformRenderer *m_renderer; void layoutForDesktop(); void layoutForSmallScreen(); }; @@ -145,7 +145,7 @@ class PathDeformWidget : public QWidget Q_OBJECT public: PathDeformWidget(QWidget *parent, bool smallScreen); - void setStyle (QStyle * style ); + void setStyle(QStyle *style); private: PathDeformRenderer *m_renderer; diff --git a/examples/widgets/painting/fontsampler/mainwindow.cpp b/examples/widgets/painting/fontsampler/mainwindow.cpp index bd15438df9..b3304b4b6d 100644 --- a/examples/widgets/painting/fontsampler/mainwindow.cpp +++ b/examples/widgets/painting/fontsampler/mainwindow.cpp @@ -71,13 +71,14 @@ MainWindow::MainWindow(QWidget *parent) markedCount = 0; setupFontTree(); - connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit())); - connect(fontTree, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), - this, SLOT(showFont(QTreeWidgetItem*))); - connect(fontTree, SIGNAL(itemChanged(QTreeWidgetItem*,int)), - this, SLOT(updateStyles(QTreeWidgetItem*,int))); - - fontTree->setItemSelected(fontTree->topLevelItem(0), true); + connect(quitAction, &QAction::triggered, + qApp, &QApplication::quit); + connect(fontTree, &QTreeWidget::currentItemChanged, + this, &MainWindow::showFont); + connect(fontTree, &QTreeWidget::itemChanged, + this, &MainWindow::updateStyles); + + fontTree->topLevelItem(0)->setSelected(true); showFont(fontTree->topLevelItem(0)); } @@ -85,9 +86,10 @@ void MainWindow::setupFontTree() { QFontDatabase database; fontTree->setColumnCount(1); - fontTree->setHeaderLabels(QStringList() << tr("Font")); + fontTree->setHeaderLabels({ tr("Font") }); - foreach (QString family, database.families()) { + const QStringList fontFamilies = database.families(); + for (const QString &family : fontFamilies) { const QStringList styles = database.styles(family); if (styles.isEmpty()) continue; @@ -97,7 +99,7 @@ void MainWindow::setupFontTree() familyItem->setCheckState(0, Qt::Unchecked); familyItem->setFlags(familyItem->flags() | Qt::ItemIsAutoTristate); - foreach (QString style, styles) { + for (const QString &style : styles) { QTreeWidgetItem *styleItem = new QTreeWidgetItem(familyItem); styleItem->setText(0, style); styleItem->setCheckState(0, Qt::Unchecked); @@ -109,10 +111,10 @@ void MainWindow::setupFontTree() void MainWindow::on_clearAction_triggered() { - QTreeWidgetItem *currentItem = fontTree->currentItem(); - foreach (QTreeWidgetItem *item, fontTree->selectedItems()) - fontTree->setItemSelected(item, false); - fontTree->setItemSelected(currentItem, true); + const QList<QTreeWidgetItem *> items = fontTree->selectedItems(); + for (QTreeWidgetItem *item : items) + item->setSelected(false); + fontTree->currentItem()->setSelected(true); } void MainWindow::on_markAction_triggered() @@ -127,8 +129,8 @@ void MainWindow::on_unmarkAction_triggered() void MainWindow::markUnmarkFonts(Qt::CheckState state) { - QList<QTreeWidgetItem *> items = fontTree->selectedItems(); - foreach (QTreeWidgetItem *item, items) { + const QList<QTreeWidgetItem *> items = fontTree->selectedItems(); + for (QTreeWidgetItem *item : items) { if (item->checkState(0) != state) item->setCheckState(0, state); } @@ -285,8 +287,8 @@ void MainWindow::on_printPreviewAction_triggered() QPrinter printer(QPrinter::HighResolution); QPrintPreviewDialog preview(&printer, this); - connect(&preview, SIGNAL(paintRequested(QPrinter*)), - this, SLOT(printDocument(QPrinter*))); + connect(&preview, &QPrintPreviewDialog::paintRequested, + this, &MainWindow::printDocument); preview.exec(); #endif } @@ -294,19 +296,19 @@ void MainWindow::on_printPreviewAction_triggered() void MainWindow::printPage(int index, QPainter *painter, QPrinter *printer) { #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog) - QString family = pageMap.keys()[index]; - StyleItems items = pageMap[family]; + const QString family = (pageMap.begin() + index).key(); + const StyleItems items = pageMap.value(family); // Find the dimensions of the text on each page. qreal width = 0.0; qreal height = 0.0; - foreach (QTreeWidgetItem *item, items) { + for (const QTreeWidgetItem *item : items) { QString style = item->text(0); int weight = item->data(0, Qt::UserRole).toInt(); bool italic = item->data(0, Qt::UserRole + 1).toBool(); // Calculate the maximum width and total height of the text. - foreach (int size, sampleSizes) { + for (int size : qAsConst(sampleSizes)) { QFont font(family, size, weight, italic); font.setStyleName(style); font = QFont(font, painter->device()); @@ -334,13 +336,13 @@ void MainWindow::printPage(int index, QPainter *painter, QPrinter *printer) qreal x = -width / 2.0; qreal y = -height / 2.0 - remainingHeight / 4.0 + spaceHeight; - foreach (QTreeWidgetItem *item, items) { + for (const QTreeWidgetItem *item : items) { QString style = item->text(0); int weight = item->data(0, Qt::UserRole).toInt(); bool italic = item->data(0, Qt::UserRole + 1).toBool(); // Draw each line of text. - foreach (int size, sampleSizes) { + for (int size : qAsConst(sampleSizes)) { QFont font(family, size, weight, italic); font.setStyleName(style); font = QFont(font, painter->device()); diff --git a/examples/widgets/painting/gradients/gradients.cpp b/examples/widgets/painting/gradients/gradients.cpp index 6d9f514a8d..8df45be8d9 100644 --- a/examples/widgets/painting/gradients/gradients.cpp +++ b/examples/widgets/painting/gradients/gradients.cpp @@ -180,7 +180,7 @@ GradientEditor::GradientEditor(QWidget *parent) { QVBoxLayout *vbox = new QVBoxLayout(this); vbox->setSpacing(1); - vbox->setMargin(1); + vbox->setContentsMargins(1, 1, 1, 1); m_red_shade = new ShadeWidget(ShadeWidget::RedShade, this); m_green_shade = new ShadeWidget(ShadeWidget::GreenShade, this); @@ -317,13 +317,11 @@ GradientWidget::GradientWidget(QWidget *parent) QPushButton *showSourceButton = new QPushButton(mainGroup); showSourceButton->setText(tr("Show Source")); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) QPushButton *enableOpenGLButton = new QPushButton(mainGroup); enableOpenGLButton->setText(tr("Use OpenGL")); enableOpenGLButton->setCheckable(true); enableOpenGLButton->setChecked(m_renderer->usesOpenGL()); - if (!QGLFormat::hasOpenGL()) - enableOpenGLButton->hide(); #endif QPushButton *whatsThisButton = new QPushButton(mainGroup); whatsThisButton->setText(tr("What's This?")); @@ -343,7 +341,7 @@ GradientWidget::GradientWidget(QWidget *parent) mainGroupLayout->addWidget(defaultsGroup); mainGroupLayout->addStretch(1); mainGroupLayout->addWidget(showSourceButton); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) mainGroupLayout->addWidget(enableOpenGLButton); #endif mainGroupLayout->addWidget(whatsThisButton); @@ -406,7 +404,7 @@ GradientWidget::GradientWidget(QWidget *parent) connect(showSourceButton, &QPushButton::clicked, m_renderer, &GradientRenderer::showSource); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) connect(enableOpenGLButton, QOverload<bool>::of(&QPushButton::clicked), m_renderer, &ArthurFrame::enableOpenGL); #endif @@ -423,7 +421,7 @@ GradientWidget::GradientWidget(QWidget *parent) m_renderer->loadSourceFile(":res/gradients/gradients.cpp"); m_renderer->loadDescription(":res/gradients/gradients.html"); - QTimer::singleShot(50, this, SLOT(setDefault1())); + QTimer::singleShot(50, this, &GradientWidget::setDefault1); } void GradientWidget::setDefault(int config) @@ -566,9 +564,7 @@ void GradientRenderer::paint(QPainter *p) } else { QLineF l(pts.at(0), pts.at(1)); - qreal angle = l.angle(QLineF(0, 0, 1, 0)); - if (l.dy() > 0) - angle = 360 - angle; + qreal angle = QLineF(0, 0, 1, 0).angleTo(l); g = QConicalGradient(pts.at(0), angle); } diff --git a/examples/widgets/painting/gradients/gradients.pro b/examples/widgets/painting/gradients/gradients.pro index 73f3974c62..14eccb6500 100644 --- a/examples/widgets/painting/gradients/gradients.pro +++ b/examples/widgets/painting/gradients/gradients.pro @@ -6,10 +6,6 @@ SHARED_FOLDER = ../shared include($$SHARED_FOLDER/shared.pri) RESOURCES += gradients.qrc -qtHaveModule(opengl) { - DEFINES += QT_OPENGL_SUPPORT - QT += opengl -} QT += widgets # install diff --git a/examples/widgets/painting/gradients/main.cpp b/examples/widgets/painting/gradients/main.cpp index 6c5261fe6b..539d67e40e 100644 --- a/examples/widgets/painting/gradients/main.cpp +++ b/examples/widgets/painting/gradients/main.cpp @@ -61,8 +61,8 @@ int main(int argc, char *argv[]) GradientWidget gradientWidget(0); QStyle *arthurStyle = new ArthurStyle(); gradientWidget.setStyle(arthurStyle); - QList<QWidget *> widgets = gradientWidget.findChildren<QWidget *>(); - foreach (QWidget *w, widgets) { + const QList<QWidget *> widgets = gradientWidget.findChildren<QWidget *>(); + for (QWidget *w : widgets) { w->setStyle(arthurStyle); w->setAttribute(Qt::WA_AcceptTouchEvents); } diff --git a/examples/widgets/painting/imagecomposition/imagecomposer.cpp b/examples/widgets/painting/imagecomposition/imagecomposer.cpp index d53017b955..ffdc8f433c 100644 --- a/examples/widgets/painting/imagecomposition/imagecomposer.cpp +++ b/examples/widgets/painting/imagecomposition/imagecomposer.cpp @@ -100,9 +100,12 @@ ImageComposer::ImageComposer() //! [2] //! [3] - connect(sourceButton, SIGNAL(clicked()), this, SLOT(chooseSource())); - connect(operatorComboBox, SIGNAL(activated(int)), this, SLOT(recalculateResult())); - connect(destinationButton, SIGNAL(clicked()), this, SLOT(chooseDestination())); + connect(sourceButton, &QAbstractButton::clicked, + this, &ImageComposer::chooseSource); + connect(operatorComboBox, QOverload<int>::of(&QComboBox::activated), + this, &ImageComposer::recalculateResult); + connect(destinationButton, &QAbstractButton::clicked, + this, &ImageComposer::chooseDestination); //! [3] //! [4] diff --git a/examples/widgets/painting/painterpaths/window.cpp b/examples/widgets/painting/painterpaths/window.cpp index a987937b39..6fb3218313 100644 --- a/examples/widgets/painting/painterpaths/window.cpp +++ b/examples/widgets/painting/painterpaths/window.cpp @@ -194,22 +194,30 @@ Window::Window() //! [12] //! [16] - connect(fillRuleComboBox, SIGNAL(activated(int)), this, SLOT(fillRuleChanged())); - connect(fillColor1ComboBox, SIGNAL(activated(int)), this, SLOT(fillGradientChanged())); - connect(fillColor2ComboBox, SIGNAL(activated(int)), this, SLOT(fillGradientChanged())); - connect(penColorComboBox, SIGNAL(activated(int)), this, SLOT(penColorChanged())); - - for(QList<RenderArea*>::iterator it = renderAreas.begin(); it != renderAreas.end(); it++) { - connect(penWidthSpinBox, SIGNAL(valueChanged(int)), *it, SLOT(setPenWidth(int))); - connect(rotationAngleSpinBox, SIGNAL(valueChanged(int)), *it, SLOT(setRotationAngle(int))); + connect(fillRuleComboBox, QOverload<int>::of(&QComboBox::activated), + this, &Window::fillRuleChanged); + connect(fillColor1ComboBox, QOverload<int>::of(&QComboBox::activated), + this, &Window::fillGradientChanged); + connect(fillColor2ComboBox, QOverload<int>::of(&QComboBox::activated), + this, &Window::fillGradientChanged); + connect(penColorComboBox, QOverload<int>::of(&QComboBox::activated), + this, &Window::penColorChanged); + + for (RenderArea *area : qAsConst(renderAreas)) { + connect(penWidthSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), + area, &RenderArea::setPenWidth); + connect(rotationAngleSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), + area, &RenderArea::setRotationAngle); } //! [16] //! [17] QGridLayout *topLayout = new QGridLayout; - int i=0; - for(QList<RenderArea*>::iterator it = renderAreas.begin(); it != renderAreas.end(); it++, i++) - topLayout->addWidget(*it, i / 3, i % 3); + int i = 0; + for (RenderArea *area : qAsConst(renderAreas)) { + topLayout->addWidget(area, i / 3, i % 3); + ++i; + } QGridLayout *mainLayout = new QGridLayout; mainLayout->addLayout(topLayout, 0, 0, 1, 4); @@ -243,8 +251,8 @@ void Window::fillRuleChanged() { Qt::FillRule rule = (Qt::FillRule)currentItemData(fillRuleComboBox).toInt(); - for (QList<RenderArea*>::iterator it = renderAreas.begin(); it != renderAreas.end(); ++it) - (*it)->setFillRule(rule); + for (RenderArea *area : qAsConst(renderAreas)) + area->setFillRule(rule); } //! [19] @@ -254,8 +262,8 @@ void Window::fillGradientChanged() QColor color1 = qvariant_cast<QColor>(currentItemData(fillColor1ComboBox)); QColor color2 = qvariant_cast<QColor>(currentItemData(fillColor2ComboBox)); - for (QList<RenderArea*>::iterator it = renderAreas.begin(); it != renderAreas.end(); ++it) - (*it)->setFillGradient(color1, color2); + for (RenderArea *area : qAsConst(renderAreas)) + area->setFillGradient(color1, color2); } //! [20] @@ -264,16 +272,16 @@ void Window::penColorChanged() { QColor color = qvariant_cast<QColor>(currentItemData(penColorComboBox)); - for (QList<RenderArea*>::iterator it = renderAreas.begin(); it != renderAreas.end(); ++it) - (*it)->setPenColor(color); + for (RenderArea *area : qAsConst(renderAreas)) + area->setPenColor(color); } //! [21] //! [22] void Window::populateWithColors(QComboBox *comboBox) { - QStringList colorNames = QColor::colorNames(); - foreach (QString name, colorNames) + const QStringList colorNames = QColor::colorNames(); + for (const QString &name : colorNames) comboBox->addItem(name, QColor(name)); } //! [22] diff --git a/examples/widgets/painting/pathstroke/main.cpp b/examples/widgets/painting/pathstroke/main.cpp index 3a63203118..57c85d73a3 100644 --- a/examples/widgets/painting/pathstroke/main.cpp +++ b/examples/widgets/painting/pathstroke/main.cpp @@ -63,8 +63,8 @@ int main(int argc, char **argv) PathStrokeWidget pathStrokeWidget(smallScreen); QStyle *arthurStyle = new ArthurStyle(); pathStrokeWidget.setStyle(arthurStyle); - QList<QWidget *> widgets = pathStrokeWidget.findChildren<QWidget *>(); - foreach (QWidget *w, widgets) { + const QList<QWidget *> widgets = pathStrokeWidget.findChildren<QWidget *>(); + for (QWidget *w : widgets) { w->setStyle(arthurStyle); w->setAttribute(Qt::WA_AcceptTouchEvents); } diff --git a/examples/widgets/painting/pathstroke/pathstroke.cpp b/examples/widgets/painting/pathstroke/pathstroke.cpp index 5a7b8fd9fe..03e55bb2a2 100644 --- a/examples/widgets/painting/pathstroke/pathstroke.cpp +++ b/examples/widgets/painting/pathstroke/pathstroke.cpp @@ -164,24 +164,24 @@ void PathStrokeControls::createCommonControls(QWidget* parent) // Connections - connect(flatCap, SIGNAL(clicked()), m_renderer, SLOT(setFlatCap())); - connect(squareCap, SIGNAL(clicked()), m_renderer, SLOT(setSquareCap())); - connect(roundCap, SIGNAL(clicked()), m_renderer, SLOT(setRoundCap())); + connect(flatCap, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setFlatCap); + connect(squareCap, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setSquareCap); + connect(roundCap, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setRoundCap); - connect(bevelJoin, SIGNAL(clicked()), m_renderer, SLOT(setBevelJoin())); - connect(miterJoin, SIGNAL(clicked()), m_renderer, SLOT(setMiterJoin())); - connect(svgMiterJoin, SIGNAL(clicked()), m_renderer, SLOT(setSvgMiterJoin())); - connect(roundJoin, SIGNAL(clicked()), m_renderer, SLOT(setRoundJoin())); + connect(bevelJoin, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setBevelJoin); + connect(miterJoin, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setMiterJoin); + connect(svgMiterJoin, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setSvgMiterJoin); + connect(roundJoin, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setRoundJoin); - connect(curveMode, SIGNAL(clicked()), m_renderer, SLOT(setCurveMode())); - connect(lineMode, SIGNAL(clicked()), m_renderer, SLOT(setLineMode())); + connect(curveMode, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setCurveMode); + connect(lineMode, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setLineMode); - connect(solidLine, SIGNAL(clicked()), m_renderer, SLOT(setSolidLine())); - connect(dashLine, SIGNAL(clicked()), m_renderer, SLOT(setDashLine())); - connect(dotLine, SIGNAL(clicked()), m_renderer, SLOT(setDotLine())); - connect(dashDotLine, SIGNAL(clicked()), m_renderer, SLOT(setDashDotLine())); - connect(dashDotDotLine, SIGNAL(clicked()), m_renderer, SLOT(setDashDotDotLine())); - connect(customDashLine, SIGNAL(clicked()), m_renderer, SLOT(setCustomDashLine())); + connect(solidLine, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setSolidLine); + connect(dashLine, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setDashLine); + connect(dotLine, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setDotLine); + connect(dashDotLine, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setDashDotLine); + connect(dashDotDotLine, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setDashDotDotLine); + connect(customDashLine, &QAbstractButton::clicked, m_renderer, &PathStrokeRenderer::setCustomDashLine); // Set the defaults: flatCap->setChecked(true); @@ -211,13 +211,11 @@ void PathStrokeControls::layoutForDesktop() QPushButton *showSourceButton = new QPushButton(mainGroup); showSourceButton->setText(tr("Show Source")); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) QPushButton *enableOpenGLButton = new QPushButton(mainGroup); enableOpenGLButton->setText(tr("Use OpenGL")); enableOpenGLButton->setCheckable(true); enableOpenGLButton->setChecked(m_renderer->usesOpenGL()); - if (!QGLFormat::hasOpenGL()) - enableOpenGLButton->hide(); #endif QPushButton *whatsThisButton = new QPushButton(mainGroup); whatsThisButton->setText(tr("What's This?")); @@ -229,11 +227,11 @@ void PathStrokeControls::layoutForDesktop() penWidthLayout->addWidget(penWidth); QVBoxLayout * mainLayout = new QVBoxLayout(this); - mainLayout->setMargin(0); + mainLayout->setContentsMargins(QMargins()); mainLayout->addWidget(mainGroup); QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup); - mainGroupLayout->setMargin(3); + mainGroupLayout->setContentsMargins(3, 3, 3, 3); mainGroupLayout->addWidget(m_capGroup); mainGroupLayout->addWidget(m_joinGroup); mainGroupLayout->addWidget(m_styleGroup); @@ -242,24 +240,24 @@ void PathStrokeControls::layoutForDesktop() mainGroupLayout->addWidget(animated); mainGroupLayout->addStretch(1); mainGroupLayout->addWidget(showSourceButton); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) mainGroupLayout->addWidget(enableOpenGLButton); #endif mainGroupLayout->addWidget(whatsThisButton); // Set up connections - connect(animated, SIGNAL(toggled(bool)), m_renderer, SLOT(setAnimation(bool))); + connect(animated, &QAbstractButton::toggled, m_renderer, &PathStrokeRenderer::setAnimation); - connect(penWidth, SIGNAL(valueChanged(int)), m_renderer, SLOT(setPenWidth(int))); + connect(penWidth, &QAbstractSlider::valueChanged, m_renderer, &PathStrokeRenderer::setPenWidth); - connect(showSourceButton, SIGNAL(clicked()), m_renderer, SLOT(showSource())); -#ifdef QT_OPENGL_SUPPORT - connect(enableOpenGLButton, SIGNAL(clicked(bool)), m_renderer, SLOT(enableOpenGL(bool))); + connect(showSourceButton, &QAbstractButton::clicked, m_renderer, &ArthurFrame::showSource); +#if QT_CONFIG(opengl) + connect(enableOpenGLButton, &QAbstractButton::clicked, m_renderer, &ArthurFrame::enableOpenGL); #endif - connect(whatsThisButton, SIGNAL(clicked(bool)), m_renderer, SLOT(setDescriptionEnabled(bool))); - connect(m_renderer, SIGNAL(descriptionEnabledChanged(bool)), - whatsThisButton, SLOT(setChecked(bool))); + connect(whatsThisButton, &QAbstractButton::clicked, m_renderer, &ArthurFrame::setDescriptionEnabled); + connect(m_renderer, &ArthurFrame::descriptionEnabledChanged, + whatsThisButton, &QAbstractButton::setChecked); // Set the defaults @@ -272,10 +270,10 @@ void PathStrokeControls::layoutForSmallScreens() { createCommonControls(this); - m_capGroup->layout()->setMargin(0); - m_joinGroup->layout()->setMargin(0); - m_styleGroup->layout()->setMargin(0); - m_pathModeGroup->layout()->setMargin(0); + m_capGroup->layout()->setContentsMargins(QMargins()); + m_joinGroup->layout()->setContentsMargins(QMargins()); + m_styleGroup->layout()->setContentsMargins(QMargins()); + m_pathModeGroup->layout()->setContentsMargins(QMargins()); QPushButton* okBtn = new QPushButton(tr("OK"), this); okBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); @@ -290,13 +288,11 @@ void PathStrokeControls::layoutForSmallScreens() penWidth->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); penWidth->setRange(0, 500); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) QPushButton *enableOpenGLButton = new QPushButton(this); enableOpenGLButton->setText(tr("Use OpenGL")); enableOpenGLButton->setCheckable(true); enableOpenGLButton->setChecked(m_renderer->usesOpenGL()); - if (!QGLFormat::hasOpenGL()) - enableOpenGLButton->hide(); #endif // Layouts: @@ -307,7 +303,7 @@ void PathStrokeControls::layoutForSmallScreens() QVBoxLayout *leftLayout = new QVBoxLayout(0); leftLayout->addWidget(m_capGroup); leftLayout->addWidget(m_joinGroup); -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) leftLayout->addWidget(enableOpenGLButton); #endif leftLayout->addLayout(penWidthLayout); @@ -317,7 +313,7 @@ void PathStrokeControls::layoutForSmallScreens() rightLayout->addWidget(m_pathModeGroup); QGridLayout *mainLayout = new QGridLayout(this); - mainLayout->setMargin(0); + mainLayout->setContentsMargins(QMargins()); // Add spacers around the form items so we don't look stupid at higher resolutions mainLayout->addItem(new QSpacerItem(0,0), 0, 0, 1, 4); @@ -330,13 +326,13 @@ void PathStrokeControls::layoutForSmallScreens() mainLayout->addWidget(quitBtn, 2, 1, Qt::AlignHCenter | Qt::AlignTop); mainLayout->addWidget(okBtn, 2, 2, Qt::AlignHCenter | Qt::AlignTop); -#ifdef QT_OPENGL_SUPPORT - connect(enableOpenGLButton, SIGNAL(clicked(bool)), m_renderer, SLOT(enableOpenGL(bool))); +#if QT_CONFIG(opengl) + connect(enableOpenGLButton, &QAbstractButton::clicked, m_renderer, &ArthurFrame::enableOpenGL); #endif - connect(penWidth, SIGNAL(valueChanged(int)), m_renderer, SLOT(setPenWidth(int))); - connect(quitBtn, SIGNAL(clicked()), this, SLOT(emitQuitSignal())); - connect(okBtn, SIGNAL(clicked()), this, SLOT(emitOkSignal())); + connect(penWidth, &QAbstractSlider::valueChanged, m_renderer, &PathStrokeRenderer::setPenWidth); + connect(quitBtn, &QAbstractButton::clicked, this, &PathStrokeControls::emitQuitSignal); + connect(okBtn, &QAbstractButton::clicked, this, &PathStrokeControls::emitOkSignal); m_renderer->setAnimation(true); penWidth->setValue(50); @@ -372,8 +368,8 @@ PathStrokeWidget::PathStrokeWidget(bool smallScreen) m_renderer->loadSourceFile(":res/pathstroke/pathstroke.cpp"); m_renderer->loadDescription(":res/pathstroke/pathstroke.html"); - connect(m_renderer, SIGNAL(clicked()), this, SLOT(showControls())); - connect(m_controls, SIGNAL(okPressed()), this, SLOT(hideControls())); + connect(m_renderer, &PathStrokeRenderer::clicked, this, &PathStrokeWidget::showControls); + connect(m_controls, &PathStrokeControls::okPressed, this, &PathStrokeWidget::hideControls); connect(m_controls, SIGNAL(quitPressed()), QApplication::instance(), SLOT(quit())); } @@ -394,8 +390,8 @@ void PathStrokeWidget::setStyle( QStyle * style ) { m_controls->setStyle(style); - QList<QWidget *> widgets = m_controls->findChildren<QWidget *>(); - foreach (QWidget *w, widgets) + const QList<QWidget *> widgets = m_controls->findChildren<QWidget *>(); + for (QWidget *w : widgets) w->setStyle(style); } } @@ -609,7 +605,7 @@ bool PathStrokeRenderer::event(QEvent *e) { const QTouchEvent *const event = static_cast<const QTouchEvent*>(e); const QList<QTouchEvent::TouchPoint> points = event->touchPoints(); - foreach (const QTouchEvent::TouchPoint &touchPoint, points) { + for (const QTouchEvent::TouchPoint &touchPoint : points) { const int id = touchPoint.id(); switch (touchPoint.state()) { case Qt::TouchPointPressed: diff --git a/examples/widgets/painting/pathstroke/pathstroke.pro b/examples/widgets/painting/pathstroke/pathstroke.pro index 8ab3cb9244..3e71b2db56 100644 --- a/examples/widgets/painting/pathstroke/pathstroke.pro +++ b/examples/widgets/painting/pathstroke/pathstroke.pro @@ -7,10 +7,6 @@ include($$SHARED_FOLDER/shared.pri) RESOURCES += pathstroke.qrc -qtHaveModule(opengl) { - DEFINES += QT_OPENGL_SUPPORT - QT += opengl -} QT += widgets # install diff --git a/examples/widgets/painting/shared/arthurstyle.cpp b/examples/widgets/painting/shared/arthurstyle.cpp index f4fc76bda6..3fc461bbd2 100644 --- a/examples/widgets/painting/shared/arthurstyle.cpp +++ b/examples/widgets/painting/shared/arthurstyle.cpp @@ -61,10 +61,10 @@ QPixmap cached(const QString &img) { - if (QPixmap *p = QPixmapCache::find(img)) - return *p; - QPixmap pm; + if (QPixmapCache::find(img, &pm)) + return pm; + pm = QPixmap::fromImage(QImage(img), Qt::OrderedDither | Qt::OrderedAlphaDither); if (pm.isNull()) return QPixmap(); @@ -443,9 +443,9 @@ void ArthurStyle::polish(QWidget *widget) if (widget->layout() && qobject_cast<QGroupBox *>(widget)) { if (widget->findChildren<QGroupBox *>().size() == 0) { widget->layout()->setSpacing(0); - widget->layout()->setMargin(12); + widget->layout()->setContentsMargins(12, 12, 12, 12); } else { - widget->layout()->setMargin(13); + widget->layout()->setContentsMargins(13, 13, 13, 13); } } diff --git a/examples/widgets/painting/shared/arthurwidgets.cpp b/examples/widgets/painting/shared/arthurwidgets.cpp index 965660a3a3..bdac5de13c 100644 --- a/examples/widgets/painting/shared/arthurwidgets.cpp +++ b/examples/widgets/painting/shared/arthurwidgets.cpp @@ -60,6 +60,10 @@ #include <QTextBrowser> #include <QBoxLayout> #include <QRegularExpression> +#include <QOffscreenSurface> +#include <QOpenGLContext> +#include <QOpenGLPaintDevice> +#include <QOpenGLWindow> extern QPixmap cached(const QString &img); @@ -67,17 +71,12 @@ ArthurFrame::ArthurFrame(QWidget *parent) : QWidget(parent) , m_prefer_image(false) { -#ifdef QT_OPENGL_SUPPORT - glw = 0; +#if QT_CONFIG(opengl) + m_glWindow = nullptr; + m_glWidget = nullptr; m_use_opengl = false; - QGLFormat f = QGLFormat::defaultFormat(); - f.setSampleBuffers(true); - f.setStencil(true); - f.setAlpha(true); - f.setAlphaBufferSize(8); - QGLFormat::setDefaultFormat(f); #endif - m_document = 0; + m_document = nullptr; m_show_doc = false; m_tile = QPixmap(128, 128); @@ -94,37 +93,55 @@ ArthurFrame::ArthurFrame(QWidget *parent) } -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) void ArthurFrame::enableOpenGL(bool use_opengl) { if (m_use_opengl == use_opengl) return; - if (!glw && use_opengl) { - glw = new GLWidget(this); - glw->setAutoFillBackground(false); - glw->disableAutoBufferSwap(); + m_use_opengl = use_opengl; + + if (!m_glWindow && use_opengl) { + createGlWindow(); QApplication::postEvent(this, new QResizeEvent(size(), size())); } - m_use_opengl = use_opengl; if (use_opengl) { - glw->show(); + m_glWidget->show(); } else { - if (glw) - glw->hide(); + 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() - 1, height() - 1); + m_glWindow->create(); + m_glWidget = QWidget::createWindowContainer(m_glWindow, this); +} #endif + void ArthurFrame::paintEvent(QPaintEvent *e) { static QImage *static_image = 0; + QPainter painter; + if (preferImage() -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) && !m_use_opengl #endif ) { @@ -142,10 +159,12 @@ void ArthurFrame::paintEvent(QPaintEvent *e) painter.fillRect(0, height() - o, o, o, bg); painter.fillRect(width() - o, height() - o, o, o, bg); } else { -#ifdef QT_OPENGL_SUPPORT - if (m_use_opengl) { - painter.begin(glw); - painter.fillRect(QRectF(0, 0, glw->width(), glw->height()), palette().color(backgroundRole())); +#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); } @@ -196,7 +215,7 @@ void ArthurFrame::paintEvent(QPaintEvent *e) painter.drawPath(clipPath); if (preferImage() -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) && !m_use_opengl #endif ) { @@ -204,18 +223,17 @@ void ArthurFrame::paintEvent(QPaintEvent *e) painter.begin(this); painter.drawImage(e->rect(), *static_image, e->rect()); } - -#ifdef QT_OPENGL_SUPPORT - if (m_use_opengl && (inherits("PathDeformRenderer") || inherits("PathStrokeRenderer") || inherits("CompositionRenderer") || m_show_doc)) - glw->swapBuffers(); +#if QT_CONFIG(opengl) + if (m_use_opengl) + m_glWindow->update(); #endif } void ArthurFrame::resizeEvent(QResizeEvent *e) { -#ifdef QT_OPENGL_SUPPORT - if (glw) - glw->setGeometry(0, 0, e->size().width()-1, e->size().height()-1); +#if QT_CONFIG(opengl) + if (m_glWidget) + m_glWidget->setGeometry(0, 0, e->size().width()-1, e->size().height()-1); #endif QWidget::resizeEvent(e); } @@ -312,32 +330,40 @@ void ArthurFrame::showSource() QString contents; if (m_sourceFileName.isEmpty()) { - contents = QString("No source for widget: '%1'").arg(objectName()); + contents = tr("No source for widget: '%1'").arg(objectName()); } else { QFile f(m_sourceFileName); if (!f.open(QFile::ReadOnly)) - contents = QString("Could not open file: '%1'").arg(m_sourceFileName); + contents = tr("Could not open file: '%1'").arg(m_sourceFileName); else contents = f.readAll(); } - contents.replace('&', "&"); - contents.replace('<', "<"); - contents.replace('>', ">"); - - QStringList keywords; - keywords << "for " << "if " << "switch " << " int " << "#include " << "const" - << "void " << "uint " << "case " << "double " << "#define " << "static" - << "new" << "this"; - - foreach (QString keyword, keywords) + contents.replace(QLatin1Char('&'), QStringLiteral("&")); + contents.replace(QLatin1Char('<'), QStringLiteral("<")); + contents.replace(QLatin1Char('>'), QStringLiteral(">")); + + static const QString keywords[] = { + QStringLiteral("for "), QStringLiteral("if "), + QStringLiteral("switch "), QStringLiteral(" int "), + QStringLiteral("#include "), QStringLiteral("const"), + QStringLiteral("void "), QStringLiteral("uint "), + QStringLiteral("case "), QStringLiteral("double "), + QStringLiteral("#define "), QStringLiteral("static"), + QStringLiteral("new"), QStringLiteral("this") + }; + + for (const QString &keyword : keywords) contents.replace(keyword, QLatin1String("<font color=olive>") + keyword + QLatin1String("</font>")); - contents.replace("(int ", "(<font color=olive><b>int </b></font>"); + contents.replace(QStringLiteral("(int "), QStringLiteral("(<font color=olive><b>int </b></font>")); - QStringList ppKeywords; - ppKeywords << "#ifdef" << "#ifndef" << "#if" << "#endif" << "#else"; + static const QString ppKeywords[] = { + QStringLiteral("#ifdef"), QStringLiteral("#ifndef"), + QStringLiteral("#if"), QStringLiteral("#endif"), + QStringLiteral("#else") + }; - foreach (QString keyword, ppKeywords) + for (const QString &keyword : ppKeywords) contents.replace(keyword, QLatin1String("<font color=navy>") + keyword + QLatin1String("</font>")); contents.replace(QRegularExpression("(\\d\\d?)"), QLatin1String("<font color=navy>\\1</font>")); @@ -348,12 +374,10 @@ void ArthurFrame::showSource() QRegularExpression stringLiteralRe("(\".+?\")"); contents.replace(stringLiteralRe, QLatin1String("<font color=green>\\1</font>")); - QString html = contents; - html.prepend("<html><pre>"); - html.append("</pre></html>"); + const QString html = QStringLiteral("<html><pre>") + contents + QStringLiteral("</pre></html>"); QTextBrowser *sourceViewer = new QTextBrowser(0); - sourceViewer->setWindowTitle("Source: " + m_sourceFileName.mid(5)); + sourceViewer->setWindowTitle(tr("Source: %1").arg(m_sourceFileName.midRef(5))); sourceViewer->setParent(this, Qt::Dialog); sourceViewer->setAttribute(Qt::WA_DeleteOnClose); sourceViewer->setLineWrapMode(QTextEdit::NoWrap); diff --git a/examples/widgets/painting/shared/arthurwidgets.h b/examples/widgets/painting/shared/arthurwidgets.h index d6e1e44b3a..73e1310c0e 100644 --- a/examples/widgets/painting/shared/arthurwidgets.h +++ b/examples/widgets/painting/shared/arthurwidgets.h @@ -56,42 +56,13 @@ #include <QPushButton> #include <QGroupBox> -#if defined(QT_OPENGL_SUPPORT) -#include <QGLWidget> -#include <QEvent> -class GLWidget : public QGLWidget -{ -public: - GLWidget(QWidget *parent) - : QGLWidget(QGLFormat(QGL::SampleBuffers), parent) - { - setAttribute(Qt::WA_AcceptTouchEvents); - } - void disableAutoBufferSwap() { setAutoBufferSwap(false); } - void paintEvent(QPaintEvent *) override { parentWidget()->update(); } -protected: - bool event(QEvent *event) override - { - switch (event->type()) { - case QEvent::TouchBegin: - case QEvent::TouchUpdate: - case QEvent::TouchEnd: - event->ignore(); - return false; - break; - default: - break; - } - return QGLWidget::event(event); - } -}; -#endif - +QT_FORWARD_DECLARE_CLASS(QOpenGLWindow) QT_FORWARD_DECLARE_CLASS(QTextDocument) QT_FORWARD_DECLARE_CLASS(QTextEdit) QT_FORWARD_DECLARE_CLASS(QVBoxLayout) class ArthurFrame : public QWidget + { Q_OBJECT public: @@ -107,9 +78,8 @@ public: void loadSourceFile(const QString &fileName); bool preferImage() const { return m_prefer_image; } - -#if defined(QT_OPENGL_SUPPORT) - QGLWidget *glWidget() const { return glw; } +#if QT_CONFIG(opengl) + QOpenGLWindow *glWindow() const { return m_glWindow; } #endif public slots: @@ -117,7 +87,7 @@ public slots: void setDescriptionEnabled(bool enabled); void showSource(); -#if defined(QT_OPENGL_SUPPORT) +#if QT_CONFIG(opengl) void enableOpenGL(bool use_opengl); bool usesOpenGL() { return m_use_opengl; } #endif @@ -129,8 +99,10 @@ protected: void paintEvent(QPaintEvent *) override; void resizeEvent(QResizeEvent *) override; -#if defined(QT_OPENGL_SUPPORT) - GLWidget *glw; +#if QT_CONFIG(opengl) + virtual void createGlWindow(); + QOpenGLWindow *m_glWindow; + QWidget *m_glWidget; bool m_use_opengl; #endif QPixmap m_tile; diff --git a/examples/widgets/painting/shared/fbopaintdevice.cpp b/examples/widgets/painting/shared/fbopaintdevice.cpp new file mode 100644 index 0000000000..8207090cc8 --- /dev/null +++ b/examples/widgets/painting/shared/fbopaintdevice.cpp @@ -0,0 +1,113 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#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); + } +} + +QFboPaintDevice::~QFboPaintDevice() +{ + delete m_framebufferObject; + delete m_surface; +} + +void QFboPaintDevice::ensureActiveTarget() +{ + if (QOpenGLContext::currentContext() != context()) + context()->makeCurrent(m_surface); + + m_framebufferObject->bind(); +} + +GLuint QFboPaintDevice::takeTexture() +{ + // We have multisamples so we can't just forward takeTexture(). + QOpenGLFramebufferObject resolvedFbo(m_framebufferObject->size(), m_framebufferObject->attachment()); + QOpenGLFramebufferObject::blitFramebuffer(&resolvedFbo, m_framebufferObject); + return resolvedFbo.takeTexture(); +} + +QImage QFboPaintDevice::toImage() const +{ + QOpenGLContext* currentContext = QOpenGLContext::currentContext(); + QSurface* currentSurface = currentContext ? currentContext->surface() : 0; + + context()->makeCurrent(m_surface); + + QImage image = m_framebufferObject->toImage(!paintFlipped()); + + if (currentContext) + currentContext->makeCurrent(currentSurface); + else + context()->doneCurrent(); + + return image; +} diff --git a/examples/widgets/painting/shared/fbopaintdevice.h b/examples/widgets/painting/shared/fbopaintdevice.h new file mode 100644 index 0000000000..78451af895 --- /dev/null +++ b/examples/widgets/painting/shared/fbopaintdevice.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the demonstration applications of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#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&, 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 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; + QSurface* m_surface; +}; + +#endif // QT_NO_OPENGL + +#endif // QFBOPAINTDEVICE_H diff --git a/examples/widgets/painting/shared/hoverpoints.cpp b/examples/widgets/painting/shared/hoverpoints.cpp index c735c83f6a..74c78088ad 100644 --- a/examples/widgets/painting/shared/hoverpoints.cpp +++ b/examples/widgets/painting/shared/hoverpoints.cpp @@ -48,10 +48,6 @@ ** ****************************************************************************/ -#ifdef QT_OPENGL_SUPPORT -#include <QGLWidget> -#endif - #include "arthurwidgets.h" #include "hoverpoints.h" @@ -77,8 +73,8 @@ HoverPoints::HoverPoints(QWidget *widget, PointShape shape) m_editable = true; m_enabled = true; - connect(this, SIGNAL(pointsChanged(QPolygonF)), - m_widget, SLOT(update())); + connect(this, &HoverPoints::pointsChanged, + m_widget, QOverload<>::of(&QWidget::update)); } @@ -178,7 +174,7 @@ bool HoverPoints::eventFilter(QObject *object, QEvent *event) const QTouchEvent *const touchEvent = static_cast<const QTouchEvent*>(event); const QList<QTouchEvent::TouchPoint> points = touchEvent->touchPoints(); const qreal pointSize = qMax(m_pointSize.width(), m_pointSize.height()); - foreach (const QTouchEvent::TouchPoint &touchPoint, points) { + for (const QTouchEvent::TouchPoint &touchPoint : points) { const int id = touchPoint.id(); switch (touchPoint.state()) { case Qt::TouchPointPressed: @@ -269,11 +265,6 @@ bool HoverPoints::eventFilter(QObject *object, QEvent *event) QApplication::sendEvent(object, event); m_widget = that_widget; paintPoints(); -#ifdef QT_OPENGL_SUPPORT - ArthurFrame *af = qobject_cast<ArthurFrame *>(that_widget); - if (af && af->usesOpenGL()) - af->glWidget()->swapBuffers(); -#endif return true; } default: @@ -288,12 +279,14 @@ bool HoverPoints::eventFilter(QObject *object, QEvent *event) void HoverPoints::paintPoints() { QPainter p; -#ifdef QT_OPENGL_SUPPORT +#if QT_CONFIG(opengl) ArthurFrame *af = qobject_cast<ArthurFrame *>(m_widget); - if (af && af->usesOpenGL()) - p.begin(af->glWidget()); - else + 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 diff --git a/examples/widgets/painting/shared/shared.pri b/examples/widgets/painting/shared/shared.pri index 362cc6819c..cb08b00348 100644 --- a/examples/widgets/painting/shared/shared.pri +++ b/examples/widgets/painting/shared/shared.pri @@ -1,8 +1,8 @@ INCLUDEPATH += $$PWD -qtHaveModule(opengl)|qtConfig(opengles2) { - DEFINES += QT_OPENGL_SUPPORT - QT += opengl widgets +qtConfig(opengl) { + SOURCES += $$PWD/fbopaintdevice.cpp + HEADERS += $$PWD/fbopaintdevice.h } SOURCES += \ diff --git a/examples/widgets/painting/transformations/window.cpp b/examples/widgets/painting/transformations/window.cpp index d8babb2e00..8261c4e12e 100644 --- a/examples/widgets/painting/transformations/window.cpp +++ b/examples/widgets/painting/transformations/window.cpp @@ -79,8 +79,8 @@ Window::Window() operationComboBoxes[i]->addItem(tr("Scale to 75%")); operationComboBoxes[i]->addItem(tr("Translate by (50, 50)")); - connect(operationComboBoxes[i], SIGNAL(activated(int)), - this, SLOT(operationChanged())); + connect(operationComboBoxes[i], QOverload<int>::of(&QComboBox::activated), + this, &Window::operationChanged); layout->addWidget(transformedRenderAreas[i], 0, i + 1); layout->addWidget(operationComboBoxes[i], 1, i + 1); @@ -159,7 +159,8 @@ void Window::setupShapes() shapes.append(text); shapes.append(truck); - connect(shapeComboBox, SIGNAL(activated(int)), this, SLOT(shapeSelected(int))); + connect(shapeComboBox, QOverload<int>::of(&QComboBox::activated), + this, &Window::shapeSelected); } //! [7] |