summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRym Bouabid <rym.bouabid@qt.io>2023-09-08 15:55:27 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-09-15 06:03:44 +0000
commit20966dc53fa4fabb7338ccf8ddd637269a7c7041 (patch)
tree4cb44e69beaa4232fc7ccc1c7e3ee0216f9c24d7
parent5f30d04ccd9a137fee49fa7403bf9c89e7947ea8 (diff)
Revamp Mandelbrot example: Add const/constexpr when applicable
Add const in front of local variables when applicable. Replace const by constexpr when the value of the variable can be calculated at compile-time. Task-number: QTBUG-108861 Change-Id: I2cd1bc97aaa07d6d564731d9ccddba9a74e96fef Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> (cherry picked from commit 7903a52d46401e224f1d85067d51bf443066cbec) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> (cherry picked from commit f67e649e9b47122c0cbe6bc03d82dfbca8a5949c)
-rw-r--r--examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp43
-rw-r--r--examples/corelib/threads/mandelbrot/renderthread.cpp6
2 files changed, 24 insertions, 25 deletions
diff --git a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
index a42a20c7c6..bbe694831d 100644
--- a/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
+++ b/examples/corelib/threads/mandelbrot/mandelbrotwidget.cpp
@@ -11,13 +11,13 @@
#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]
@@ -62,46 +62,45 @@ 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);
@@ -187,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/renderthread.cpp b/examples/corelib/threads/mandelbrot/renderthread.cpp
index 7e4ff5ba8e..77a14a6ac1 100644
--- a/examples/corelib/threads/mandelbrot/renderthread.cpp
+++ b/examples/corelib/threads/mandelbrot/renderthread.cpp
@@ -69,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();