Add isValid() method for QgsScaleBarRenderer::RendererContext

This commit is contained in:
Nyall Dawson 2024-10-15 13:57:37 +10:00
parent 55d6db6230
commit 4eb6ce1053
6 changed files with 57 additions and 1 deletions

View File

@ -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();

View File

@ -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();

View File

@ -418,3 +418,8 @@ QList<double> QgsScaleBarRenderer::segmentWidths( const ScaleBarContext &scaleCo
return widths;
}
bool QgsScaleBarRenderer::ScaleBarContext::isValid() const
{
return !std::isnan( segmentWidth );
}

View File

@ -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;

View File

@ -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)

View File

@ -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()