From 08ca000e71bc6d1043e6608d9da06c9b31bfaa06 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sun, 21 Mar 2021 08:44:08 +1000 Subject: [PATCH] Use c++17 std::clamp instead of qBound --- .../terrain/qgsdemterraintilegeometry_p.cpp | 16 ++++---- src/3d/terrain/qgsdemterraintileloader_p.cpp | 4 +- .../qgsclassificationmethod.cpp | 2 +- src/core/effects/qgscoloreffect.cpp | 11 +++++ src/core/effects/qgscoloreffect.h | 4 +- src/core/effects/qgsimageoperation.cpp | 8 ++-- src/core/expression/qgsexpressionfunction.cpp | 6 +-- .../labeling/qgslabelobstaclesettings.cpp | 2 +- src/core/labeling/qgspallabeling.cpp | 6 +-- src/core/qgscolorramp.cpp | 6 +-- src/core/qgspropertytransformer.cpp | 40 +++++++++---------- src/core/qgstiles.cpp | 16 ++++---- .../raster/qgsbrightnesscontrastfilter.cpp | 19 ++++++++- src/core/raster/qgsbrightnesscontrastfilter.h | 6 +-- src/core/raster/qgshillshaderenderer.cpp | 14 +++---- src/core/raster/qgshuesaturationfilter.cpp | 2 +- src/core/symbology/qgsrendererrange.cpp | 2 +- .../vector/qgsvectorlayerdiagramprovider.cpp | 2 +- src/gui/layout/qgslayoutview.cpp | 4 +- .../models/qgsmodelgraphicsview.cpp | 2 +- src/gui/qgsfloatingwidget.cpp | 2 +- src/gui/qgsgradientcolorrampdialog.cpp | 10 ++--- src/gui/qgsgradientstopeditor.cpp | 2 +- src/gui/qgshighlight.cpp | 2 +- src/gui/qgsmapcanvas.cpp | 4 +- src/gui/tableeditor/qgstableeditorwidget.cpp | 4 +- src/providers/wms/qgswmscapabilities.cpp | 8 ++-- tests/code_layout/test_banned_keywords.sh | 3 ++ tests/qt_modeltest/modeltest.cpp | 2 +- 29 files changed, 119 insertions(+), 90 deletions(-) diff --git a/src/3d/terrain/qgsdemterraintilegeometry_p.cpp b/src/3d/terrain/qgsdemterraintilegeometry_p.cpp index 0caaa43fc8f..cba304df13a 100644 --- a/src/3d/terrain/qgsdemterraintilegeometry_p.cpp +++ b/src/3d/terrain/qgsdemterraintilegeometry_p.cpp @@ -64,14 +64,14 @@ static QByteArray createPlaneVertexData( int res, float side, float vertScale, f // Iterate over z for ( int j = -1; j <= resolution.height(); ++j ) { - int jBound = qBound( 0, j, jMax ); + int jBound = std::clamp( j, 0, jMax ); const float z = z0 + static_cast( jBound ) * dz; const float v = static_cast( jBound ) * dv; // Iterate over x for ( int i = -1; i <= resolution.width(); ++i ) { - int iBound = qBound( 0, i, iMax ); + int iBound = std::clamp( i, 0, iMax ); const float x = x0 + static_cast( iBound ) * dx; const float u = static_cast( iBound ) * du; @@ -95,10 +95,10 @@ static QByteArray createPlaneVertexData( int res, float side, float vertScale, f // calculate normal coordinates #define zAt( ii, jj ) zData[ jj * resolution.width() + ii ] * vertScale - float zi0 = zAt( qBound( 0, i - 1, iMax ), jBound ); - float zi1 = zAt( qBound( 0, i + 1, iMax ), jBound ); - float zj0 = zAt( iBound, qBound( 0, j - 1, jMax ) ); - float zj1 = zAt( iBound, qBound( 0, j + 1, jMax ) ); + float zi0 = zAt( std::clamp( i - 1, 0, iMax ), jBound ); + float zi1 = zAt( std::clamp( i + 1, 0, iMax ), jBound ); + float zj0 = zAt( iBound, std::clamp( j - 1, 0, jMax ) ); + float zj1 = zAt( iBound, std::clamp( j + 1, 0, jMax ) ); QVector3D n; if ( std::isnan( zi0 ) || std::isnan( zi1 ) || std::isnan( zj0 ) || std::isnan( zj1 ) ) @@ -137,8 +137,8 @@ static QByteArray createPlaneVertexData( int res, float side, float vertScale, f inline int ijToHeightMapIndex( int i, int j, int resX, int resZ ) { - i = qBound( 1, i, resX ) - 1; - j = qBound( 1, j, resZ ) - 1; + i = std::clamp( i, 1, resX ) - 1; + j = std::clamp( j, 1, resZ ) - 1; return j * resX + i; } diff --git a/src/3d/terrain/qgsdemterraintileloader_p.cpp b/src/3d/terrain/qgsdemterraintileloader_p.cpp index 87bce73a8e0..1a0fc9ab855 100644 --- a/src/3d/terrain/qgsdemterraintileloader_p.cpp +++ b/src/3d/terrain/qgsdemterraintileloader_p.cpp @@ -293,8 +293,8 @@ float QgsDemHeightMapGenerator::heightAt( double x, double y ) int cellX = ( int )( ( x - rect.xMinimum() ) / rect.width() * res + .5f ); int cellY = ( int )( ( rect.yMaximum() - y ) / rect.height() * res + .5f ); - cellX = qBound( 0, cellX, res - 1 ); - cellY = qBound( 0, cellY, res - 1 ); + cellX = std::clamp( cellX, 0, res - 1 ); + cellY = std::clamp( cellY, 0, res - 1 ); const float *data = ( const float * ) mDtmCoarseData.constData(); return data[cellX + cellY * res]; diff --git a/src/core/classification/qgsclassificationmethod.cpp b/src/core/classification/qgsclassificationmethod.cpp index cc12200f581..dd0aa7dba0f 100644 --- a/src/core/classification/qgsclassificationmethod.cpp +++ b/src/core/classification/qgsclassificationmethod.cpp @@ -142,7 +142,7 @@ void QgsClassificationMethod::setSymmetricMode( bool enabled, double symmetryPoi void QgsClassificationMethod::setLabelPrecision( int precision ) { // Limit the range of decimal places to a reasonable range - precision = qBound( MIN_PRECISION, precision, MAX_PRECISION ); + precision = std::clamp( precision, MIN_PRECISION, MAX_PRECISION ); mLabelPrecision = precision; mLabelNumberScale = 1.0; mLabelNumberSuffix.clear(); diff --git a/src/core/effects/qgscoloreffect.cpp b/src/core/effects/qgscoloreffect.cpp index 1280860fe4a..495da21b528 100644 --- a/src/core/effects/qgscoloreffect.cpp +++ b/src/core/effects/qgscoloreffect.cpp @@ -18,6 +18,7 @@ #include "qgscoloreffect.h" #include "qgsimageoperation.h" #include "qgssymbollayerutils.h" +#include QgsPaintEffect *QgsColorEffect::create( const QVariantMap &map ) { @@ -119,6 +120,16 @@ QgsColorEffect *QgsColorEffect::clone() const return newEffect; } +void QgsColorEffect::setBrightness( int brightness ) +{ + mBrightness = std::clamp( brightness, -255, 255 ); +} + +void QgsColorEffect::setContrast( int contrast ) +{ + mContrast = std::clamp( contrast, -100, 100 ); +} + void QgsColorEffect::setColorizeColor( const QColor &colorizeColor ) { mColorizeColor = colorizeColor; diff --git a/src/core/effects/qgscoloreffect.h b/src/core/effects/qgscoloreffect.h index 6f8f175a36a..1b964f6b755 100644 --- a/src/core/effects/qgscoloreffect.h +++ b/src/core/effects/qgscoloreffect.h @@ -58,7 +58,7 @@ class CORE_EXPORT QgsColorEffect : public QgsPaintEffect SIP_NODEFAULTCTORS * lightening * \see setBrightness */ - void setBrightness( int brightness ) { mBrightness = qBound( -255, brightness, 255 ); } + void setBrightness( int brightness ); /** * Returns the brightness modification for the effect. @@ -76,7 +76,7 @@ class CORE_EXPORT QgsColorEffect : public QgsPaintEffect SIP_NODEFAULTCTORS * greater contrast * \see setContrast */ - void setContrast( int contrast ) { mContrast = qBound( -100, contrast, 100 ); } + void setContrast( int contrast ); /** * Returns the contrast modification for the effect. diff --git a/src/core/effects/qgsimageoperation.cpp b/src/core/effects/qgsimageoperation.cpp index 53dc542fe56..d722a3443de 100644 --- a/src/core/effects/qgsimageoperation.cpp +++ b/src/core/effects/qgsimageoperation.cpp @@ -260,7 +260,7 @@ void QgsImageOperation::BrightnessContrastPixelOperation::operator()( QRgb &rgb, int QgsImageOperation::adjustColorComponent( int colorComponent, int brightness, double contrastFactor ) { - return qBound( 0, static_cast< int >( ( ( ( ( ( colorComponent / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ), 255 ); + return std::clamp( static_cast< int >( ( ( ( ( ( colorComponent / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ), 0, 255 ); } //hue/saturation @@ -348,7 +348,7 @@ void QgsImageOperation::MultiplyOpacityPixelOperation::operator()( QRgb &rgb, co { Q_UNUSED( x ) Q_UNUSED( y ) - rgb = qRgba( qRed( rgb ), qGreen( rgb ), qBlue( rgb ), qBound( 0.0, std::round( mFactor * qAlpha( rgb ) ), 255.0 ) ); + rgb = qRgba( qRed( rgb ), qGreen( rgb ), qBlue( rgb ), std::clamp( std::round( mFactor * qAlpha( rgb ) ), 0.0, 255.0 ) ); } // overlay color @@ -694,7 +694,7 @@ inline QRgb QgsImageOperation::GaussianBlurOperation::gaussianBlurVertical( cons for ( int i = 0; i <= mRadius * 2; ++i ) { - y = qBound( 0, posy + ( i - mRadius ), height - 1 ); + y = std::clamp( posy + ( i - mRadius ), 0, height - 1 ); ref = sourceFirstLine + sourceBpl * y; QRgb *refRgb = reinterpret_cast< QRgb * >( ref ); @@ -718,7 +718,7 @@ inline QRgb QgsImageOperation::GaussianBlurOperation::gaussianBlurHorizontal( co for ( int i = 0; i <= mRadius * 2; ++i ) { - x = qBound( 0, posx + ( i - mRadius ), width - 1 ); + x = std::clamp( posx + ( i - mRadius ), 0, width - 1 ); ref = sourceFirstLine + x * 4; QRgb *refRgb = reinterpret_cast< QRgb * >( ref ); diff --git a/src/core/expression/qgsexpressionfunction.cpp b/src/core/expression/qgsexpressionfunction.cpp index 9b09e053a9a..4247ddfda48 100644 --- a/src/core/expression/qgsexpressionfunction.cpp +++ b/src/core/expression/qgsexpressionfunction.cpp @@ -2741,9 +2741,9 @@ static QVariant fcnSmooth( const QVariantList &values, const QgsExpressionContex return QVariant(); int iterations = std::min( QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent ), 10 ); - double offset = qBound( 0.0, QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent ), 0.5 ); + double offset = std::clamp( QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent ), 0.0, 0.5 ); double minLength = QgsExpressionUtils::getDoubleValue( values.at( 3 ), parent ); - double maxAngle = qBound( 0.0, QgsExpressionUtils::getDoubleValue( values.at( 4 ), parent ), 180.0 ); + double maxAngle = std::clamp( QgsExpressionUtils::getDoubleValue( values.at( 4 ), parent ), 0.0, 180.0 ); QgsGeometry smoothed = geom.smooth( static_cast( iterations ), offset, minLength, maxAngle ); if ( smoothed.isNull() ) @@ -4055,7 +4055,7 @@ static QVariant fcnHausdorffDistance( const QVariantList &values, const QgsExpre if ( values.length() == 3 && values.at( 2 ).isValid() ) { double densify = QgsExpressionUtils::getDoubleValue( values.at( 2 ), parent ); - densify = qBound( 0.0, densify, 1.0 ); + densify = std::clamp( densify, 0.0, 1.0 ); res = g1.hausdorffDistanceDensify( g2, densify ); } else diff --git a/src/core/labeling/qgslabelobstaclesettings.cpp b/src/core/labeling/qgslabelobstaclesettings.cpp index 5e64ee09d15..4344e29038a 100644 --- a/src/core/labeling/qgslabelobstaclesettings.cpp +++ b/src/core/labeling/qgslabelobstaclesettings.cpp @@ -40,7 +40,7 @@ void QgsLabelObstacleSettings::updateDataDefinedProperties( const QgsPropertyCol double factorD = exprVal.toDouble( &ok ); if ( ok ) { - factorD = qBound( 0.0, factorD, 10.0 ); + factorD = std::clamp( factorD, 0.0, 10.0 ); factorD = factorD / 5.0 + 0.0001; // convert 0 -> 10 to 0.0001 -> 2.0 mObstacleFactor = factorD; } diff --git a/src/core/labeling/qgspallabeling.cpp b/src/core/labeling/qgspallabeling.cpp index 8cf2a602450..34bc9395625 100644 --- a/src/core/labeling/qgspallabeling.cpp +++ b/src/core/labeling/qgspallabeling.cpp @@ -1937,8 +1937,8 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext const QPointF maxcharanglePt = QgsSymbolLayerUtils::toPoint( exprVal, &ok ); if ( ok ) { - maxcharanglein = qBound( 20.0, static_cast< double >( maxcharanglePt.x() ), 60.0 ); - maxcharangleout = qBound( 20.0, static_cast< double >( maxcharanglePt.y() ), 95.0 ); + maxcharanglein = std::clamp( static_cast< double >( maxcharanglePt.x() ), 20.0, 60.0 ); + maxcharangleout = std::clamp( static_cast< double >( maxcharanglePt.y() ), 20.0, 95.0 ); } } // make sure maxcharangleout is always negative @@ -2592,7 +2592,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext double priorityD = exprVal.toDouble( &ok ); if ( ok ) { - priorityD = qBound( 0.0, priorityD, 10.0 ); + priorityD = std::clamp( priorityD, 0.0, 10.0 ); priorityD = 1 - priorityD / 10.0; // convert 0..10 --> 1..0 ( *labelFeature )->setPriority( priorityD ); } diff --git a/src/core/qgscolorramp.cpp b/src/core/qgscolorramp.cpp index c9e1f575aac..f97fc1d0651 100644 --- a/src/core/qgscolorramp.cpp +++ b/src/core/qgscolorramp.cpp @@ -411,9 +411,9 @@ QList QgsLimitedRandomColorRamp::randomColors( int count, //see http://basecase.org/env/on-rainbows for more details currentHueAngle += 137.50776; //scale hue to between hueMax and hueMin - h = qBound( 0.0, std::round( ( std::fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 359.0 ); - s = qBound( 0, ( qrand() % ( safeSatMax - safeSatMin + 1 ) ) + safeSatMin, 255 ); - v = qBound( 0, ( qrand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 255 ); + h = std::clamp( std::round( ( std::fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 0.0, 359.0 ); + s = std::clamp( ( qrand() % ( safeSatMax - safeSatMin + 1 ) ) + safeSatMin, 0, 255 ); + v = std::clamp( ( qrand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 0, 255 ); colors.append( QColor::fromHsv( h, s, v ) ); } return colors; diff --git a/src/core/qgspropertytransformer.cpp b/src/core/qgspropertytransformer.cpp index a60e7ecccb1..08e3a44672c 100644 --- a/src/core/qgspropertytransformer.cpp +++ b/src/core/qgspropertytransformer.cpp @@ -178,13 +178,13 @@ bool QgsGenericNumericTransformer::loadVariant( const QVariant &transformer ) double QgsGenericNumericTransformer::value( double input ) const { if ( qgsDoubleNear( mMaxValue, mMinValue ) ) - return qBound( mMinOutput, input, mMaxOutput ); + return std::clamp( input, mMinOutput, mMaxOutput ); input = transformNumeric( input ); if ( qgsDoubleNear( mExponent, 1.0 ) ) - return mMinOutput + ( qBound( mMinValue, input, mMaxValue ) - mMinValue ) * ( mMaxOutput - mMinOutput ) / ( mMaxValue - mMinValue ); + return mMinOutput + ( std::clamp( input, mMinValue, mMaxValue ) - mMinValue ) * ( mMaxOutput - mMinOutput ) / ( mMaxValue - mMinValue ); else - return mMinOutput + std::pow( qBound( mMinValue, input, mMaxValue ) - mMinValue, mExponent ) * ( mMaxOutput - mMinOutput ) / std::pow( mMaxValue - mMinValue, mExponent ); + return mMinOutput + std::pow( std::clamp( input, mMinValue, mMaxValue ) - mMinValue, mExponent ) * ( mMaxOutput - mMinOutput ) / std::pow( mMaxValue - mMinValue, mExponent ); } QVariant QgsGenericNumericTransformer::transform( const QgsExpressionContext &context, const QVariant &v ) const @@ -360,12 +360,12 @@ double QgsSizeScaleTransformer::size( double value ) const switch ( mType ) { case Linear: - return mMinSize + ( qBound( mMinValue, value, mMaxValue ) - mMinValue ) * ( mMaxSize - mMinSize ) / ( mMaxValue - mMinValue ); + return mMinSize + ( std::clamp( value, mMinValue, mMaxValue ) - mMinValue ) * ( mMaxSize - mMinSize ) / ( mMaxValue - mMinValue ); case Area: case Flannery: case Exponential: - return mMinSize + std::pow( qBound( mMinValue, value, mMaxValue ) - mMinValue, mExponent ) * ( mMaxSize - mMinSize ) / std::pow( mMaxValue - mMinValue, mExponent ); + return mMinSize + std::pow( std::clamp( value, mMinValue, mMaxValue ) - mMinValue, mExponent ) * ( mMaxSize - mMinSize ) / std::pow( mMaxValue - mMinValue, mExponent ); } return 0; @@ -631,7 +631,7 @@ QString QgsColorRampTransformer::toExpression( const QString &baseExpression ) c QColor QgsColorRampTransformer::color( double value ) const { value = transformNumeric( value ); - double scaledVal = qBound( 0.0, ( value - mMinValue ) / ( mMaxValue - mMinValue ), 1.0 ); + double scaledVal = std::clamp( ( value - mMinValue ) / ( mMaxValue - mMinValue ), 0.0, 1.0 ); if ( !mGradientRamp ) return mNullColor; @@ -708,8 +708,8 @@ void QgsCurveTransform::setControlPoints( const QList &points ) std::sort( mControlPoints.begin(), mControlPoints.end(), sortByX ); for ( int i = 0; i < mControlPoints.count(); ++i ) { - mControlPoints[ i ] = QgsPointXY( qBound( 0.0, mControlPoints.at( i ).x(), 1.0 ), - qBound( 0.0, mControlPoints.at( i ).y(), 1.0 ) ); + mControlPoints[ i ] = QgsPointXY( std::clamp( mControlPoints.at( i ).x(), 0.0, 1.0 ), + std::clamp( mControlPoints.at( i ).y(), 0.0, 1.0 ) ); } calcSecondDerivativeArray(); } @@ -747,27 +747,27 @@ double QgsCurveTransform::y( double x ) const { int n = mControlPoints.count(); if ( n < 2 ) - return qBound( 0.0, x, 1.0 ); // invalid + return std::clamp( x, 0.0, 1.0 ); // invalid else if ( n < 3 ) { // linear if ( x <= mControlPoints.at( 0 ).x() ) - return qBound( 0.0, mControlPoints.at( 0 ).y(), 1.0 ); + return std::clamp( mControlPoints.at( 0 ).y(), 0.0, 1.0 ); else if ( x >= mControlPoints.at( n - 1 ).x() ) - return qBound( 0.0, mControlPoints.at( 1 ).y(), 1.0 ); + return std::clamp( mControlPoints.at( 1 ).y(), 0.0, 1.0 ); else { double dx = mControlPoints.at( 1 ).x() - mControlPoints.at( 0 ).x(); double dy = mControlPoints.at( 1 ).y() - mControlPoints.at( 0 ).y(); - return qBound( 0.0, ( x - mControlPoints.at( 0 ).x() ) * ( dy / dx ) + mControlPoints.at( 0 ).y(), 1.0 ); + return std::clamp( ( x - mControlPoints.at( 0 ).x() ) * ( dy / dx ) + mControlPoints.at( 0 ).y(), 0.0, 1.0 ); } } // safety check if ( x <= mControlPoints.at( 0 ).x() ) - return qBound( 0.0, mControlPoints.at( 0 ).y(), 1.0 ); + return std::clamp( mControlPoints.at( 0 ).y(), 0.0, 1.0 ); if ( x >= mControlPoints.at( n - 1 ).x() ) - return qBound( 0.0, mControlPoints.at( n - 1 ).y(), 1.0 ); + return std::clamp( mControlPoints.at( n - 1 ).y(), 0.0, 1.0 ); // find corresponding segment QList::const_iterator pointIt = mControlPoints.constBegin(); @@ -785,8 +785,8 @@ double QgsCurveTransform::y( double x ) const double a = 1 - t; - return qBound( 0.0, a * currentControlPoint.y() + t * nextControlPoint.y() + ( h * h / 6 ) * ( ( a * a * a - a ) * mSecondDerivativeArray[i] + ( t * t * t - t ) * mSecondDerivativeArray[i + 1] ), - 1.0 ); + return std::clamp( a * currentControlPoint.y() + t * nextControlPoint.y() + ( h * h / 6 ) * ( ( a * a * a - a ) * mSecondDerivativeArray[i] + ( t * t * t - t ) * mSecondDerivativeArray[i + 1] ), + 0.0, 1.0 ); } ++pointIt; @@ -798,7 +798,7 @@ double QgsCurveTransform::y( double x ) const } //should not happen - return qBound( 0.0, x, 1.0 ); + return std::clamp( x, 0.0, 1.0 ); } // this code is adapted from https://github.com/OpenFibers/Photoshop-Curves @@ -831,7 +831,7 @@ QVector QgsCurveTransform::y( const QVector &x ) const // safety check while ( currentX <= currentControlPoint.x() ) { - result << qBound( 0.0, currentControlPoint.y(), 1.0 ); + result << std::clamp( currentControlPoint.y(), 0.0, 1.0 ); xIndex++; currentX = x.at( xIndex ); } @@ -847,7 +847,7 @@ QVector QgsCurveTransform::y( const QVector &x ) const double a = 1 - t; - result << qBound( 0.0, a * currentControlPoint.y() + t * nextControlPoint.y() + ( h * h / 6 ) * ( ( a * a * a - a )*mSecondDerivativeArray[i] + ( t * t * t - t )*mSecondDerivativeArray[i + 1] ), 1.0 ); + result << std::clamp( a * currentControlPoint.y() + t * nextControlPoint.y() + ( h * h / 6 ) * ( ( a * a * a - a )*mSecondDerivativeArray[i] + ( t * t * t - t )*mSecondDerivativeArray[i + 1] ), 0.0, 1.0 ); xIndex++; if ( xIndex == x.count() ) return result; @@ -866,7 +866,7 @@ QVector QgsCurveTransform::y( const QVector &x ) const // safety check while ( xIndex < x.count() ) { - result << qBound( 0.0, nextControlPoint.y(), 1.0 ); + result << std::clamp( nextControlPoint.y(), 0.0, 1.0 ); xIndex++; } diff --git a/src/core/qgstiles.cpp b/src/core/qgstiles.cpp index 3beade14fca..4764d4bafb7 100644 --- a/src/core/qgstiles.cpp +++ b/src/core/qgstiles.cpp @@ -53,10 +53,10 @@ QgsPointXY QgsTileMatrix::tileCenter( QgsTileXYZ id ) const QgsTileRange QgsTileMatrix::tileRangeFromExtent( const QgsRectangle &r ) { - double x0 = qBound( mExtent.xMinimum(), r.xMinimum(), mExtent.xMaximum() ); - double y0 = qBound( mExtent.yMinimum(), r.yMinimum(), mExtent.yMaximum() ); - double x1 = qBound( mExtent.xMinimum(), r.xMaximum(), mExtent.xMaximum() ); - double y1 = qBound( mExtent.yMinimum(), r.yMaximum(), mExtent.yMaximum() ); + double x0 = std::clamp( r.xMinimum(), mExtent.xMinimum(), mExtent.xMaximum() ); + double y0 = std::clamp( r.yMinimum(), mExtent.yMinimum(), mExtent.yMaximum() ); + double x1 = std::clamp( r.xMaximum(), mExtent.xMinimum(), mExtent.xMaximum() ); + double y1 = std::clamp( r.yMaximum(), mExtent.yMinimum(), mExtent.yMaximum() ); if ( x0 >= x1 || y0 >= y1 ) return QgsTileRange(); // nothing to display @@ -68,10 +68,10 @@ QgsTileRange QgsTileMatrix::tileRangeFromExtent( const QgsRectangle &r ) QgsDebugMsgLevel( QStringLiteral( "Tile range of edges [%1,%2] - [%3,%4]" ).arg( tileX1 ).arg( tileY1 ).arg( tileX2 ).arg( tileY2 ), 2 ); // figure out tile range from zoom - int startColumn = qBound( 0, static_cast( floor( tileX1 ) ), mMatrixWidth - 1 ); - int endColumn = qBound( 0, static_cast( floor( tileX2 ) ), mMatrixWidth - 1 ); - int startRow = qBound( 0, static_cast( floor( tileY1 ) ), mMatrixHeight - 1 ); - int endRow = qBound( 0, static_cast( floor( tileY2 ) ), mMatrixHeight - 1 ); + int startColumn = std::clamp( static_cast( floor( tileX1 ) ), 0, mMatrixWidth - 1 ); + int endColumn = std::clamp( static_cast( floor( tileX2 ) ), 0, mMatrixWidth - 1 ); + int startRow = std::clamp( static_cast( floor( tileY1 ) ), 0, mMatrixHeight - 1 ); + int endRow = std::clamp( static_cast( floor( tileY2 ) ), 0, mMatrixHeight - 1 ); return QgsTileRange( startColumn, endColumn, startRow, endRow ); } diff --git a/src/core/raster/qgsbrightnesscontrastfilter.cpp b/src/core/raster/qgsbrightnesscontrastfilter.cpp index 9862caf5a7a..1cff8bb4423 100644 --- a/src/core/raster/qgsbrightnesscontrastfilter.cpp +++ b/src/core/raster/qgsbrightnesscontrastfilter.cpp @@ -163,12 +163,27 @@ QgsRasterBlock *QgsBrightnessContrastFilter::block( int bandNo, QgsRectangle co return outputBlock.release(); } +void QgsBrightnessContrastFilter::setBrightness( int brightness ) +{ + mBrightness = std::clamp( brightness, -255, 255 ); +} + +void QgsBrightnessContrastFilter::setContrast( int contrast ) +{ + mContrast = std::clamp( contrast, -100, 100 ); +} + +void QgsBrightnessContrastFilter::setGamma( double gamma ) +{ + mGamma = std::clamp( gamma, 0.1, 10.0 ); +} + int QgsBrightnessContrastFilter::adjustColorComponent( int colorComponent, int alpha, int brightness, double contrastFactor, double gammaCorrection ) const { if ( alpha == 255 ) { // Opaque pixel, do simpler math - return qBound( 0, ( int )( 255 * std::pow( ( ( ( ( ( ( colorComponent / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ) / 255.0, gammaCorrection ) ), 255 ); + return std::clamp( ( int )( 255 * std::pow( ( ( ( ( ( ( colorComponent / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ) / 255.0, gammaCorrection ) ), 0, 255 ); } else if ( alpha == 0 ) { @@ -183,7 +198,7 @@ int QgsBrightnessContrastFilter::adjustColorComponent( int colorComponent, int a double adjustedColor = colorComponent / alphaFactor; // Make sure to return a premultiplied color - return alphaFactor * qBound( 0., 255 * std::pow( ( ( ( ( ( ( adjustedColor / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ) / 255, gammaCorrection ), 255. ); + return alphaFactor * std::clamp( 255 * std::pow( ( ( ( ( ( ( adjustedColor / 255.0 ) - 0.5 ) * contrastFactor ) + 0.5 ) * 255 ) + brightness ) / 255, gammaCorrection ), 0., 255. ); } } diff --git a/src/core/raster/qgsbrightnesscontrastfilter.h b/src/core/raster/qgsbrightnesscontrastfilter.h index d3516aa4d59..d32f81cfeed 100644 --- a/src/core/raster/qgsbrightnesscontrastfilter.h +++ b/src/core/raster/qgsbrightnesscontrastfilter.h @@ -64,7 +64,7 @@ class CORE_EXPORT QgsBrightnessContrastFilter : public QgsRasterInterface * Set brightness level. Acceptable value range is -255…255 * \see brightness() */ - void setBrightness( int brightness ) { mBrightness = qBound( -255, brightness, 255 ); } + void setBrightness( int brightness ); /** * Returns current brightness level. @@ -76,7 +76,7 @@ class CORE_EXPORT QgsBrightnessContrastFilter : public QgsRasterInterface * Set contrast level. Acceptable value range is -100…100 * \see contrast() */ - void setContrast( int contrast ) { mContrast = qBound( -100, contrast, 100 ); } + void setContrast( int contrast ); /** * Returns current contrast level. @@ -90,7 +90,7 @@ class CORE_EXPORT QgsBrightnessContrastFilter : public QgsRasterInterface * * \since QGIS 3.16 */ - void setGamma( double gamma ) { mGamma = qBound( 0.1, gamma, 10.0 ); } + void setGamma( double gamma ); /** * Returns current gamma value. diff --git a/src/core/raster/qgshillshaderenderer.cpp b/src/core/raster/qgshillshaderenderer.cpp index 8d5dba1be86..679a4f07d04 100644 --- a/src/core/raster/qgshillshaderenderer.cpp +++ b/src/core/raster/qgshillshaderenderer.cpp @@ -448,11 +448,11 @@ QgsRasterBlock *QgsHillshadeRenderer::block( int bandNo, const QgsRectangle &ext if ( !mMultiDirectional ) { // Standard single direction hillshade - grayValue = qBound( 0.0, ( sin_altRadians_mul_254 - - ( derY * cos_az_mul_cos_alt_mul_z_mul_254 - - derX * sin_az_mul_cos_alt_mul_z_mul_254 ) ) / - std::sqrt( 1 + square_z * ( derX * derX + derY * derY ) ) - , 255.0 ); + grayValue = std::clamp( ( sin_altRadians_mul_254 - + ( derY * cos_az_mul_cos_alt_mul_z_mul_254 - + derX * sin_az_mul_cos_alt_mul_z_mul_254 ) ) / + std::sqrt( 1 + square_z * ( derX * derX + derY * derY ) ), + 0.0, 255.0 ); } else { @@ -464,7 +464,7 @@ QgsRasterBlock *QgsHillshadeRenderer::block( int bandNo, const QgsRectangle &ext // Flat? if ( xx_plus_yy == 0.0 ) { - grayValue = qBound( 0.0f, static_cast( 1.0 + sin_altRadians_mul_254 ), 255.0f ); + grayValue = std::clamp( static_cast( 1.0 + sin_altRadians_mul_254 ), 0.0f, 255.0f ); } else { @@ -494,7 +494,7 @@ QgsRasterBlock *QgsHillshadeRenderer::block( int bandNo, const QgsRectangle &ext weight_360 * val360_mul_127 ) / xx_plus_yy ) / ( 1 + square_z * xx_plus_yy ); - grayValue = qBound( 0.0f, 1.0f + cang_mul_127, 255.0f ); + grayValue = std::clamp( 1.0f + cang_mul_127, 0.0f, 255.0f ); } } diff --git a/src/core/raster/qgshuesaturationfilter.cpp b/src/core/raster/qgshuesaturationfilter.cpp index 5c0e5aaa613..ab7b158ec3c 100644 --- a/src/core/raster/qgshuesaturationfilter.cpp +++ b/src/core/raster/qgshuesaturationfilter.cpp @@ -313,7 +313,7 @@ void QgsHueSaturationFilter::processSaturation( int &r, int &g, int &b, int &h, void QgsHueSaturationFilter::setSaturation( int saturation ) { - mSaturation = qBound( -100, saturation, 100 ); + mSaturation = std::clamp( saturation, -100, 100 ); // Scale saturation value to [0-2], where 0 = desaturated mSaturationScale = ( ( double ) mSaturation / 100 ) + 1; diff --git a/src/core/symbology/qgsrendererrange.cpp b/src/core/symbology/qgsrendererrange.cpp index 789763f0c61..c5139a44a12 100644 --- a/src/core/symbology/qgsrendererrange.cpp +++ b/src/core/symbology/qgsrendererrange.cpp @@ -193,7 +193,7 @@ bool QgsRendererRangeLabelFormat::operator!=( const QgsRendererRangeLabelFormat void QgsRendererRangeLabelFormat::setPrecision( int precision ) { // Limit the range of decimal places to a reasonable range - precision = qBound( MIN_PRECISION, precision, MAX_PRECISION ); + precision = std::clamp( precision, MIN_PRECISION, MAX_PRECISION ); mPrecision = precision; mNumberScale = 1.0; mNumberSuffix.clear(); diff --git a/src/core/vector/qgsvectorlayerdiagramprovider.cpp b/src/core/vector/qgsvectorlayerdiagramprovider.cpp index 842e920f9c1..4104f1d7601 100644 --- a/src/core/vector/qgsvectorlayerdiagramprovider.cpp +++ b/src/core/vector/qgsvectorlayerdiagramprovider.cpp @@ -314,7 +314,7 @@ QgsLabelFeature *QgsVectorLayerDiagramProvider::registerDiagram( QgsFeature &fea { context.expressionContext().setOriginalValueVariable( mSettings.priority() ); double priorityD = mSettings.dataDefinedProperties().valueAsDouble( QgsDiagramLayerSettings::Priority, context.expressionContext(), mSettings.priority() ); - priorityD = qBound( 0.0, priorityD, 10.0 ); + priorityD = std::clamp( priorityD, 0.0, 10.0 ); priorityD = 1 - priorityD / 10.0; // convert 0..10 --> 1..0 lf->setPriority( priorityD ); } diff --git a/src/gui/layout/qgslayoutview.cpp b/src/gui/layout/qgslayoutview.cpp index fec49b64bbf..7cb93caf5a2 100644 --- a/src/gui/layout/qgslayoutview.cpp +++ b/src/gui/layout/qgslayoutview.cpp @@ -190,7 +190,7 @@ void QgsLayoutView::scaleSafe( double scale ) { double currentScale = transform().m11(); scale *= currentScale; - scale = qBound( MIN_VIEW_SCALE, scale, MAX_VIEW_SCALE ); + scale = std::clamp( scale, MIN_VIEW_SCALE, MAX_VIEW_SCALE ); setTransform( QTransform::fromScale( scale, scale ) ); emit zoomLevelChanged(); viewChanged(); @@ -213,7 +213,7 @@ void QgsLayoutView::setZoomLevel( double level ) dpi = 72; //desired pixel width for 1mm on screen - level = qBound( MIN_VIEW_SCALE, level, MAX_VIEW_SCALE ); + level = std::clamp( level, MIN_VIEW_SCALE, MAX_VIEW_SCALE ); double mmLevel = currentLayout()->convertFromLayoutUnits( level, QgsUnitTypes::LayoutMillimeters ).length() * dpi / 25.4; setTransform( QTransform::fromScale( mmLevel, mmLevel ) ); } diff --git a/src/gui/processing/models/qgsmodelgraphicsview.cpp b/src/gui/processing/models/qgsmodelgraphicsview.cpp index aee41d174c7..14d25229d4e 100644 --- a/src/gui/processing/models/qgsmodelgraphicsview.cpp +++ b/src/gui/processing/models/qgsmodelgraphicsview.cpp @@ -170,7 +170,7 @@ void QgsModelGraphicsView::scaleSafe( double scale ) { double currentScale = transform().m11(); scale *= currentScale; - scale = qBound( MIN_VIEW_SCALE, scale, MAX_VIEW_SCALE ); + scale = std::clamp( scale, MIN_VIEW_SCALE, MAX_VIEW_SCALE ); setTransform( QTransform::fromScale( scale, scale ) ); } diff --git a/src/gui/qgsfloatingwidget.cpp b/src/gui/qgsfloatingwidget.cpp index 9dc79362074..6d4c50cca9f 100644 --- a/src/gui/qgsfloatingwidget.cpp +++ b/src/gui/qgsfloatingwidget.cpp @@ -183,7 +183,7 @@ void QgsFloatingWidget::onAnchorPointChanged() } // constrain x so that widget floats within parent widget - anchorX = qBound( 0, anchorX, parentWidget()->width() - width() ); + anchorX = std::clamp( anchorX, 0, parentWidget()->width() - width() ); move( anchorX, anchorY ); } diff --git a/src/gui/qgsgradientcolorrampdialog.cpp b/src/gui/qgsgradientcolorrampdialog.cpp index 2ea7d21b165..d7b28d0e88c 100644 --- a/src/gui/qgsgradientcolorrampdialog.cpp +++ b/src/gui/qgsgradientcolorrampdialog.cpp @@ -445,15 +445,15 @@ void QgsGradientColorRampDialog::plotMouseMove( QPointF point ) QColor newColor = mStopEditor->selectedStop().color; if ( mCurrentPlotColorComponent == 0 ) - newColor = QColor::fromHslF( qBound( qreal( 0.0 ), point.y(), qreal( 1.0 ) ), newColor.hslSaturationF(), newColor.lightnessF(), newColor.alphaF() ); + newColor = QColor::fromHslF( std::clamp( point.y(), qreal( 0.0 ), qreal( 1.0 ) ), newColor.hslSaturationF(), newColor.lightnessF(), newColor.alphaF() ); else if ( mCurrentPlotColorComponent == 1 ) - newColor = QColor::fromHslF( newColor.hslHueF(), newColor.hslSaturationF(), qBound( qreal( 0.0 ), point.y(), qreal( 1.0 ) ), newColor.alphaF() ); + newColor = QColor::fromHslF( newColor.hslHueF(), newColor.hslSaturationF(), std::clamp( point.y(), qreal( 0.0 ), qreal( 1.0 ) ), newColor.alphaF() ); else if ( mCurrentPlotColorComponent == 2 ) - newColor = QColor::fromHslF( newColor.hslHueF(), qBound( qreal( 0.0 ), point.y(), qreal( 1.0 ) ), newColor.lightnessF(), newColor.alphaF() ); + newColor = QColor::fromHslF( newColor.hslHueF(), std::clamp( point.y(), qreal( 0.0 ), qreal( 1.0 ) ), newColor.lightnessF(), newColor.alphaF() ); else if ( mCurrentPlotColorComponent == 3 ) - newColor = QColor::fromHslF( newColor.hslHueF(), newColor.hslSaturationF(), newColor.lightnessF(), qBound( qreal( 0.0 ), point.y(), qreal( 1.0 ) ) ); + newColor = QColor::fromHslF( newColor.hslHueF(), newColor.hslSaturationF(), newColor.lightnessF(), std::clamp( point.y(), qreal( 0.0 ), qreal( 1.0 ) ) ); - mStopEditor->setSelectedStopDetails( newColor, qBound( qreal( 0.0 ), point.x(), qreal( 1.0 ) ) ); + mStopEditor->setSelectedStopDetails( newColor, std::clamp( point.x(), qreal( 0.0 ), qreal( 1.0 ) ) ); } bool byX( QPointF p1, QPointF p2 ) diff --git a/src/gui/qgsgradientstopeditor.cpp b/src/gui/qgsgradientstopeditor.cpp index a294a8f3d07..349dca0e7dc 100644 --- a/src/gui/qgsgradientstopeditor.cpp +++ b/src/gui/qgsgradientstopeditor.cpp @@ -365,7 +365,7 @@ void QgsGradientStopEditor::keyPressEvent( QKeyEvent *e ) if ( e->key() == Qt::Key_Left ) offsetDiff *= -1; - mStops[ mSelectedStop - 1 ].offset = qBound( 0.0, mStops[ mSelectedStop - 1 ].offset + offsetDiff, 1.0 ); + mStops[ mSelectedStop - 1 ].offset = std::clamp( mStops[ mSelectedStop - 1 ].offset + offsetDiff, 0.0, 1.0 ); mGradient.setStops( mStops ); update(); e->accept(); diff --git a/src/gui/qgshighlight.cpp b/src/gui/qgshighlight.cpp index c09ad7caca8..1cbf98a545a 100644 --- a/src/gui/qgshighlight.cpp +++ b/src/gui/qgshighlight.cpp @@ -372,7 +372,7 @@ void QgsHighlight::paint( QPainter *p ) if ( alpha > 0 ) { int green = qGreen( line[c] ); - line[c] = qRgba( penRed, penGreen, penBlue, qBound( 0, alpha - ( green * k ), 255 ) ); + line[c] = qRgba( penRed, penGreen, penBlue, std::clamp( static_cast< int >( alpha - ( green * k ) ), 0, 255 ) ); } } } diff --git a/src/gui/qgsmapcanvas.cpp b/src/gui/qgsmapcanvas.cpp index 6b44d151a39..c31d0b809ca 100644 --- a/src/gui/qgsmapcanvas.cpp +++ b/src/gui/qgsmapcanvas.cpp @@ -290,7 +290,7 @@ void QgsMapCanvas::setMagnificationFactor( double factor, const QgsPointXY *cent // do not go higher or lower than min max magnification ratio double magnifierMin = QgsGuiUtils::CANVAS_MAGNIFICATION_MIN; double magnifierMax = QgsGuiUtils::CANVAS_MAGNIFICATION_MAX; - factor = qBound( magnifierMin, factor, magnifierMax ); + factor = std::clamp( factor, magnifierMin, magnifierMax ); // the magnifier widget is in integer percent if ( !qgsDoubleNear( factor, mSettings.magnificationFactor(), 0.01 ) ) @@ -1151,7 +1151,7 @@ void QgsMapCanvas::setExtent( const QgsRectangle &r, bool magnified ) ScaleRestorer restorer( this ); const double ratio { extent().width() / extent().height() }; const double factor { r.width() / r.height() > ratio ? extent().width() / r.width() : extent().height() / r.height() }; - const double scaleFactor { qBound( QgsGuiUtils::CANVAS_MAGNIFICATION_MIN, mSettings.magnificationFactor() * factor, QgsGuiUtils::CANVAS_MAGNIFICATION_MAX ) }; + const double scaleFactor { std::clamp( mSettings.magnificationFactor() * factor, QgsGuiUtils::CANVAS_MAGNIFICATION_MIN, QgsGuiUtils::CANVAS_MAGNIFICATION_MAX ) }; const QgsPointXY newCenter { r.center() }; mSettings.setMagnificationFactor( scaleFactor, &newCenter ); emit magnificationChanged( scaleFactor ); diff --git a/src/gui/tableeditor/qgstableeditorwidget.cpp b/src/gui/tableeditor/qgstableeditorwidget.cpp index efd528db74a..f346f66162c 100644 --- a/src/gui/tableeditor/qgstableeditorwidget.cpp +++ b/src/gui/tableeditor/qgstableeditorwidget.cpp @@ -1364,8 +1364,8 @@ void QgsTableEditorTextEdit::resizeToContents() int parentWidth = parent->width(); int maxWidth = isRightToLeft() ? position.x() + oldWidth : parentWidth - position.x(); int maxHeight = parent->height() - position.y(); - int newWidth = qBound( mOriginalWidth, hintWidth, maxWidth ); - int newHeight = qBound( mOriginalHeight, hintHeight, maxHeight ); + int newWidth = std::clamp( hintWidth, mOriginalWidth, maxWidth ); + int newHeight = std::clamp( hintHeight, mOriginalHeight, maxHeight ); if ( mWidgetOwnsGeometry ) { diff --git a/src/providers/wms/qgswmscapabilities.cpp b/src/providers/wms/qgswmscapabilities.cpp index 8dda3d2aadc..4d50741d3e1 100644 --- a/src/providers/wms/qgswmscapabilities.cpp +++ b/src/providers/wms/qgswmscapabilities.cpp @@ -2567,10 +2567,10 @@ void QgsWmtsTileMatrix::viewExtentIntersection( const QgsRectangle &viewExtent, maxTileRow = tml->maxTileRow; } - col0 = qBound( minTileCol, ( int ) std::floor( ( viewExtent.xMinimum() - topLeft.x() ) / twMap ), maxTileCol ); - row0 = qBound( minTileRow, ( int ) std::floor( ( topLeft.y() - viewExtent.yMaximum() ) / thMap ), maxTileRow ); - col1 = qBound( minTileCol, ( int ) std::floor( ( viewExtent.xMaximum() - topLeft.x() ) / twMap ), maxTileCol ); - row1 = qBound( minTileRow, ( int ) std::floor( ( topLeft.y() - viewExtent.yMinimum() ) / thMap ), maxTileRow ); + col0 = std::clamp( ( int ) std::floor( ( viewExtent.xMinimum() - topLeft.x() ) / twMap ), minTileCol, maxTileCol ); + row0 = std::clamp( ( int ) std::floor( ( topLeft.y() - viewExtent.yMaximum() ) / thMap ), minTileRow, maxTileRow ); + col1 = std::clamp( ( int ) std::floor( ( viewExtent.xMaximum() - topLeft.x() ) / twMap ), minTileCol, maxTileCol ); + row1 = std::clamp( ( int ) std::floor( ( topLeft.y() - viewExtent.yMinimum() ) / thMap ), minTileRow, maxTileRow ); } const QgsWmtsTileMatrix *QgsWmtsTileMatrixSet::findNearestResolution( double vres ) const diff --git a/tests/code_layout/test_banned_keywords.sh b/tests/code_layout/test_banned_keywords.sh index cd797703626..4b5140ed027 100755 --- a/tests/code_layout/test_banned_keywords.sh +++ b/tests/code_layout/test_banned_keywords.sh @@ -137,6 +137,9 @@ HINTS[37]="Use range based for loops instead" KEYWORDS[38]="foreach" HINTS[38]="Use range based for loops instead" +KEYWORDS[39]="\bqBound(" +HINTS[39]="Use std::clamp instead (but be careful of the different argument order!!)" + RES= DIR=$(git rev-parse --show-toplevel) diff --git a/tests/qt_modeltest/modeltest.cpp b/tests/qt_modeltest/modeltest.cpp index de63369b4ac..ebdbc185dfd 100644 --- a/tests/qt_modeltest/modeltest.cpp +++ b/tests/qt_modeltest/modeltest.cpp @@ -511,7 +511,7 @@ void ModelTest::rowsInserted( const QModelIndex &parent, int start, int end ) void ModelTest::layoutAboutToBeChanged() { - for ( int i = 0; i < qBound( 0, model->rowCount(), 100 ); ++i ) + for ( int i = 0; i < std::clamp( model->rowCount(), 0, 100 ); ++i ) changing.append( QPersistentModelIndex( model->index( i, 0 ) ) ); }