QGIS API Documentation  3.0.2-Girona (307d082)
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 
33 class QgsExpression;
34 class QgsPathResolver;
37 
38 typedef QMap<QString, QString> QgsStringMap;
39 typedef QMap<QString, QgsSymbol * > QgsSymbolMap;
40 typedef QList< QPair< QColor, QString > > QgsNamedColorList SIP_SKIP;
41 
42 class QDomDocument;
43 class QDomElement;
44 class QIcon;
45 class QPixmap;
46 class QPointF;
47 class QSize;
48 
53 class CORE_EXPORT QgsSymbolLayerUtils
54 {
55  public:
56 
57  static QString encodeColor( const QColor &color );
58  static QColor decodeColor( const QString &str );
59 
60  static QString encodeSldAlpha( int alpha );
61  static int decodeSldAlpha( const QString &str );
62 
63  static QString encodeSldFontStyle( QFont::Style style );
64  static QFont::Style decodeSldFontStyle( const QString &str );
65 
66  static QString encodeSldFontWeight( int weight );
67  static int decodeSldFontWeight( const QString &str );
68 
69  static QString encodePenStyle( Qt::PenStyle style );
70  static Qt::PenStyle decodePenStyle( const QString &str );
71 
72  static QString encodePenJoinStyle( Qt::PenJoinStyle style );
73  static Qt::PenJoinStyle decodePenJoinStyle( const QString &str );
74 
75  static QString encodePenCapStyle( Qt::PenCapStyle style );
76  static Qt::PenCapStyle decodePenCapStyle( const QString &str );
77 
78  static QString encodeSldLineJoinStyle( Qt::PenJoinStyle style );
79  static Qt::PenJoinStyle decodeSldLineJoinStyle( const QString &str );
80 
81  static QString encodeSldLineCapStyle( Qt::PenCapStyle style );
82  static Qt::PenCapStyle decodeSldLineCapStyle( const QString &str );
83 
84  static QString encodeBrushStyle( Qt::BrushStyle style );
85  static Qt::BrushStyle decodeBrushStyle( const QString &str );
86 
87  static QString encodeSldBrushStyle( Qt::BrushStyle style );
88  static Qt::BrushStyle decodeSldBrushStyle( const QString &str );
89 
95  static QString encodePoint( QPointF point );
96 
102  static QPointF decodePoint( const QString &string );
103 
110  static QString encodeSize( QSizeF size );
111 
118  static QSizeF decodeSize( const QString &string );
119 
120  static QString encodeMapUnitScale( const QgsMapUnitScale &mapUnitScale );
121  static QgsMapUnitScale decodeMapUnitScale( const QString &str );
122 
123  static QString encodeRealVector( const QVector<qreal> &v );
124  static QVector<qreal> decodeRealVector( const QString &s );
125 
126  static QString encodeSldRealVector( const QVector<qreal> &v );
127  static QVector<qreal> decodeSldRealVector( const QString &s );
128 
136  static QString encodeSldUom( QgsUnitTypes::RenderUnit unit, double *scaleFactor );
137 
145  static QgsUnitTypes::RenderUnit decodeSldUom( const QString &str, double *scaleFactor );
146 
154  static double sizeInPixelsFromSldUom( const QString &uom, double size );
155 
156  static QString encodeScaleMethod( QgsSymbol::ScaleMethod scaleMethod );
157  static QgsSymbol::ScaleMethod decodeScaleMethod( const QString &str );
158 
159  static QPainter::CompositionMode decodeBlendMode( const QString &s );
160 
168  static QIcon symbolPreviewIcon( QgsSymbol *symbol, QSize size, int padding = 0 );
169 
179  static QPixmap symbolPreviewPixmap( QgsSymbol *symbol, QSize size, int padding = 0, QgsRenderContext *customContext = nullptr );
180 
191  static QPicture symbolLayerPreviewPicture( QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit units, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
192 
202  static QIcon symbolLayerPreviewIcon( QgsSymbolLayer *layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale &scale = QgsMapUnitScale() );
203 
211  static QIcon colorRampPreviewIcon( QgsColorRamp *ramp, QSize size, int padding = 0 );
212 
220  static QPixmap colorRampPreviewPixmap( QgsColorRamp *ramp, QSize size, int padding = 0 );
221 
222  static void drawStippledBackground( QPainter *painter, QRect rect );
223 
225  static double estimateMaxSymbolBleed( QgsSymbol *symbol, const QgsRenderContext &context );
226 
233  static QgsSymbol *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
234 
243  template <class SymbolType> static SymbolType *loadSymbol( const QDomElement &element, const QgsReadWriteContext &context ) SIP_SKIP
244  {
245  QgsSymbol *tmpSymbol = QgsSymbolLayerUtils::loadSymbol( element, context );
246  SymbolType *symbolCastToType = dynamic_cast<SymbolType *>( tmpSymbol );
247 
248  if ( symbolCastToType )
249  {
250  return symbolCastToType;
251  }
252  else
253  {
254  //could not cast
255  delete tmpSymbol;
256  return nullptr;
257  }
258  }
259 
261  static QgsSymbolLayer *loadSymbolLayer( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
263  static QDomElement saveSymbol( const QString &symbolName, QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context );
264 
270  static QString symbolProperties( QgsSymbol *symbol );
271 
272  static bool createSymbolLayerListFromSld( QDomElement &element, QgsWkbTypes::GeometryType geomType, QgsSymbolLayerList &layers );
273 
274  static QgsSymbolLayer *createFillLayerFromSld( QDomElement &element ) SIP_FACTORY;
275  static QgsSymbolLayer *createLineLayerFromSld( QDomElement &element ) SIP_FACTORY;
276  static QgsSymbolLayer *createMarkerLayerFromSld( QDomElement &element ) SIP_FACTORY;
277 
278  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QgsSymbolLayerList &layerList );
279  static bool hasExternalGraphic( QDomElement &element );
280  static bool hasWellKnownMark( QDomElement &element );
281 
282  static bool needFontMarker( QDomElement &element );
283  static bool needSvgMarker( QDomElement &element );
284  static bool needEllipseMarker( QDomElement &element );
285  static bool needMarkerLine( QDomElement &element );
286  static bool needLinePatternFill( QDomElement &element );
287  static bool needPointPatternFill( QDomElement &element );
288  static bool needSvgFill( QDomElement &element );
289 
290  static void fillToSld( QDomDocument &doc, QDomElement &element,
291  Qt::BrushStyle brushStyle, const QColor &color = QColor() );
292  static bool fillFromSld( QDomElement &element,
293  Qt::BrushStyle &brushStyle, QColor &color );
294 
296  static void lineToSld( QDomDocument &doc, QDomElement &element,
297  Qt::PenStyle penStyle, const QColor &color, double width = -1,
298  const Qt::PenJoinStyle *penJoinStyle = nullptr, const Qt::PenCapStyle *penCapStyle = nullptr,
299  const QVector<qreal> *customDashPattern = nullptr, double dashOffset = 0.0 ) SIP_SKIP;
300  static bool lineFromSld( QDomElement &element,
301  Qt::PenStyle &penStyle, QColor &color, double &width,
302  Qt::PenJoinStyle *penJoinStyle = nullptr, Qt::PenCapStyle *penCapStyle = nullptr,
303  QVector<qreal> *customDashPattern = nullptr, double *dashOffset = nullptr );
304 
305  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
306  const QString &path, const QString &mime,
307  const QColor &color, double size = -1 );
308  static bool externalGraphicFromSld( QDomElement &element,
309  QString &path, QString &mime,
310  QColor &color, double &size );
311 
312  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
313  const QString &name, const QColor &color, const QColor &strokeColor, Qt::PenStyle strokeStyle,
314  double strokeWidth = -1, double size = -1 );
315 
317  static bool wellKnownMarkerFromSld( QDomElement &element,
318  QString &name, QColor &color, QColor &strokeColor, Qt::PenStyle &strokeStyle,
319  double &strokeWidth, double &size ) SIP_PYNAME( wellKnownMarkerFromSld2 );
320 
321  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
322  const QString &path, const QString &format, int *markIndex = nullptr,
323  const QColor &color = QColor(), double size = -1 );
324  static bool externalMarkerFromSld( QDomElement &element,
325  QString &path, QString &format, int &markIndex,
326  QColor &color, double &size );
327 
328 
329  static void labelTextToSld( QDomDocument &doc, QDomElement &element, const QString &label,
330  const QFont &font, const QColor &color = QColor(), double size = -1 );
331 
333  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor &c,
334  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
335  Qt::PenCapStyle capStyle = Qt::FlatCap,
336  double offset = 0.0,
337  const QVector<qreal> *dashPattern = nullptr );
338 
342  static QString ogrFeatureStyleBrush( const QColor &fillColr );
343 
344  static void createRotationElement( QDomDocument &doc, QDomElement &element, const QString &rotationFunc );
345  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
346 
347  static void createOpacityElement( QDomDocument &doc, QDomElement &element, const QString &alphaFunc );
348  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
349 
350  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
351  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
352 
359  static void createAnchorPointElement( QDomDocument &doc, QDomElement &element, QPointF anchor );
360 
361  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, const QString &path, const QString &format );
362  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
363 
364  static void createGeometryElement( QDomDocument &doc, QDomElement &element, const QString &geomFunc );
365  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
366 
374  static bool createExpressionElement( QDomDocument &doc, QDomElement &element, const QString &function );
375  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, const QString &function );
376  static bool functionFromSldElement( QDomElement &element, QString &function );
377 
378  static QDomElement createSvgParameterElement( QDomDocument &doc, const QString &name, const QString &value );
379  static QgsStringMap getSvgParameterList( QDomElement &element );
380 
381  static QDomElement createVendorOptionElement( QDomDocument &doc, const QString &name, const QString &value );
382  static QgsStringMap getVendorOptionList( QDomElement &element );
383 
384  static QgsStringMap parseProperties( QDomElement &element );
385  static void saveProperties( QgsStringMap props, QDomDocument &doc, QDomElement &element );
386 
388  static QgsSymbolMap loadSymbols( QDomElement &element, const QgsReadWriteContext &context ) SIP_FACTORY;
390  static QDomElement saveSymbols( QgsSymbolMap &symbols, const QString &tagName, QDomDocument &doc, const QgsReadWriteContext &context );
391 
392  static void clearSymbolMap( QgsSymbolMap &symbols );
393 
401  static QMimeData *symbolToMimeData( QgsSymbol *symbol ) SIP_FACTORY;
402 
409  static QgsSymbol *symbolFromMimeData( const QMimeData *data ) SIP_FACTORY;
410 
417  static QgsColorRamp *loadColorRamp( QDomElement &element ) SIP_FACTORY;
418 
427  static QDomElement saveColorRamp( const QString &name, QgsColorRamp *ramp, QDomDocument &doc );
428 
435  static QVariant colorRampToVariant( const QString &name, QgsColorRamp *ramp );
436 
443  static QgsColorRamp *loadColorRamp( const QVariant &value ) SIP_FACTORY;
444 
451  static QString colorToName( const QColor &color );
452 
460  static QList< QColor > parseColorList( const QString &colorStr );
461 
469  static QMimeData *colorToMimeData( const QColor &color ) SIP_FACTORY;
470 
480  static QColor colorFromMimeData( const QMimeData *data, bool &hasAlpha );
481 
488  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
489 
497  static QMimeData *colorListToMimeData( const QgsNamedColorList &colorList, const bool allFormats = true ) SIP_FACTORY;
498 
507  static bool saveColorsToGpl( QFile &file, const QString &paletteName, const QgsNamedColorList &colors );
508 
517  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString &name );
518 
527  static QColor parseColor( const QString &colorStr, bool strictEval = false );
528 
538  static QColor parseColorWithAlpha( const QString &colorStr, bool &containsAlpha, bool strictEval = false );
539 
543  static void multiplyImageOpacity( QImage *image, qreal opacity );
544 
546  static void blurImageInPlace( QImage &image, QRect rect, int radius, bool alphaOnly );
547 
552  static void premultiplyColor( QColor &rgb, int alpha );
553 
555  static void sortVariantList( QList<QVariant> &list, Qt::SortOrder order );
557  static QPointF pointOnLineWithDistance( QPointF startPoint, QPointF directionPoint, double distance );
558 
560  static QStringList listSvgFiles();
561 
563  static QStringList listSvgFilesAt( const QString &directory );
564 
570  static QString svgSymbolNameToPath( QString name, const QgsPathResolver &pathResolver );
571 
573  static QString svgSymbolPathToName( QString path, const QgsPathResolver &pathResolver );
574 
576  static QPointF polygonCentroid( const QPolygonF &points );
577 
579  static QPointF polygonPointOnSurface( const QPolygonF &points );
580 
582  static bool pointInPolygon( const QPolygonF &points, QPointF point );
583 
591  static QgsExpression *fieldOrExpressionToExpression( const QString &fieldOrExpression ) SIP_FACTORY;
592 
600  static QString fieldOrExpressionFromExpression( QgsExpression *expression );
601 
608  static QList<double> prettyBreaks( double minimum, double maximum, int classes );
609 
615  static double rescaleUom( double size, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props );
616 
622  static QPointF rescaleUom( QPointF point, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props ) SIP_PYNAME( rescalePointUom );
623 
629  static QVector<qreal> rescaleUom( const QVector<qreal> &array, QgsUnitTypes::RenderUnit unit, const QgsStringMap &props ) SIP_PYNAME( rescaleArrayUom );
630 
635  static void applyScaleDependency( QDomDocument &doc, QDomElement &ruleElem, QgsStringMap &props );
636 
641  static void mergeScaleDependencies( int mScaleMinDenom, int mScaleMaxDenom, QgsStringMap &props );
642 
649  static void parametricSvgToSld( QDomDocument &doc, QDomElement &graphicElem,
650  const QString &path,
651  const QColor &fillColor, double size, const QColor &strokeColor, double strokeWidth );
652 
657  static QString getSvgParametricPath( const QString &basePath, const QColor &fillColor, const QColor &strokeColor, double strokeWidth );
658 
659 };
660 
661 class QPolygonF;
662 
664 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QgsWkbTypes::GeometryType geometryType ) SIP_SKIP;
665 
666 #endif
667 
668 
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:479
QMap< QString, QString > QgsStringMap
#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.
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.
QMap< QString, QgsSymbol *> QgsSymbolMap
Definition: qgsrenderer.h:44
QMap< QString, QgsSymbol *> QgsSymbolMap
Resolves relative paths into absolute paths and vice versa.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:100
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74