QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgssymbollayerv2utils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssymbollayerv2utils.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 QGSSYMBOLLAYERV2UTILS_H
18 #define QGSSYMBOLLAYERV2UTILS_H
19 
20 #include <QMap>
21 #include <Qt>
22 #include <QtCore>
23 #include <QFont>
24 #include <QColor>
25 #include <QPainter>
26 #include "qgssymbolv2.h"
27 #include "qgis.h"
28 #include "qgsmapunitscale.h"
29 
30 class QgsExpression;
31 class QgsSymbolLayerV2;
33 
34 typedef QMap<QString, QString> QgsStringMap;
35 typedef QMap<QString, QgsSymbolV2* > QgsSymbolV2Map;
36 typedef QList< QPair< QColor, QString > > QgsNamedColorList;
37 
38 class QDomDocument;
39 class QDomElement;
40 class QIcon;
41 class QPixmap;
42 class QPointF;
43 class QSize;
44 
45 class CORE_EXPORT QgsSymbolLayerV2Utils
46 {
47  public:
48 
49  static QString encodeColor( QColor color );
50  static QColor decodeColor( QString str );
51 
52  static QString encodeSldAlpha( int alpha );
53  static int decodeSldAlpha( QString str );
54 
55  static QString encodeSldFontStyle( QFont::Style style );
56  static QFont::Style decodeSldFontStyle( QString str );
57 
58  static QString encodeSldFontWeight( int weight );
59  static int decodeSldFontWeight( QString str );
60 
61  static QString encodePenStyle( Qt::PenStyle style );
62  static Qt::PenStyle decodePenStyle( QString str );
63 
64  static QString encodePenJoinStyle( Qt::PenJoinStyle style );
65  static Qt::PenJoinStyle decodePenJoinStyle( QString str );
66 
67  static QString encodePenCapStyle( Qt::PenCapStyle style );
68  static Qt::PenCapStyle decodePenCapStyle( QString str );
69 
70  static QString encodeSldLineJoinStyle( Qt::PenJoinStyle style );
71  static Qt::PenJoinStyle decodeSldLineJoinStyle( QString str );
72 
73  static QString encodeSldLineCapStyle( Qt::PenCapStyle style );
74  static Qt::PenCapStyle decodeSldLineCapStyle( QString str );
75 
76  static QString encodeBrushStyle( Qt::BrushStyle style );
77  static Qt::BrushStyle decodeBrushStyle( QString str );
78 
79  static QString encodeSldBrushStyle( Qt::BrushStyle style );
80  static Qt::BrushStyle decodeSldBrushStyle( QString str );
81 
82  static QString encodePoint( QPointF point );
83  static QPointF decodePoint( QString str );
84 
85  static QString encodeMapUnitScale( const QgsMapUnitScale& mapUnitScale );
86  static QgsMapUnitScale decodeMapUnitScale( const QString& str );
87 
88  static QString encodeRealVector( const QVector<qreal>& v );
89  static QVector<qreal> decodeRealVector( const QString& s );
90 
91  static QString encodeSldRealVector( const QVector<qreal>& v );
92  static QVector<qreal> decodeSldRealVector( const QString& s );
93 
94  static QString encodeOutputUnit( QgsSymbolV2::OutputUnit unit );
95  static QgsSymbolV2::OutputUnit decodeOutputUnit( QString str );
96 
97  static QString encodeSldUom( QgsSymbolV2::OutputUnit unit, double *scaleFactor );
98  static QgsSymbolV2::OutputUnit decodeSldUom( QString str, double *scaleFactor );
99 
100  static QString encodeScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
101  static QgsSymbolV2::ScaleMethod decodeScaleMethod( QString str );
102 
103  static QPainter::CompositionMode decodeBlendMode( const QString& s );
104 
105  static QIcon symbolPreviewIcon( QgsSymbolV2* symbol, QSize size );
106  static QIcon symbolLayerPreviewIcon( QgsSymbolLayerV2* layer, QgsSymbolV2::OutputUnit u, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );
107  static QIcon colorRampPreviewIcon( QgsVectorColorRampV2* ramp, QSize size );
108 
109  static void drawStippledBackground( QPainter* painter, QRect rect );
110 
112  static QPixmap symbolPreviewPixmap( QgsSymbolV2* symbol, QSize size, QgsRenderContext* customContext = 0 );
113  static QPixmap colorRampPreviewPixmap( QgsVectorColorRampV2* ramp, QSize size );
114 
116  static double estimateMaxSymbolBleed( QgsSymbolV2* symbol );
117 
122  static QgsSymbolV2* loadSymbol( const QDomElement& element );
123 
130  template <class SymbolType> static SymbolType* loadSymbol( const QDomElement& element )
131  {
132  QgsSymbolV2* tmpSymbol = QgsSymbolLayerV2Utils::loadSymbol( element );
133  SymbolType* symbolCastToType = dynamic_cast<SymbolType*>( tmpSymbol );
134 
135  if ( symbolCastToType )
136  {
137  return symbolCastToType;
138  }
139  else
140  {
141  //could not cast
142  delete tmpSymbol;
143  return NULL;
144  }
145  }
146 
147  static QgsSymbolLayerV2* loadSymbolLayer( QDomElement& element );
148  static QDomElement saveSymbol( QString symbolName, QgsSymbolV2* symbol, QDomDocument& doc );
149 
150  static bool createSymbolLayerV2ListFromSld( QDomElement& element, QGis::GeometryType geomType, QgsSymbolLayerV2List &layers );
151 
152  static QgsSymbolLayerV2* createFillLayerFromSld( QDomElement &element );
153  static QgsSymbolLayerV2* createLineLayerFromSld( QDomElement &element );
154  static QgsSymbolLayerV2* createMarkerLayerFromSld( QDomElement &element );
155 
156  static bool convertPolygonSymbolizerToPointMarker( QDomElement &element, QgsSymbolLayerV2List &layerList );
157  static bool hasExternalGraphic( QDomElement &element );
158  static bool hasWellKnownMark( QDomElement &element );
159 
160  static bool needFontMarker( QDomElement &element );
161  static bool needSvgMarker( QDomElement &element );
162  static bool needEllipseMarker( QDomElement &element );
163  static bool needMarkerLine( QDomElement &element );
164  static bool needLinePatternFill( QDomElement &element );
165  static bool needPointPatternFill( QDomElement &element );
166  static bool needSvgFill( QDomElement &element );
167 
168  static void fillToSld( QDomDocument &doc, QDomElement &element,
169  Qt::BrushStyle brushStyle, QColor color = QColor() );
170  static bool fillFromSld( QDomElement &element,
171  Qt::BrushStyle &brushStyle, QColor &color );
172 
174  static void lineToSld( QDomDocument &doc, QDomElement &element,
175  Qt::PenStyle penStyle, QColor color, double width = -1,
176  const Qt::PenJoinStyle *penJoinStyle = 0, const Qt::PenCapStyle *penCapStyle = 0,
177  const QVector<qreal> *customDashPattern = 0, double dashOffset = 0.0 );
178  static bool lineFromSld( QDomElement &element,
179  Qt::PenStyle &penStyle, QColor &color, double &width,
180  Qt::PenJoinStyle *penJoinStyle = 0, Qt::PenCapStyle *penCapStyle = 0,
181  QVector<qreal> *customDashPattern = 0, double *dashOffset = 0 );
182 
183  static void externalGraphicToSld( QDomDocument &doc, QDomElement &element,
184  QString path, QString mime,
185  QColor color, double size = -1 );
186  static bool externalGraphicFromSld( QDomElement &element,
187  QString &path, QString &mime,
188  QColor &color, double &size );
189 
191  Q_DECL_DEPRECATED static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
192  QString name, QColor color, QColor borderColor = QColor(),
193  double borderWidth = -1, double size = -1 );
194  static void wellKnownMarkerToSld( QDomDocument &doc, QDomElement &element,
195  QString name, QColor color, QColor borderColor, Qt::PenStyle borderStyle,
196  double borderWidth = -1, double size = -1 );
198  Q_DECL_DEPRECATED static bool wellKnownMarkerFromSld( QDomElement &element,
199  QString &name, QColor &color, QColor &borderColor,
200  double &borderWidth, double &size );
201  static bool wellKnownMarkerFromSld( QDomElement &element,
202  QString &name, QColor &color, QColor &borderColor, Qt::PenStyle &borderStyle,
203  double &borderWidth, double &size );
204 
205  static void externalMarkerToSld( QDomDocument &doc, QDomElement &element,
206  QString path, QString format, int *markIndex = 0,
207  QColor color = QColor(), double size = -1 );
208  static bool externalMarkerFromSld( QDomElement &element,
209  QString &path, QString &format, int &markIndex,
210  QColor &color, double &size );
211 
212 
213  static void labelTextToSld( QDomDocument &doc, QDomElement &element, QString label,
214  QFont font, QColor color = QColor(), double size = -1 );
215 
217  static QString ogrFeatureStylePen( double width, double mmScaleFactor, double mapUnitsScaleFactor, const QColor& c,
218  Qt::PenJoinStyle joinStyle = Qt::MiterJoin,
219  Qt::PenCapStyle capStyle = Qt::FlatCap,
220  double offset = 0.0,
221  const QVector<qreal>* dashPattern = 0 );
224  static QString ogrFeatureStyleBrush( const QColor& fillColr );
225 
226  static void createRotationElement( QDomDocument &doc, QDomElement &element, QString rotationFunc );
227  static bool rotationFromSldElement( QDomElement &element, QString &rotationFunc );
228 
229  static void createOpacityElement( QDomDocument &doc, QDomElement &element, QString alphaFunc );
230  static bool opacityFromSldElement( QDomElement &element, QString &alphaFunc );
231 
232  static void createDisplacementElement( QDomDocument &doc, QDomElement &element, QPointF offset );
233  static bool displacementFromSldElement( QDomElement &element, QPointF &offset );
234 
235  static void createOnlineResourceElement( QDomDocument &doc, QDomElement &element, QString path, QString format );
236  static bool onlineResourceFromSldElement( QDomElement &element, QString &path, QString &format );
237 
238  static void createGeometryElement( QDomDocument &doc, QDomElement &element, QString geomFunc );
239  static bool geometryFromSldElement( QDomElement &element, QString &geomFunc );
240 
241  static bool createFunctionElement( QDomDocument &doc, QDomElement &element, QString function );
242  static bool functionFromSldElement( QDomElement &element, QString &function );
243 
244  static QDomElement createSvgParameterElement( QDomDocument &doc, QString name, QString value );
245  static QgsStringMap getSvgParameterList( QDomElement &element );
246 
247  static QDomElement createVendorOptionElement( QDomDocument &doc, QString name, QString value );
248  static QgsStringMap getVendorOptionList( QDomElement &element );
249 
250  static QgsStringMap parseProperties( QDomElement& element );
251  static void saveProperties( QgsStringMap props, QDomDocument& doc, QDomElement& element );
252 
253  static QgsSymbolV2Map loadSymbols( QDomElement& element );
254  static QDomElement saveSymbols( QgsSymbolV2Map& symbols, QString tagName, QDomDocument& doc );
255 
256  static void clearSymbolMap( QgsSymbolV2Map& symbols );
257 
258  static QgsVectorColorRampV2* loadColorRamp( QDomElement& element );
259  static QDomElement saveColorRamp( QString name, QgsVectorColorRampV2* ramp, QDomDocument& doc );
260 
267  static QString colorToName( const QColor& color );
268 
276  static QList< QColor > parseColorList( const QString colorStr );
277 
285  static QMimeData *colorToMimeData( const QColor &color );
286 
296  static QColor colorFromMimeData( const QMimeData *data, bool& hasAlpha );
297 
304  static QgsNamedColorList colorListFromMimeData( const QMimeData *data );
305 
313  static QMimeData* colorListToMimeData( const QgsNamedColorList colorList, const bool allFormats = true );
314 
323  static bool saveColorsToGpl( QFile &file, const QString paletteName, QgsNamedColorList colors );
324 
333  static QgsNamedColorList importColorsFromGpl( QFile &file, bool &ok, QString& name );
334 
343  static QColor parseColor( QString colorStr, bool strictEval = false );
344 
354  static QColor parseColorWithAlpha( const QString colorStr, bool &containsAlpha, bool strictEval = false );
355 
357  static double lineWidthScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
359  static double pixelSizeScaleFactor( const QgsRenderContext& c, QgsSymbolV2::OutputUnit u, const QgsMapUnitScale& scale = QgsMapUnitScale() );
361  static QgsRenderContext createRenderContext( QPainter* p );
362 
364  static void multiplyImageOpacity( QImage* image, qreal alpha );
365 
367  static void blurImageInPlace( QImage& image, const QRect& rect, int radius, bool alphaOnly );
368 
372  static void premultiplyColor( QColor& rgb, int alpha );
373 
375  static void sortVariantList( QList<QVariant>& list, Qt::SortOrder order );
377  static QPointF pointOnLineWithDistance( const QPointF& startPoint, const QPointF& directionPoint, double distance );
378 
380  static QStringList listSvgFiles();
381 
383  static QStringList listSvgFilesAt( QString directory );
384 
389  static QString symbolNameToPath( QString name );
390 
392  static QString symbolPathToName( QString path );
393 
395  static QPointF polygonCentroid( const QPolygonF& points );
396 
398  static QPointF polygonPointOnSurface( const QPolygonF& points );
399 
401  static bool pointInPolygon( const QPolygonF &points, const QPointF &point );
402 
409  static QgsExpression* fieldOrExpressionToExpression( const QString& fieldOrExpression );
410 
417  static QString fieldOrExpressionFromExpression( QgsExpression* expression );
418 
419 };
420 
421 class QPolygonF;
422 
424 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist );
426 QList<QPolygonF> offsetLine( QPolygonF polyline, double dist, QGis::GeometryType geometryType );
427 
428 #endif
429 
430