diff options
Diffstat (limited to 'examples')
21 files changed, 866 insertions, 73 deletions
diff --git a/examples/corelib/mimetypes/mimetypebrowser/main.cpp b/examples/corelib/mimetypes/mimetypebrowser/main.cpp index daa92a2344..f4462df411 100644 --- a/examples/corelib/mimetypes/mimetypebrowser/main.cpp +++ b/examples/corelib/mimetypes/mimetypebrowser/main.cpp @@ -1,12 +1,22 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: +** 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 diff --git a/examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp b/examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp index edd48021f3..1d36b7e5ef 100644 --- a/examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp +++ b/examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp @@ -1,12 +1,22 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: +** 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 diff --git a/examples/corelib/mimetypes/mimetypebrowser/mainwindow.h b/examples/corelib/mimetypes/mimetypebrowser/mainwindow.h index 11b1f004a0..978cdec4f4 100644 --- a/examples/corelib/mimetypes/mimetypebrowser/mainwindow.h +++ b/examples/corelib/mimetypes/mimetypebrowser/mainwindow.h @@ -1,12 +1,22 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: +** 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 diff --git a/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.cpp b/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.cpp index 46e603621a..ee83de092d 100644 --- a/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.cpp +++ b/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.cpp @@ -1,12 +1,22 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: +** 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 diff --git a/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.h b/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.h index 0656e64e8f..6c537f4001 100644 --- a/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.h +++ b/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.h @@ -1,12 +1,22 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: +** 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 diff --git a/examples/network/network-chat/peermanager.cpp b/examples/network/network-chat/peermanager.cpp index 4e2ea3e6f3..c70cc5e56d 100644 --- a/examples/network/network-chat/peermanager.cpp +++ b/examples/network/network-chat/peermanager.cpp @@ -63,18 +63,14 @@ PeerManager::PeerManager(Client *client) this->client = client; QStringList envVariables; - envVariables << "USERNAME.*" << "USER.*" << "USERDOMAIN.*" - << "HOSTNAME.*" << "DOMAINNAME.*"; + envVariables << "USERNAME" << "USER" << "USERDOMAIN" + << "HOSTNAME" << "DOMAINNAME"; - QStringList environment = QProcess::systemEnvironment(); + QProcessEnvironment environment = QProcessEnvironment::systemEnvironment(); foreach (QString string, envVariables) { - int index = environment.indexOf(QRegularExpression(string)); - if (index != -1) { - QStringList stringList = environment.at(index).split('='); - if (stringList.size() == 2) { - username = stringList.at(1).toUtf8(); - break; - } + if (environment.contains(string)) { + username = environment.value(string).toUtf8(); + break; } } diff --git a/examples/network/network.pro b/examples/network/network.pro index 8334165ed9..d64b16c760 100644 --- a/examples/network/network.pro +++ b/examples/network/network.pro @@ -20,7 +20,7 @@ qtHaveModule(widgets) { multicastsender qtConfig(bearermanagement) { - qtConfig(process): SUBDIRS += network-chat + qtConfig(processenvironment): SUBDIRS += network-chat SUBDIRS += \ bearermonitor \ diff --git a/examples/opengl/computegles31/Qt-logo-medium.png b/examples/opengl/computegles31/Qt-logo-medium.png Binary files differnew file mode 100644 index 0000000000..a1ca1f1830 --- /dev/null +++ b/examples/opengl/computegles31/Qt-logo-medium.png diff --git a/examples/opengl/computegles31/computegles31.pro b/examples/opengl/computegles31/computegles31.pro new file mode 100644 index 0000000000..5b9d7e4387 --- /dev/null +++ b/examples/opengl/computegles31/computegles31.pro @@ -0,0 +1,9 @@ +HEADERS = $$PWD/glwindow.h + +SOURCES = $$PWD/glwindow.cpp \ + $$PWD/main.cpp + +RESOURCES += computegles31.qrc + +target.path = $$[QT_INSTALL_EXAMPLES]/opengl/computegles31 +INSTALLS += target diff --git a/examples/opengl/computegles31/computegles31.qrc b/examples/opengl/computegles31/computegles31.qrc new file mode 100644 index 0000000000..b99eb0a82e --- /dev/null +++ b/examples/opengl/computegles31/computegles31.qrc @@ -0,0 +1,5 @@ +<RCC> + <qresource prefix="/"> + <file>Qt-logo-medium.png</file> + </qresource> +</RCC> diff --git a/examples/opengl/computegles31/glwindow.cpp b/examples/opengl/computegles31/glwindow.cpp new file mode 100644 index 0000000000..7a14cba66d --- /dev/null +++ b/examples/opengl/computegles31/glwindow.cpp @@ -0,0 +1,446 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples 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 "glwindow.h" +#include <QImage> +#include <QOpenGLShaderProgram> +#include <QOpenGLContext> +#include <QOpenGLFunctions> +#include <QOpenGLExtraFunctions> +#include <QOpenGLVertexArrayObject> +#include <QtGui/qopengl.h> +#include <QDebug> +#include <QTimer> +#include <math.h> + +#ifndef GL_READ_WRITE +#define GL_READ_WRITE 0x88BA +#endif + +#ifndef GL_RGBA8 +#define GL_RGBA8 0x8058 +#endif + +#ifndef GL_SHADER_IMAGE_ACCESS_BARRIER_BIT +#define GL_SHADER_IMAGE_ACCESS_BARRIER_BIT 0x00000020 +#endif + +GLWindow::GLWindow() + : m_texImageInput(0), + m_texImageTmp(0), + m_texImageProcessed(0), + m_shaderDisplay(0), + m_shaderComputeV(0), + m_shaderComputeH(0), + m_blurRadius(0.0f), + m_animate(true), + m_vao(0) +{ + const float animationStart = 0.0; + const float animationEnd = 10.0; + const float animationLength = 1000; + + m_animationGroup = new QSequentialAnimationGroup(this); + m_animationGroup->setLoopCount(-1); + + m_animationForward = new QPropertyAnimation(this, QByteArrayLiteral("blurRadius")); + m_animationForward->setStartValue(animationStart); + m_animationForward->setEndValue(animationEnd); + m_animationForward->setDuration(animationLength); + m_animationGroup->addAnimation(m_animationForward); + + m_animationBackward = new QPropertyAnimation(this, QByteArrayLiteral("blurRadius")); + m_animationBackward->setStartValue(animationEnd); + m_animationBackward->setEndValue(animationStart); + m_animationBackward->setDuration(animationLength); + m_animationGroup->addAnimation(m_animationBackward); + + m_animationGroup->start(); +} + +GLWindow::~GLWindow() +{ + makeCurrent(); + delete m_texImageInput; + delete m_texImageProcessed; + delete m_texImageTmp; + delete m_shaderDisplay; + delete m_shaderComputeH; + delete m_shaderComputeV; + delete m_animationGroup; + delete m_animationForward; + delete m_animationBackward; + delete m_vao; +} + +void GLWindow::setBlurRadius(float blurRadius) +{ + int radius = int(blurRadius); + if (radius != m_blurRadius) { + m_blurRadius = radius; + update(); + } +} + +void GLWindow::setAnimating(bool animate) +{ + m_animate = animate; + if (animate) + m_animationGroup->start(); + else + m_animationGroup->stop(); +} + +void GLWindow::keyPressEvent(QKeyEvent *e) +{ + if (e->key() == Qt::Key_Space) { // pause + setAnimating(!m_animate); + } + update(); +} + + + + +static const char *vsDisplaySource = + "const vec4 vertices[4] = vec4[4] (\n" + " vec4( -1.0, 1.0, 0.0, 1.0),\n" + " vec4( -1.0, -1.0, 0.0, 1.0),\n" + " vec4( 1.0, 1.0, 0.0, 1.0),\n" + " vec4( 1.0, -1.0, 0.0, 1.0)\n" + ");\n" + "const vec2 texCoords[4] = vec2[4] (\n" + " vec2( 0.0, 1.0),\n" + " vec2( 0.0, 0.0),\n" + " vec2( 1.0, 1.0),\n" + " vec2( 1.0, 0.0)\n" + ");\n" + "out vec2 texCoord;\n" + "uniform mat4 matProjection;\n" + "uniform vec2 imageRatio;\n" + "void main() {\n" + " gl_Position = matProjection * ( vertices[gl_VertexID] * vec4(imageRatio,0,1) );\n" + " texCoord = texCoords[gl_VertexID];\n" + "}\n"; + +static const char *fsDisplaySource = + "in lowp vec2 texCoord; \n" + "uniform sampler2D samImage; \n" + "layout(location = 0) out lowp vec4 color;\n" + "void main() {\n" + " lowp vec4 texColor = texture(samImage,texCoord);\n" + " color = vec4(texColor.rgb, 1.0);\n" + "}\n"; + +static const char *csComputeSourceV = + "#define COMPUTEPATCHSIZE 32 \n" + "#define IMGFMT rgba8 \n" + "layout (local_size_x = COMPUTEPATCHSIZE, local_size_y = COMPUTEPATCHSIZE) in;\n" + "layout(binding=0, IMGFMT) uniform readonly highp image2D inputImage; // Use a sampler to improve performance \n" + "layout(binding=1, IMGFMT) uniform writeonly highp image2D resultImage;\n" + "uniform int radius;\n" + "const float cutoff = 2.2;\n" + + "float expFactor() { // a function, otherwise MESA produces error: initializer of global variable `expFactor' must be a constant expression\n" + " float sigma = clamp(float(radius) / cutoff,0.02,100.0);\n" + " return 1.0 / (2.0 * sigma * sigma);\n" + "}\n" + + "float gaussian(float distance, float expfactor) {\n" + " return exp( -(distance * distance) * expfactor);\n" + "}\n" + + "void main() {\n" + " ivec2 imgSize = imageSize(resultImage);\n" + " int x = int(gl_GlobalInvocationID.x);\n" + " int y = int(gl_GlobalInvocationID.y);\n" + " if ( (x >= imgSize.x) || (y >= imgSize.y) ) return;\n" + " vec4 sumPixels = vec4(0.0);\n" + " float sumWeights = 0.0;\n" + " int left = clamp(x - radius, 0, imgSize.x - 1);\n" + " int right = clamp(x + radius, 0, imgSize.x - 1);\n" + " int top = clamp(y - radius, 0, imgSize.y - 1);\n" + " int bottom = clamp(y + radius, 0, imgSize.y - 1);\n" + " float expfactor = expFactor();\n" + " for (int iY = top; iY <= bottom; iY++) {\n" + " float dy = float(abs(iY - y));\n" + " vec4 imgValue = imageLoad(inputImage, ivec2(x,iY));\n" + " float weight = gaussian(dy, expfactor);\n" + " sumWeights += weight;\n" + " sumPixels += (imgValue * weight);\n" + " }\n" + " sumPixels /= sumWeights;\n" + " imageStore(resultImage, ivec2(x,y), sumPixels);\n" + "}\n"; + +static const char *csComputeSourceH = + "#define COMPUTEPATCHSIZE 32 \n" + "#define IMGFMT rgba8 \n" + "layout (local_size_x = COMPUTEPATCHSIZE, local_size_y = COMPUTEPATCHSIZE) in;\n" + "layout(binding=0, IMGFMT) uniform readonly highp image2D inputImage; // Use a sampler to improve performance \n" + "layout(binding=1, IMGFMT) uniform writeonly highp image2D resultImage;\n" + "uniform int radius;\n" + "const float cutoff = 2.2;\n" + + "float expFactor() { // a function, otherwise MESA produces error: initializer of global variable `expFactor' must be a constant expression\n" + " float sigma = clamp(float(radius) / cutoff,0.02,100.0);\n" + " return 1.0 / (2.0 * sigma * sigma);\n" + "}\n" + + "float gaussian(float distance, float expfactor) {\n" + " return exp( -(distance * distance) * expfactor);\n" + "}\n" + + "void main() {\n" + " ivec2 imgSize = imageSize(resultImage);\n" + " int x = int(gl_GlobalInvocationID.x);\n" + " int y = int(gl_GlobalInvocationID.y);\n" + " if ( (x >= imgSize.x) || (y >= imgSize.y) ) return;\n" + " vec4 sumPixels = vec4(0.0);\n" + " float sumWeights = 0.0;\n" + " int left = clamp(x - radius, 0, imgSize.x - 1);\n" + " int right = clamp(x + radius, 0, imgSize.x - 1);\n" + " int top = clamp(y - radius, 0, imgSize.y - 1);\n" + " int bottom = clamp(y + radius, 0, imgSize.y - 1);\n" + " float expfactor = expFactor();\n" + " for (int iX = left; iX <= right; iX++) {\n" + " float dx = float(abs(iX - x));\n" + " vec4 imgValue = imageLoad(inputImage, ivec2(iX,y));\n" + " float weight = gaussian(dx, expfactor);\n" + " sumWeights += weight;\n" + " sumPixels += (imgValue * weight);\n" + " }\n" + " sumPixels /= sumWeights;\n" + " imageStore(resultImage, ivec2(x,y), sumPixels);\n" + "}\n"; + + + +QByteArray versionedShaderCode(const char *src) +{ + QByteArray versionedSrc; + + if (QOpenGLContext::currentContext()->isOpenGLES()) + versionedSrc.append(QByteArrayLiteral("#version 310 es\n")); + else + versionedSrc.append(QByteArrayLiteral("#version 430 core\n")); + + versionedSrc.append(src); + return versionedSrc; +} + +void computeProjection(int winWidth, int winHeight, int imgWidth, int imgHeight, QMatrix4x4 &outProjection, QSizeF &outQuadSize) +{ + float ratioImg = float(imgWidth) / float(imgHeight); + float ratioCanvas = float(winWidth) / float(winHeight); + + float correction = ratioImg / ratioCanvas; + float rescaleFactor = 1.0f; + float quadWidth = 1.0f; + float quadHeight = 1.0f; + + if (correction < 1.0f) // canvas larger than image -- height = 1.0, vertical black bands + { + quadHeight = 1.0f; + quadWidth = 1.0f * ratioImg; + rescaleFactor = ratioCanvas; + correction = 1.0f / rescaleFactor; + } + else // image larger than canvas -- width = 1.0, horizontal black bands + { + quadWidth = 1.0f; + quadHeight = 1.0f / ratioImg; + correction = 1.0f / ratioCanvas; + } + + const float frustumWidth = 1.0f * rescaleFactor; + const float frustumHeight = 1.0f * rescaleFactor * correction; + + outProjection = QMatrix4x4(); + outProjection.ortho( + -frustumWidth, + frustumWidth, + -frustumHeight, + frustumHeight, + -1.0f, + 1.0f); + outQuadSize = QSizeF(quadWidth,quadHeight); +} + +void GLWindow::initializeGL() +{ + QOpenGLContext *ctx = QOpenGLContext::currentContext(); + qDebug() << "Got a " + << ctx->format().majorVersion() + << "." + << ctx->format().minorVersion() + << ((ctx->format().renderableType() == QSurfaceFormat::OpenGLES) ? (" GLES") : (" GL")) + << " context"; + + if (m_texImageInput) { + delete m_texImageInput; + m_texImageInput = 0; + } + QImage img(":/Qt-logo-medium.png"); + Q_ASSERT(!img.isNull()); + m_texImageInput = new QOpenGLTexture(img.convertToFormat(QImage::Format_RGBA8888).mirrored()); + + if (m_texImageTmp) { + delete m_texImageTmp; + m_texImageTmp = 0; + } + m_texImageTmp = new QOpenGLTexture(QOpenGLTexture::Target2D); + m_texImageTmp->setFormat(m_texImageInput->format()); + m_texImageTmp->setSize(m_texImageInput->width(),m_texImageInput->height()); + m_texImageTmp->allocateStorage(QOpenGLTexture::RGBA,QOpenGLTexture::UInt8); // WTF? + + if (m_texImageProcessed) { + delete m_texImageProcessed; + m_texImageProcessed = 0; + } + m_texImageProcessed = new QOpenGLTexture(QOpenGLTexture::Target2D); + m_texImageProcessed->setFormat(m_texImageInput->format()); + m_texImageProcessed->setSize(m_texImageInput->width(),m_texImageInput->height()); + m_texImageProcessed->allocateStorage(QOpenGLTexture::RGBA,QOpenGLTexture::UInt8); + + m_texImageProcessed->setMagnificationFilter(QOpenGLTexture::Linear); + m_texImageProcessed->setMinificationFilter(QOpenGLTexture::Linear); + m_texImageProcessed->setWrapMode(QOpenGLTexture::ClampToEdge); + + if (m_shaderDisplay) { + delete m_shaderDisplay; + m_shaderDisplay = 0; + } + m_shaderDisplay = new QOpenGLShaderProgram; + // Prepend the correct version directive to the sources. The rest is the + // same, thanks to the common GLSL syntax. + m_shaderDisplay->addShaderFromSourceCode(QOpenGLShader::Vertex, versionedShaderCode(vsDisplaySource)); + m_shaderDisplay->addShaderFromSourceCode(QOpenGLShader::Fragment, versionedShaderCode(fsDisplaySource)); + m_shaderDisplay->link(); + + if (m_shaderComputeV) { + delete m_shaderComputeV; + m_shaderComputeV = 0; + } + m_shaderComputeV = new QOpenGLShaderProgram; + m_shaderComputeV->addShaderFromSourceCode(QOpenGLShader::Compute, versionedShaderCode(csComputeSourceV)); + m_shaderComputeV->link(); + + if (m_shaderComputeH) { + delete m_shaderComputeH; + m_shaderComputeH = 0; + } + m_shaderComputeH = new QOpenGLShaderProgram; + m_shaderComputeH->addShaderFromSourceCode(QOpenGLShader::Compute, versionedShaderCode(csComputeSourceH)); + m_shaderComputeH->link(); + + // Create a VAO. Not strictly required for ES 3, but it is for plain OpenGL core context. + m_vao = new QOpenGLVertexArrayObject; + m_vao->create(); +} + +void GLWindow::resizeGL(int w, int h) +{ + computeProjection(w,h,m_texImageInput->width(),m_texImageInput->height(),m_proj,m_quadSize); +} + +QSize getWorkGroups(int workGroupSize, const QSize &imageSize) +{ + int x = imageSize.width(); + x = (x % workGroupSize) ? (x / workGroupSize) + 1 : (x / workGroupSize); + int y = imageSize.height(); + y = (y % workGroupSize) ? (y / workGroupSize) + 1 : (y / workGroupSize); + return QSize(x,y); +} + +void GLWindow::paintGL() +{ + // Now use QOpenGLExtraFunctions instead of QOpenGLFunctions as we want to + // do more than what GL(ES) 2.0 offers. + QOpenGLExtraFunctions *f = QOpenGLContext::currentContext()->extraFunctions(); + + + // Process input image + QSize workGroups = getWorkGroups( 32, QSize(m_texImageInput->width(), m_texImageInput->height())); + // Pass 1 + f->glBindImageTexture(0, m_texImageInput->textureId(), 0, 0, 0, GL_READ_WRITE, GL_RGBA8); + f->glBindImageTexture(1, m_texImageTmp->textureId(), 0, 0, 0, GL_READ_WRITE, GL_RGBA8); + m_shaderComputeV->bind(); + m_shaderComputeV->setUniformValue("radius",m_blurRadius); + f->glDispatchCompute(workGroups.width(),workGroups.height(),1); + f->glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + m_shaderComputeV->release(); + // Pass 2 + f->glBindImageTexture(0, m_texImageTmp->textureId(), 0, 0, 0, GL_READ_WRITE, GL_RGBA8); + f->glBindImageTexture(1, m_texImageProcessed->textureId(), 0, 0, 0, GL_READ_WRITE, GL_RGBA8); + m_shaderComputeH->bind(); + m_shaderComputeH->setUniformValue("radius",m_blurRadius); + f->glDispatchCompute(workGroups.width(),workGroups.height(),1); + f->glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); + m_shaderComputeH->release(); + // Compute cleanup + f->glBindImageTexture(0, 0, 0, 0, 0, GL_READ_WRITE, GL_RGBA8); + f->glBindImageTexture(1, 0, 0, 0, 0, GL_READ_WRITE, GL_RGBA8); + + // Display processed image + f->glClearColor(0, 0, 0, 1); + f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + m_texImageProcessed->bind(0); + m_shaderDisplay->bind(); + m_shaderDisplay->setUniformValue("matProjection",m_proj); + m_shaderDisplay->setUniformValue("imageRatio",m_quadSize); + m_shaderDisplay->setUniformValue("samImage",0); + m_vao->bind(); + f->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + m_vao->release(); + m_shaderDisplay->release(); + m_texImageProcessed->release(0); +} + diff --git a/examples/opengl/computegles31/glwindow.h b/examples/opengl/computegles31/glwindow.h new file mode 100644 index 0000000000..877f7b732e --- /dev/null +++ b/examples/opengl/computegles31/glwindow.h @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples 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 GLWIDGET_H +#define GLWIDGET_H + +#include <QOpenGLWindow> +#include <QOpenGLTexture> +#include <QMatrix4x4> +#include <QVector3D> +#include <QKeyEvent> +#include <QPropertyAnimation> +#include <QSequentialAnimationGroup> +#include <QRectF> + +QT_BEGIN_NAMESPACE + +class QOpenGLTexture; +class QOpenGLShaderProgram; +class QOpenGLBuffer; +class QOpenGLVertexArrayObject; + +QT_END_NAMESPACE + +class GLWindow : public QOpenGLWindow +{ + Q_OBJECT + Q_PROPERTY(float blurRadius READ blurRadius WRITE setBlurRadius) + +public: + GLWindow(); + ~GLWindow(); + + void initializeGL() override; + void resizeGL(int w, int h) override; + void paintGL() override; + + float blurRadius() const { return m_blurRadius; } + void setBlurRadius(float blurRadius); + +protected: + void keyPressEvent(QKeyEvent *e) Q_DECL_OVERRIDE; + void setAnimating(bool animate); + +private: + QPropertyAnimation *m_animationForward; + QPropertyAnimation *m_animationBackward; + QSequentialAnimationGroup *m_animationGroup; + QOpenGLTexture *m_texImageInput; + QOpenGLTexture *m_texImageTmp; + QOpenGLTexture *m_texImageProcessed; + QOpenGLShaderProgram *m_shaderDisplay; + QOpenGLShaderProgram *m_shaderComputeV; + QOpenGLShaderProgram *m_shaderComputeH; + QMatrix4x4 m_proj; + QSizeF m_quadSize; + + int m_blurRadius; + bool m_animate; + QOpenGLVertexArrayObject *m_vao; +}; + +#endif diff --git a/examples/opengl/computegles31/main.cpp b/examples/opengl/computegles31/main.cpp new file mode 100644 index 0000000000..88f0f36115 --- /dev/null +++ b/examples/opengl/computegles31/main.cpp @@ -0,0 +1,148 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the examples 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 <QGuiApplication> +#include <QSurfaceFormat> +#include <QOffscreenSurface> +#include <QOpenGLContext> +#include <QDebug> +#include <QPair> +#include "glwindow.h" + +bool OGLSupports(int major, int minor, bool gles = false, QSurfaceFormat::OpenGLContextProfile profile = QSurfaceFormat::NoProfile) +{ + QOpenGLContext ctx; + QSurfaceFormat fmt; + fmt.setVersion(major, minor); + if (gles) { + fmt.setRenderableType(QSurfaceFormat::OpenGLES); + } else { + fmt.setRenderableType(QSurfaceFormat::OpenGL); + fmt.setProfile(profile); + } + + ctx.setFormat(fmt); + ctx.create(); + if (!ctx.isValid()) + return false; + int ctxMajor = ctx.format().majorVersion(); + int ctxMinor = ctx.format().minorVersion(); + bool isGles = (ctx.format().renderableType() == QSurfaceFormat::OpenGLES); + + if (isGles != gles) return false; + if (ctxMajor < major) return false; + if (ctxMajor == major && ctxMinor < minor) + return false; + if (!gles && ctx.format().profile() != profile) + return false; + return true; +} + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc, argv); + + qDebug() << "Support for GL 2.0 noprof "<<( OGLSupports(2,0,false) ? "yes" : "no"); + qDebug() << "Support for GL 2.0 core "<<( OGLSupports(2,0,false, QSurfaceFormat::CoreProfile) ? "yes" : "no"); + qDebug() << "Support for GL 2.0 compat "<<( OGLSupports(2,0,false, QSurfaceFormat::CompatibilityProfile) ? "yes" : "no"); + qDebug() << "Support for GL 2.1 noprof "<<( OGLSupports(2,1,false) ? "yes" : "no"); + qDebug() << "Support for GL 2.1 core "<<( OGLSupports(2,1,false, QSurfaceFormat::CoreProfile) ? "yes" : "no"); + qDebug() << "Support for GL 2.1 compat "<<( OGLSupports(2,1,false, QSurfaceFormat::CompatibilityProfile) ? "yes" : "no"); + qDebug() << "Support for GL 3.0 noprof "<<( OGLSupports(3,0,false) ? "yes" : "no"); + qDebug() << "Support for GL 3.0 core "<<( OGLSupports(3,0,false, QSurfaceFormat::CoreProfile) ? "yes" : "no"); + qDebug() << "Support for GL 3.0 compat "<<( OGLSupports(3,0,false, QSurfaceFormat::CompatibilityProfile) ? "yes" : "no"); + qDebug() << "Support for GL 3.1 noprof "<<( OGLSupports(3,1,false) ? "yes" : "no"); + qDebug() << "Support for GL 3.1 core "<<( OGLSupports(3,1,false, QSurfaceFormat::CoreProfile) ? "yes" : "no"); + qDebug() << "Support for GL 3.1 compat "<<( OGLSupports(3,1,false, QSurfaceFormat::CompatibilityProfile) ? "yes" : "no"); + qDebug() << "Support for GL 3.2 core "<<( OGLSupports(3,2,false,QSurfaceFormat::CoreProfile) ? "yes" : "no"); + qDebug() << "Support for GL 3.2 compat "<<( OGLSupports(3,2,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no"); + qDebug() << "Support for GL 3.3 core "<<( OGLSupports(3,3,false,QSurfaceFormat::CoreProfile) ? "yes" : "no"); + qDebug() << "Support for GL 3.3 compat "<<( OGLSupports(3,3,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no"); + qDebug() << "Support for GL 4.0 core "<<( OGLSupports(4,0,false,QSurfaceFormat::CoreProfile) ? "yes" : "no"); + qDebug() << "Support for GL 4.0 compat "<<( OGLSupports(4,0,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no"); + qDebug() << "Support for GL 4.1 core "<<( OGLSupports(4,1,false,QSurfaceFormat::CoreProfile) ? "yes" : "no"); + qDebug() << "Support for GL 4.1 compat "<<( OGLSupports(4,1,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no"); + qDebug() << "Support for GL 4.2 core "<<( OGLSupports(4,2,false,QSurfaceFormat::CoreProfile) ? "yes" : "no"); + qDebug() << "Support for GL 4.2 compat "<<( OGLSupports(4,2,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no"); + qDebug() << "Support for GL 4.3 core "<<( OGLSupports(4,3,false,QSurfaceFormat::CoreProfile) ? "yes" : "no"); + qDebug() << "Support for GL 4.3 compat "<<( OGLSupports(4,3,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no"); + qDebug() << "Support for GL 4.4 core "<<( OGLSupports(4,4,false,QSurfaceFormat::CoreProfile) ? "yes" : "no"); + qDebug() << "Support for GL 4.4 compat "<<( OGLSupports(4,4,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no"); + qDebug() << "Support for GL 4.5 core "<<( OGLSupports(4,5,false,QSurfaceFormat::CoreProfile) ? "yes" : "no"); + qDebug() << "Support for GL 4.5 compat "<<( OGLSupports(4,5,false,QSurfaceFormat::CompatibilityProfile) ? "yes" : "no"); + qDebug() << "Support for GLES 2.0 "<<( OGLSupports(2,0,true) ? "yes" : "no"); + qDebug() << "Support for GLES 3.0 "<<( OGLSupports(3,0,true) ? "yes" : "no"); + qDebug() << "Support for GLES 3.1 "<<( OGLSupports(3,1,true) ? "yes" : "no"); + qDebug() << "Support for GLES 3.2 "<<( OGLSupports(3,2,true) ? "yes" : "no"); + + QSurfaceFormat fmt; + fmt.setDepthBufferSize(24); + + // Request OpenGL ES 3.1 context, as this is a GLES example. If not available, go for OpenGL 4.3 core. + if (OGLSupports(3,1,true)) { + qDebug("Requesting 3.1 GLES context"); + fmt.setVersion(3, 1); + fmt.setRenderableType(QSurfaceFormat::OpenGLES); + } else if (OGLSupports(4,3,false,QSurfaceFormat::CoreProfile)) { + qDebug("Requesting 4.3 core context"); + fmt.setVersion(4, 3); + fmt.setRenderableType(QSurfaceFormat::OpenGL); + fmt.setProfile(QSurfaceFormat::CoreProfile); + } else { + qWarning("Error: This system does not support OpenGL Compute Shaders! Exiting."); + return -1; + } + QSurfaceFormat::setDefaultFormat(fmt); + + GLWindow glWindow; + glWindow.showMaximized(); + + return app.exec(); +} diff --git a/examples/opengl/opengl.pro b/examples/opengl/opengl.pro index a102e08733..ef44201494 100644 --- a/examples/opengl/opengl.pro +++ b/examples/opengl/opengl.pro @@ -14,7 +14,8 @@ qtHaveModule(widgets) { qopenglwidget \ cube \ textures \ - hellogles3 + hellogles3 \ + computegles31 } EXAMPLE_FILES += \ diff --git a/examples/widgets/desktop/systray/window.cpp b/examples/widgets/desktop/systray/window.cpp index 5e98996ff3..05944c92a7 100644 --- a/examples/widgets/desktop/systray/window.cpp +++ b/examples/widgets/desktop/systray/window.cpp @@ -160,10 +160,16 @@ void Window::iconActivated(QSystemTrayIcon::ActivationReason reason) void Window::showMessage() { showIconCheckBox->setChecked(true); - QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon( + QSystemTrayIcon::MessageIcon msgIcon = QSystemTrayIcon::MessageIcon( typeComboBox->itemData(typeComboBox->currentIndex()).toInt()); - trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon, + if (msgIcon == QSystemTrayIcon::NoIcon) { + QIcon icon(iconComboBox->itemIcon(iconComboBox->currentIndex())); + trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), icon, durationSpinBox->value() * 1000); + } else { + trayIcon->showMessage(titleEdit->text(), bodyEdit->toPlainText(), msgIcon, + durationSpinBox->value() * 1000); + } } //! [5] @@ -215,6 +221,8 @@ void Window::createMessageGroupBox() typeComboBox->addItem(style()->standardIcon( QStyle::SP_MessageBoxCritical), tr("Critical"), QSystemTrayIcon::Critical); + typeComboBox->addItem(QIcon(), tr("Custom icon"), + QSystemTrayIcon::NoIcon); typeComboBox->setCurrentIndex(1); durationLabel = new QLabel(tr("Duration:")); diff --git a/examples/widgets/mac/qmacnativewidget/main.mm b/examples/widgets/mac/qmacnativewidget/main.mm index ad1e28fdd9..a7c4fe1a21 100644 --- a/examples/widgets/mac/qmacnativewidget/main.mm +++ b/examples/widgets/mac/qmacnativewidget/main.mm @@ -119,12 +119,11 @@ QApplication *qtApp = 0; QVBoxLayout *vlayout = new QVBoxLayout(); vlayout->addLayout(hlayout); - //RedWidget * redWidget = new RedWidget; - //vlayout->addWidget(redWidget); + RedWidget *redWidget = new RedWidget; + vlayout->addWidget(redWidget); nativeWidget->setLayout(vlayout); - // show() must be called on nativeWiget to get the widgets int he correct state. nativeWidget->show(); diff --git a/examples/widgets/painting/fontsampler/mainwindow.cpp b/examples/widgets/painting/fontsampler/mainwindow.cpp index 88b23d5cfb..192ffbd369 100644 --- a/examples/widgets/painting/fontsampler/mainwindow.cpp +++ b/examples/widgets/painting/fontsampler/mainwindow.cpp @@ -212,9 +212,9 @@ QMap<QString, StyleItems> MainWindow::currentPageMap() return pageMap; } -#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog) void MainWindow::on_printAction_triggered() { +#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog) pageMap = currentPageMap(); if (pageMap.count() == 0) @@ -231,10 +231,12 @@ void MainWindow::on_printAction_triggered() printer.setFromTo(1, pageMap.keys().count()); printDocument(&printer); +#endif } void MainWindow::printDocument(QPrinter *printer) { +#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog) printer->setFromTo(1, pageMap.count()); QProgressDialog progress(tr("Preparing font samples..."), tr("&Cancel"), @@ -263,10 +265,12 @@ void MainWindow::printDocument(QPrinter *printer) } painter.end(); +#endif } void MainWindow::on_printPreviewAction_triggered() { +#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog) pageMap = currentPageMap(); if (pageMap.count() == 0) @@ -277,10 +281,12 @@ void MainWindow::on_printPreviewAction_triggered() connect(&preview, SIGNAL(paintRequested(QPrinter*)), this, SLOT(printDocument(QPrinter*))); preview.exec(); +#endif } 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]; @@ -343,5 +349,5 @@ void MainWindow::printPage(int index, QPainter *painter, QPrinter *printer) } painter->restore(); -} #endif +} diff --git a/examples/widgets/painting/fontsampler/mainwindow.h b/examples/widgets/painting/fontsampler/mainwindow.h index 21e76f1a62..8f59e1021b 100644 --- a/examples/widgets/painting/fontsampler/mainwindow.h +++ b/examples/widgets/painting/fontsampler/mainwindow.h @@ -78,13 +78,10 @@ public slots: void on_clearAction_triggered(); void on_markAction_triggered(); void on_unmarkAction_triggered(); - -#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog) void on_printAction_triggered(); void on_printPreviewAction_triggered(); void printDocument(QPrinter *printer); void printPage(int index, QPainter *painter, QPrinter *printer); -#endif void showFont(QTreeWidgetItem *item); void updateStyles(QTreeWidgetItem *item, int column); diff --git a/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp b/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp index 7a67c763d8..371e2bda4b 100644 --- a/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp +++ b/examples/widgets/tools/regularexpression/regularexpressiondialog.cpp @@ -109,6 +109,19 @@ RegularExpressionDialog::RegularExpressionDialog(QWidget *parent) refresh(); } +void RegularExpressionDialog::setResultUiEnabled(bool enabled) +{ + matchDetailsTreeWidget->setEnabled(enabled); + namedGroupsTreeWidget->setEnabled(enabled); +} + +static void setTextColor(QWidget *widget, const QColor &color) +{ + QPalette palette = widget->palette(); + palette.setColor(QPalette::Text, color); + widget->setPalette(palette); +} + void RegularExpressionDialog::refresh() { setUpdatesEnabled(false); @@ -125,7 +138,30 @@ void RegularExpressionDialog::refresh() escaped.append(QLatin1Char('"')); escapedPatternLineEdit->setText(escaped); + setTextColor(patternLineEdit, subjectTextEdit->palette().color(QPalette::Text)); + matchDetailsTreeWidget->clear(); + namedGroupsTreeWidget->clear(); + regexpStatusLabel->setText(QString()); + + if (pattern.isEmpty()) { + setResultUiEnabled(false); + setUpdatesEnabled(true); + return; + } + QRegularExpression rx(pattern); + if (!rx.isValid()) { + setTextColor(patternLineEdit, Qt::red); + regexpStatusLabel->setText(tr("Invalid: syntax error at position %1 (%2)") + .arg(rx.patternErrorOffset()) + .arg(rx.errorString())); + setResultUiEnabled(false); + setUpdatesEnabled(true); + return; + } + + setResultUiEnabled(true); + QRegularExpression::MatchType matchType = matchTypeComboBox->currentData().value<QRegularExpression::MatchType>(); QRegularExpression::PatternOptions patternOptions = QRegularExpression::NoPatternOption; QRegularExpression::MatchOptions matchOptions = QRegularExpression::NoMatchOption; @@ -156,60 +192,40 @@ void RegularExpressionDialog::refresh() rx.setPatternOptions(patternOptions); - QPalette palette = patternLineEdit->palette(); - if (rx.isValid()) - palette.setColor(QPalette::Text, subjectTextEdit->palette().color(QPalette::Text)); - else - palette.setColor(QPalette::Text, Qt::red); - patternLineEdit->setPalette(palette); - - matchDetailsTreeWidget->clear(); - matchDetailsTreeWidget->setEnabled(rx.isValid()); + const int capturingGroupsCount = rx.captureCount() + 1; - if (rx.isValid()) { - const int capturingGroupsCount = rx.captureCount() + 1; + QRegularExpressionMatchIterator iterator = rx.globalMatch(text, offsetSpinBox->value(), matchType, matchOptions); + int i = 0; - QRegularExpressionMatchIterator iterator = rx.globalMatch(text, offsetSpinBox->value(), matchType, matchOptions); - int i = 0; + while (iterator.hasNext()) { + QRegularExpressionMatch match = iterator.next(); - while (iterator.hasNext()) { - QRegularExpressionMatch match = iterator.next(); + QTreeWidgetItem *matchDetailTopItem = new QTreeWidgetItem(matchDetailsTreeWidget); + matchDetailTopItem->setText(0, QString::number(i)); - QTreeWidgetItem *matchDetailTopItem = new QTreeWidgetItem(matchDetailsTreeWidget); - matchDetailTopItem->setText(0, QString::number(i)); - - for (int captureGroupIndex = 0; captureGroupIndex < capturingGroupsCount; ++captureGroupIndex) { - QTreeWidgetItem *matchDetailItem = new QTreeWidgetItem(matchDetailTopItem); - matchDetailItem->setText(1, QString::number(captureGroupIndex)); - matchDetailItem->setText(2, match.captured(captureGroupIndex)); - } - - ++i; + for (int captureGroupIndex = 0; captureGroupIndex < capturingGroupsCount; ++captureGroupIndex) { + QTreeWidgetItem *matchDetailItem = new QTreeWidgetItem(matchDetailTopItem); + matchDetailItem->setText(1, QString::number(captureGroupIndex)); + matchDetailItem->setText(2, match.captured(captureGroupIndex)); } + + ++i; } matchDetailsTreeWidget->expandAll(); - namedGroupsTreeWidget->clear(); - namedGroupsTreeWidget->setEnabled(rx.isValid()); - - if (rx.isValid()) { - regexpStatusLabel->setText(tr("Valid")); + regexpStatusLabel->setText(tr("Valid")); - const QStringList namedCaptureGroups = rx.namedCaptureGroups(); - for (int i = 0; i < namedCaptureGroups.size(); ++i) { - const QString currentNamedCaptureGroup = namedCaptureGroups.at(i); + const QStringList namedCaptureGroups = rx.namedCaptureGroups(); + for (int i = 0; i < namedCaptureGroups.size(); ++i) { + const QString currentNamedCaptureGroup = namedCaptureGroups.at(i); - QTreeWidgetItem *namedGroupItem = new QTreeWidgetItem(namedGroupsTreeWidget); - namedGroupItem->setText(0, QString::number(i)); - namedGroupItem->setText(1, currentNamedCaptureGroup.isNull() ? tr("<no name>") : currentNamedCaptureGroup); - } - } else { - regexpStatusLabel->setText(tr("Invalid: syntax error at position %1 (%2)") - .arg(rx.patternErrorOffset()) - .arg(rx.errorString())); + QTreeWidgetItem *namedGroupItem = new QTreeWidgetItem(namedGroupsTreeWidget); + namedGroupItem->setText(0, QString::number(i)); + namedGroupItem->setText(1, currentNamedCaptureGroup.isNull() ? tr("<no name>") : currentNamedCaptureGroup); } + setUpdatesEnabled(true); } diff --git a/examples/widgets/tools/regularexpression/regularexpressiondialog.h b/examples/widgets/tools/regularexpression/regularexpressiondialog.h index e75abc83e8..f7d64085fc 100644 --- a/examples/widgets/tools/regularexpression/regularexpressiondialog.h +++ b/examples/widgets/tools/regularexpression/regularexpressiondialog.h @@ -78,6 +78,7 @@ private: void setupUi(); QWidget *setupLeftUi(); QWidget *setupRightUi(); + void setResultUiEnabled(bool enabled); QLineEdit *patternLineEdit; QLineEdit *escapedPatternLineEdit; diff --git a/examples/widgets/widgets/tablet/tabletcanvas.cpp b/examples/widgets/widgets/tablet/tabletcanvas.cpp index ba797500e9..03e05e42f6 100644 --- a/examples/widgets/widgets/tablet/tabletcanvas.cpp +++ b/examples/widgets/widgets/tablet/tabletcanvas.cpp @@ -67,6 +67,7 @@ TabletCanvas::TabletCanvas() resize(500, 500); initPixmap(); setAutoFillBackground(true); + setAttribute(Qt::WA_TabletTracking); } void TabletCanvas::initPixmap() |