Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #33987 from elpaso/bugfix-gh33840-loadstyle-render…
Browse files Browse the repository at this point in the history
…ing-scalevisibility

Fix scale visibility in style read/write
  • Loading branch information
elpaso committed Jan 23, 2020
2 parents 09061d1 + 28ce3cd commit 9c48368
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2407,7 +2407,7 @@ bool QgsVectorLayer::readStyle( const QDomNode &node, QString &errorMessage,
}
}

// get and set the layer transparency if it exists
// get and set the layer transparency and scale visibility if they exists
if ( categories.testFlag( Rendering ) )
{
QDomNode layerTransparencyNode = node.namedItem( QStringLiteral( "layerTransparency" ) );
Expand All @@ -2422,6 +2422,20 @@ bool QgsVectorLayer::readStyle( const QDomNode &node, QString &errorMessage,
QDomElement e = layerOpacityNode.toElement();
setOpacity( e.text().toDouble() );
}

const bool hasScaleBasedVisibiliy { node.attributes().namedItem( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).nodeValue() == '1' };
setScaleBasedVisibility( hasScaleBasedVisibiliy );
bool ok;
const double maxScale { node.attributes().namedItem( QStringLiteral( "maxScale" ) ).nodeValue().toDouble( &ok ) };
if ( ok )
{
setMaximumScale( maxScale );
}
const double minScale { node.attributes().namedItem( QStringLiteral( "minScale" ) ).nodeValue().toDouble( &ok ) };
if ( ok )
{
setMinimumScale( minScale );
}
}

if ( categories.testFlag( Rendering ) )
Expand Down Expand Up @@ -2807,13 +2821,16 @@ bool QgsVectorLayer::writeStyle( QDomNode &node, QDomDocument &doc, QString &err
node.appendChild( featureBlendModeElem );
}

// add the layer opacity
// add the layer opacity and scale visibility
if ( categories.testFlag( Rendering ) )
{
QDomElement layerOpacityElem = doc.createElement( QStringLiteral( "layerOpacity" ) );
QDomText layerOpacityText = doc.createTextNode( QString::number( opacity() ) );
layerOpacityElem.appendChild( layerOpacityText );
node.appendChild( layerOpacityElem );
mapLayerNode.setAttribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ), hasScaleBasedVisibility() ? 1 : 0 );
mapLayerNode.setAttribute( QStringLiteral( "maxScale" ), maximumScale() );
mapLayerNode.setAttribute( QStringLiteral( "minScale" ), minimumScale() );
}

if ( categories.testFlag( Diagrams ) && mDiagramRenderer )
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -248,6 +248,7 @@ ADD_PYTHON_TEST(PyQgsVectorFileWriterTask test_qgsvectorfilewritertask.py)
ADD_PYTHON_TEST(PyQgsVectorLayer test_qgsvectorlayer.py)
ADD_PYTHON_TEST(PyQgsVectorLayerCache test_qgsvectorlayercache.py)
ADD_PYTHON_TEST(PyQgsVectorLayerEditBuffer test_qgsvectorlayereditbuffer.py)
ADD_PYTHON_TEST(PyQgsVectorLayerNamedStyle test_qgsvectorlayer_namedstyle.py)
ADD_PYTHON_TEST(PyQgsVectorLayerSelectedFeatureSource test_qgsvectorlayerselectedfeaturesource.py)
ADD_PYTHON_TEST(PyQgsVectorLayerUtils test_qgsvectorlayerutils.py)
ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)
Expand Down
61 changes: 61 additions & 0 deletions tests/src/python/test_qgsvectorlayer_namedstyle.py
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsVectorLayer load/write named style.
.. 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.
"""
__author__ = 'Alessandro Pasotti'
__date__ = '22/01/2020'
__copyright__ = 'Copyright 2020, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import tempfile
from qgis.core import (
QgsVectorLayer,
QgsMapLayer,
QgsReadWriteContext,
)
from qgis.PyQt.QtXml import QDomDocument, QDomNode

from qgis.testing import unittest


class TestPyQgsVectorLayerNamedStyle(unittest.TestCase):

@classmethod
def setUpClass(cls):
"""Run before all tests"""
pass

def testLoadWriteRenderingScaleVisibility(self):
"""Test write and load scale visibility, see GH #33840"""

vl = QgsVectorLayer("LineString?crs=epsg:4326", "result", "memory")
vl.setScaleBasedVisibility(True)
vl.setMinimumScale(125.0)
vl.setMaximumScale(1.25)
style = QDomDocument()
style.setContent("<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'><qgis></qgis>")
node = style.firstChild()
self.assertTrue(vl.writeStyle(node, style, "Error writing style", QgsReadWriteContext(), QgsMapLayer.Rendering))

style_content = style.toString()
del(vl)

# Read
vl2 = QgsVectorLayer("LineString?crs=epsg:4326", "result", "memory")
self.assertFalse(vl2.hasScaleBasedVisibility())
style2 = QDomDocument()
style2.setContent(style_content)
self.assertTrue(vl2.readStyle(style.namedItem('qgis'), "Error reading style", QgsReadWriteContext(), QgsMapLayer.Rendering))
self.assertTrue(vl2.hasScaleBasedVisibility())
self.assertEqual(vl2.minimumScale(), 125.0)
self.assertEqual(vl2.maximumScale(), 1.25)


if __name__ == '__main__':
unittest.main()

0 comments on commit 9c48368

Please sign in to comment.