QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
34 class QgsExpression;
35 class QgsPathResolver;
37 class QgsSymbolLayer;
38 
39 typedef QMap<QString, QString> QgsStringMap;
40 typedef QMap<QString, QgsSymbol * > QgsSymbolMap;
41 typedef QList< QPair< QColor, QString > > QgsNamedColorList SIP_SKIP;
42 
43 class QDomDocument;
44 class QDomElement;
45 class QIcon;
46 class QPixmap;
47 class QPointF;
48 class QSize;
49 class QMimeData;
50 class QgsFeatureRenderer;
51 class QgsSymbolLayerId;
52 
57 class CORE_EXPORT QgsSymbolLayerUtils
58 {
59  public:
60 
63  {
66  NoMarker
67  };
68 
69  static QString encodeColor( const QColor &color );
70  static QColor decodeColor( const QString &str );
71 
72  static QString encodeSldAlpha( int alpha );
73  static int decodeSldAlpha( const QString &str );
74 
75  static QString encodeSldFontStyle( QFont::Style style );
76  static QFont::Style decodeSldFontStyle( const QString &str );
77 
78  static QString encodeSldFontWeight( int weight );
79  static int decodeSldFontWeight( const QString &str );
80 
81  static QString encodePenStyle( Qt::PenStyle style );
82  static Qt::PenStyle decodePenStyle( const QString &str );
83 
84  static QString encodePenJoinStyle( Qt::PenJoinStyle style );
85  static Qt::PenJoinStyle decodePenJoinStyle( const QString &str );
86 
87  static QString encodePenCapStyle( Qt::PenCapStyle style );
88  static Qt::PenCapStyle decodePenCapStyle( const QString &str );
89 
90  static QString encodeSldLineJoinStyle( Qt::PenJoinStyle style );
91  static Qt::PenJoinStyle decodeSldLineJoinStyle( const QString &str );
92 
93  static QString encodeSldLineCapStyle( Qt::PenCapStyle style );
94  static Qt::PenCapStyle decodeSldLineCapStyle( const QString &str );
95 
96  static QString encodeBrushStyle( Qt::BrushStyle style );
97  static Qt::BrushStyle decodeBrushStyle( const QString &str );
98 
99  static QString encodeSldBrushStyle( Qt::BrushStyle style );
100  static Qt::BrushStyle decodeSldBrushStyle( const QString &str );
101 
106  static QgsArrowSymbolLayer::HeadType decodeArrowHeadType( const QVariant &value, bool *ok SIP_OUT = nullptr );
107 
112  static QgsArrowSymbolLayer::ArrowType decodeArrowType( const QVariant &value, bool *ok SIP_OUT = nullptr );
113 
119  static QString encodePoint( QPointF point );
120 
126  static QPointF decodePoint( const QString &string );
127 
140  static QPointF toPoint( const QVariant &value, bool *ok SIP_OUT = nullptr );
141 
148  static QString encodeSize( QSizeF size );
149 
156  static QSizeF decodeSize( const QString &string );
157 
170  static QSizeF toSize( const QVariant &value, bool *ok SIP_OUT = nullptr );
171 
172  static QString encodeMapUnitScale( const QgsMapUnitScale &mapUnitScale );
173  static QgsMapUnitScale decodeMapUnitScale( const QString &str );
174 
175  static QString encodeRealVector( const QVector<qreal> &v );
176  static QVector<qreal> decodeRealVector( const QString &s );
177 
178  static QString encodeSldRealVector( const QVector<qreal> &v );
179  static QVector<qreal> decodeSldRealVector( const QString &s );
180 
188  static QString encodeSldUom( QgsUnitTypes::RenderUnit unit, double *scaleFactor );
189 
197  static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor = nullptr );
198 
206  static double sizeInPixelsFromSldUom( const QString &uom, double size );
207 
213  static QString encodeScaleMethod( Qgis::ScaleMethod scaleMethod );
214 
220  static Qgis::ScaleMethod decodeScaleMethod( const QString &str );
221 
222  static QPainter::CompositionMode decodeBlendMode( const QString &s );
223 
232  static QIcon symbolPreviewIcon( const QgsSymbol *symbol, QSize size, int padding = 0, QgsLegendPatchShape *shape = nullptr );
233 
249  static QPixmap symbolPreviewPixmap( const QgsSymbol *symbol, QSize size, int padding = 0, QgsRenderContext *customContext = nullptr, bool selected = false,
250  const QgsExpressionContext *expressionContext = nullptr,
251  const QgsLegendPatchShape *shape = nullptr );
252 
263  static QPicture symbolLayerPreviewPicture( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit units, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
264 
274  static QIcon symbolLayerPreviewIcon( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
275 
283  static QIcon colorRampPreviewIcon( QgsColorRamp *ramp, QSize size, int padding = 0 );
284 
297  static QPixmap colorRampPreviewPixmap( QgsColorRamp *ramp, QSize size, int padding = 0, Qt::Orientation direction = Qt::Horizontal,
298  bool flipDirection = false, bool drawTransparentBackground = true );
299 
300  static void drawStippledBackground( QPainter *painter, QRect rect );
301 
306  static void drawVertexMarker( double x, double y, QPainter &p, QgsSymbolLayerUtils::VertexMarkerType type, int markerSize );
307 
309  static double estimateMaxSymbolBleed( QgsSymbol *symbol, const QgsRenderContext &context );
310 
317  static QgsSymbol *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
318 
327  template <class SymbolType> static SymbolType *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_SKIP
328  {
329  QgsSymbol *tmpSymbol = QgsSymbolLayerUtils::loadSymbol( element, context );
330  SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );
331 
332  if ( symbolCastToType )
333  {
334  return symbolCastToType;
335  }
336  else
337  {
338  //could not cast
339  delete tmpSymbol;
340  return nullptr;
341  }
342  }
343 
345  static QgsSymbolLayer *loadSymbolLayer( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
347  static QDomElement saveSymbol( const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context );
348 
354  static QString symbolProperties( QgsSymbol *symbol );
355 
359  static bool createSymbolLayerListFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType, QList<QgsSymbolLayer *> &layers );
360 
361  static QgsSymbolLayer *createFillLayerFromSld( QDomElement &element ) SIP_FACTORY;
362  static QgsSymbolLayer *createLineLayerFromSld( QDomElement &element ) SIP_FACTORY;
363  static QgsSymbolLayer *createMarkerLayerFromSld( QDomElement &element ) SIP_FACTORY;
364 
368  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QList<QgsSymbolLayer *> &layerList );
369  static bool hasExternalGraphic( QDomElement &element );
370  static bool hasWellKnownMark( QDomElement &element );
371 
372  static bool needFontMarker( QDomElement &element );
373  static bool needSvgMarker( QDomElement &element );
374  static bool needEllipseMarker( QDomElement &element );
375  static bool needMarkerLine( QDomElement &element );
376  static bool needLinePatternFill( QDomElement &element );
377  static bool needPointPatternFill( QDomElement &element );
378  static bool needSvgFill( QDomElement &element );
379 
380  static void fillToSld( QDomDocument &doc, QDomElement &element,
381  Qt::BrushStyle brushStyle, const QColor &color = QColor() );
382  static bool fillFromSld( QDomElement &element,
383  Qt::BrushStyle &brushStyle, QColor &color );
384 
386  static void lineToSld( QDomDocument &doc, QDomElement &element,
387  Qt::PenStyle penStyle, const QColor &color, double width = -1,
388  const Qt::PenJoinStyle *penJoinStyle = nullptr, const Qt::PenCapStyle *penCapStyle = nullptr,
389  const QVector<qreal> *customDashPattern = nullptr, double dashOffset = 0.0 ) SIP_SKIP;
390  static bool lineFromSld( QDomElement &element,
391  Qt::PenStyle &penStyle, QColor &color, double &width,
392  Qt::PenJoinStyle *penJoinStyle = nullptr, Qt::PenCapStyle *penCapStyle = nullptr,
393  QVector<qreal> *customDashPattern = nullptr, double *dashOffset = nullptr );
394 
395  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
396  const QString &path, const QString &mime,
397  const QColor &color, double size = -1 );
398  static bool externalGraphicFromSld( QDomElement &element,
399  QString &path, QString &mime,
400  QColor &color, double &size );
401 
402  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
403  const QString &name, const QColor &color, const QColor &strokeColor, Qt::PenStyle strokeStyle,
404  double strokeWidth = -1, double size = -1 );
405 
407  static bool wellKnownMarkerFromSld( QDomElement &element,
408  QString &name, QColor &color, QColor &strokeColor, Qt::PenStyle &strokeStyle,
409  double &strokeWidth, double &size ) SIP_PYNAME( wellKnownMarkerFromSld2 );
410 
411  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
412  const QString &path, const QString &format, int *markIndex = nullptr,
413  const QColor &color = QColor(), double size = -1 );
414  static bool externalMarkerFromSld( QDomElement &element,
415  QString &path, QString &format, int &markIndex,
416  QColor &color, double &size );
417 
418 
419  static void labelTextToSld( QDomDocument &doc, QDomElement &element, const QString &label,
420  const QFont &font, const QColor &color = QColor(), double size = -1 );
421 
423  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor &c,
424  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
425  Qt::PenCapStyle capStyle = Qt::FlatCap,
426  double offset = 0.0,
427  const QVector<qreal> *dashPattern = nullptr );
428 
433  static QString ogrFeatureStyleBrush( const QColor &fillColr );
434 
435  static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString &rotationFunc );
436  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
437 
438  static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString &alphaFunc );
439  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
440 
441  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
442  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
443 
450  static void createAnchorPointElement( QDomDocument &doc, QDomElement &element, QPointF anchor );
451 
452  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString &path, const QString &format );
453  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
454 
455  static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString &geomFunc );
456  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
457 
464  static bool createExpressionElement( QDomDocument &doc, QDomElement &element, const QString &function );
465  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString &function );
466  static bool functionFromSldElement( QDomElement &element, QString &function );
467 
468  static QDomElement createSvgParameterElement( QDomDocument &doc, const QString &name, const QString &value );
469  static QgsStringMap getSvgParameterList( QDomElement &element );
470 
471  static QDomElement createVendorOptionElement( QDomDocument &doc, const QString &name, const QString &value );
472  static QgsStringMap getVendorOptionList( QDomElement &element );
473 
475  static QVariantMap parseProperties( const QDomElement &element );
477  static void saveProperties( QVariantMap props, QDomDocument &doc, QDomElement &element );
478 
480  static QgsSymbolMap loadSymbols( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
482  static QDomElement saveSymbols( QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context );
483 
484  static void clearSymbolMap( QgsSymbolMap &symbols );
485 
493  static QMimeData *symbolToMimeData( const QgsSymbol *symbol ) SIP_FACTORY;
494 
501  static QgsSymbol *symbolFromMimeData( const QMimeData *data ) SIP_FACTORY;
502 
509  static QgsColorRamp *loadColorRamp( QDomElement &element ) SIP_FACTORY;
510 
519  static QDomElement saveColorRamp( const QString &name, QgsColorRamp *ramp, QDomDocument &doc );
520 
527  static QVariant colorRampToVariant( const QString &name, QgsColorRamp *ramp );
528 
535  static QgsColorRamp *loadColorRamp( const QVariant &value ) SIP_FACTORY;
536 
543  static QString colorToName( const QColor &color );
544 
552  static QList< QColor > parseColorList( const QString &colorStr );
553 
561  static QMimeData *colorToMimeData( const QColor &color ) SIP_FACTORY;
562 
571  static QColor colorFromMimeData( const QMimeData *data, bool &hasAlpha SIP_OUT );
572 
579  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
580 
588  static QMimeData *colorListToMimeData( const QgsNamedColorList &colorList, bool allFormats = true ) SIP_FACTORY;
589 
598  static bool saveColorsToGpl( QFile &file, const QString &paletteName, const QgsNamedColorList &colors );
599 
608  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString &name );
609 
618  static QColor parseColor( const QString &colorStr, bool strictEval = false );
619 
629  static QColor parseColorWithAlpha( const QString &colorStr, bool &containsAlpha, bool strictEval = false );
630 
634  static void multiplyImageOpacity( QImage *image, qreal opacity );
635 
637  static void blurImageInPlace( QImage &image, QRect rect, int radius, bool alphaOnly );
638 
643  static void premultiplyColor( QColor &rgb, int alpha );
644 
657  static bool condenseFillAndOutline( QgsFillSymbolLayer *fill, QgsLineSymbolLayer *outline );
658 
660  static void sortVariantList( QList<QVariant> &list, Qt::SortOrder order );
662  static QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance );
663 
665  static QStringList listSvgFiles();
666 
668  static QStringList listSvgFilesAt( const QString &directory );
669 
676  static QString svgSymbolNameToPath( const QString &name, const QgsPathResolver &pathResolver );
677 
682  static QString svgSymbolPathToName( const QString &path, const QgsPathResolver &pathResolver );
683 
685  static QPointF polygonCentroid( const QPolygonF &points );
686 
688  static QPointF polygonPointOnSurface( const QPolygonF &points, const QVector<QPolygonF> *rings = nullptr );
689 
691  static bool pointInPolygon( const QPolygonF &points, QPointF point );
692 
698  static double polylineLength( const QPolygonF &polyline );
699 
712  static QPolygonF polylineSubstring( const QPolygonF &polyline, double startOffset, double endOffset );
713 
721  static bool isSharpCorner( QPointF p1, QPointF p2, QPointF p3 );
722 
730  static void appendPolyline( QPolygonF &target, const QPolygonF &line );
731 
739  static QgsExpression *fieldOrExpressionToExpression( const QString &fieldOrExpression ) SIP_FACTORY;
740 
748  static QString fieldOrExpressionFromExpression( QgsExpression *expression );
749 
756  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
757 
763  static double rescaleUom( double size, QgsUnitTypes::RenderUnit unit, const QVariantMap &props );
764 
770  static QPointF rescaleUom( QPointF point, QgsUnitTypes::RenderUnit unit, const QVariantMap &props ) SIP_PYNAME( rescalePointUom );
771 
777  static QVector<qreal> rescaleUom( const QVector<qreal> &array, QgsUnitTypes::RenderUnit unit, const QVariantMap &props ) SIP_PYNAME( rescaleArrayUom );
778 
783  static void applyScaleDependency( QDomDocument &doc, QDomElement &ruleElem, QVariantMap &props );
784 
789  static void mergeScaleDependencies( double mScaleMinDenom, double mScaleMaxDenom, QVariantMap &props );
790 
797  static void parametricSvgToSld( QDomDocument &doc, QDomElement &graphicElem,
798  const QString &path,
799  const QColor &fillColor, double size, const QColor &strokeColor, double strokeWidth );
800 
805  static QString getSvgParametricPath( const QString &basePath, const QColor &fillColor, const QColor &strokeColor, double strokeWidth );
806 
811  static QSet<const QgsSymbolLayer *> toSymbolLayerPointers( QgsFeatureRenderer *renderer, const QSet<QgsSymbolLayerId> &symbolLayerIds );
812 
823  static QgsSymbol *restrictedSizeSymbol( const QgsSymbol *s, double minSize, double maxSize, QgsRenderContext *context, double &width, double &height );
824 
829  static QgsStringMap evaluatePropertiesMap( const QMap<QString, QgsProperty> &propertiesMap, const QgsExpressionContext &context );
830 
832 #ifndef SIP_RUN
833  static QgsProperty rotateWholeSymbol( double additionalRotation, const QgsProperty &property )
834  {
835  QString exprString = property.asExpression();
836  return QgsProperty::fromExpression( QString::number( additionalRotation ) + " + (" + exprString + ')' );
837  }
838 
839  static QgsProperty scaleWholeSymbol( double scaleFactor, const QgsProperty &property )
840  {
841  QString exprString = property.asExpression();
842  return QgsProperty::fromExpression( QString::number( scaleFactor ) + "*(" + exprString + ')' );
843  }
844 
845  static QgsProperty scaleWholeSymbol( double scaleFactorX, double scaleFactorY, const QgsProperty &property )
846  {
847  QString exprString = property.asExpression();
849  ( !qgsDoubleNear( scaleFactorX, 0.0 ) ? "tostring(" + QString::number( scaleFactorX ) + "*(" + exprString + "))" : QStringLiteral( "'0'" ) ) +
850  "|| ',' || " +
851  ( !qgsDoubleNear( scaleFactorY, 0.0 ) ? "tostring(" + QString::number( scaleFactorY ) + "*(" + exprString + "))" : QStringLiteral( "'0'" ) ) );
852  }
853 #endif
855 
856 };
857 
858 class QPolygonF;
859 
861 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType ) SIP_SKIP;
862 
863 #endif
864 
865 
ScaleMethod
Scale methods.
Definition: qgis.h:182
HeadType
Possible head types.
ArrowType
Possible arrow types.
Abstract base class for color ramps.
Definition: qgscolorramp.h:32
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.
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:232
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.
VertexMarkerType
Editing vertex markers.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
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:598
QMap< QString, QString > QgsStringMap
Definition: qgis.h:1041
#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:44
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