diff --git a/python/PyQt6/core/auto_additions/qgsscaleutils.py b/python/PyQt6/core/auto_additions/qgsscaleutils.py index 16e1f921bc36..498b6b854fee 100644 --- a/python/PyQt6/core/auto_additions/qgsscaleutils.py +++ b/python/PyQt6/core/auto_additions/qgsscaleutils.py @@ -2,5 +2,7 @@ try: QgsScaleUtils.saveScaleList = staticmethod(QgsScaleUtils.saveScaleList) QgsScaleUtils.loadScaleList = staticmethod(QgsScaleUtils.loadScaleList) + QgsScaleUtils.equalToOrGreaterThanMinimumScale = staticmethod(QgsScaleUtils.equalToOrGreaterThanMinimumScale) + QgsScaleUtils.lessThanMaximumScale = staticmethod(QgsScaleUtils.lessThanMaximumScale) except NameError: pass diff --git a/python/PyQt6/core/auto_generated/qgis.sip.in b/python/PyQt6/core/auto_generated/qgis.sip.in index f9b27aeabdca..d39314ce0a58 100644 --- a/python/PyQt6/core/auto_generated/qgis.sip.in +++ b/python/PyQt6/core/auto_generated/qgis.sip.in @@ -3511,36 +3511,6 @@ QVariant data types (such as strings, numeric values, dates and times) .. seealso:: :py:func:`qgsVariantLessThan` %End -bool qgsEqualToOrGreaterThanMinimumScale( const double scale, const double minScale ); -%Docstring -Returns whether the ``scale`` is equal to or greater than the ``minScale``, -taking non-round numbers into account. - -:param scale: The current scale to be compared. -:param minScale: The minimum map scale (i.e. most "zoomed out" scale) at - which features, labels or diagrams will be visible. The scale value - indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. - -.. seealso:: :py:func:`qgsLessThanMaximumScale` - -.. versionadded:: 3.40 -%End - -bool qgsLessThanMaximumScale( const double scale, const double maxScale ); -%Docstring -Returns whether the ``scale`` is less than the ``maxScale``, taking non-round -numbers into account. - -:param scale: The current scale to be compared. -:param maxScale: The maximum map scale (i.e. most "zoomed in" scale) at which - features, labels or diagrams will be visible. The scale value indicates the - scale denominator, e.g. 1000.0 for a 1:1000 map. - -.. seealso:: :py:func:`qgsEqualToOrGreaterThanMinimumScale` - -.. versionadded:: 3.40 -%End - bool operator> ( const QVariant &v1, const QVariant &v2 ); diff --git a/python/PyQt6/core/auto_generated/qgsscaleutils.sip.in b/python/PyQt6/core/auto_generated/qgsscaleutils.sip.in index 23b5d395e799..8b4fddb232ca 100644 --- a/python/PyQt6/core/auto_generated/qgsscaleutils.sip.in +++ b/python/PyQt6/core/auto_generated/qgsscaleutils.sip.in @@ -42,6 +42,36 @@ Load scales from the given file went wrong :return: ``True`` on success and ``False`` if failed +%End + + static bool equalToOrGreaterThanMinimumScale( const double scale, const double minScale ); +%Docstring +Returns whether the ``scale`` is equal to or greater than the ``minScale``, +taking non-round numbers into account. + +:param scale: The current scale to be compared. +:param minScale: The minimum map scale (i.e. most "zoomed out" scale) at + which features, labels or diagrams will be visible. The scale value + indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. + +.. seealso:: :py:func:`lessThanMaximumScale` + +.. versionadded:: 3.40 +%End + + static bool lessThanMaximumScale( const double scale, const double maxScale ); +%Docstring +Returns whether the ``scale`` is less than the ``maxScale``, taking non-round +numbers into account. + +:param scale: The current scale to be compared. +:param maxScale: The maximum map scale (i.e. most "zoomed in" scale) at which + features, labels or diagrams will be visible. The scale value indicates the + scale denominator, e.g. 1000.0 for a 1:1000 map. + +.. seealso:: :py:func:`equalToOrGreaterThanMinimumScale` + +.. versionadded:: 3.40 %End }; diff --git a/python/core/auto_additions/qgsscaleutils.py b/python/core/auto_additions/qgsscaleutils.py index 16e1f921bc36..498b6b854fee 100644 --- a/python/core/auto_additions/qgsscaleutils.py +++ b/python/core/auto_additions/qgsscaleutils.py @@ -2,5 +2,7 @@ try: QgsScaleUtils.saveScaleList = staticmethod(QgsScaleUtils.saveScaleList) QgsScaleUtils.loadScaleList = staticmethod(QgsScaleUtils.loadScaleList) + QgsScaleUtils.equalToOrGreaterThanMinimumScale = staticmethod(QgsScaleUtils.equalToOrGreaterThanMinimumScale) + QgsScaleUtils.lessThanMaximumScale = staticmethod(QgsScaleUtils.lessThanMaximumScale) except NameError: pass diff --git a/python/core/auto_generated/qgis.sip.in b/python/core/auto_generated/qgis.sip.in index 74051bf9edfe..d8de59fa7ec6 100644 --- a/python/core/auto_generated/qgis.sip.in +++ b/python/core/auto_generated/qgis.sip.in @@ -3511,36 +3511,6 @@ QVariant data types (such as strings, numeric values, dates and times) .. seealso:: :py:func:`qgsVariantLessThan` %End -bool qgsEqualToOrGreaterThanMinimumScale( const double scale, const double minScale ); -%Docstring -Returns whether the ``scale`` is equal to or greater than the ``minScale``, -taking non-round numbers into account. - -:param scale: The current scale to be compared. -:param minScale: The minimum map scale (i.e. most "zoomed out" scale) at - which features, labels or diagrams will be visible. The scale value - indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. - -.. seealso:: :py:func:`qgsLessThanMaximumScale` - -.. versionadded:: 3.40 -%End - -bool qgsLessThanMaximumScale( const double scale, const double maxScale ); -%Docstring -Returns whether the ``scale`` is less than the ``maxScale``, taking non-round -numbers into account. - -:param scale: The current scale to be compared. -:param maxScale: The maximum map scale (i.e. most "zoomed in" scale) at which - features, labels or diagrams will be visible. The scale value indicates the - scale denominator, e.g. 1000.0 for a 1:1000 map. - -.. seealso:: :py:func:`qgsEqualToOrGreaterThanMinimumScale` - -.. versionadded:: 3.40 -%End - bool operator> ( const QVariant &v1, const QVariant &v2 ); diff --git a/python/core/auto_generated/qgsscaleutils.sip.in b/python/core/auto_generated/qgsscaleutils.sip.in index 23b5d395e799..8b4fddb232ca 100644 --- a/python/core/auto_generated/qgsscaleutils.sip.in +++ b/python/core/auto_generated/qgsscaleutils.sip.in @@ -42,6 +42,36 @@ Load scales from the given file went wrong :return: ``True`` on success and ``False`` if failed +%End + + static bool equalToOrGreaterThanMinimumScale( const double scale, const double minScale ); +%Docstring +Returns whether the ``scale`` is equal to or greater than the ``minScale``, +taking non-round numbers into account. + +:param scale: The current scale to be compared. +:param minScale: The minimum map scale (i.e. most "zoomed out" scale) at + which features, labels or diagrams will be visible. The scale value + indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. + +.. seealso:: :py:func:`lessThanMaximumScale` + +.. versionadded:: 3.40 +%End + + static bool lessThanMaximumScale( const double scale, const double maxScale ); +%Docstring +Returns whether the ``scale`` is less than the ``maxScale``, taking non-round +numbers into account. + +:param scale: The current scale to be compared. +:param maxScale: The maximum map scale (i.e. most "zoomed in" scale) at which + features, labels or diagrams will be visible. The scale value indicates the + scale denominator, e.g. 1000.0 for a 1:1000 map. + +.. seealso:: :py:func:`equalToOrGreaterThanMinimumScale` + +.. versionadded:: 3.40 %End }; diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index 59a25d23dbf3..99ab5b93fc3e 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -94,6 +94,7 @@ #include "qgsexpressioncontextutils.h" #include "qgsauxiliarystorage.h" #include "qgsvectortileutils.h" +#include "qgsscaleutils.h" #include "qgsbrowserwidget.h" #include "annotations/qgsannotationitempropertieswidget.h" @@ -12121,12 +12122,12 @@ void QgisApp::zoomToLayerScale() { const double scale = mMapCanvas->scale(); - if ( layer->minimumScale() > 0 && qgsEqualToOrGreaterThanMinimumScale( scale, layer->minimumScale() ) ) + if ( layer->minimumScale() > 0 && QgsScaleUtils::equalToOrGreaterThanMinimumScale( scale, layer->minimumScale() ) ) { // minimum is exclusive ( >= --> out of range ), decrease by 1 to be sure mMapCanvas->zoomScale( layer->minimumScale() - 1 ); } - else if ( layer->maximumScale() > 0 && qgsLessThanMaximumScale( scale, layer->maximumScale() ) ) + else if ( layer->maximumScale() > 0 && QgsScaleUtils::lessThanMaximumScale( scale, layer->maximumScale() ) ) { // maximum is inclusive ( < --> out of range ), pass maximum mMapCanvas->zoomScale( layer->maximumScale() ); diff --git a/src/core/labeling/qgsrulebasedlabeling.cpp b/src/core/labeling/qgsrulebasedlabeling.cpp index ab36f353a63a..f757a934822f 100644 --- a/src/core/labeling/qgsrulebasedlabeling.cpp +++ b/src/core/labeling/qgsrulebasedlabeling.cpp @@ -13,6 +13,7 @@ * * ***************************************************************************/ #include "qgsrulebasedlabeling.h" +#include "qgsscaleutils.h" #include "qgssymbollayerutils.h" #include "qgsstyleentityvisitor.h" @@ -429,11 +430,11 @@ bool QgsRuleBasedLabeling::Rule::isScaleOK( double scale ) const return true; // maxScale is inclusive ( < --> no label ) - if ( !qgsDoubleNear( mMaximumScale, 0.0 ) && qgsLessThanMaximumScale( scale, mMaximumScale ) ) + if ( !qgsDoubleNear( mMaximumScale, 0.0 ) && QgsScaleUtils::lessThanMaximumScale( scale, mMaximumScale ) ) return false; // minScale is exclusive ( >= --> no label ) - if ( !qgsDoubleNear( mMinimumScale, 0.0 ) && qgsEqualToOrGreaterThanMinimumScale( scale, mMinimumScale ) ) + if ( !qgsDoubleNear( mMinimumScale, 0.0 ) && QgsScaleUtils::equalToOrGreaterThanMinimumScale( scale, mMinimumScale ) ) return false; return true; } diff --git a/src/core/qgis.cpp b/src/core/qgis.cpp index ac16a40c90b4..5dabe1d5aabe 100644 --- a/src/core/qgis.cpp +++ b/src/core/qgis.cpp @@ -190,17 +190,6 @@ bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs ) return ! qgsVariantLessThan( lhs, rhs ); } -bool qgsEqualToOrGreaterThanMinimumScale( const double scale, const double minScale ) -{ - return scale > minScale || qgsDoubleNear( scale, minScale, 1E-8 ); -} - -bool qgsLessThanMaximumScale( const double scale, const double maxScale ) -{ - return scale < maxScale && !qgsDoubleNear( scale, maxScale, 1E-8 ); -} - - QString qgsVsiPrefix( const QString &path ) { return QgsGdalUtils::vsiPrefixForPath( path ); diff --git a/src/core/qgis.h b/src/core/qgis.h index 259b7652da34..aa2aaa8eef80 100644 --- a/src/core/qgis.h +++ b/src/core/qgis.h @@ -6258,34 +6258,6 @@ CORE_EXPORT bool qgsVariantEqual( const QVariant &lhs, const QVariant &rhs ); */ CORE_EXPORT bool qgsVariantGreaterThan( const QVariant &lhs, const QVariant &rhs ); -/** - * Returns whether the \a scale is equal to or greater than the \a minScale, - * taking non-round numbers into account. - * - * \param scale The current scale to be compared. - * \param minScale The minimum map scale (i.e. most "zoomed out" scale) at - * which features, labels or diagrams will be visible. The scale value - * indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. - * \see qgsLessThanMaximumScale() - * - * \since QGIS 3.40 - */ -CORE_EXPORT bool qgsEqualToOrGreaterThanMinimumScale( const double scale, const double minScale ); - -/** - * Returns whether the \a scale is less than the \a maxScale, taking non-round - * numbers into account. - * - * \param scale The current scale to be compared. - * \param maxScale The maximum map scale (i.e. most "zoomed in" scale) at which - * features, labels or diagrams will be visible. The scale value indicates the - * scale denominator, e.g. 1000.0 for a 1:1000 map. - * \see qgsEqualToOrGreaterThanMinimumScale() - * - * \since QGIS 3.40 - */ -CORE_EXPORT bool qgsLessThanMaximumScale( const double scale, const double maxScale ); - #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) /** diff --git a/src/core/qgsdiagramrenderer.cpp b/src/core/qgsdiagramrenderer.cpp index 2c235ae776e7..040016fdb40f 100644 --- a/src/core/qgsdiagramrenderer.cpp +++ b/src/core/qgsdiagramrenderer.cpp @@ -31,6 +31,7 @@ #include "qgslinesymbol.h" #include "qgsmarkersymbol.h" #include "qgsunittypes.h" +#include "qgsscaleutils.h" #include #include @@ -540,14 +541,14 @@ QSizeF QgsDiagramRenderer::sizeMapUnits( const QgsFeature &feature, const QgsRen // maxScale is inclusive ( < --> no size ) double maxScale = s.maximumScale; - if ( maxScale > 0 && qgsLessThanMaximumScale( rendererScale, maxScale ) ) + if ( maxScale > 0 && QgsScaleUtils::lessThanMaximumScale( rendererScale, maxScale ) ) { return QSizeF(); } // minScale is exclusive ( >= --> no size) double minScale = s.minimumScale; - if ( minScale > 0 && qgsEqualToOrGreaterThanMinimumScale( rendererScale, minScale ) ) + if ( minScale > 0 && QgsScaleUtils::equalToOrGreaterThanMinimumScale( rendererScale, minScale ) ) { return QSizeF(); } @@ -949,14 +950,14 @@ void QgsStackedDiagramRenderer::renderDiagram( const QgsFeature &feature, QgsRen // maxScale is inclusive ( < --> no diagram ) double maxScale = s.maximumScale; - if ( maxScale > 0 && qgsLessThanMaximumScale( rendererScale, maxScale ) ) + if ( maxScale > 0 && QgsScaleUtils::lessThanMaximumScale( rendererScale, maxScale ) ) { continue; } // minScale is exclusive ( >= --> no diagram) double minScale = s.minimumScale; - if ( minScale > 0 && qgsEqualToOrGreaterThanMinimumScale( rendererScale, minScale ) ) + if ( minScale > 0 && QgsScaleUtils::equalToOrGreaterThanMinimumScale( rendererScale, minScale ) ) { continue; } diff --git a/src/core/qgsmaplayer.cpp b/src/core/qgsmaplayer.cpp index 67ddcffb77e6..2c8036eb6a14 100644 --- a/src/core/qgsmaplayer.cpp +++ b/src/core/qgsmaplayer.cpp @@ -37,6 +37,7 @@ #include "qgsrasterlayer.h" #include "qgsreadwritecontext.h" #include "qgsrectangle.h" +#include "qgsscaleutils.h" #include "qgssldexportcontext.h" #include "qgsvectorlayer.h" #include "qgsxmlutils.h" @@ -1156,8 +1157,8 @@ bool QgsMapLayer::isInScaleRange( double scale ) const // mMinScale (denominator!) is inclusive ( >= --> In range ) // mMaxScale (denominator!) is exclusive ( < --> In range ) return !mScaleBasedVisibility - || ( ( mMinScale == 0 || !qgsLessThanMaximumScale( scale, mMinScale ) ) - && ( mMaxScale == 0 || !qgsEqualToOrGreaterThanMinimumScale( scale, mMaxScale ) ) ); + || ( ( mMinScale == 0 || !QgsScaleUtils::lessThanMaximumScale( scale, mMinScale ) ) + && ( mMaxScale == 0 || !QgsScaleUtils::equalToOrGreaterThanMinimumScale( scale, mMaxScale ) ) ); } bool QgsMapLayer::hasScaleBasedVisibility() const diff --git a/src/core/qgsscaleutils.cpp b/src/core/qgsscaleutils.cpp index d2f13ddb4a61..78c269c5945a 100644 --- a/src/core/qgsscaleutils.cpp +++ b/src/core/qgsscaleutils.cpp @@ -13,12 +13,13 @@ * * ***************************************************************************/ +#include "qgsscaleutils.h" +#include "qgis.h" + #include #include #include -#include "qgsscaleutils.h" - bool QgsScaleUtils::saveScaleList( const QString &fileName, const QStringList &scales, QString &errorMessage ) { QDomDocument doc; @@ -84,3 +85,13 @@ bool QgsScaleUtils::loadScaleList( const QString &fileName, QStringList &scales, return true; } + +bool QgsScaleUtils::equalToOrGreaterThanMinimumScale( const double scale, const double minScale ) +{ + return scale > minScale || qgsDoubleNear( scale, minScale, 1E-8 ); +} + +bool QgsScaleUtils::lessThanMaximumScale( const double scale, const double maxScale ) +{ + return scale < maxScale && !qgsDoubleNear( scale, maxScale, 1E-8 ); +} diff --git a/src/core/qgsscaleutils.h b/src/core/qgsscaleutils.h index c7e6cea4ac99..63fc770e05f1 100644 --- a/src/core/qgsscaleutils.h +++ b/src/core/qgsscaleutils.h @@ -48,6 +48,34 @@ class CORE_EXPORT QgsScaleUtils * \returns TRUE on success and FALSE if failed */ static bool loadScaleList( const QString &fileName, QStringList &scales, QString &errorMessage ); + + /** + * Returns whether the \a scale is equal to or greater than the \a minScale, + * taking non-round numbers into account. + * + * \param scale The current scale to be compared. + * \param minScale The minimum map scale (i.e. most "zoomed out" scale) at + * which features, labels or diagrams will be visible. The scale value + * indicates the scale denominator, e.g. 1000.0 for a 1:1000 map. + * \see lessThanMaximumScale() + * + * \since QGIS 3.40 + */ + static bool equalToOrGreaterThanMinimumScale( const double scale, const double minScale ); + + /** + * Returns whether the \a scale is less than the \a maxScale, taking non-round + * numbers into account. + * + * \param scale The current scale to be compared. + * \param maxScale The maximum map scale (i.e. most "zoomed in" scale) at which + * features, labels or diagrams will be visible. The scale value indicates the + * scale denominator, e.g. 1000.0 for a 1:1000 map. + * \see equalToOrGreaterThanMinimumScale() + * + * \since QGIS 3.40 + */ + static bool lessThanMaximumScale( const double scale, const double maxScale ); }; #endif diff --git a/src/core/symbology/qgsrulebasedrenderer.cpp b/src/core/symbology/qgsrulebasedrenderer.cpp index 1cf4e7073e2e..4ed4f107a4e4 100644 --- a/src/core/symbology/qgsrulebasedrenderer.cpp +++ b/src/core/symbology/qgsrulebasedrenderer.cpp @@ -30,6 +30,7 @@ #include "qgsfillsymbol.h" #include "qgsmarkersymbol.h" #include "qgspointdistancerenderer.h" +#include "qgsscaleutils.h" #include @@ -299,11 +300,11 @@ bool QgsRuleBasedRenderer::Rule::isScaleOK( double scale ) const return true; // maxScale is inclusive ( < --> no render ) - if ( !qgsDoubleNear( mMaximumScale, 0.0 ) && qgsLessThanMaximumScale( scale, mMaximumScale ) ) + if ( !qgsDoubleNear( mMaximumScale, 0.0 ) && QgsScaleUtils::lessThanMaximumScale( scale, mMaximumScale ) ) return false; // minScale is exclusive ( >= --> no render ) - if ( !qgsDoubleNear( mMinimumScale, 0.0 ) && qgsEqualToOrGreaterThanMinimumScale( scale, mMinimumScale ) ) + if ( !qgsDoubleNear( mMinimumScale, 0.0 ) && QgsScaleUtils::equalToOrGreaterThanMinimumScale( scale, mMinimumScale ) ) return false; return true; diff --git a/src/core/vector/qgsvectorlayerdiagramprovider.cpp b/src/core/vector/qgsvectorlayerdiagramprovider.cpp index ac36457cab78..92107bf6ac3e 100644 --- a/src/core/vector/qgsvectorlayerdiagramprovider.cpp +++ b/src/core/vector/qgsvectorlayerdiagramprovider.cpp @@ -24,6 +24,7 @@ #include "qgslabelingresults.h" #include "qgsrendercontext.h" #include "qgsexpressioncontextutils.h" +#include "qgsscaleutils.h" #include "feature.h" #include "labelposition.h" @@ -199,14 +200,14 @@ QgsLabelFeature *QgsVectorLayerDiagramProvider::registerDiagram( const QgsFeatur // maxScale is inclusive ( < --> no diagram ) double maxScale = settingList.at( 0 ).maximumScale; - if ( maxScale > 0 && qgsLessThanMaximumScale( rendererScale, maxScale ) ) + if ( maxScale > 0 && QgsScaleUtils::lessThanMaximumScale( rendererScale, maxScale ) ) { return nullptr; } // minScale is exclusive ( >= --> no diagram) double minScale = settingList.at( 0 ).minimumScale; - if ( minScale > 0 && qgsEqualToOrGreaterThanMinimumScale( rendererScale, minScale ) ) + if ( minScale > 0 && QgsScaleUtils::equalToOrGreaterThanMinimumScale( rendererScale, minScale ) ) { return nullptr; } diff --git a/tests/src/core/CMakeLists.txt b/tests/src/core/CMakeLists.txt index 81b8326760a7..01dc8881ea7f 100644 --- a/tests/src/core/CMakeLists.txt +++ b/tests/src/core/CMakeLists.txt @@ -169,6 +169,7 @@ set(TESTS testqgsrenderers.cpp testqgsrulebasedrenderer.cpp testqgsruntimeprofiler.cpp + testqgsscaleutils.cpp testqgssensorthingsconnection.cpp testqgssettings.cpp testqgssettingsentry.cpp diff --git a/tests/src/core/testqgis.cpp b/tests/src/core/testqgis.cpp index 356c29dd3c93..7badfd2cce18 100644 --- a/tests/src/core/testqgis.cpp +++ b/tests/src/core/testqgis.cpp @@ -58,10 +58,6 @@ class TestQgis : public QgsTest void qVariantCompare(); void testNanCompatibleEquals_data(); void testNanCompatibleEquals(); - void testMaximumScaleComparisons_data(); - void testMaximumScaleComparisons(); - void testMinimumScaleComparisons_data(); - void testMinimumScaleComparisons(); void testQgsAsConst(); void testQgsRound(); void testQgsVariantEqual(); @@ -353,50 +349,6 @@ void TestQgis::testNanCompatibleEquals() QCOMPARE( qgsNanCompatibleEquals( rhs, lhs ), expected ); } -void TestQgis::testMaximumScaleComparisons_data() -{ - QTest::addColumn( "scale" ); - QTest::addColumn( "maxScale" ); - QTest::addColumn( "expected" ); - - QTest::newRow( "same scales" ) << 2500.0 << 2500.0 << false; - QTest::newRow( "same scales, non-round below" ) << 2499.9999999966526 << 2500.0 << false; - QTest::newRow( "same scales, non-round above" ) << 2500.0000000027226 << 2500.0 << false; - QTest::newRow( "scale greater than max scale" ) << 3000.0 << 2500.0 << false; - QTest::newRow( "scale less than max scale" ) << 2000.0 << 2500.0 << true; -} - -void TestQgis::testMaximumScaleComparisons() -{ - QFETCH( double, scale ); - QFETCH( double, maxScale ); - QFETCH( bool, expected ); - - QCOMPARE( qgsLessThanMaximumScale( scale, maxScale ), expected ); -} - -void TestQgis::testMinimumScaleComparisons_data() -{ - QTest::addColumn( "scale" ); - QTest::addColumn( "minScale" ); - QTest::addColumn( "expected" ); - - QTest::newRow( "same scales" ) << 5000.0 << 5000.0 << true; - QTest::newRow( "same scales, non-round below" ) << 4999.999999997278 << 5000.0 << true; - QTest::newRow( "same scales, non-round above" ) << 5000.000000003348 << 5000.0 << true; - QTest::newRow( "scale greater than min scale" ) << 10000.0 << 5000.0 << true; - QTest::newRow( "scale less than min scale" ) << 3000.0 << 5000.0 << false; -} - -void TestQgis::testMinimumScaleComparisons() -{ - QFETCH( double, scale ); - QFETCH( double, minScale ); - QFETCH( bool, expected ); - - QCOMPARE( qgsEqualToOrGreaterThanMinimumScale( scale, minScale ), expected ); -} - class ConstTester { public: diff --git a/tests/src/core/testqgsscaleutils.cpp b/tests/src/core/testqgsscaleutils.cpp new file mode 100644 index 000000000000..457fb9d26790 --- /dev/null +++ b/tests/src/core/testqgsscaleutils.cpp @@ -0,0 +1,100 @@ +/*************************************************************************** + testqgsscaleutils.cpp + -------------------------------------- + Date : October 2024 + Copyright : (C) 2024 by Germán Carrillo + Email : gcarrillo at linuxmail dot org + *************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ +#include "qgstest.h" +#include +#include + +#include "qgsscaleutils.h" + +class TestQgsScaleUtils: public QObject +{ + Q_OBJECT + + private slots: + void initTestCase();// will be called before the first testfunction is executed. + void cleanupTestCase();// will be called after the last testfunction was executed. + void testMaximumScaleComparisons_data(); + void testMaximumScaleComparisons(); + void testMinimumScaleComparisons_data(); + void testMinimumScaleComparisons(); +}; + +void TestQgsScaleUtils::initTestCase() +{ + // Runs once before any tests are run + QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) ); + QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) ); + QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) ); + + QgsApplication::init(); + QgsApplication::initQgis(); +} + +void TestQgsScaleUtils::cleanupTestCase() +{ + // Runs once after all tests are run + QgsApplication::exitQgis(); +} + +void TestQgsScaleUtils::testMaximumScaleComparisons_data() +{ + QTest::addColumn( "scale" ); + QTest::addColumn( "maxScale" ); + QTest::addColumn( "expected" ); + + QTest::newRow( "same scales" ) << 2500.0 << 2500.0 << false; + QTest::newRow( "same scales, non-round below" ) << 2499.9999999966526 << 2500.0 << false; + QTest::newRow( "same scales, non-round above" ) << 2500.0000000027226 << 2500.0 << false; + QTest::newRow( "scale greater than max scale" ) << 3000.0 << 2500.0 << false; + QTest::newRow( "scale less than max scale" ) << 2000.0 << 2500.0 << true; +} + +void TestQgsScaleUtils::testMaximumScaleComparisons() +{ + QFETCH( double, scale ); + QFETCH( double, maxScale ); + QFETCH( bool, expected ); + + QCOMPARE( QgsScaleUtils::lessThanMaximumScale( scale, maxScale ), expected ); +} + +void TestQgsScaleUtils::testMinimumScaleComparisons_data() +{ + QTest::addColumn( "scale" ); + QTest::addColumn( "minScale" ); + QTest::addColumn( "expected" ); + + QTest::newRow( "same scales" ) << 5000.0 << 5000.0 << true; + QTest::newRow( "same scales, non-round below" ) << 4999.999999997278 << 5000.0 << true; + QTest::newRow( "same scales, non-round above" ) << 5000.000000003348 << 5000.0 << true; + QTest::newRow( "scale greater than min scale" ) << 10000.0 << 5000.0 << true; + QTest::newRow( "scale less than min scale" ) << 3000.0 << 5000.0 << false; +} + +void TestQgsScaleUtils::testMinimumScaleComparisons() +{ + QFETCH( double, scale ); + QFETCH( double, minScale ); + QFETCH( bool, expected ); + + QCOMPARE( QgsScaleUtils::equalToOrGreaterThanMinimumScale( scale, minScale ), expected ); +} + +QGSTEST_MAIN( TestQgsScaleUtils ) +#include "testqgsscaleutils.moc" + + + +