Skip to content

Commit

Permalink
Fix custom stroke patterns incorrectly scale when data defined
Browse files Browse the repository at this point in the history
stroke width is set

Fixes #39201
  • Loading branch information
nyalldawson committed Oct 8, 2020
1 parent 1cd5a33 commit 03fdc0c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/core/symbology/qgslinesymbollayer.cpp
Expand Up @@ -594,6 +594,18 @@ void QgsSimpleLineSymbolLayer::applyDataDefinedSymbology( QgsSymbolRenderContext
pen.setDashPattern( dashVector );
}
}
else if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyStrokeWidth ) && mUseCustomDashPattern )
{
//re-scale pattern vector after data defined pen width was applied

QVector<qreal> scaledVector;
for ( double v : mCustomDashVector )
{
//the dash is specified in terms of pen widths, therefore the division
scaledVector << context.renderContext().convertToPainterUnits( v, mCustomDashPatternUnit, mCustomDashPatternMapUnitScale ) / dashWidthDiv;
}
mPen.setDashPattern( scaledVector );
}

// dash pattern offset
double patternOffset = mDashPatternOffset;
Expand Down
18 changes: 17 additions & 1 deletion tests/src/python/test_qgssimplelinesymbollayer.py
Expand Up @@ -41,7 +41,9 @@
QgsLineSymbolLayer,
QgsLineSymbol,
QgsUnitTypes,
QgsMapUnitScale
QgsMapUnitScale,
QgsSymbolLayer,
QgsProperty
)

from qgis.testing import unittest, start_app
Expand All @@ -60,6 +62,20 @@ def tearDown(self):
with open(report_file_path, 'a') as report_file:
report_file.write(self.report)

def testDashPatternWithDataDefinedWidth(self):
# rendering test
s = QgsLineSymbol.createSimple({'outline_color': '#ff0000', 'outline_width': '2'})

s[0].setUseCustomDashPattern(True)
s[0].setPenCapStyle(Qt.FlatCap)
s[0].setCustomDashVector([3, 4, 5, 6])

s[0].dataDefinedProperties().setProperty(QgsSymbolLayer.PropertyStrokeWidth, QgsProperty.fromExpression('3'))

g = QgsGeometry.fromWkt('LineString(0 0, 10 0, 10 10, 0 10)')
rendered_image = self.renderGeometry(s, g)
assert self.imageCheck('simpleline_dashpattern_datadefined_width', 'simpleline_dashpattern_datadefined_width', rendered_image)

def testDashPatternOffset(self):

s = QgsLineSymbol.createSimple({'outline_color': '#ff0000', 'outline_width': '0.6'})
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 03fdc0c

Please sign in to comment.