From 9aa293270155f1c50870f0119932629e31ab0713 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 14 Jan 2020 08:31:02 +0100 Subject: Polish the Mandelbrot Example Fix warnings about float constants and comparison. Use member or constructor initialization. Introduce some const for clarity. Use Qt_CONFIG. Task-number: QTBUG-81254 Change-Id: I71a6ebfba397c0f8e1dd2e61167233c8e5c137af Reviewed-by: Paul Wicking --- .../threads/mandelbrot/mandelbrotwidget.cpp | 29 +++++++++++----------- .../corelib/threads/mandelbrot/renderthread.cpp | 27 +++++++++----------- examples/corelib/threads/mandelbrot/renderthread.h | 6 ++--- 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp index 822791533b..420fd6186a 100644 --- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp +++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp @@ -56,29 +56,28 @@ #include //! [0] -const double DefaultCenterX = -0.637011f; -const double DefaultCenterY = -0.0395159f; -const double DefaultScale = 0.00403897f; +const double DefaultCenterX = -0.637011; +const double DefaultCenterY = -0.0395159; +const double DefaultScale = 0.00403897; -const double ZoomInFactor = 0.8f; +const double ZoomInFactor = 0.8; const double ZoomOutFactor = 1 / ZoomInFactor; const int ScrollStep = 20; //! [0] //! [1] -MandelbrotWidget::MandelbrotWidget(QWidget *parent) - : QWidget(parent) +MandelbrotWidget::MandelbrotWidget(QWidget *parent) : + QWidget(parent), + centerX(DefaultCenterX), + centerY(DefaultCenterY), + pixmapScale(DefaultScale), + curScale(DefaultScale) { - centerX = DefaultCenterX; - centerY = DefaultCenterY; - pixmapScale = DefaultScale; - curScale = DefaultScale; - connect(&thread, &RenderThread::renderedImage, this, &MandelbrotWidget::updatePixmap); setWindowTitle(tr("Mandelbrot")); -#ifndef QT_NO_CURSOR +#if QT_CONFIG(cursor) setCursor(Qt::CrossCursor); #endif resize(550, 400); @@ -102,7 +101,7 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */) //! [4] //! [5] - if (curScale == pixmapScale) { + if (qFuzzyCompare(curScale, pixmapScale)) { //! [5] //! [6] painter.drawPixmap(pixmapOffset, pixmap); //! [6] //! [7] @@ -176,8 +175,8 @@ void MandelbrotWidget::keyPressEvent(QKeyEvent *event) //! [12] void MandelbrotWidget::wheelEvent(QWheelEvent *event) { - int numDegrees = event->angleDelta().y() / 8; - double numSteps = numDegrees / 15.0f; + const int numDegrees = event->angleDelta().y() / 8; + const double numSteps = numDegrees / double(15); zoom(pow(ZoomInFactor, numSteps)); } //! [12] diff --git a/examples/corelib/threads/mandelbrot/renderthread.cpp b/examples/corelib/threads/mandelbrot/renderthread.cpp index eee44c7242..22ce81c32d 100644 --- a/examples/corelib/threads/mandelbrot/renderthread.cpp +++ b/examples/corelib/threads/mandelbrot/renderthread.cpp @@ -57,9 +57,6 @@ RenderThread::RenderThread(QObject *parent) : QThread(parent) { - restart = false; - abort = false; - for (int i = 0; i < ColormapSize; ++i) colormap[i] = rgbFromWaveLength(380.0 + (i * 400.0 / ColormapSize)); } @@ -102,10 +99,10 @@ void RenderThread::run() { forever { mutex.lock(); - QSize resultSize = this->resultSize; - double scaleFactor = this->scaleFactor; - double centerX = this->centerX; - double centerY = this->centerY; + const QSize resultSize = this->resultSize; + const double scaleFactor = this->scaleFactor; + const double centerX = this->centerX; + const double centerY = this->centerY; mutex.unlock(); //! [3] @@ -128,20 +125,20 @@ void RenderThread::run() if (abort) return; - uint *scanLine = + auto scanLine = reinterpret_cast(image.scanLine(y + halfHeight)); - double ay = centerY + (y * scaleFactor); + const double ay = centerY + (y * scaleFactor); for (int x = -halfWidth; x < halfWidth; ++x) { - double ax = centerX + (x * scaleFactor); + const double ax = centerX + (x * scaleFactor); double a1 = ax; double b1 = ay; int numIterations = 0; do { ++numIterations; - double a2 = (a1 * a1) - (b1 * b1) + ax; - double b2 = (2 * a1 * b1) + ay; + const double a2 = (a1 * a1) - (b1 * b1) + ax; + const double b2 = (2 * a1 * b1) + ay; if ((a2 * a2) + (b2 * b2) > Limit) break; @@ -187,9 +184,9 @@ void RenderThread::run() //! [10] uint RenderThread::rgbFromWaveLength(double wave) { - double r = 0.0; - double g = 0.0; - double b = 0.0; + double r = 0; + double g = 0; + double b = 0; if (wave >= 380.0 && wave <= 440.0) { r = -1.0 * (wave - 440.0) / (440.0 - 380.0); diff --git a/examples/corelib/threads/mandelbrot/renderthread.h b/examples/corelib/threads/mandelbrot/renderthread.h index 4f0394d554..934cc17d74 100644 --- a/examples/corelib/threads/mandelbrot/renderthread.h +++ b/examples/corelib/threads/mandelbrot/renderthread.h @@ -78,7 +78,7 @@ protected: void run() override; private: - uint rgbFromWaveLength(double wave); + static uint rgbFromWaveLength(double wave); QMutex mutex; QWaitCondition condition; @@ -86,8 +86,8 @@ private: double centerY; double scaleFactor; QSize resultSize; - bool restart; - bool abort; + bool restart = false; + bool abort = false; enum { ColormapSize = 512 }; uint colormap[ColormapSize]; -- cgit v1.2.3