QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgssymbollayerutils.h
Go to the documentation of this file.
1/***************************************************************************
2 qgssymbollayerutils.h
3 ---------------------
4 begin : November 2009
5 copyright : (C) 2009 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16
17#ifndef QGSSYMBOLLAYERUTILS_H
18#define QGSSYMBOLLAYERUTILS_H
19
20#include "qgis_core.h"
21#include "qgis_sip.h"
22#include <QMap>
23#include <QFont>
24#include <QColor>
25#include <QPainter>
26#include "qgis.h"
27#include "qgsmapunitscale.h"
28#include "qgscolorramp.h"
29#include "qgsarrowsymbollayer.h"
30#include "qgssymbol.h"
31
32#include <QFile>
33
34class QgsExpression;
35class QgsPathResolver;
37class QgsSymbolLayer;
38
39typedef QMap<QString, QString> QgsStringMap;
40typedef QMap<QString, QgsSymbol * > QgsSymbolMap;
41typedef QList< QPair< QColor, QString > > QgsNamedColorList SIP_SKIP;
42
43class QDomDocument;
44class QDomElement;
45class QIcon;
46class QPixmap;
47class QPointF;
48class QSize;
49class QMimeData;
52
57class CORE_EXPORT QgsSymbolLayerUtils
58{
59 public:
60
61 static QString encodeColor( const QColor &color );
62 static QColor decodeColor( const QString &str );
63
64 static QString encodeSldAlpha( int alpha );
65 static int decodeSldAlpha( const QString &str );
66
67 static QString encodeSldFontStyle( QFont::Style style );
68 static QFont::Style decodeSldFontStyle( const QString &str );
69
70 static QString encodeSldFontWeight( int weight );
71 static int decodeSldFontWeight( const QString &str );
72
73 static QString encodePenStyle( Qt::PenStyle style );
74 static Qt::PenStyle decodePenStyle( const QString &str );
75
76 static QString encodePenJoinStyle( Qt::PenJoinStyle style );
77 static Qt::PenJoinStyle decodePenJoinStyle( const QString &str );
78
79 static QString encodePenCapStyle( Qt::PenCapStyle style );
80 static Qt::PenCapStyle decodePenCapStyle( const QString &str );
81
82 static QString encodeSldLineJoinStyle( Qt::PenJoinStyle style );
83 static Qt::PenJoinStyle decodeSldLineJoinStyle( const QString &str );
84
85 static QString encodeSldLineCapStyle( Qt::PenCapStyle style );
86 static Qt::PenCapStyle decodeSldLineCapStyle( const QString &str );
87
88 static QString encodeBrushStyle( Qt::BrushStyle style );
89 static Qt::BrushStyle decodeBrushStyle( const QString &str );
90
91 static QString encodeSldBrushStyle( Qt::BrushStyle style );
92 static Qt::BrushStyle decodeSldBrushStyle( const QString &str );
93
104 static Qgis::SymbolCoordinateReference decodeCoordinateReference( const QString &string, bool *ok SIP_OUT = nullptr );
105
113 static QString encodeCoordinateReference( Qgis::SymbolCoordinateReference coordinateReference );
114
119 static QgsArrowSymbolLayer::HeadType decodeArrowHeadType( const QVariant &value, bool *ok SIP_OUT = nullptr );
120
125 static QgsArrowSymbolLayer::ArrowType decodeArrowType( const QVariant &value, bool *ok SIP_OUT = nullptr );
126
137 static Qgis::MarkerClipMode decodeMarkerClipMode( const QString &string, bool *ok SIP_OUT = nullptr );
138
145 static QString encodeMarkerClipMode( Qgis::MarkerClipMode mode );
146
157 static Qgis::LineClipMode decodeLineClipMode( const QString &string, bool *ok SIP_OUT = nullptr );
158
165 static QString encodeLineClipMode( Qgis::LineClipMode mode );
166
172 static QString encodePoint( QPointF point );
173
179 static QPointF decodePoint( const QString &string );
180
193 static QPointF toPoint( const QVariant &value, bool *ok SIP_OUT = nullptr );
194
201 static QString encodeSize( QSizeF size );
202
209 static QSizeF decodeSize( const QString &string );
210
223 static QSizeF toSize( const QVariant &value, bool *ok SIP_OUT = nullptr );
224
225 static QString encodeMapUnitScale( const QgsMapUnitScale &mapUnitScale );
226 static QgsMapUnitScale decodeMapUnitScale( const QString &str );
227
228 static QString encodeRealVector( const QVector<qreal> &v );
229 static QVector<qreal> decodeRealVector( const QString &s );
230
231 static QString encodeSldRealVector( const QVector<qreal> &v );
232 static QVector<qreal> decodeSldRealVector( const QString &s );
233
241 static QString encodeSldUom( QgsUnitTypes::RenderUnit unit, double *scaleFactor );
242
250 static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor = nullptr );
251
259 static double sizeInPixelsFromSldUom( const QString &uom, double size );
260
266 static QString encodeScaleMethod( Qgis::ScaleMethod scaleMethod );
267
273 static Qgis::ScaleMethod decodeScaleMethod( const QString &str );
274
275 static QPainter::CompositionMode decodeBlendMode( const QString &s );
276
285 static QIcon symbolPreviewIcon( const QgsSymbol *symbol, QSize size, int padding = 0, QgsLegendPatchShape *shape = nullptr );
286
302 static QPixmap symbolPreviewPixmap( const QgsSymbol *symbol, QSize size, int padding = 0, QgsRenderContext *customContext = nullptr, bool selected = false,
303 const QgsExpressionContext *expressionContext = nullptr,
304 const QgsLegendPatchShape *shape = nullptr );
305
317 static QPicture symbolLayerPreviewPicture( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit units, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale(), Qgis::SymbolType parentSymbolType = Qgis::SymbolType::Hybrid );
318
330 static QIcon symbolLayerPreviewIcon( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale(), Qgis::SymbolType parentSymbolType = Qgis::SymbolType::Hybrid, QgsMapLayer *mapLayer = nullptr );
331
339 static QIcon colorRampPreviewIcon( QgsColorRamp *ramp, QSize size, int padding = 0 );
340
353 static QPixmap colorRampPreviewPixmap( QgsColorRamp *ramp, QSize size, int padding = 0, Qt::Orientation direction = Qt::Horizontal,
354 bool flipDirection = false, bool drawTransparentBackground = true );
355
356 static void drawStippledBackground( QPainter *painter, QRect rect );
357
362 static void drawVertexMarker( double x, double y, QPainter &p, Qgis::VertexMarkerType type, int markerSize );
363
365 static double estimateMaxSymbolBleed( QgsSymbol *symbol, const QgsRenderContext &context );
366
373 static QgsSymbol *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
374
383 template <class SymbolType> static SymbolType *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_SKIP
384 {
385 QgsSymbol *tmpSymbol = QgsSymbolLayerUtils::loadSymbol( element, context );
386 SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );
387
388 if ( symbolCastToType )
389 {
390 return symbolCastToType;
391 }
392 else
393 {
394 //could not cast
395 delete tmpSymbol;
396 return nullptr;
397 }
398 }
399
401 static QgsSymbolLayer *loadSymbolLayer( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
403 static QDomElement saveSymbol( const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context );
404
410 static QString symbolProperties( QgsSymbol *symbol );
411
415 static bool createSymbolLayerListFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType, QList<QgsSymbolLayer *> &layers );
416
417 static QgsSymbolLayer *createFillLayerFromSld( QDomElement &element ) SIP_FACTORY;
418 static QgsSymbolLayer *createLineLayerFromSld( QDomElement &element ) SIP_FACTORY;
419 static QgsSymbolLayer *createMarkerLayerFromSld( QDomElement &element ) SIP_FACTORY;
420
424 static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QList<QgsSymbolLayer *> &layerList );
425 static bool hasExternalGraphic( QDomElement &element );
426 static bool hasWellKnownMark( QDomElement &element );
427
428 static bool needFontMarker( QDomElement &element );
429 static bool needSvgMarker( QDomElement &element );
430 static bool needEllipseMarker( QDomElement &element );
431 static bool needMarkerLine( QDomElement &element );
432 static bool needLinePatternFill( QDomElement &element );
433 static bool needPointPatternFill( QDomElement &element );
434 static bool needSvgFill( QDomElement &element );
435
436 static void fillToSld( QDomDocument &doc, QDomElement &element,
437 Qt::BrushStyle brushStyle, const QColor &color = QColor() );
438 static bool fillFromSld( QDomElement &element,
439 Qt::BrushStyle &brushStyle, QColor &color );
440
442 static void lineToSld( QDomDocument &doc, QDomElement &element,
443 Qt::PenStyle penStyle, const QColor &color, double width = -1,
444 const Qt::PenJoinStyle *penJoinStyle = nullptr, const Qt::PenCapStyle *penCapStyle = nullptr,
445 const QVector<qreal> *customDashPattern = nullptr, double dashOffset = 0.0 ) SIP_SKIP;
446 static bool lineFromSld( QDomElement &element,
447 Qt::PenStyle &penStyle, QColor &color, double &width,
448 Qt::PenJoinStyle *penJoinStyle = nullptr, Qt::PenCapStyle *penCapStyle = nullptr,
449 QVector<qreal> *customDashPattern = nullptr, double *dashOffset = nullptr );
450
451 static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
452 const QString &path, const QString &mime,
453 const QColor &color, double size = -1 );
454 static bool externalGraphicFromSld( QDomElement &element,
455 QString &path, QString &mime,
456 QColor &color, double &size );
457
458 static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
459 const QString &name, const QColor &color, const QColor &strokeColor, Qt::PenStyle strokeStyle,
460 double strokeWidth = -1, double size = -1 );
461
463 static bool wellKnownMarkerFromSld( QDomElement &element,
464 QString &name, QColor &color, QColor &strokeColor, Qt::PenStyle &strokeStyle,
465 double &strokeWidth, double &size ) SIP_PYNAME( wellKnownMarkerFromSld2 );
466
467 static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
468 const QString &path, const QString &format, int *markIndex = nullptr,
469 const QColor &color = QColor(), double size = -1 );
470 static bool externalMarkerFromSld( QDomElement &element,
471 QString &path, QString &format, int &markIndex,
472 QColor &color, double &size );
473
474
475 static void labelTextToSld( QDomDocument &doc, QDomElement &element, const QString &label,
476 const QFont &font, const QColor &color = QColor(), double size = -1 );
477
479 static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor &c,
480 Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
481 Qt::PenCapStyle capStyle = Qt::FlatCap,
482 double offset = 0.0,
483 const QVector<qreal> *dashPattern = nullptr );
484
489 static QString ogrFeatureStyleBrush( const QColor &fillColr );
490
491 static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString &rotationFunc );
492 static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
493
494 static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString &alphaFunc );
495 static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
496
497 static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
498 static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
499
506 static void createAnchorPointElement( QDomDocument &doc, QDomElement &element, QPointF anchor );
507
508 static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString &path, const QString &format );
509 static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
510
511 static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString &geomFunc );
512 static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
513
520 static bool createExpressionElement( QDomDocument &doc, QDomElement &element, const QString &function );
521 static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString &function );
522 static bool functionFromSldElement( QDomElement &element, QString &function );
523
524 static QDomElement createSvgParameterElement( QDomDocument &doc, const QString &name, const QString &value );
525 static QgsStringMap getSvgParameterList( QDomElement &element );
526
527 static QDomElement createVendorOptionElement( QDomDocument &doc, const QString &name, const QString &value );
528 static QgsStringMap getVendorOptionList( QDomElement &element );
529
531 static QVariantMap parseProperties( const QDomElement &element );
533 static void saveProperties( QVariantMap props, QDomDocument &doc, QDomElement &element );
534
536 static QgsSymbolMap loadSymbols( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
538 static QDomElement saveSymbols( QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context );
539
540 static void clearSymbolMap( QgsSymbolMap &symbols );
541
549 static QMimeData *symbolToMimeData( const QgsSymbol *symbol ) SIP_FACTORY;
550
557 static QgsSymbol *symbolFromMimeData( const QMimeData *data ) SIP_FACTORY;
558
565 static QgsColorRamp *loadColorRamp( QDomElement &element ) SIP_FACTORY;
566
575 static QDomElement saveColorRamp( const QString &name, QgsColorRamp *ramp, QDomDocument &doc );
576
583 static QVariant colorRampToVariant( const QString &name, QgsColorRamp *ramp );
584
591 static QgsColorRamp *loadColorRamp( const QVariant &value ) SIP_FACTORY;
592
599 static QString colorToName( const QColor &color );
600
608 static QList< QColor > parseColorList( const QString &colorStr );
609
617 static QMimeData *colorToMimeData( const QColor &color ) SIP_FACTORY;
618
627 static QColor colorFromMimeData( const QMimeData *data, bool &hasAlpha SIP_OUT );
628
635 static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
636
644 static QMimeData *colorListToMimeData( const QgsNamedColorList &colorList, bool allFormats = true ) SIP_FACTORY;
645
654 static bool saveColorsToGpl( QFile &file, const QString &paletteName, const QgsNamedColorList &colors );
655
664 static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString &name );
665
674 static QColor parseColor( const QString &colorStr, bool strictEval = false );
675
685 static QColor parseColorWithAlpha( const QString &colorStr, bool &containsAlpha, bool strictEval = false );
686
690 static void multiplyImageOpacity( QImage *image, qreal opacity );
691
693 static void blurImageInPlace( QImage &image, QRect rect, int radius, bool alphaOnly );
694
699 static void premultiplyColor( QColor &rgb, int alpha );
700
713 static bool condenseFillAndOutline( QgsFillSymbolLayer *fill, QgsLineSymbolLayer *outline );
714
716 static void sortVariantList( QList<QVariant> &list, Qt::SortOrder order );
718 static QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance );
719
721 static QStringList listSvgFiles();
722
724 static QStringList listSvgFilesAt( const QString &directory );
725
732 static QString svgSymbolNameToPath( const QString &name, const QgsPathResolver &pathResolver );
733
738 static QString svgSymbolPathToName( const QString &path, const QgsPathResolver &pathResolver );
739
741 static QPointF polygonCentroid( const QPolygonF &points );
742
744 static QPointF polygonPointOnSurface( const QPolygonF &points, const QVector<QPolygonF> *rings = nullptr );
745
747 static bool pointInPolygon( const QPolygonF &points, QPointF point );
748
754 static double polylineLength( const QPolygonF &polyline );
755
768 static QPolygonF polylineSubstring( const QPolygonF &polyline, double startOffset, double endOffset );
769
777 static bool isSharpCorner( QPointF p1, QPointF p2, QPointF p3 );
778
786 static void appendPolyline( QPolygonF &target, const QPolygonF &line );
787
795 static QgsExpression *fieldOrExpressionToExpression( const QString &fieldOrExpression ) SIP_FACTORY;
796
804 static QString fieldOrExpressionFromExpression( QgsExpression *expression );
805
812 static QList<double> prettyBreaks( double minimum, double maximum, int classes );
813
819 static double rescaleUom( double size, QgsUnitTypes::RenderUnit unit, const QVariantMap &props );
820
826 static QPointF rescaleUom( QPointF point, QgsUnitTypes::RenderUnit unit, const QVariantMap &props ) SIP_PYNAME( rescalePointUom );
827
833 static QVector<qreal> rescaleUom( const QVector<qreal> &array, QgsUnitTypes::RenderUnit unit, const QVariantMap &props ) SIP_PYNAME( rescaleArrayUom );
834
839 static void applyScaleDependency( QDomDocument &doc, QDomElement &ruleElem, QVariantMap &props );
840
845 static void mergeScaleDependencies( double mScaleMinDenom, double mScaleMaxDenom, QVariantMap &props );
846
853 static void parametricSvgToSld( QDomDocument &doc, QDomElement &graphicElem,
854 const QString &path,
855 const QColor &fillColor, double size, const QColor &strokeColor, double strokeWidth );
856
861 static QString getSvgParametricPath( const QString &basePath, const QColor &fillColor, const QColor &strokeColor, double strokeWidth );
862
867 static QSet<const QgsSymbolLayer *> toSymbolLayerPointers( QgsFeatureRenderer *renderer, const QSet<QgsSymbolLayerId> &symbolLayerIds );
868
876 static double rendererFrameRate( const QgsFeatureRenderer *renderer );
877
892 static QgsSymbol *restrictedSizeSymbol( const QgsSymbol *s, double minSize, double maxSize, QgsRenderContext *context, double &width, double &height, bool *ok = nullptr );
893
898 static QgsStringMap evaluatePropertiesMap( const QMap<QString, QgsProperty> &propertiesMap, const QgsExpressionContext &context );
899
901#ifndef SIP_RUN
902 static QgsProperty rotateWholeSymbol( double additionalRotation, const QgsProperty &property )
903 {
904 const QString exprString = property.asExpression();
905 return QgsProperty::fromExpression( QString::number( additionalRotation ) + " + (" + exprString + ')' );
906 }
907
908 static QgsProperty scaleWholeSymbol( double scaleFactor, const QgsProperty &property )
909 {
910 const QString exprString = property.asExpression();
911 return QgsProperty::fromExpression( QString::number( scaleFactor ) + "*(" + exprString + ')' );
912 }
913
914 static QgsProperty scaleWholeSymbol( double scaleFactorX, double scaleFactorY, const QgsProperty &property )
915 {
916 const QString exprString = property.asExpression();
918 ( !qgsDoubleNear( scaleFactorX, 0.0 ) ? "tostring(" + QString::number( scaleFactorX ) + "*(" + exprString + "))" : QStringLiteral( "'0'" ) ) +
919 "|| ',' || " +
920 ( !qgsDoubleNear( scaleFactorY, 0.0 ) ? "tostring(" + QString::number( scaleFactorY ) + "*(" + exprString + "))" : QStringLiteral( "'0'" ) ) );
921 }
922#endif
924
925};
926
927class QPolygonF;
928
930QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType ) SIP_SKIP;
931
932#endif
MarkerClipMode
Marker clipping modes.
Definition: qgis.h:1674
LineClipMode
Line clipping modes.
Definition: qgis.h:1688
ScaleMethod
Scale methods.
Definition: qgis.h:220
VertexMarkerType
Editing vertex markers, used for showing vertices during a edit operation.
Definition: qgis.h:860
SymbolType
Symbol types.
Definition: qgis.h:206
@ Hybrid
Hybrid symbol.
SymbolCoordinateReference
Symbol coordinate reference modes.
Definition: qgis.h:1632
HeadType
Possible head types.
ArrowType
Possible arrow types.
Abstract base class for color ramps.
Definition: qgscolorramp.h:30
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
Represents a patch shape for use in map legends.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Struct for storing maximum and minimum scales for measurements in map units.
Resolves relative paths into absolute paths and vice versa.
A store for object properties.
Definition: qgsproperty.h:230
QString asExpression() const
Returns an expression string representing the state of the property, or an empty string if the proper...
static QgsProperty fromExpression(const QString &expression, bool isActive=true)
Returns a new ExpressionBasedProperty created from the specified expression.
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
We may need stable references to symbol layers, when pointers to symbol layers is not usable (when a ...
static SymbolType * loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element and cast it to a particular symbol type.
static QgsSymbol * loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:93
Helper functions for various unit types.
Definition: qgsunittypes.h:39
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:168
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define str(x)
Definition: qgis.cpp:37
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2527
QMap< QString, QString > QgsStringMap
Definition: qgis.h:3022
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_FACTORY
Definition: qgis_sip.h:76
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgsrenderer.h:45
QList< QPolygonF > offsetLine(QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType)
calculate geometry shifted by a specified distance
QMap< QString, QgsSymbol * > QgsSymbolMap
QMap< QString, QString > QgsStringMap