diff options
Diffstat (limited to 'examples/corelib/threads/mandelbrot')
6 files changed, 59 insertions, 56 deletions
diff --git a/examples/corelib/threads/mandelbrot/CMakeLists.txt b/examples/corelib/threads/mandelbrot/CMakeLists.txt index 7d9b78932c..be296918cc 100644 --- a/examples/corelib/threads/mandelbrot/CMakeLists.txt +++ b/examples/corelib/threads/mandelbrot/CMakeLists.txt @@ -4,16 +4,10 @@ cmake_minimum_required(VERSION 3.16) project(mandelbrot LANGUAGES CXX) -set(CMAKE_AUTOMOC ON) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/threads/mandelbrot") - find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) +qt_standard_project_setup() + qt_add_executable(mandelbrot main.cpp mandelbrotwidget.cpp mandelbrotwidget.h @@ -25,14 +19,21 @@ set_target_properties(mandelbrot PROPERTIES MACOSX_BUNDLE TRUE ) -target_link_libraries(mandelbrot PUBLIC - Qt::Core - Qt::Gui - Qt::Widgets +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 d0d4680978..8aafebf7d7 100644 --- a/examples/corelib/threads/mandelbrot/main.cpp +++ b/examples/corelib/threads/mandelbrot/main.cpp @@ -2,15 +2,14 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "mandelbrotwidget.h" +#include "renderthread.h" #include <QApplication> - -#include <QScreen> - #include <QCommandLineParser> #include <QCommandLineOption> #include <QDebug> -#include <QRect> + +using namespace Qt::StringLiterals; //! [0] int main(int argc, char *argv[]) @@ -18,10 +17,10 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); QCommandLineParser parser; - parser.setApplicationDescription("Qt Mandelbrot Example"); + parser.setApplicationDescription(u"Qt Mandelbrot Example"_s); parser.addHelpOption(); parser.addVersionOption(); - QCommandLineOption passesOption("passes", "Number of passes (1-8)", "passes"); + QCommandLineOption passesOption(u"passes"_s, u"Number of passes (1-8)"_s, u"passes"_s); parser.addOption(passesOption); parser.process(app); diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp index e0f33a2b0b..bbe694831d 100644 --- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp +++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp @@ -4,19 +4,20 @@ #include "mandelbrotwidget.h" #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] @@ -46,7 +47,8 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */) if (pixmap.isNull()) { painter.setPen(Qt::white); - painter.drawText(rect(), Qt::AlignCenter|Qt::TextWordWrap, tr("Rendering initial image, please wait...")); + painter.drawText(rect(), Qt::AlignCenter|Qt::TextWordWrap, + tr("Rendering initial image, please wait...")); //! [2] //! [3] return; //! [3] //! [4] @@ -60,47 +62,47 @@ 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.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] - QFontMetrics metrics = painter.fontMetrics(); + const QFontMetrics metrics = painter.fontMetrics(); if (!info.isEmpty()){ - int infoWidth = metrics.horizontalAdvance(info); - int infoHeight = metrics.height(); + 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)); - infoHeight = (infoWidth/width()+1) * (infoHeight + 5); painter.drawRect((width() - infoWidth) / 2 - 5, 0, infoWidth + 10, infoHeight); painter.setPen(Qt::white); painter.drawText(rect(), Qt::AlignHCenter|Qt::AlignTop|Qt::TextWordWrap, info); } - int helpWidth = metrics.horizontalAdvance(help); - int helpHeight = metrics.height(); + 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)); - helpHeight = (helpWidth/width()+1) * (helpHeight + 5); - painter.drawRect((width() - helpWidth) / 2 - 5, height()-helpHeight, helpWidth + 10, helpHeight); + painter.drawRect((width() - helpWidth) / 2 - 5, height()-helpHeight, helpWidth + 10, + helpHeight); painter.setPen(Qt::white); painter.drawText(rect(), Qt::AlignHCenter|Qt::AlignBottom|Qt::TextWordWrap, help); @@ -184,8 +186,8 @@ void MandelbrotWidget::mouseReleaseEvent(QMouseEvent *event) lastDragPos = QPoint(); const auto pixmapSize = pixmap.deviceIndependentSize().toSize(); - int deltaX = (width() - pixmapSize.width()) / 2 - pixmapOffset.x(); - int deltaY = (height() - pixmapSize.height()) / 2 - pixmapOffset.y(); + const int deltaX = (width() - pixmapSize.width()) / 2 - pixmapOffset.x(); + const int deltaY = (height() - pixmapSize.height()) / 2 - pixmapOffset.y(); scroll(deltaX, deltaY); } } diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.h b/examples/corelib/threads/mandelbrot/mandelbrotwidget.h index 23c3a2bf40..0d0fce56b3 100644 --- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.h +++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.h @@ -4,16 +4,20 @@ #ifndef MANDELBROTWIDGET_H #define MANDELBROTWIDGET_H -#include <QGestureEvent> +#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); @@ -33,11 +37,9 @@ protected: 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); diff --git a/examples/corelib/threads/mandelbrot/renderthread.cpp b/examples/corelib/threads/mandelbrot/renderthread.cpp index 9e6c884f91..77a14a6ac1 100644 --- a/examples/corelib/threads/mandelbrot/renderthread.cpp +++ b/examples/corelib/threads/mandelbrot/renderthread.cpp @@ -4,7 +4,6 @@ #include "renderthread.h" #include <QImage> - #include <QElapsedTimer> #include <QTextStream> @@ -70,16 +69,16 @@ 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); int pass = 0; while (pass < numPasses) { const int MaxIterations = (1 << (2 * pass + 6)) + 32; - const int Limit = 4; + constexpr int Limit = 4; bool allBlack = true; timer.restart(); diff --git a/examples/corelib/threads/mandelbrot/renderthread.h b/examples/corelib/threads/mandelbrot/renderthread.h index 6e509eee94..ecd8ae084d 100644 --- a/examples/corelib/threads/mandelbrot/renderthread.h +++ b/examples/corelib/threads/mandelbrot/renderthread.h @@ -49,7 +49,7 @@ private: bool restart = false; bool abort = false; - enum { ColormapSize = 512 }; + static constexpr int ColormapSize = 512; uint colormap[ColormapSize]; }; //! [0] |