QGIS API Documentation  3.2.0-Bonn (bc43194)
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 <Qt>
24 #include <QtCore>
25 #include <QFont>
26 #include <QColor>
27 #include <QPainter>
28 #include "qgssymbol.h"
29 #include "qgis.h"
30 #include "qgsmapunitscale.h"
31 #include "qgscolorramp.h"
32 #include "qgsarrowsymbollayer.h"
33 
34 class QgsExpression;
35 class QgsPathResolver;
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 
54 class CORE_EXPORT QgsSymbolLayerUtils
55 {
56  public:
57 
58  static QString encodeColor( const QColor &color );
59  static QColor decodeColor( const QString &str );
60 
61  static QString encodeSldAlpha( int alpha );
62  static int decodeSldAlpha( const QString &str );
63 
64  static QString encodeSldFontStyle( QFont::Style style );
65  static QFont::Style decodeSldFontStyle( const QString &str );
66 
67  static QString encodeSldFontWeight( int weight );
68  static int decodeSldFontWeight( const QString &str );
69 
70  static QString encodePenStyle( Qt::PenStyle style );
71  static Qt::PenStyle decodePenStyle( const QString &str );
72 
73  static QString encodePenJoinStyle( Qt::PenJoinStyle style );
74  static Qt::PenJoinStyle decodePenJoinStyle( const QString &str );
75 
76  static QString encodePenCapStyle( Qt::PenCapStyle style );
77  static Qt::PenCapStyle decodePenCapStyle( const QString &str );
78 
79  static QString encodeSldLineJoinStyle( Qt::PenJoinStyle style );
80  static Qt::PenJoinStyle decodeSldLineJoinStyle( const QString &str );
81 
82  static QString encodeSldLineCapStyle( Qt::PenCapStyle style );
83  static Qt::PenCapStyle decodeSldLineCapStyle( const QString &str );
84 
85  static QString encodeBrushStyle( Qt::BrushStyle style );
86  static Qt::BrushStyle decodeBrushStyle( const QString &str );
87 
88  static QString encodeSldBrushStyle( Qt::BrushStyle style );
89  static Qt::BrushStyle decodeSldBrushStyle( const QString &str );
90 
95  static QgsArrowSymbolLayer::HeadType decodeArrowHeadType( const QVariant &value, bool *ok SIP_OUT = nullptr );
96 
101  static QgsArrowSymbolLayer::ArrowType decodeArrowType( const QVariant &value, bool *ok SIP_OUT = nullptr );
102 
108  static QString encodePoint( QPointF point );
109 
115  static QPointF decodePoint( const QString &string );
116 
123  static QString encodeSize( QSizeF size );
124 
131  static QSizeF decodeSize( const QString &string );
132 
133  static QString encodeMapUnitScale( const QgsMapUnitScale &mapUnitScale );
134  static QgsMapUnitScale decodeMapUnitScale( const QString &str );
135 
136  static QString encodeRealVector( const QVector<qreal> &v );
137  static QVector<qreal> decodeRealVector( const QString &s );
138 
139  static QString encodeSldRealVector( const QVector<qreal> &v );
140  static QVector<qreal> decodeSldRealVector( const QString &s );
141 
149  static QString encodeSldUom( QgsUnitTypes::RenderUnit unit, double *scaleFactor );
150 
158  static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor );
159 
167  static double sizeInPixelsFromSldUom( const QString &uom, double size );
168 
169  static QString encodeScaleMethod( QgsSymbol::ScaleMethod scaleMethod );
170  static QgsSymbol::ScaleMethod decodeScaleMethod( const QString &str );
171 
172  static QPainter::CompositionMode decodeBlendMode( const QString &s );
173 
181  static QIcon symbolPreviewIcon( QgsSymbol *symbol, QSize size, int padding = 0 );
182 
192  static QPixmap symbolPreviewPixmap( QgsSymbol *symbol, QSize size, int padding = 0, QgsRenderContext *customContext = nullptr );
193 
204  static QPicture symbolLayerPreviewPicture( QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit units, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
205 
215  static QIcon symbolLayerPreviewIcon( QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
216 
224  static QIcon colorRampPreviewIcon( QgsColorRamp *ramp, QSize size, int padding = 0 );
225 
233  static QPixmap colorRampPreviewPixmap( QgsColorRamp *ramp, QSize size, int padding = 0 );
234 
235  static void drawStippledBackground( QPainter *painter, QRect rect );
236 
238  static double estimateMaxSymbolBleed( QgsSymbol *symbol, const QgsRenderContext &context );
239 
246  static QgsSymbol *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
247 
256  template <class SymbolType> static SymbolType *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_SKIP
257  {
258  QgsSymbol *tmpSymbol = QgsSymbolLayerUtils::loadSymbol( element, context );
259  SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );
260 
261  if ( symbolCastToType )
262  {
263  return symbolCastToType;
264  }
265  else
266  {
267  //could not cast
268  delete tmpSymbol;
269  return nullptr;
270  }
271  }
272 
274  static QgsSymbolLayer *loadSymbolLayer( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
276  static QDomElement saveSymbol( const QString &symbolName, QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context );
277 
283  static QString symbolProperties( QgsSymbol *symbol );
284 
285  static bool createSymbolLayerListFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType, QgsSymbolLayerList &layers );
286 
287  static QgsSymbolLayer *createFillLayerFromSld( QDomElement &element ) SIP_FACTORY;
288  static QgsSymbolLayer *createLineLayerFromSld( QDomElement &element ) SIP_FACTORY;
289  static QgsSymbolLayer *createMarkerLayerFromSld( QDomElement &element ) SIP_FACTORY;
290 
291  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QgsSymbolLayerList &layerList );
292  static bool hasExternalGraphic( QDomElement &element );
293  static bool hasWellKnownMark( QDomElement &element );
294 
295  static bool needFontMarker( QDomElement &element );
296  static bool needSvgMarker( QDomElement &element );
297  static bool needEllipseMarker( QDomElement &element );
298  static bool needMarkerLine( QDomElement &element );
299  static bool needLinePatternFill( QDomElement &element );
300  static bool needPointPatternFill( QDomElement &element );
301  static bool needSvgFill( QDomElement &element );
302 
303  static void fillToSld( QDomDocument &doc, QDomElement &element,
304  Qt::BrushStyle brushStyle, const QColor &color = QColor() );
305  static bool fillFromSld( QDomElement &element,
306  Qt::BrushStyle &brushStyle, QColor &color );
307 
309  static void lineToSld( QDomDocument &doc, QDomElement &element,
310  Qt::PenStyle penStyle, const QColor &color, double width = -1,
311  const Qt::PenJoinStyle *penJoinStyle = nullptr, const Qt::PenCapStyle *penCapStyle = nullptr,
312  const QVector<qreal> *customDashPattern = nullptr, double dashOffset = 0.0 ) SIP_SKIP;
313  static bool lineFromSld( QDomElement &element,
314  Qt::PenStyle &penStyle, QColor &color, double &width,
315  Qt::PenJoinStyle *penJoinStyle = nullptr, Qt::PenCapStyle *penCapStyle = nullptr,
316  QVector<qreal> *customDashPattern = nullptr, double *dashOffset = nullptr );
317 
318  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
319  const QString &path, const QString &mime,
320  const QColor &color, double size = -1 );
321  static bool externalGraphicFromSld( QDomElement &element,
322  QString &path, QString &mime,
323  QColor &color, double &size );
324 
325  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
326  const QString &name, const QColor &color, const QColor &strokeColor, Qt::PenStyle strokeStyle,
327  double strokeWidth = -1, double size = -1 );
328 
330  static bool wellKnownMarkerFromSld( QDomElement &element,
331  QString &name, QColor &color, QColor &strokeColor, Qt::PenStyle &strokeStyle,
332  double &strokeWidth, double &size ) SIP_PYNAME( wellKnownMarkerFromSld2 );
333 
334  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
335  const QString &path, const QString &format, int *markIndex = nullptr,
336  const QColor &color = QColor(), double size = -1 );
337  static bool externalMarkerFromSld( QDomElement &element,
338  QString &path, QString &format, int &markIndex,
339  QColor &color, double &size );
340 
341 
342  static void labelTextToSld( QDomDocument &doc, QDomElement &element, const QString &label,
343  const QFont &font, const QColor &color = QColor(), double size = -1 );
344 
346  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor &c,
347  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
348  Qt::PenCapStyle capStyle = Qt::FlatCap,
349  double offset = 0.0,
350  const QVector<qreal> *dashPattern = nullptr );
351 
355  static QString ogrFeatureStyleBrush( const QColor &fillColr );
356 
357  static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString &rotationFunc );
358  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
359 
360  static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString &alphaFunc );
361  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
362 
363  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
364  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
365 
372  static void createAnchorPointElement( QDomDocument &doc, QDomElement &element, QPointF anchor );
373 
374  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString &path, const QString &format );
375  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
376 
377  static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString &geomFunc );
378  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
379 
386  static bool createExpressionElement( QDomDocument &doc, QDomElement &element, const QString &function );
387  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString &function );
388  static bool functionFromSldElement( QDomElement &element, QString &function );
389 
390  static QDomElement createSvgParameterElement( QDomDocument &doc, const QString &name, const QString &value );
391  static QgsStringMap getSvgParameterList( QDomElement &element );
392 
393  static QDomElement createVendorOptionElement( QDomDocument &doc, const QString &name, const QString &value );
394  static QgsStringMap getVendorOptionList( QDomElement &element );
395 
396  static QgsStringMap parseProperties( QDomElement &element );
397  static void saveProperties( QgsStringMap props, QDomDocument &doc, QDomElement &element );
398 
400  static QgsSymbolMap loadSymbols( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
402  static QDomElement saveSymbols( QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context );
403 
404  static void clearSymbolMap( QgsSymbolMap &symbols );
405 
413  static QMimeData *symbolToMimeData( QgsSymbol *symbol ) SIP_FACTORY;
414 
421  static QgsSymbol *symbolFromMimeData( const QMimeData *data ) SIP_FACTORY;
422 
429  static QgsColorRamp *loadColorRamp( QDomElement &element ) SIP_FACTORY;
430 
439  static QDomElement saveColorRamp( const QString &name, QgsColorRamp *ramp, QDomDocument &doc );
440 
447  static QVariant colorRampToVariant( const QString &name, QgsColorRamp *ramp );
448 
455  static QgsColorRamp *loadColorRamp( const QVariant &value ) SIP_FACTORY;
456 
463  static QString colorToName( const QColor &color );
464 
472  static QList< QColor > parseColorList( const QString &colorStr );
473 
481  static QMimeData *colorToMimeData( const QColor &color ) SIP_FACTORY;
482 
492  static QColor colorFromMimeData( const QMimeData *data, bool &hasAlpha );
493 
500  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
501 
509  static QMimeData *colorListToMimeData( const QgsNamedColorList &colorList, bool allFormats = true ) SIP_FACTORY;
510 
519  static bool saveColorsToGpl( QFile &file, const QString &paletteName, const QgsNamedColorList &colors );
520 
529  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString &name );
530 
539  static QColor parseColor( const QString &colorStr, bool strictEval = false );
540 
550  static QColor parseColorWithAlpha( const QString &colorStr, bool &containsAlpha, bool strictEval = false );
551 
555  static void multiplyImageOpacity( QImage *image, qreal opacity );
556 
558  static void blurImageInPlace( QImage &image, QRect rect, int radius, bool alphaOnly );
559 
564  static void premultiplyColor( QColor &rgb, int alpha );
565 
567  static void sortVariantList( QList<QVariant> &list, Qt::SortOrder order );
569  static QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance );
570 
572  static QStringList listSvgFiles();
573 
575  static QStringList listSvgFilesAt( const QString &directory );
576 
583  static QString svgSymbolNameToPath( const QString &name, const QgsPathResolver &pathResolver );
584 
589  static QString svgSymbolPathToName( const QString &path, const QgsPathResolver &pathResolver );
590 
592  static QPointF polygonCentroid( const QPolygonF &points );
593 
595  static QPointF polygonPointOnSurface( const QPolygonF &points );
596 
598  static bool pointInPolygon( const QPolygonF &points, QPointF point );
599 
607  static QgsExpression *fieldOrExpressionToExpression( const QString &fieldOrExpression ) SIP_FACTORY;
608 
616  static QString fieldOrExpressionFromExpression( QgsExpression *expression );
617 
624  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
625 
631  static double rescaleUom( double size, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props );
632 
638  static QPointF rescaleUom( QPointF point, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props ) SIP_PYNAME( rescalePointUom );
639 
645  static QVector<qreal> rescaleUom( const QVector<qreal> &array, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props ) SIP_PYNAME( rescaleArrayUom );
646 
651  static void applyScaleDependency( QDomDocument &doc, QDomElement &ruleElem, QgsStringMap &props );
652 
657  static void mergeScaleDependencies( int mScaleMinDenom, int mScaleMaxDenom, QgsStringMap &props );
658 
665  static void parametricSvgToSld( QDomDocument &doc, QDomElement &graphicElem,
666  const QString &path,
667  const QColor &fillColor, double size, const QColor &strokeColor, double strokeWidth );
668 
673  static QString getSvgParametricPath( const QString &basePath, const QColor &fillColor, const QColor &strokeColor, double strokeWidth );
674 
675 };
676 
677 class QPolygonF;
678 
680 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType ) SIP_SKIP;
681 
682 #endif
683 
684 
Class for parsing and evaluation of expressions (formerly called "search strings").
The class is used as a container of context for various read/write operations on other objects...
Abstract base class for color ramps.
Definition: qgscolorramp.h:31
QList< QPolygonF > offsetLine(QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType)
calculate geometry shifted by a specified distance
static QgsSymbol * loadSymbol(const QDomElement &element, const QgsReadWriteContext &context)
Attempts to load a symbol from a DOM element.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:501
QMap< QString, QString > QgsStringMap
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 SIP_SKIP
Definition: qgis_sip.h:119
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
ArrowType
Possible arrow types.
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition: qgssymbol.h:53
#define SIP_FACTORY
Definition: qgis_sip.h:69
ScaleMethod
Scale method.
Definition: qgssymbol.h:94
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.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:137
Contains information about the context of a rendering operation.
Struct for storing maximum and minimum scales for measurements in map units.
#define SIP_OUT
Definition: qgis_sip.h:51
QMap< QString, QgsSymbol *> QgsSymbolMap
Definition: qgsrenderer.h:44
QMap< QString, QgsSymbol *> QgsSymbolMap
Resolves relative paths into absolute paths and vice versa.
HeadType
Possible head types.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:100
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74