Skip to content

Commit

Permalink
Address review: move qgsLessThanMaximumScale and qgsEqualToOrGreaterT…
Browse files Browse the repository at this point in the history
…hanMinimumScale functions to QgsScaleUtils
  • Loading branch information
gacarrillor committed Oct 4, 2024
1 parent ac87dbb commit 2b4deba
Show file tree
Hide file tree
Showing 19 changed files with 226 additions and 163 deletions.
2 changes: 2 additions & 0 deletions python/PyQt6/core/auto_additions/qgsscaleutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 0 additions & 30 deletions python/PyQt6/core/auto_generated/qgis.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
30 changes: 30 additions & 0 deletions python/PyQt6/core/auto_generated/qgsscaleutils.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
2 changes: 2 additions & 0 deletions python/core/auto_additions/qgsscaleutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 0 additions & 30 deletions python/core/auto_generated/qgis.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
30 changes: 30 additions & 0 deletions python/core/auto_generated/qgsscaleutils.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
5 changes: 3 additions & 2 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
#include "qgsexpressioncontextutils.h"
#include "qgsauxiliarystorage.h"
#include "qgsvectortileutils.h"
#include "qgsscaleutils.h"

#include "qgsbrowserwidget.h"
#include "annotations/qgsannotationitempropertieswidget.h"
Expand Down Expand Up @@ -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() );
Expand Down
5 changes: 3 additions & 2 deletions src/core/labeling/qgsrulebasedlabeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* *
***************************************************************************/
#include "qgsrulebasedlabeling.h"
#include "qgsscaleutils.h"
#include "qgssymbollayerutils.h"
#include "qgsstyleentityvisitor.h"

Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 0 additions & 11 deletions src/core/qgis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
28 changes: 0 additions & 28 deletions src/core/qgis.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

/**
Expand Down
9 changes: 5 additions & 4 deletions src/core/qgsdiagramrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "qgslinesymbol.h"
#include "qgsmarkersymbol.h"
#include "qgsunittypes.h"
#include "qgsscaleutils.h"

#include <QDomElement>
#include <QPainter>
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions src/core/qgsmaplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions src/core/qgsscaleutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
* *
***************************************************************************/

#include "qgsscaleutils.h"
#include "qgis.h"

#include <QFile>
#include <QDomDocument>
#include <QTextStream>

#include "qgsscaleutils.h"

bool QgsScaleUtils::saveScaleList( const QString &fileName, const QStringList &scales, QString &errorMessage )
{
QDomDocument doc;
Expand Down Expand Up @@ -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 );
}
28 changes: 28 additions & 0 deletions src/core/qgsscaleutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit 2b4deba

Please sign in to comment.