diff --git a/python/PyQt6/core/auto_generated/scalebar/qgsscalebarrenderer.sip.in b/python/PyQt6/core/auto_generated/scalebar/qgsscalebarrenderer.sip.in index 2b69270142e..e8c56dfb471 100644 --- a/python/PyQt6/core/auto_generated/scalebar/qgsscalebarrenderer.sip.in +++ b/python/PyQt6/core/auto_generated/scalebar/qgsscalebarrenderer.sip.in @@ -55,6 +55,13 @@ custom labeling. Flags flags; + bool isValid() const; +%Docstring +Returns ``True`` if the context has valid settings. + +.. versionadded:: 3.40 +%End + }; QgsScaleBarRenderer(); diff --git a/python/core/auto_generated/scalebar/qgsscalebarrenderer.sip.in b/python/core/auto_generated/scalebar/qgsscalebarrenderer.sip.in index 0e440e077ea..74571cc8e93 100644 --- a/python/core/auto_generated/scalebar/qgsscalebarrenderer.sip.in +++ b/python/core/auto_generated/scalebar/qgsscalebarrenderer.sip.in @@ -55,6 +55,13 @@ custom labeling. Flags flags; + bool isValid() const; +%Docstring +Returns ``True`` if the context has valid settings. + +.. versionadded:: 3.40 +%End + }; QgsScaleBarRenderer(); diff --git a/src/core/scalebar/qgsscalebarrenderer.cpp b/src/core/scalebar/qgsscalebarrenderer.cpp index 54f7e785bd1..d0ed572be5c 100644 --- a/src/core/scalebar/qgsscalebarrenderer.cpp +++ b/src/core/scalebar/qgsscalebarrenderer.cpp @@ -418,3 +418,8 @@ QList QgsScaleBarRenderer::segmentWidths( const ScaleBarContext &scaleCo return widths; } + +bool QgsScaleBarRenderer::ScaleBarContext::isValid() const +{ + return !std::isnan( segmentWidth ); +} diff --git a/src/core/scalebar/qgsscalebarrenderer.h b/src/core/scalebar/qgsscalebarrenderer.h index b23572061cb..038fb7a9b46 100644 --- a/src/core/scalebar/qgsscalebarrenderer.h +++ b/src/core/scalebar/qgsscalebarrenderer.h @@ -66,7 +66,7 @@ class CORE_EXPORT QgsScaleBarRenderer * Contains parameters regarding scalebar calculations. * \note The need to attribute the parameters vary depending on the targeted scalebar. */ - struct ScaleBarContext + struct CORE_EXPORT ScaleBarContext { /** @@ -88,6 +88,13 @@ class CORE_EXPORT QgsScaleBarRenderer //! Scalebar renderer flags Flags flags; + /** + * Returns TRUE if the context has valid settings. + * + * \since QGIS 3.40 + */ + bool isValid() const; + }; QgsScaleBarRenderer() = default; diff --git a/tests/src/python/CMakeLists.txt b/tests/src/python/CMakeLists.txt index ea0d56a5af7..ce1dde961d8 100644 --- a/tests/src/python/CMakeLists.txt +++ b/tests/src/python/CMakeLists.txt @@ -302,6 +302,7 @@ ADD_PYTHON_TEST(PyQgsRenderedItemResults test_qgsrendereditemresults.py) ADD_PYTHON_TEST(PyQgsRenderer test_qgsrenderer.py) ADD_PYTHON_TEST(PyQgsReport test_qgsreport.py) ADD_PYTHON_TEST(PyQgsScaleBarRendererRegistry test_qgsscalebarrendererregistry.py) +ADD_PYTHON_TEST(PyQgsScaleBarRenderers test_qgsscalebarrenderers.py) ADD_PYTHON_TEST(PyQgsScaleCalculator test_qgsscalecalculator.py) ADD_PYTHON_TEST(PyQgsSingleBandColorDataRenderer test_qgssinglebandcolordatarenderer.py) ADD_PYTHON_TEST(PyQgsSingleBandGrayRenderer test_qgssinglebandgrayrenderer.py) diff --git a/tests/src/python/test_qgsscalebarrenderers.py b/tests/src/python/test_qgsscalebarrenderers.py new file mode 100644 index 00000000000..c659acf4dd6 --- /dev/null +++ b/tests/src/python/test_qgsscalebarrenderers.py @@ -0,0 +1,29 @@ +"""QGIS Unit tests for scale bar renderers + +.. note:: 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. +""" + +import math + +from qgis.core import QgsScaleBarRenderer +import unittest +from qgis.testing import start_app, QgisTestCase + +start_app() + + +class TestQgsScaleBarRenderers(QgisTestCase): + + def test_context(self): + context = QgsScaleBarRenderer.ScaleBarContext() + context.segmentWidth = 5 + self.assertTrue(context.isValid()) + context.segmentWidth = math.nan + self.assertFalse(context.isValid()) + + +if __name__ == '__main__': + unittest.main()