QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
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 
98  static QgsArrowSymbolLayer::HeadType decodeArrowHeadType( const QVariant &value, bool *ok SIP_OUT = nullptr );
99 
104  static QgsArrowSymbolLayer::ArrowType decodeArrowType( const QVariant &value, bool *ok SIP_OUT = nullptr );
105 
111  static QString encodePoint( QPointF point );
112 
118  static QPointF decodePoint( const QString &string );
119 
132  static QPointF toPoint( const QVariant &value, bool *ok SIP_OUT = nullptr );
133 
140  static QString encodeSize( QSizeF size );
141 
148  static QSizeF decodeSize( const QString &string );
149 
162  static QSizeF toSize( const QVariant &value, bool *ok SIP_OUT = nullptr );
163 
164  static QString encodeMapUnitScale( const QgsMapUnitScale &mapUnitScale );
165  static QgsMapUnitScale decodeMapUnitScale( const QString &str );
166 
167  static QString encodeRealVector( const QVector<qreal> &v );
168  static QVector<qreal> decodeRealVector( const QString &s );
169 
170  static QString encodeSldRealVector( const QVector<qreal> &v );
171  static QVector<qreal> decodeSldRealVector( const QString &s );
172 
180  static QString encodeSldUom( QgsUnitTypes::RenderUnit unit, double *scaleFactor );
181 
189  static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor = nullptr );
190 
198  static double sizeInPixelsFromSldUom( const QString &uom, double size );
199 
205  static QString encodeScaleMethod( Qgis::ScaleMethod scaleMethod );
206 
212  static Qgis::ScaleMethod decodeScaleMethod( const QString &str );
213 
214  static QPainter::CompositionMode decodeBlendMode( const QString &s );
215 
224  static QIcon symbolPreviewIcon( const QgsSymbol *symbol, QSize size, int padding = 0, QgsLegendPatchShape *shape = nullptr );
225 
241  static QPixmap symbolPreviewPixmap( const QgsSymbol *symbol, QSize size, int padding = 0, QgsRenderContext *customContext = nullptr, bool selected = false,
242  const QgsExpressionContext *expressionContext = nullptr,
243  const QgsLegendPatchShape *shape = nullptr );
244 
256  static QPicture symbolLayerPreviewPicture( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit units, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale(), Qgis::SymbolType parentSymbolType = Qgis::SymbolType::Hybrid );
257 
268  static QIcon symbolLayerPreviewIcon( const QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale(), Qgis::SymbolType parentSymbolType = Qgis::SymbolType::Hybrid );
269 
277  static QIcon colorRampPreviewIcon( QgsColorRamp *ramp, QSize size, int padding = 0 );
278 
291  static QPixmap colorRampPreviewPixmap( QgsColorRamp *ramp, QSize size, int padding = 0, Qt::Orientation direction = Qt::Horizontal,
292  bool flipDirection = false, bool drawTransparentBackground = true );
293 
294  static void drawStippledBackground( QPainter *painter, QRect rect );
295 
300  static void drawVertexMarker( double x, double y, QPainter &p, Qgis::VertexMarkerType type, int markerSize );
301 
303  static double estimateMaxSymbolBleed( QgsSymbol *symbol, const QgsRenderContext &context );
304 
311  static QgsSymbol *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
312 
321  template <class SymbolType> static SymbolType *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_SKIP
322  {
323  QgsSymbol *tmpSymbol = QgsSymbolLayerUtils::loadSymbol( element, context );
324  SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );
325 
326  if ( symbolCastToType )
327  {
328  return symbolCastToType;
329  }
330  else
331  {
332  //could not cast
333  delete tmpSymbol;
334  return nullptr;
335  }
336  }
337 
339  static QgsSymbolLayer *loadSymbolLayer( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
341  static QDomElement saveSymbol( const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context );
342 
348  static QString symbolProperties( QgsSymbol *symbol );
349 
353  static bool createSymbolLayerListFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType, QList<QgsSymbolLayer *> &layers );
354 
355  static QgsSymbolLayer *createFillLayerFromSld( QDomElement &element ) SIP_FACTORY;
356  static QgsSymbolLayer *createLineLayerFromSld( QDomElement &element ) SIP_FACTORY;
357  static QgsSymbolLayer *createMarkerLayerFromSld( QDomElement &element ) SIP_FACTORY;
358 
362  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QList<QgsSymbolLayer *> &layerList );
363  static bool hasExternalGraphic( QDomElement &element );
364  static bool hasWellKnownMark( QDomElement &element );
365 
366  static bool needFontMarker( QDomElement &element );
367  static bool needSvgMarker( QDomElement &element );
368  static bool needEllipseMarker( QDomElement &element );
369  static bool needMarkerLine( QDomElement &element );
370  static bool needLinePatternFill( QDomElement &element );
371  static bool needPointPatternFill( QDomElement &element );
372  static bool needSvgFill( QDomElement &element );
373 
374  static void fillToSld( QDomDocument &doc, QDomElement &element,
375  Qt::BrushStyle brushStyle, const QColor &color = QColor() );
376  static bool fillFromSld( QDomElement &element,
377  Qt::BrushStyle &brushStyle, QColor &color );
378 
380  static void lineToSld( QDomDocument &doc, QDomElement &element,
381  Qt::PenStyle penStyle, const QColor &color, double width = -1,
382  const Qt::PenJoinStyle *penJoinStyle = nullptr, const Qt::PenCapStyle *penCapStyle = nullptr,
383  const QVector<qreal> *customDashPattern = nullptr, double dashOffset = 0.0 ) SIP_SKIP;
384  static bool lineFromSld( QDomElement &element,
385  Qt::PenStyle &penStyle, QColor &color, double &width,
386  Qt::PenJoinStyle *penJoinStyle = nullptr, Qt::PenCapStyle *penCapStyle = nullptr,
387  QVector<qreal> *customDashPattern = nullptr, double *dashOffset = nullptr );
388 
389  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
390  const QString &path, const QString &mime,
391  const QColor &color, double size = -1 );
392  static bool externalGraphicFromSld( QDomElement &element,
393  QString &path, QString &mime,
394  QColor &color, double &size );
395 
396  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
397  const QString &name, const QColor &color, const QColor &strokeColor, Qt::PenStyle strokeStyle,
398  double strokeWidth = -1, double size = -1 );
399 
401  static bool wellKnownMarkerFromSld( QDomElement &element,
402  QString &name, QColor &color, QColor &strokeColor, Qt::PenStyle &strokeStyle,
403  double &strokeWidth, double &size ) SIP_PYNAME( wellKnownMarkerFromSld2 );
404 
405  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
406  const QString &path, const QString &format, int *markIndex = nullptr,
407  const QColor &color = QColor(), double size = -1 );
408  static bool externalMarkerFromSld( QDomElement &element,
409  QString &path, QString &format, int &markIndex,
410  QColor &color, double &size );
411 
412 
413  static void labelTextToSld( QDomDocument &doc, QDomElement &element, const QString &label,
414  const QFont &font, const QColor &color = QColor(), double size = -1 );
415 
417  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor &c,
418  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
419  Qt::PenCapStyle capStyle = Qt::FlatCap,
420  double offset = 0.0,
421  const QVector<qreal> *dashPattern = nullptr );
422 
427  static QString ogrFeatureStyleBrush( const QColor &fillColr );
428 
429  static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString &rotationFunc );
430  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
431 
432  static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString &alphaFunc );
433  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
434 
435  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
436  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
437 
444  static void createAnchorPointElement( QDomDocument &doc, QDomElement &element, QPointF anchor );
445 
446  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString &path, const QString &format );
447  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
448 
449  static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString &geomFunc );
450  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
451 
458  static bool createExpressionElement( QDomDocument &doc, QDomElement &element, const QString &function );
459  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString &function );
460  static bool functionFromSldElement( QDomElement &element, QString &function );
461 
462  static QDomElement createSvgParameterElement( QDomDocument &doc, const QString &name, const QString &value );
463  static QgsStringMap getSvgParameterList( QDomElement &element );
464 
465  static QDomElement createVendorOptionElement( QDomDocument &doc, const QString &name, const QString &value );
466  static QgsStringMap getVendorOptionList( QDomElement &element );
467 
469  static QVariantMap parseProperties( const QDomElement &element );
471  static void saveProperties( QVariantMap props, QDomDocument &doc, QDomElement &element );
472 
474  static QgsSymbolMap loadSymbols( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
476  static QDomElement saveSymbols( QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context );
477 
478  static void clearSymbolMap( QgsSymbolMap &symbols );
479 
487  static QMimeData *symbolToMimeData( const QgsSymbol *symbol ) SIP_FACTORY;
488 
495  static QgsSymbol *symbolFromMimeData( const QMimeData *data ) SIP_FACTORY;
496 
503  static QgsColorRamp *loadColorRamp( QDomElement &element ) SIP_FACTORY;
504 
513  static QDomElement saveColorRamp( const QString &name, QgsColorRamp *ramp, QDomDocument &doc );
514 
521  static QVariant colorRampToVariant( const QString &name, QgsColorRamp *ramp );
522 
529  static QgsColorRamp *loadColorRamp( const QVariant &value ) SIP_FACTORY;
530 
537  static QString colorToName( const QColor &color );
538 
546  static QList< QColor > parseColorList( const QString &colorStr );
547 
555  static QMimeData *colorToMimeData( const QColor &color ) SIP_FACTORY;
556 
565  static QColor colorFromMimeData( const QMimeData *data, bool &hasAlpha SIP_OUT );
566 
573  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
574 
582  static QMimeData *colorListToMimeData( const QgsNamedColorList &colorList, bool allFormats = true ) SIP_FACTORY;
583 
592  static bool saveColorsToGpl( QFile &file, const QString &paletteName, const QgsNamedColorList &colors );
593 
602  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString &name );
603 
612  static QColor parseColor( const QString &colorStr, bool strictEval = false );
613 
623  static QColor parseColorWithAlpha( const QString &colorStr, bool &containsAlpha, bool strictEval = false );
624 
628  static void multiplyImageOpacity( QImage *image, qreal opacity );
629 
631  static void blurImageInPlace( QImage &image, QRect rect, int radius, bool alphaOnly );
632 
637  static void premultiplyColor( QColor &rgb, int alpha );
638 
651  static bool condenseFillAndOutline( QgsFillSymbolLayer *fill, QgsLineSymbolLayer *outline );
652 
654  static void sortVariantList( QList<QVariant> &list, Qt::SortOrder order );
656  static QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance );
657 
659  static QStringList listSvgFiles();
660 
662  static QStringList listSvgFilesAt( const QString &directory );
663 
670  static QString svgSymbolNameToPath( const QString &name, const QgsPathResolver &pathResolver );
671 
676  static QString svgSymbolPathToName( const QString &path, const QgsPathResolver &pathResolver );
677 
679  static QPointF polygonCentroid( const QPolygonF &points );
680 
682  static QPointF polygonPointOnSurface( const QPolygonF &points, const QVector<QPolygonF> *rings = nullptr );
683 
685  static bool pointInPolygon( const QPolygonF &points, QPointF point );
686 
692  static double polylineLength( const QPolygonF &polyline );
693 
706  static QPolygonF polylineSubstring( const QPolygonF &polyline, double startOffset, double endOffset );
707 
715  static bool isSharpCorner( QPointF p1, QPointF p2, QPointF p3 );
716 
724  static void appendPolyline( QPolygonF &target, const QPolygonF &line );
725 
733  static QgsExpression *fieldOrExpressionToExpression( const QString &fieldOrExpression ) SIP_FACTORY;
734 
742  static QString fieldOrExpressionFromExpression( QgsExpression *expression );
743 
750  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
751 
757  static double rescaleUom( double size, QgsUnitTypes::RenderUnit unit, const QVariantMap &props );
758 
764  static QPointF rescaleUom( QPointF point, QgsUnitTypes::RenderUnit unit, const QVariantMap &props ) SIP_PYNAME( rescalePointUom );
765 
771  static QVector<qreal> rescaleUom( const QVector<qreal> &array, QgsUnitTypes::RenderUnit unit, const QVariantMap &props ) SIP_PYNAME( rescaleArrayUom );
772 
777  static void applyScaleDependency( QDomDocument &doc, QDomElement &ruleElem, QVariantMap &props );
778 
783  static void mergeScaleDependencies( double mScaleMinDenom, double mScaleMaxDenom, QVariantMap &props );
784 
791  static void parametricSvgToSld( QDomDocument &doc, QDomElement &graphicElem,
792  const QString &path,
793  const QColor &fillColor, double size, const QColor &strokeColor, double strokeWidth );
794 
799  static QString getSvgParametricPath( const QString &basePath, const QColor &fillColor, const QColor &strokeColor, double strokeWidth );
800 
805  static QSet<const QgsSymbolLayer *> toSymbolLayerPointers( QgsFeatureRenderer *renderer, const QSet<QgsSymbolLayerId> &symbolLayerIds );
806 
817  static QgsSymbol *restrictedSizeSymbol( const QgsSymbol *s, double minSize, double maxSize, QgsRenderContext *context, double &width, double &height );
818 
823  static QgsStringMap evaluatePropertiesMap( const QMap<QString, QgsProperty> &propertiesMap, const QgsExpressionContext &context );
824 
826 #ifndef SIP_RUN
827  static QgsProperty rotateWholeSymbol( double additionalRotation, const QgsProperty &property )
828  {
829  const QString exprString = property.asExpression();
830  return QgsProperty::fromExpression( QString::number( additionalRotation ) + " + (" + exprString + ')' );
831  }
832 
833  static QgsProperty scaleWholeSymbol( double scaleFactor, const QgsProperty &property )
834  {
835  const QString exprString = property.asExpression();
836  return QgsProperty::fromExpression( QString::number( scaleFactor ) + "*(" + exprString + ')' );
837  }
838 
839  static QgsProperty scaleWholeSymbol( double scaleFactorX, double scaleFactorY, const QgsProperty &property )
840  {
841  const QString exprString = property.asExpression();
843  ( !qgsDoubleNear( scaleFactorX, 0.0 ) ? "tostring(" + QString::number( scaleFactorX ) + "*(" + exprString + "))" : QStringLiteral( "'0'" ) ) +
844  "|| ',' || " +
845  ( !qgsDoubleNear( scaleFactorY, 0.0 ) ? "tostring(" + QString::number( scaleFactorY ) + "*(" + exprString + "))" : QStringLiteral( "'0'" ) ) );
846  }
847 #endif
849 
850 };
851 
852 class QPolygonF;
853 
855 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType ) SIP_SKIP;
856 
857 #endif
858 
859 
ScaleMethod
Scale methods.
Definition: qgis.h:183
VertexMarkerType
Editing vertex markers, used for showing vertices during a edit operation.
Definition: qgis.h:574
SymbolType
Symbol types.
Definition: qgis.h:169
@ Hybrid
Hybrid symbol.
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.
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:1246
QMap< QString, QString > QgsStringMap
Definition: qgis.h:1703
#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