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
Pick-to: 6.6 6.5 6.6.0
Change-Id: I2cd1bc97aaa07d6d564731d9ccddba9a74e96fef
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Rym Bouabid 2023-09-08 15:55:27 +02:00
parent 32e2a910f1
commit 7903a52d46
2 changed files with 24 additions and 25 deletions

View File

@ -11,13 +11,13 @@
#include <math.h> #include <math.h>
//! [0] //! [0]
const double DefaultCenterX = -0.637011; constexpr double DefaultCenterX = -0.637011;
const double DefaultCenterY = -0.0395159; constexpr double DefaultCenterY = -0.0395159;
const double DefaultScale = 0.00403897; constexpr double DefaultScale = 0.00403897;
const double ZoomInFactor = 0.8; constexpr double ZoomInFactor = 0.8;
const double ZoomOutFactor = 1 / ZoomInFactor; constexpr double ZoomOutFactor = 1 / ZoomInFactor;
const int ScrollStep = 20; constexpr int ScrollStep = 20;
//! [0] //! [0]
//! [1] //! [1]
@ -62,46 +62,45 @@ void MandelbrotWidget::paintEvent(QPaintEvent * /* event */)
//! [6] //! [7] //! [6] //! [7]
} else { } else {
//! [7] //! [8] //! [7] //! [8]
auto previewPixmap = qFuzzyCompare(pixmap.devicePixelRatio(), qreal(1)) const auto previewPixmap = qFuzzyCompare(pixmap.devicePixelRatio(), qreal(1))
? pixmap ? pixmap
: pixmap.scaled(pixmap.deviceIndependentSize().toSize(), Qt::KeepAspectRatio, : pixmap.scaled(pixmap.deviceIndependentSize().toSize(), Qt::KeepAspectRatio,
Qt::SmoothTransformation); Qt::SmoothTransformation);
double scaleFactor = pixmapScale / curScale; const double scaleFactor = pixmapScale / curScale;
int newWidth = int(previewPixmap.width() * scaleFactor); const int newWidth = int(previewPixmap.width() * scaleFactor);
int newHeight = int(previewPixmap.height() * scaleFactor); const int newHeight = int(previewPixmap.height() * scaleFactor);
int newX = pixmapOffset.x() + (previewPixmap.width() - newWidth) / 2; const int newX = pixmapOffset.x() + (previewPixmap.width() - newWidth) / 2;
int newY = pixmapOffset.y() + (previewPixmap.height() - newHeight) / 2; const int newY = pixmapOffset.y() + (previewPixmap.height() - newHeight) / 2;
painter.save(); painter.save();
painter.translate(newX, newY); painter.translate(newX, newY);
painter.scale(scaleFactor, scaleFactor); 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.drawPixmap(exposed, previewPixmap, exposed);
painter.restore(); painter.restore();
} }
//! [8] //! [9] //! [8] //! [9]
QFontMetrics metrics = painter.fontMetrics(); const QFontMetrics metrics = painter.fontMetrics();
if (!info.isEmpty()){ if (!info.isEmpty()){
int infoWidth = metrics.horizontalAdvance(info); const int infoWidth = metrics.horizontalAdvance(info);
int infoHeight = metrics.height(); const int infoHeight = (infoWidth/width() + 1) * (metrics.height() + 5);
painter.setPen(Qt::NoPen); painter.setPen(Qt::NoPen);
painter.setBrush(QColor(0, 0, 0, 127)); painter.setBrush(QColor(0, 0, 0, 127));
infoHeight = (infoWidth/width()+1) * (infoHeight + 5);
painter.drawRect((width() - infoWidth) / 2 - 5, 0, infoWidth + 10, infoHeight); painter.drawRect((width() - infoWidth) / 2 - 5, 0, infoWidth + 10, infoHeight);
painter.setPen(Qt::white); painter.setPen(Qt::white);
painter.drawText(rect(), Qt::AlignHCenter|Qt::AlignTop|Qt::TextWordWrap, info); painter.drawText(rect(), Qt::AlignHCenter|Qt::AlignTop|Qt::TextWordWrap, info);
} }
int helpWidth = metrics.horizontalAdvance(help); const int helpWidth = metrics.horizontalAdvance(help);
int helpHeight = metrics.height(); const int helpHeight = (helpWidth/width() + 1) * (metrics.height() + 5);
painter.setPen(Qt::NoPen); painter.setPen(Qt::NoPen);
painter.setBrush(QColor(0, 0, 0, 127)); painter.setBrush(QColor(0, 0, 0, 127));
helpHeight = (helpWidth/width()+1) * (helpHeight + 5);
painter.drawRect((width() - helpWidth) / 2 - 5, height()-helpHeight, helpWidth + 10, painter.drawRect((width() - helpWidth) / 2 - 5, height()-helpHeight, helpWidth + 10,
helpHeight); helpHeight);
@ -187,8 +186,8 @@ void MandelbrotWidget::mouseReleaseEvent(QMouseEvent *event)
lastDragPos = QPoint(); lastDragPos = QPoint();
const auto pixmapSize = pixmap.deviceIndependentSize().toSize(); const auto pixmapSize = pixmap.deviceIndependentSize().toSize();
int deltaX = (width() - pixmapSize.width()) / 2 - pixmapOffset.x(); const int deltaX = (width() - pixmapSize.width()) / 2 - pixmapOffset.x();
int deltaY = (height() - pixmapSize.height()) / 2 - pixmapOffset.y(); const int deltaY = (height() - pixmapSize.height()) / 2 - pixmapOffset.y();
scroll(deltaX, deltaY); scroll(deltaX, deltaY);
} }
} }

View File

@ -69,16 +69,16 @@ void RenderThread::run()
//! [3] //! [3]
//! [4] //! [4]
int halfWidth = resultSize.width() / 2; const int halfWidth = resultSize.width() / 2;
//! [4] //! [5] //! [4] //! [5]
int halfHeight = resultSize.height() / 2; const int halfHeight = resultSize.height() / 2;
QImage image(resultSize, QImage::Format_RGB32); QImage image(resultSize, QImage::Format_RGB32);
image.setDevicePixelRatio(devicePixelRatio); image.setDevicePixelRatio(devicePixelRatio);
int pass = 0; int pass = 0;
while (pass < numPasses) { while (pass < numPasses) {
const int MaxIterations = (1 << (2 * pass + 6)) + 32; const int MaxIterations = (1 << (2 * pass + 6)) + 32;
const int Limit = 4; constexpr int Limit = 4;
bool allBlack = true; bool allBlack = true;
timer.restart(); timer.restart();