diff --git a/src/core/symbology-ng/qgssymbollayerutils.cpp b/src/core/symbology-ng/qgssymbollayerutils.cpp index 4b2af9177d2..efdc34c598f 100644 --- a/src/core/symbology-ng/qgssymbollayerutils.cpp +++ b/src/core/symbology-ng/qgssymbollayerutils.cpp @@ -2566,6 +2566,7 @@ QDomElement QgsSymbolLayerUtils::createSvgParameterElement( QDomDocument &doc, c QgsStringMap QgsSymbolLayerUtils::getSvgParameterList( QDomElement &element ) { QgsStringMap params; + QString value; QDomElement paramElem = element.firstChildElement(); while ( !paramElem.isNull() ) @@ -2573,7 +2574,23 @@ QgsStringMap QgsSymbolLayerUtils::getSvgParameterList( QDomElement &element ) if ( paramElem.localName() == QLatin1String( "SvgParameter" ) || paramElem.localName() == QLatin1String( "CssParameter" ) ) { QString name = paramElem.attribute( QStringLiteral( "name" ) ); - QString value = paramElem.firstChild().nodeValue(); + if ( paramElem.firstChild().nodeType() == QDomNode::TextNode ) + { + value = paramElem.firstChild().nodeValue(); + } + else + { + if ( paramElem.firstChild().nodeType() == QDomNode::ElementNode && + paramElem.firstChild().localName() == QLatin1String( "Literal" ) ) + { + QgsDebugMsg( paramElem.firstChild().localName() ); + value = paramElem.firstChild().firstChild().nodeValue(); + } + else + { + QgsDebugMsg( QString( "unexpected child of %1" ).arg( paramElem.localName() ) ); + } + } if ( !name.isEmpty() && !value.isEmpty() ) params[ name ] = value; diff --git a/tests/src/python/CMakeLists.txt b/tests/src/python/CMakeLists.txt index 6f8d978ac93..f44f696d435 100644 --- a/tests/src/python/CMakeLists.txt +++ b/tests/src/python/CMakeLists.txt @@ -105,6 +105,7 @@ ADD_PYTHON_TEST(PyQgsSQLStatement test_qgssqlstatement.py) ADD_PYTHON_TEST(PyQgsStringStatisticalSummary test_qgsstringstatisticalsummary.py) ADD_PYTHON_TEST(PyQgsSymbolLayer test_qgssymbollayer.py) ADD_PYTHON_TEST(PyQgsSymbolLayerCreateSld test_qgssymbollayer_createsld.py) +ADD_PYTHON_TEST(PyQgsSymbolLayerReadSld test_qgssymbollayer_readsld.py) ADD_PYTHON_TEST(PyQgsArrowSymbolLayer test_qgsarrowsymbollayer.py) ADD_PYTHON_TEST(PyQgsSymbolExpressionVariables test_qgssymbolexpressionvariables.py) ADD_PYTHON_TEST(PyQgsSyntacticSugar test_syntactic_sugar.py) diff --git a/tests/src/python/test_qgssymbollayer_readsld.py b/tests/src/python/test_qgssymbollayer_readsld.py new file mode 100644 index 00000000000..0c09f206941 --- /dev/null +++ b/tests/src/python/test_qgssymbollayer_readsld.py @@ -0,0 +1,89 @@ +# -*- coding: utf-8 -*- + +""" +*************************************************************************** + test_qgssymbollayer_readsld.py + --------------------- + Date : January 2017 + Copyright : (C) 2017, Jorge Gustavo Rocha + Email : jgr at di dot uminho dot pt +*************************************************************************** +* * +* 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. * +* * +*************************************************************************** +""" + +__author__ = 'Jorge Gustavo Rocha' +__date__ = 'January 2017' +__copyright__ = '(C) 2017, Jorge Gustavo Rocha' +# This will get replaced with a git SHA1 when you do a git archive +__revision__ = '$Format:%H$' + +import qgis # NOQA + +import os +from qgis.testing import start_app, unittest +from qgis.core import (QgsVectorLayer, + QgsFeature, + QgsGeometry, + QgsPoint + ) +from qgis.testing import unittest +from qgis.testing.mocked import get_iface +from utilities import unitTestDataPath + +start_app() + +TEST_DATA_DIR = unitTestDataPath() + +def createLayerWithOneLine(): + # create a temporary layer + # linelayer = iface.addVectorLayer("LineString?crs=epsg:4326&field=gid:int&field=name:string", "simple_line", "memory") + linelayer = QgsVectorLayer("LineString?crs=epsg:4326&field=gid:int&field=name:string", "simple_line", "memory") + one = QgsFeature(linelayer.dataProvider().fields(), 0) + one.setAttributes([1, 'one']) + one.setGeometry(QgsGeometry.fromPolyline([QgsPoint(-7, 38), QgsPoint(-8, 42)])) + linelayer.dataProvider().addFeatures([one]) + return linelayer + +class TestQgsSymbolLayerReadSld(unittest.TestCase): + + """ + This class checks if SLD styles are properly applied + """ + + def setUp(self): + self.iface = get_iface() + + # test VALUE + # test VALUE + def test_Literal_within_CSSParameter(self): + layer = createLayerWithOneLine() + mFilePath = os.path.join(TEST_DATA_DIR, 'symbol_layer/external_sld/simple_streams.sld') + layer.loadSldStyle(mFilePath) + props = layer.renderer().symbol().symbolLayers()[0].properties() + + def testLineColor(): + # stroke CSSParameter within ogc:Literal + # expected color is #003EBA, RGB 0,62,186 + self.assertEqual(layer.renderer().symbol().symbolLayers()[0].color().name(), '#003eba') + + def testLineWidth(): + # stroke-width CSSParameter within ogc:Literal + self.assertEqual(props['line_width'], '2') + + def testLineOpacity(): + # stroke-opacity CSSParameter NOT within ogc:Literal + # stroke-opacity=0.1 + self.assertEqual(props['line_color'], '0,62,186,25') + + testLineColor() + testLineWidth() + testLineOpacity() + +if __name__ == '__main__': + unittest.main() diff --git a/tests/testdata/symbol_layer/external_sld/simple_streams.sld b/tests/testdata/symbol_layer/external_sld/simple_streams.sld new file mode 100644 index 00000000000..30c2f7fc462 --- /dev/null +++ b/tests/testdata/symbol_layer/external_sld/simple_streams.sld @@ -0,0 +1,30 @@ + + + + Simple Streams + + + Default Styler for streams segments + Blue lines, 2px wide + + Feature + + Streams + + + + #003EBA + + + 2 + + 0.1 + + + + + + + \ No newline at end of file