From 8f4893d8b25add32412469797b00e1cfa24bfc7c Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 12 Feb 2025 13:22:47 +1000 Subject: [PATCH] Move some useful utility methods to QgsSymbolLayerUtils --- .../auto_additions/qgssymbollayerutils.py | 2 + .../symbology/qgssymbollayerutils.sip.in | 14 +++++++ .../auto_additions/qgssymbollayerutils.py | 2 + .../symbology/qgssymbollayerutils.sip.in | 14 +++++++ src/core/painting/qgsgeometrypaintdevice.cpp | 41 ++----------------- src/core/painting/qgsgeometrypaintdevice.h | 2 - src/core/symbology/qgssymbollayerutils.cpp | 36 ++++++++++++++++ src/core/symbology/qgssymbollayerutils.h | 14 +++++++ 8 files changed, 85 insertions(+), 40 deletions(-) diff --git a/python/PyQt6/core/auto_additions/qgssymbollayerutils.py b/python/PyQt6/core/auto_additions/qgssymbollayerutils.py index 58e412ae683..d5b01d0b775 100644 --- a/python/PyQt6/core/auto_additions/qgssymbollayerutils.py +++ b/python/PyQt6/core/auto_additions/qgssymbollayerutils.py @@ -22,6 +22,8 @@ try: QgsSymbolLayerUtils.decodeBrushStyle = staticmethod(QgsSymbolLayerUtils.decodeBrushStyle) QgsSymbolLayerUtils.encodeSldBrushStyle = staticmethod(QgsSymbolLayerUtils.encodeSldBrushStyle) QgsSymbolLayerUtils.decodeSldBrushStyle = staticmethod(QgsSymbolLayerUtils.decodeSldBrushStyle) + QgsSymbolLayerUtils.penCapStyleToEndCapStyle = staticmethod(QgsSymbolLayerUtils.penCapStyleToEndCapStyle) + QgsSymbolLayerUtils.penJoinStyleToJoinStyle = staticmethod(QgsSymbolLayerUtils.penJoinStyleToJoinStyle) QgsSymbolLayerUtils.hasSldSymbolizer = staticmethod(QgsSymbolLayerUtils.hasSldSymbolizer) QgsSymbolLayerUtils.decodeCoordinateReference = staticmethod(QgsSymbolLayerUtils.decodeCoordinateReference) QgsSymbolLayerUtils.encodeCoordinateReference = staticmethod(QgsSymbolLayerUtils.encodeCoordinateReference) diff --git a/python/PyQt6/core/auto_generated/symbology/qgssymbollayerutils.sip.in b/python/PyQt6/core/auto_generated/symbology/qgssymbollayerutils.sip.in index b0fc83d6b5a..2e5dbdb348b 100644 --- a/python/PyQt6/core/auto_generated/symbology/qgssymbollayerutils.sip.in +++ b/python/PyQt6/core/auto_generated/symbology/qgssymbollayerutils.sip.in @@ -60,6 +60,20 @@ Contains utility functions for working with symbols and symbol layers. static QString encodeSldBrushStyle( Qt::BrushStyle style ); static Qt::BrushStyle decodeSldBrushStyle( const QString &str ); + static Qgis::EndCapStyle penCapStyleToEndCapStyle( Qt::PenCapStyle style ); +%Docstring +Converts a Qt pen cap style to a QGIS end cap style. + +.. versionadded:: 3.42 +%End + + static Qgis::JoinStyle penJoinStyleToJoinStyle( Qt::PenJoinStyle style ); +%Docstring +Converts a Qt pen joinstyle to a QGIS join style. + +.. versionadded:: 3.42 +%End + static bool hasSldSymbolizer( const QDomElement &element ); %Docstring Returns ``True`` if a DOM ``element`` contains an SLD Symbolizer element. diff --git a/python/core/auto_additions/qgssymbollayerutils.py b/python/core/auto_additions/qgssymbollayerutils.py index 58e412ae683..d5b01d0b775 100644 --- a/python/core/auto_additions/qgssymbollayerutils.py +++ b/python/core/auto_additions/qgssymbollayerutils.py @@ -22,6 +22,8 @@ try: QgsSymbolLayerUtils.decodeBrushStyle = staticmethod(QgsSymbolLayerUtils.decodeBrushStyle) QgsSymbolLayerUtils.encodeSldBrushStyle = staticmethod(QgsSymbolLayerUtils.encodeSldBrushStyle) QgsSymbolLayerUtils.decodeSldBrushStyle = staticmethod(QgsSymbolLayerUtils.decodeSldBrushStyle) + QgsSymbolLayerUtils.penCapStyleToEndCapStyle = staticmethod(QgsSymbolLayerUtils.penCapStyleToEndCapStyle) + QgsSymbolLayerUtils.penJoinStyleToJoinStyle = staticmethod(QgsSymbolLayerUtils.penJoinStyleToJoinStyle) QgsSymbolLayerUtils.hasSldSymbolizer = staticmethod(QgsSymbolLayerUtils.hasSldSymbolizer) QgsSymbolLayerUtils.decodeCoordinateReference = staticmethod(QgsSymbolLayerUtils.decodeCoordinateReference) QgsSymbolLayerUtils.encodeCoordinateReference = staticmethod(QgsSymbolLayerUtils.encodeCoordinateReference) diff --git a/python/core/auto_generated/symbology/qgssymbollayerutils.sip.in b/python/core/auto_generated/symbology/qgssymbollayerutils.sip.in index b0fc83d6b5a..2e5dbdb348b 100644 --- a/python/core/auto_generated/symbology/qgssymbollayerutils.sip.in +++ b/python/core/auto_generated/symbology/qgssymbollayerutils.sip.in @@ -60,6 +60,20 @@ Contains utility functions for working with symbols and symbol layers. static QString encodeSldBrushStyle( Qt::BrushStyle style ); static Qt::BrushStyle decodeSldBrushStyle( const QString &str ); + static Qgis::EndCapStyle penCapStyleToEndCapStyle( Qt::PenCapStyle style ); +%Docstring +Converts a Qt pen cap style to a QGIS end cap style. + +.. versionadded:: 3.42 +%End + + static Qgis::JoinStyle penJoinStyleToJoinStyle( Qt::PenJoinStyle style ); +%Docstring +Converts a Qt pen joinstyle to a QGIS join style. + +.. versionadded:: 3.42 +%End + static bool hasSldSymbolizer( const QDomElement &element ); %Docstring Returns ``True`` if a DOM ``element`` contains an SLD Symbolizer element. diff --git a/src/core/painting/qgsgeometrypaintdevice.cpp b/src/core/painting/qgsgeometrypaintdevice.cpp index 4422bc21c19..b74afe68cdb 100644 --- a/src/core/painting/qgsgeometrypaintdevice.cpp +++ b/src/core/painting/qgsgeometrypaintdevice.cpp @@ -20,6 +20,7 @@ #include "qgsmultipolygon.h" #include "qgsmultilinestring.h" #include "qgspainting.h" +#include "qgssymbollayerutils.h" // // QgsGeometryPaintEngine @@ -395,42 +396,6 @@ void QgsGeometryPaintEngine::addStrokedLine( const QgsLineString *line, double p } } -Qgis::EndCapStyle QgsGeometryPaintEngine::penStyleToCapStyle( Qt::PenCapStyle style ) -{ - switch ( style ) - { - case Qt::FlatCap: - return Qgis::EndCapStyle::Flat; - case Qt::SquareCap: - return Qgis::EndCapStyle::Square; - case Qt::RoundCap: - return Qgis::EndCapStyle::Round; - case Qt::MPenCapStyle: - // undocumented? - break; - } - - return Qgis::EndCapStyle::Round; -} - -Qgis::JoinStyle QgsGeometryPaintEngine::penStyleToJoinStyle( Qt::PenJoinStyle style ) -{ - switch ( style ) - { - case Qt::MiterJoin: - case Qt::SvgMiterJoin: - return Qgis::JoinStyle::Miter; - case Qt::BevelJoin: - return Qgis::JoinStyle::Bevel; - case Qt::RoundJoin: - return Qgis::JoinStyle::Round; - case Qt::MPenJoinStyle: - // undocumented? - break; - } - return Qgis::JoinStyle::Round; -} - // based on QPainterPath::toSubpathPolygons() void QgsGeometryPaintEngine::addSubpathGeometries( const QPainterPath &path, const QTransform &matrix ) { @@ -439,8 +404,8 @@ void QgsGeometryPaintEngine::addSubpathGeometries( const QPainterPath &path, con const bool transformIsIdentity = matrix.isIdentity(); - const Qgis::EndCapStyle endCapStyle = penStyleToCapStyle( mPen.capStyle() ); - const Qgis::JoinStyle joinStyle = penStyleToJoinStyle( mPen.joinStyle() ); + const Qgis::EndCapStyle endCapStyle = QgsSymbolLayerUtils::penCapStyleToEndCapStyle( mPen.capStyle() ); + const Qgis::JoinStyle joinStyle = QgsSymbolLayerUtils::penJoinStyleToJoinStyle( mPen.joinStyle() ); const double penWidth = mPen.widthF() <= 0 ? 1 : mPen.widthF(); const double miterLimit = mPen.miterLimit(); diff --git a/src/core/painting/qgsgeometrypaintdevice.h b/src/core/painting/qgsgeometrypaintdevice.h index 9c781b38535..44a74e0b0c4 100644 --- a/src/core/painting/qgsgeometrypaintdevice.h +++ b/src/core/painting/qgsgeometrypaintdevice.h @@ -103,8 +103,6 @@ class QgsGeometryPaintEngine: public QPaintEngine void addSubpathGeometries( const QPainterPath &path, const QTransform &matrix ); void addStrokedLine( const QgsLineString *line, double penWidth, Qgis::EndCapStyle endCapStyle, Qgis::JoinStyle joinStyle, double miterLimit, const QTransform *matrix ); - static Qgis::EndCapStyle penStyleToCapStyle( Qt::PenCapStyle style ); - static Qgis::JoinStyle penStyleToJoinStyle( Qt::PenJoinStyle style ); bool mUsePathStroker = false; QPen mPen; diff --git a/src/core/symbology/qgssymbollayerutils.cpp b/src/core/symbology/qgssymbollayerutils.cpp index 24d9f032ddf..91dee5189e5 100644 --- a/src/core/symbology/qgssymbollayerutils.cpp +++ b/src/core/symbology/qgssymbollayerutils.cpp @@ -392,6 +392,42 @@ Qt::BrushStyle QgsSymbolLayerUtils::decodeSldBrushStyle( const QString &str ) return Qt::NoBrush; } +Qgis::EndCapStyle QgsSymbolLayerUtils::penCapStyleToEndCapStyle( Qt::PenCapStyle style ) +{ + switch ( style ) + { + case Qt::FlatCap: + return Qgis::EndCapStyle::Flat; + case Qt::SquareCap: + return Qgis::EndCapStyle::Square; + case Qt::RoundCap: + return Qgis::EndCapStyle::Round; + case Qt::MPenCapStyle: + // undocumented? + break; + } + + return Qgis::EndCapStyle::Round; +} + +Qgis::JoinStyle QgsSymbolLayerUtils::penJoinStyleToJoinStyle( Qt::PenJoinStyle style ) +{ + switch ( style ) + { + case Qt::MiterJoin: + case Qt::SvgMiterJoin: + return Qgis::JoinStyle::Miter; + case Qt::BevelJoin: + return Qgis::JoinStyle::Bevel; + case Qt::RoundJoin: + return Qgis::JoinStyle::Round; + case Qt::MPenJoinStyle: + // undocumented? + break; + } + return Qgis::JoinStyle::Round; +} + bool QgsSymbolLayerUtils::hasSldSymbolizer( const QDomElement &element ) { const QDomNodeList children = element.childNodes(); diff --git a/src/core/symbology/qgssymbollayerutils.h b/src/core/symbology/qgssymbollayerutils.h index 16017ff3450..00201ae1c59 100644 --- a/src/core/symbology/qgssymbollayerutils.h +++ b/src/core/symbology/qgssymbollayerutils.h @@ -94,6 +94,20 @@ class CORE_EXPORT QgsSymbolLayerUtils static QString encodeSldBrushStyle( Qt::BrushStyle style ); static Qt::BrushStyle decodeSldBrushStyle( const QString &str ); + /** + * Converts a Qt pen cap style to a QGIS end cap style. + * + * \since QGIS 3.42 + */ + static Qgis::EndCapStyle penCapStyleToEndCapStyle( Qt::PenCapStyle style ); + + /** + * Converts a Qt pen joinstyle to a QGIS join style. + * + * \since QGIS 3.42 + */ + static Qgis::JoinStyle penJoinStyleToJoinStyle( Qt::PenJoinStyle style ); + /** * Returns TRUE if a DOM \a element contains an SLD Symbolizer element. *