mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-22 00:06:12 -05:00
Add tests for QgsLegendSettings, QgsLegendStyle
This commit is contained in:
parent
2ffb8c00db
commit
227db80139
@ -162,6 +162,8 @@ ADD_PYTHON_TEST(PyQgsLayoutSnapper test_qgslayoutsnapper.py)
|
||||
ADD_PYTHON_TEST(PyQgsLayoutTable test_qgslayouttable.py)
|
||||
ADD_PYTHON_TEST(PyQgsLegendPatchShape test_qgslegendpatchshape.py)
|
||||
ADD_PYTHON_TEST(PyQgsLegendRenderer test_qgslegendrenderer.py)
|
||||
ADD_PYTHON_TEST(PyQgsLegendSettings test_qgslegendsettings.py)
|
||||
ADD_PYTHON_TEST(PyQgsLegendStyle test_qgslegendstyle.py)
|
||||
ADD_PYTHON_TEST(PyQgsLinearReferencingSymbolLayer test_qgslinearreferencingsymbollayer.py)
|
||||
ADD_PYTHON_TEST(PyQgsLineSegment test_qgslinesegment.py)
|
||||
ADD_PYTHON_TEST(PyQgsLineString test_qgslinestring.py)
|
||||
|
188
tests/src/python/test_qgslegendsettings.py
Normal file
188
tests/src/python/test_qgslegendsettings.py
Normal file
@ -0,0 +1,188 @@
|
||||
""""Test QgsLegendSettings
|
||||
|
||||
.. 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.
|
||||
|
||||
Run with ctest -V -R PyQgsLegendSettings
|
||||
|
||||
"""
|
||||
|
||||
from qgis.PyQt.QtCore import Qt, QSizeF
|
||||
from qgis.PyQt.QtGui import QColor
|
||||
|
||||
from qgis.core import (
|
||||
Qgis,
|
||||
QgsLegendStyle,
|
||||
QgsLegendSettings,
|
||||
QgsExpressionContext,
|
||||
QgsExpressionContextScope,
|
||||
QgsTextFormat,
|
||||
QgsRenderContext,
|
||||
QgsPalLayerSettings,
|
||||
QgsProperty,
|
||||
)
|
||||
import unittest
|
||||
from qgis.testing import start_app, QgisTestCase
|
||||
|
||||
from utilities import unitTestDataPath
|
||||
|
||||
QGISAPP = start_app()
|
||||
TEST_DATA_DIR = unitTestDataPath()
|
||||
|
||||
|
||||
class TestPyQgsLegendSettings(QgisTestCase):
|
||||
|
||||
def test_getters_setters(self):
|
||||
settings = QgsLegendSettings()
|
||||
|
||||
title = "Test Legend"
|
||||
settings.setTitle(title)
|
||||
self.assertEqual(settings.title(), title)
|
||||
|
||||
settings.setTitleAlignment(Qt.AlignRight)
|
||||
self.assertEqual(settings.titleAlignment(), Qt.AlignRight)
|
||||
|
||||
test_style = QgsLegendStyle()
|
||||
test_style.setIndent(33)
|
||||
settings.setStyle(QgsLegendStyle.Style.Symbol, test_style)
|
||||
self.assertEqual(settings.style(QgsLegendStyle.Style.Symbol).indent(), 33)
|
||||
|
||||
test_style2 = QgsLegendStyle()
|
||||
test_style2.setIndent(35)
|
||||
settings.setStyle(QgsLegendStyle.Style.SymbolLabel, test_style2)
|
||||
self.assertEqual(settings.style(QgsLegendStyle.Style.Symbol).indent(), 33)
|
||||
self.assertEqual(settings.style(QgsLegendStyle.Style.SymbolLabel).indent(), 35)
|
||||
|
||||
settings.setBoxSpace(5.0)
|
||||
self.assertEqual(settings.boxSpace(), 5.0)
|
||||
|
||||
settings.setWrapChar("\n")
|
||||
self.assertEqual(settings.wrapChar(), "\n")
|
||||
|
||||
settings.setColumnSpace(5.0)
|
||||
self.assertEqual(settings.columnSpace(), 5.0)
|
||||
|
||||
settings.setColumnCount(3)
|
||||
self.assertEqual(settings.columnCount(), 3)
|
||||
|
||||
settings.setSplitLayer(True)
|
||||
self.assertTrue(settings.splitLayer())
|
||||
settings.setSplitLayer(False)
|
||||
self.assertFalse(settings.splitLayer())
|
||||
|
||||
settings.setEqualColumnWidth(True)
|
||||
self.assertTrue(settings.equalColumnWidth())
|
||||
settings.setEqualColumnWidth(False)
|
||||
self.assertFalse(settings.equalColumnWidth())
|
||||
|
||||
settings.setSymbolSize(QSizeF(10.0, 10.0))
|
||||
self.assertEqual(settings.symbolSize(), QSizeF(10.0, 10.0))
|
||||
settings.setMaximumSymbolSize(20.0)
|
||||
settings.setMinimumSymbolSize(5.0)
|
||||
self.assertEqual(settings.maximumSymbolSize(), 20.0)
|
||||
self.assertEqual(settings.minimumSymbolSize(), 5.0)
|
||||
|
||||
settings.setSymbolAlignment(Qt.AlignRight)
|
||||
self.assertEqual(settings.symbolAlignment(), Qt.AlignRight)
|
||||
|
||||
settings.setDrawRasterStroke(True)
|
||||
self.assertTrue(settings.drawRasterStroke())
|
||||
|
||||
settings.setRasterStrokeColor(QColor(255, 0, 0))
|
||||
self.assertEqual(settings.rasterStrokeColor(), QColor(255, 0, 0))
|
||||
|
||||
settings.setRasterStrokeWidth(2.0)
|
||||
self.assertEqual(settings.rasterStrokeWidth(), 2.0)
|
||||
|
||||
settings.setWmsLegendSize(QSizeF(50.0, 150.0))
|
||||
self.assertEqual(settings.wmsLegendSize(), QSizeF(50.0, 150.0))
|
||||
|
||||
settings.setSynchronousLegendRequests(True)
|
||||
self.assertTrue(settings.synchronousLegendRequests())
|
||||
settings.setSynchronousLegendRequests(False)
|
||||
self.assertFalse(settings.synchronousLegendRequests())
|
||||
|
||||
settings.setJsonRenderFlags(Qgis.LegendJsonRenderFlag.ShowRuleDetails)
|
||||
self.assertEqual(
|
||||
settings.jsonRenderFlags(), Qgis.LegendJsonRenderFlag.ShowRuleDetails
|
||||
)
|
||||
|
||||
def test_split_string_for_wrapping(self):
|
||||
settings = QgsLegendSettings()
|
||||
settings.setWrapChar("|")
|
||||
self.assertEqual(
|
||||
settings.splitStringForWrapping("Test|String|Wrapping"),
|
||||
["Test", "String", "Wrapping"],
|
||||
)
|
||||
|
||||
def test_evaluate_item_text(self):
|
||||
settings = QgsLegendSettings()
|
||||
settings.setWrapChar("|")
|
||||
|
||||
expression_context_scope = QgsExpressionContextScope()
|
||||
expression_context_scope.setVariable("test_string", "Test|String")
|
||||
expression_context = QgsExpressionContext()
|
||||
expression_context.appendScope(expression_context_scope)
|
||||
|
||||
self.assertEqual(
|
||||
settings.evaluateItemText(
|
||||
"[% @test_string %]|Wrapping", expression_context
|
||||
),
|
||||
["Test", "String", "Wrapping"],
|
||||
)
|
||||
|
||||
def test_update_data_defined_properties(self):
|
||||
style = QgsLegendStyle()
|
||||
text_format = QgsTextFormat()
|
||||
text_format.setColor(QColor(255, 0, 0, 255))
|
||||
text_format.dataDefinedProperties().setProperty(
|
||||
QgsPalLayerSettings.Property.Color,
|
||||
QgsProperty.fromExpression("@text_color"),
|
||||
)
|
||||
style.setTextFormat(text_format)
|
||||
settings = QgsLegendSettings()
|
||||
settings.setStyle(QgsLegendStyle.Style.Group, style)
|
||||
|
||||
style2 = QgsLegendStyle()
|
||||
text_format = QgsTextFormat()
|
||||
text_format.setColor(QColor(255, 0, 255, 255))
|
||||
text_format.dataDefinedProperties().setProperty(
|
||||
QgsPalLayerSettings.Property.Color,
|
||||
QgsProperty.fromExpression("@text_color2"),
|
||||
)
|
||||
style2.setTextFormat(text_format)
|
||||
settings.setStyle(QgsLegendStyle.Style.Subgroup, style2)
|
||||
|
||||
self.assertEqual(
|
||||
settings.style(QgsLegendStyle.Style.Group).textFormat().color(),
|
||||
QColor(255, 0, 0, 255),
|
||||
)
|
||||
self.assertEqual(
|
||||
settings.style(QgsLegendStyle.Style.Subgroup).textFormat().color(),
|
||||
QColor(255, 0, 255, 255),
|
||||
)
|
||||
|
||||
# apply data defined properties
|
||||
rc = QgsRenderContext()
|
||||
expression_context_scope = QgsExpressionContextScope()
|
||||
expression_context_scope.setVariable("text_color", "0,255,0")
|
||||
expression_context_scope.setVariable("text_color2", "255,255,255")
|
||||
expression_context = QgsExpressionContext()
|
||||
expression_context.appendScope(expression_context_scope)
|
||||
rc.setExpressionContext(expression_context)
|
||||
settings.updateDataDefinedProperties(rc)
|
||||
|
||||
self.assertEqual(
|
||||
settings.style(QgsLegendStyle.Style.Group).textFormat().color(),
|
||||
QColor(0, 255, 0, 255),
|
||||
)
|
||||
self.assertEqual(
|
||||
settings.style(QgsLegendStyle.Style.Subgroup).textFormat().color(),
|
||||
QColor(255, 255, 255, 255),
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
147
tests/src/python/test_qgslegendstyle.py
Normal file
147
tests/src/python/test_qgslegendstyle.py
Normal file
@ -0,0 +1,147 @@
|
||||
""""Test QgsLegendStyle
|
||||
|
||||
.. 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.
|
||||
|
||||
Run with ctest -V -R PyQgsLegendStyle
|
||||
|
||||
"""
|
||||
|
||||
from qgis.PyQt.QtCore import Qt
|
||||
from qgis.PyQt.QtGui import QColor
|
||||
from qgis.PyQt.QtXml import QDomDocument
|
||||
|
||||
from qgis.core import (
|
||||
QgsLegendStyle,
|
||||
QgsTextFormat,
|
||||
QgsReadWriteContext,
|
||||
QgsPalLayerSettings,
|
||||
QgsProperty,
|
||||
QgsRenderContext,
|
||||
QgsExpressionContext,
|
||||
QgsExpressionContextScope,
|
||||
)
|
||||
import unittest
|
||||
from qgis.testing import start_app, QgisTestCase
|
||||
|
||||
from utilities import unitTestDataPath
|
||||
|
||||
QGISAPP = start_app()
|
||||
TEST_DATA_DIR = unitTestDataPath()
|
||||
|
||||
|
||||
class TestPyQgsLegendStyle(QgisTestCase):
|
||||
|
||||
def test_getters_setters(self):
|
||||
style = QgsLegendStyle()
|
||||
style.setMargin(QgsLegendStyle.Side.Top, 1.5)
|
||||
style.setMargin(QgsLegendStyle.Side.Right, 3.6)
|
||||
style.setMargin(QgsLegendStyle.Side.Bottom, 4.2)
|
||||
style.setMargin(QgsLegendStyle.Side.Left, 5.2)
|
||||
self.assertEqual(style.margin(QgsLegendStyle.Side.Top), 1.5)
|
||||
self.assertEqual(style.margin(QgsLegendStyle.Side.Right), 3.6)
|
||||
self.assertEqual(style.margin(QgsLegendStyle.Side.Bottom), 4.2)
|
||||
self.assertEqual(style.margin(QgsLegendStyle.Side.Left), 5.2)
|
||||
|
||||
style.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
self.assertEqual(style.alignment(), Qt.AlignCenter)
|
||||
|
||||
style.setIndent(33)
|
||||
self.assertEqual(style.indent(), 33)
|
||||
|
||||
text_format = QgsTextFormat()
|
||||
text_format.setColor(QColor(255, 0, 0, 0))
|
||||
style.setTextFormat(text_format)
|
||||
|
||||
self.assertEqual(style.textFormat().color(), QColor(255, 0, 0, 0))
|
||||
|
||||
def test_write_read(self):
|
||||
doc = QDomDocument("testdoc")
|
||||
elem = doc.createElement("test")
|
||||
|
||||
style = QgsLegendStyle()
|
||||
style.setMargin(QgsLegendStyle.Side.Top, 1.5)
|
||||
style.setMargin(QgsLegendStyle.Side.Right, 3.6)
|
||||
style.setMargin(QgsLegendStyle.Side.Bottom, 4.2)
|
||||
style.setMargin(QgsLegendStyle.Side.Left, 5.2)
|
||||
style.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
style.setIndent(33)
|
||||
text_format = QgsTextFormat()
|
||||
text_format.setColor(QColor(255, 0, 0, 0))
|
||||
style.setTextFormat(text_format)
|
||||
|
||||
style.writeXml("style", elem, doc, QgsReadWriteContext())
|
||||
|
||||
style2 = QgsLegendStyle()
|
||||
style2.readXml(elem.firstChildElement("style"), doc, QgsReadWriteContext())
|
||||
|
||||
self.assertEqual(style2.margin(QgsLegendStyle.Side.Top), 1.5)
|
||||
self.assertEqual(style2.margin(QgsLegendStyle.Side.Right), 3.6)
|
||||
self.assertEqual(style2.margin(QgsLegendStyle.Side.Bottom), 4.2)
|
||||
self.assertEqual(style2.margin(QgsLegendStyle.Side.Left), 5.2)
|
||||
self.assertEqual(style2.alignment(), Qt.AlignCenter)
|
||||
self.assertEqual(style2.indent(), 33)
|
||||
self.assertEqual(style2.textFormat().color(), QColor(255, 0, 0, 0))
|
||||
|
||||
def test_update_data_defined_properties(self):
|
||||
style = QgsLegendStyle()
|
||||
text_format = QgsTextFormat()
|
||||
text_format.setColor(QColor(255, 0, 0, 255))
|
||||
text_format.dataDefinedProperties().setProperty(
|
||||
QgsPalLayerSettings.Property.Color,
|
||||
QgsProperty.fromExpression("@text_color"),
|
||||
)
|
||||
style.setTextFormat(text_format)
|
||||
self.assertEqual(style.textFormat().color(), QColor(255, 0, 0, 255))
|
||||
|
||||
# apply data defined properties
|
||||
rc = QgsRenderContext()
|
||||
expression_context_scope = QgsExpressionContextScope()
|
||||
expression_context_scope.setVariable("text_color", "0,255,0")
|
||||
expression_context = QgsExpressionContext()
|
||||
expression_context.appendScope(expression_context_scope)
|
||||
rc.setExpressionContext(expression_context)
|
||||
style.updateDataDefinedProperties(rc)
|
||||
|
||||
self.assertEqual(style.textFormat().color(), QColor(0, 255, 0, 255))
|
||||
|
||||
def test_style_name(self):
|
||||
self.assertEqual(
|
||||
QgsLegendStyle.styleName(QgsLegendStyle.Style.Hidden), "hidden"
|
||||
)
|
||||
self.assertEqual(
|
||||
QgsLegendStyle.styleFromName("hidden"), QgsLegendStyle.Style.Hidden
|
||||
)
|
||||
self.assertEqual(QgsLegendStyle.styleName(QgsLegendStyle.Style.Title), "title")
|
||||
self.assertEqual(
|
||||
QgsLegendStyle.styleFromName("title"), QgsLegendStyle.Style.Title
|
||||
)
|
||||
self.assertEqual(QgsLegendStyle.styleName(QgsLegendStyle.Style.Group), "group")
|
||||
self.assertEqual(
|
||||
QgsLegendStyle.styleFromName("group"), QgsLegendStyle.Style.Group
|
||||
)
|
||||
self.assertEqual(
|
||||
QgsLegendStyle.styleName(QgsLegendStyle.Style.Subgroup), "subgroup"
|
||||
)
|
||||
self.assertEqual(
|
||||
QgsLegendStyle.styleFromName("subgroup"), QgsLegendStyle.Style.Subgroup
|
||||
)
|
||||
self.assertEqual(
|
||||
QgsLegendStyle.styleName(QgsLegendStyle.Style.Symbol), "symbol"
|
||||
)
|
||||
self.assertEqual(
|
||||
QgsLegendStyle.styleFromName("symbol"), QgsLegendStyle.Style.Symbol
|
||||
)
|
||||
self.assertEqual(
|
||||
QgsLegendStyle.styleName(QgsLegendStyle.Style.SymbolLabel), "symbolLabel"
|
||||
)
|
||||
self.assertEqual(
|
||||
QgsLegendStyle.styleFromName("symbolLabel"),
|
||||
QgsLegendStyle.Style.SymbolLabel,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user