diff options
Diffstat (limited to 'examples/corelib/threads/mandelbrot')
-rw-r--r-- | examples/corelib/threads/mandelbrot/CMakeLists.txt | 54 | ||||
-rw-r--r-- | examples/corelib/threads/mandelbrot/main.cpp | 78 | ||||
-rw-r--r-- | examples/corelib/threads/mandelbrot/mandelbrot.pro | 2 | ||||
-rw-r--r-- | examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp | 148 | ||||
-rw-r--r-- | examples/corelib/threads/mandelbrot/mandelbrotwidget.h | 73 | ||||
-rw-r--r-- | examples/corelib/threads/mandelbrot/renderthread.cpp | 82 | ||||
-rw-r--r-- | examples/corelib/threads/mandelbrot/renderthread.h | 58 |
7 files changed, 180 insertions, 315 deletions
diff --git a/examples/corelib/threads/mandelbrot/CMakeLists.txt b/examples/corelib/threads/mandelbrot/CMakeLists.txt index 5ad1a37474..be296918cc 100644 --- a/examples/corelib/threads/mandelbrot/CMakeLists.txt +++ b/examples/corelib/threads/mandelbrot/CMakeLists.txt @@ -1,43 +1,39 @@ -# Generated from mandelbrot.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -cmake_minimum_required(VERSION 3.14) +cmake_minimum_required(VERSION 3.16) project(mandelbrot LANGUAGES CXX) -set(CMAKE_INCLUDE_CURRENT_DIR ON) +find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) +qt_standard_project_setup() -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/threads/mandelbrot") - -find_package(Qt6 COMPONENTS Core) -find_package(Qt6 COMPONENTS Gui) -find_package(Qt6 COMPONENTS Widgets) - -add_qt_gui_executable(mandelbrot +qt_add_executable(mandelbrot main.cpp mandelbrotwidget.cpp mandelbrotwidget.h renderthread.cpp renderthread.h ) -target_link_libraries(mandelbrot PUBLIC - Qt::Core - Qt::Gui - Qt::Widgets + +set_target_properties(mandelbrot PROPERTIES + WIN32_EXECUTABLE TRUE + MACOSX_BUNDLE TRUE ) -if(UNIX AND NOT APPLE AND NOT HAIKU AND NOT INTEGRITY AND NOT VXWORKS) - target_link_libraries(mandelbrot PUBLIC - m - ) -endif() +target_link_libraries(mandelbrot PRIVATE + Qt6::Core + Qt6::Gui + Qt6::Widgets +) install(TARGETS mandelbrot - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET mandelbrot + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR ) +install(SCRIPT ${deploy_script}) diff --git a/examples/corelib/threads/mandelbrot/main.cpp b/examples/corelib/threads/mandelbrot/main.cpp index 9832d55514..8aafebf7d7 100644 --- a/examples/corelib/threads/mandelbrot/main.cpp +++ b/examples/corelib/threads/mandelbrot/main.cpp @@ -1,62 +1,42 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://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$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "mandelbrotwidget.h" +#include "renderthread.h" #include <QApplication> +#include <QCommandLineParser> +#include <QCommandLineOption> +#include <QDebug> + +using namespace Qt::StringLiterals; //! [0] int main(int argc, char *argv[]) { QApplication app(argc, argv); + + QCommandLineParser parser; + parser.setApplicationDescription(u"Qt Mandelbrot Example"_s); + parser.addHelpOption(); + parser.addVersionOption(); + QCommandLineOption passesOption(u"passes"_s, u"Number of passes (1-8)"_s, u"passes"_s); + parser.addOption(passesOption); + parser.process(app); + + if (parser.isSet(passesOption)) { + const auto passesStr = parser.value(passesOption); + bool ok; + const int passes = passesStr.toInt(&ok); + if (!ok || passes < 1 || passes > 8) { + qWarning() << "Invalid value:" << passesStr; + return -1; + } + RenderThread::setNumPasses(passes); + } + MandelbrotWidget widget; + widget.grabGesture(Qt::PinchGesture); widget.show(); return app.exec(); } diff --git a/examples/corelib/threads/mandelbrot/mandelbrot.pro b/examples/corelib/threads/mandelbrot/mandelbrot.pro index 92010fdc1f..7edf01ff30 100644 --- a/examples/corelib/threads/mandelbrot/mandelbrot.pro +++ b/examples/corelib/threads/mandelbrot/mandelbrot.pro @@ -6,8 +6,6 @@ SOURCES = main.cpp \ mandelbrotwidget.cpp \ renderthread.cpp -unix:!mac:!vxworks:!integrity:!haiku:LIBS += -lm - # install target.path = $$[QT_INSTALL_EXAMPLES]/corelib/threads/mandelbrot INSTALLS += target diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp index b0c3733b22..bbe694831d 100644 --- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp +++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp @@ -1,68 +1,23 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://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$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "mandelbrotwidget.h" -#include <QPainter> +#include <QGesture> +#include <QGestureEvent> #include <QKeyEvent> +#include <QPainter> #include <math.h> //! [0] -const double DefaultCenterX = -0.637011; -const double DefaultCenterY = -0.0395159; -const double DefaultScale = 0.00403897; +constexpr double DefaultCenterX = -0.637011; +constexpr double DefaultCenterY = -0.0395159; +constexpr double DefaultScale = 0.00403897; -const double ZoomInFactor = 0.8; -const double ZoomOutFactor = 1 / ZoomInFactor; -const int ScrollStep = 20; +constexpr double ZoomInFactor = 0.8; +constexpr double ZoomOutFactor = 1 / ZoomInFactor; +constexpr int ScrollStep = 20; //! [0] //! [1] @@ -73,6 +28,7 @@ MandelbrotWidget::MandelbrotWidget(QWidget *parent) : pixmapScale(DefaultScale), curScale(DefaultScale) { + help = tr("Zoom with mouse wheel, +/- keys or pinch. Scroll with arrow keys or by dragging."); connect(&thread, &RenderThread::renderedImage, this, &MandelbrotWidget::updatePixmap); @@ -80,8 +36,6 @@ MandelbrotWidget::MandelbrotWidget(QWidget *parent) : #if QT_CONFIG(cursor) setCursor(Qt::CrossCursor); #endif - resize(550, 400); - } //! [1] @@ -93,7 +47,8 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */) if (pixmap.isNull()) { painter.setPen(Qt::white); - painter.drawText(rect(), Qt::AlignCenter, tr("Rendering initial image, please wait...")); + painter.drawText(rect(), Qt::AlignCenter|Qt::TextWordWrap, + tr("Rendering initial image, please wait...")); //! [2] //! [3] return; //! [3] //! [4] @@ -107,36 +62,51 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */) //! [6] //! [7] } else { //! [7] //! [8] - auto previewPixmap = qFuzzyCompare(pixmap.devicePixelRatio(), qreal(1)) + const auto previewPixmap = qFuzzyCompare(pixmap.devicePixelRatio(), qreal(1)) ? pixmap - : pixmap.scaled(pixmap.size() / pixmap.devicePixelRatio(), Qt::KeepAspectRatio, + : pixmap.scaled(pixmap.deviceIndependentSize().toSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation); - double scaleFactor = pixmapScale / curScale; - int newWidth = int(previewPixmap.width() * scaleFactor); - int newHeight = int(previewPixmap.height() * scaleFactor); - int newX = pixmapOffset.x() + (previewPixmap.width() - newWidth) / 2; - int newY = pixmapOffset.y() + (previewPixmap.height() - newHeight) / 2; + const double scaleFactor = pixmapScale / curScale; + const int newWidth = int(previewPixmap.width() * scaleFactor); + const int newHeight = int(previewPixmap.height() * scaleFactor); + const int newX = pixmapOffset.x() + (previewPixmap.width() - newWidth) / 2; + const int newY = pixmapOffset.y() + (previewPixmap.height() - newHeight) / 2; painter.save(); painter.translate(newX, newY); painter.scale(scaleFactor, scaleFactor); - QRectF exposed = painter.transform().inverted().mapRect(rect()).adjusted(-1, -1, 1, 1); + const QRectF exposed = painter.transform().inverted().mapRect(rect()) + .adjusted(-1, -1, 1, 1); painter.drawPixmap(exposed, previewPixmap, exposed); painter.restore(); } //! [8] //! [9] - QString text = tr("Use mouse wheel or the '+' and '-' keys to zoom. " - "Press and hold left mouse button to scroll."); - QFontMetrics metrics = painter.fontMetrics(); - int textWidth = metrics.horizontalAdvance(text); + const QFontMetrics metrics = painter.fontMetrics(); + if (!info.isEmpty()){ + const int infoWidth = metrics.horizontalAdvance(info); + const int infoHeight = (infoWidth/width() + 1) * (metrics.height() + 5); + + painter.setPen(Qt::NoPen); + painter.setBrush(QColor(0, 0, 0, 127)); + painter.drawRect((width() - infoWidth) / 2 - 5, 0, infoWidth + 10, infoHeight); + + painter.setPen(Qt::white); + painter.drawText(rect(), Qt::AlignHCenter|Qt::AlignTop|Qt::TextWordWrap, info); + } + + const int helpWidth = metrics.horizontalAdvance(help); + const int helpHeight = (helpWidth/width() + 1) * (metrics.height() + 5); painter.setPen(Qt::NoPen); painter.setBrush(QColor(0, 0, 0, 127)); - painter.drawRect((width() - textWidth) / 2 - 5, 0, textWidth + 10, metrics.lineSpacing() + 5); + painter.drawRect((width() - helpWidth) / 2 - 5, height()-helpHeight, helpWidth + 10, + helpHeight); + painter.setPen(Qt::white); - painter.drawText((width() - textWidth) / 2, metrics.leading() + metrics.ascent(), text); + painter.drawText(rect(), Qt::AlignHCenter|Qt::AlignBottom|Qt::TextWordWrap, help); + } //! [9] @@ -169,6 +139,9 @@ void MandelbrotWidget::keyPressEvent(QKeyEvent *event) case Qt::Key_Up: scroll(0, +ScrollStep); break; + case Qt::Key_Q: + close(); + break; default: QWidget::keyPressEvent(event); } @@ -212,9 +185,9 @@ void MandelbrotWidget::mouseReleaseEvent(QMouseEvent *event) pixmapOffset += event->position().toPoint() - lastDragPos; lastDragPos = QPoint(); - const auto pixmapSize = pixmap.size() / pixmap.devicePixelRatio(); - int deltaX = (width() - pixmapSize.width()) / 2 - pixmapOffset.x(); - int deltaY = (height() - pixmapSize.height()) / 2 - pixmapOffset.y(); + const auto pixmapSize = pixmap.deviceIndependentSize().toSize(); + const int deltaX = (width() - pixmapSize.width()) / 2 - pixmapOffset.x(); + const int deltaY = (height() - pixmapSize.height()) / 2 - pixmapOffset.y(); scroll(deltaX, deltaY); } } @@ -226,6 +199,8 @@ void MandelbrotWidget::updatePixmap(const QImage &image, double scaleFactor) if (!lastDragPos.isNull()) return; + info = image.text(RenderThread::infoKey()); + pixmap = QPixmap::fromImage(image); pixmapOffset = QPoint(); lastDragPos = QPoint(); @@ -252,3 +227,24 @@ void MandelbrotWidget::scroll(int deltaX, int deltaY) thread.render(centerX, centerY, curScale, size(), devicePixelRatio()); } //! [18] + +//! [gesture1] +#ifndef QT_NO_GESTURES +bool MandelbrotWidget::gestureEvent(QGestureEvent *event) +{ + if (auto *pinch = static_cast<QPinchGesture *>(event->gesture(Qt::PinchGesture))) { + if (pinch->changeFlags().testFlag(QPinchGesture::ScaleFactorChanged)) + zoom(1.0 / pinch->scaleFactor()); + return true; + } + return false; +} + +bool MandelbrotWidget::event(QEvent *event) +{ + if (event->type() == QEvent::Gesture) + return gestureEvent(static_cast<QGestureEvent*>(event)); + return QWidget::event(event); +} +#endif +//! [gesture1] diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.h b/examples/corelib/threads/mandelbrot/mandelbrotwidget.h index cb40962535..0d0fce56b3 100644 --- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.h +++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.h @@ -1,70 +1,29 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://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$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef MANDELBROTWIDGET_H #define MANDELBROTWIDGET_H +#include "renderthread.h" + +#include <QCoreApplication> #include <QPixmap> #include <QWidget> -#include "renderthread.h" +QT_BEGIN_NAMESPACE +class QGestureEvent; +QT_END_NAMESPACE //! [0] class MandelbrotWidget : public QWidget { - Q_OBJECT + Q_DECLARE_TR_FUNCTIONS(MandelbrotWidget) public: MandelbrotWidget(QWidget *parent = nullptr); protected: + QSize sizeHint() const override { return {1024, 768}; }; void paintEvent(QPaintEvent *event) override; void resizeEvent(QResizeEvent *event) override; void keyPressEvent(QKeyEvent *event) override; @@ -74,18 +33,24 @@ protected: void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; +#ifndef QT_NO_GESTURES + bool event(QEvent *event) override; +#endif -private slots: +private: void updatePixmap(const QImage &image, double scaleFactor); void zoom(double zoomFactor); - -private: void scroll(int deltaX, int deltaY); +#ifndef QT_NO_GESTURES + bool gestureEvent(QGestureEvent *event); +#endif RenderThread thread; QPixmap pixmap; QPoint pixmapOffset; QPoint lastDragPos; + QString help; + QString info; double centerX; double centerY; double pixmapScale; diff --git a/examples/corelib/threads/mandelbrot/renderthread.cpp b/examples/corelib/threads/mandelbrot/renderthread.cpp index 4d2009471c..77a14a6ac1 100644 --- a/examples/corelib/threads/mandelbrot/renderthread.cpp +++ b/examples/corelib/threads/mandelbrot/renderthread.cpp @@ -1,58 +1,16 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://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$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "renderthread.h" #include <QImage> +#include <QElapsedTimer> +#include <QTextStream> + #include <cmath> +int RenderThread::numPasses = 8; + //! [0] RenderThread::RenderThread(QObject *parent) : QThread(parent) @@ -98,6 +56,7 @@ void RenderThread::render(double centerX, double centerY, double scaleFactor, //! [3] void RenderThread::run() { + QElapsedTimer timer; forever { mutex.lock(); const double devicePixelRatio = this->devicePixelRatio; @@ -110,19 +69,20 @@ void RenderThread::run() //! [3] //! [4] - int halfWidth = resultSize.width() / 2; + const int halfWidth = resultSize.width() / 2; //! [4] //! [5] - int halfHeight = resultSize.height() / 2; + const int halfHeight = resultSize.height() / 2; QImage image(resultSize, QImage::Format_RGB32); image.setDevicePixelRatio(devicePixelRatio); - const int NumPasses = 8; int pass = 0; - while (pass < NumPasses) { + while (pass < numPasses) { const int MaxIterations = (1 << (2 * pass + 6)) + 32; - const int Limit = 4; + constexpr int Limit = 4; bool allBlack = true; + timer.restart(); + for (int y = -halfHeight; y < halfHeight; ++y) { if (restart) break; @@ -165,8 +125,20 @@ void RenderThread::run() if (allBlack && pass == 0) { pass = 4; } else { - if (!restart) + if (!restart) { + QString message; + QTextStream str(&message); + str << " Pass " << (pass + 1) << '/' << numPasses + << ", max iterations: " << MaxIterations << ", time: "; + const auto elapsed = timer.elapsed(); + if (elapsed > 2000) + str << (elapsed / 1000) << 's'; + else + str << elapsed << "ms"; + image.setText(infoKey(), message); + emit renderedImage(image, requestedScaleFactor); + } //! [5] //! [6] ++pass; } diff --git a/examples/corelib/threads/mandelbrot/renderthread.h b/examples/corelib/threads/mandelbrot/renderthread.h index 6174e0ed3d..ecd8ae084d 100644 --- a/examples/corelib/threads/mandelbrot/renderthread.h +++ b/examples/corelib/threads/mandelbrot/renderthread.h @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://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$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef RENDERTHREAD_H #define RENDERTHREAD_H @@ -72,6 +25,10 @@ public: void render(double centerX, double centerY, double scaleFactor, QSize resultSize, double devicePixelRatio); + static void setNumPasses(int n) { numPasses = n; } + + static QString infoKey() { return QStringLiteral("info"); } + signals: void renderedImage(const QImage &image, double scaleFactor); @@ -88,10 +45,11 @@ private: double scaleFactor; double devicePixelRatio; QSize resultSize; + static int numPasses; bool restart = false; bool abort = false; - enum { ColormapSize = 512 }; + static constexpr int ColormapSize = 512; uint colormap[ColormapSize]; }; //! [0] |