QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgssymbollayerv2.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssymbollayerv2.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 #ifndef QGSSYMBOLLAYERV2_H
16 #define QGSSYMBOLLAYERV2_H
17 
18 // MSVC compiler doesn't have defined M_PI in math.h
19 #ifndef M_PI
20 #define M_PI 3.14159265358979323846
21 #endif
22 
23 #define DEG2RAD(x) ((x)*M_PI/180)
24 #define DEFAULT_SCALE_METHOD QgsSymbolV2::ScaleArea
25 
26 #include <QColor>
27 #include <QMap>
28 #include <QPointF>
29 #include <QSet>
30 #include <QDomDocument>
31 #include <QDomElement>
32 
33 #include "qgssymbolv2.h"
34 
35 #include "qgssymbollayerv2utils.h" // QgsStringMap
36 
37 class QPainter;
38 class QSize;
39 class QPolygonF;
40 
41 class QgsDxfExport;
42 class QgsExpression;
43 class QgsRenderContext;
44 
45 class CORE_EXPORT QgsSymbolLayerV2
46 {
47  public:
48 
49  // not necessarily supported by all symbol layers...
50  virtual QColor color() const { return mColor; }
51  virtual void setColor( const QColor& color ) { mColor = color; }
54  virtual void setOutlineColor( const QColor& color ) { Q_UNUSED( color ); }
57  virtual QColor outlineColor() const { return QColor(); }
60  virtual void setFillColor( const QColor& color ) { Q_UNUSED( color ); }
63  virtual QColor fillColor() const { return QColor(); }
64 
65  virtual ~QgsSymbolLayerV2() { removeDataDefinedProperties(); }
66 
67  virtual QString layerType() const = 0;
68 
69  virtual void startRender( QgsSymbolV2RenderContext& context ) = 0;
70  virtual void stopRender( QgsSymbolV2RenderContext& context ) = 0;
71 
72  virtual QgsSymbolLayerV2* clone() const = 0;
73 
74  virtual void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const
75  { Q_UNUSED( props ); element.appendChild( doc.createComment( QString( "SymbolLayerV2 %1 not implemented yet" ).arg( layerType() ) ) ); }
76 
77  virtual QString ogrFeatureStyle( double mmScaleFactor, double mapUnitScaleFactor ) const { Q_UNUSED( mmScaleFactor ); Q_UNUSED( mapUnitScaleFactor ); return QString(); }
78 
79  virtual QgsStringMap properties() const = 0;
80 
81  virtual void drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size ) = 0;
82 
83  virtual QgsSymbolV2* subSymbol() { return NULL; }
84  // set layer's subsymbol. takes ownership of the passed symbol
85  virtual bool setSubSymbol( QgsSymbolV2* symbol ) { delete symbol; return false; }
86 
87  QgsSymbolV2::SymbolType type() const { return mType; }
88 
89  void setLocked( bool locked ) { mLocked = locked; }
90  bool isLocked() const { return mLocked; }
91 
96  virtual double estimateMaxBleed() const { return 0; }
97 
98  virtual void setOutputUnit( QgsSymbolV2::OutputUnit unit ) { Q_UNUSED( unit ); } //= 0;
99  virtual QgsSymbolV2::OutputUnit outputUnit() const { return QgsSymbolV2::Mixed; } //= 0;
100 
101  // used only with rending with symbol levels is turned on (0 = first pass, 1 = second, ...)
102  void setRenderingPass( int renderingPass ) { mRenderingPass = renderingPass; }
103  int renderingPass() const { return mRenderingPass; }
104 
105  // symbol layers normally only use additional attributes to provide data defined settings
106  virtual QSet<QString> usedAttributes() const;
107 
108  virtual const QgsExpression* dataDefinedProperty( const QString& property ) const;
109  virtual QString dataDefinedPropertyString( const QString& property ) const;
110  virtual void setDataDefinedProperty( const QString& property, const QString& expressionString );
111  virtual void removeDataDefinedProperty( const QString& property );
112  virtual void removeDataDefinedProperties();
113  bool hasDataDefinedProperties() const { return mDataDefinedProperties.size() > 0; }
114 
115  virtual bool writeDxf( QgsDxfExport& e,
116  double mmMapUnitScaleFactor,
117  const QString& layerName,
118  const QgsSymbolV2RenderContext* context,
119  const QgsFeature* f,
120  const QPointF& shift = QPointF( 0.0, 0.0 ) ) const;
121 
122  virtual double dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const;
123 
124  virtual QColor dxfColor( const QgsSymbolV2RenderContext& context ) const;
125 
126  virtual QVector<qreal> dxfCustomDashPattern( QgsSymbolV2::OutputUnit& unit ) const;
127  virtual Qt::PenStyle dxfPenStyle() const;
128 
129  protected:
130  QgsSymbolLayerV2( QgsSymbolV2::SymbolType type, bool locked = false )
131  : mType( type ), mLocked( locked ), mRenderingPass( 0 ) {}
132 
134  bool mLocked;
135  QColor mColor;
137 
138  QMap< QString, QgsExpression* > mDataDefinedProperties;
139 
140  // Configuration of selected symbology implementation
141  static const bool selectionIsOpaque = true; // Selection ignores symbol alpha
142  static const bool selectFillBorder = false; // Fill symbol layer also selects border symbology
143  static const bool selectFillStyle = false; // Fill symbol uses symbol layer style..
144 
145  virtual void prepareExpressions( const QgsVectorLayer* vl, double scale = -1.0 );
146  virtual QgsExpression* expression( const QString& property ) const;
148  void saveDataDefinedProperties( QgsStringMap& stringMap ) const;
150  void copyDataDefinedProperties( QgsSymbolLayerV2* destLayer ) const;
151 };
152 
154 
155 class CORE_EXPORT QgsMarkerSymbolLayerV2 : public QgsSymbolLayerV2
156 {
157  public:
158 
160  {
163  Right
164  };
165 
167  {
170  Bottom
171  };
172 
173  void startRender( QgsSymbolV2RenderContext& context );
174 
175  virtual void renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context ) = 0;
176 
177  void drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size );
178 
179  void setAngle( double angle ) { mAngle = angle; }
180  double angle() const { return mAngle; }
181 
182  void setSize( double size ) { mSize = size; }
183  double size() const { return mSize; }
184 
185  void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod ) { mScaleMethod = scaleMethod; }
186  QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }
187 
188  void setOffset( QPointF offset ) { mOffset = offset; }
189  QPointF offset() { return mOffset; }
190 
191  virtual void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;
192 
193  virtual void writeSldMarker( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const
194  { Q_UNUSED( props ); element.appendChild( doc.createComment( QString( "QgsMarkerSymbolLayerV2 %1 not implemented yet" ).arg( layerType() ) ) ); }
195 
196  void setOffsetUnit( QgsSymbolV2::OutputUnit unit ) { mOffsetUnit = unit; }
197  QgsSymbolV2::OutputUnit offsetUnit() const { return mOffsetUnit; }
198 
199  void setSizeUnit( QgsSymbolV2::OutputUnit unit ) { mSizeUnit = unit; }
200  QgsSymbolV2::OutputUnit sizeUnit() const { return mSizeUnit; }
201 
202  virtual void setOutputUnit( QgsSymbolV2::OutputUnit unit );
203  virtual QgsSymbolV2::OutputUnit outputUnit() const;
204 
205  void setHorizontalAnchorPoint( HorizontalAnchorPoint h ) { mHorizontalAnchorPoint = h; }
206  HorizontalAnchorPoint horizontalAnchorPoint() const { return mHorizontalAnchorPoint; }
207 
208  void setVerticalAnchorPoint( VerticalAnchorPoint v ) { mVerticalAnchorPoint = v; }
209  VerticalAnchorPoint verticalAnchorPoint() const { return mVerticalAnchorPoint; }
210 
211  protected:
212  QgsMarkerSymbolLayerV2( bool locked = false );
213 
214  //handles marker offset and anchor point shift together
215  void markerOffset( const QgsSymbolV2RenderContext& context, double& offsetX, double& offsetY ) const;
216 
218  void markerOffset( const QgsSymbolV2RenderContext& context, double width, double height,
219  QgsSymbolV2::OutputUnit widthUnit, QgsSymbolV2::OutputUnit heightUnit,
220  double& offsetX, double& offsetY ) const;
221 
222  static QPointF _rotatedOffset( const QPointF& offset, double angle );
223 
224  double mAngle;
225  double mSize;
227  QPointF mOffset;
232 
233  private:
234  static QgsMarkerSymbolLayerV2::HorizontalAnchorPoint decodeHorizontalAnchorPoint( const QString& str );
235  static QgsMarkerSymbolLayerV2::VerticalAnchorPoint decodeVerticalAnchorPoint( const QString& str );
236 
240 };
241 
242 class CORE_EXPORT QgsLineSymbolLayerV2 : public QgsSymbolLayerV2
243 {
244  public:
245  virtual void renderPolyline( const QPolygonF& points, QgsSymbolV2RenderContext& context ) = 0;
246 
248  virtual void renderPolygonOutline( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context );
249 
250  virtual void setWidth( double width ) { mWidth = width; }
251  virtual double width() const { return mWidth; }
252 
253  void setWidthUnit( QgsSymbolV2::OutputUnit unit ) { mWidthUnit = unit; }
254  QgsSymbolV2::OutputUnit widthUnit() const { return mWidthUnit; }
255 
256  void drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size );
257 
258  virtual double dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const;
259 
260  protected:
261  QgsLineSymbolLayerV2( bool locked = false );
262 
263  double mWidth;
265 };
266 
267 class CORE_EXPORT QgsFillSymbolLayerV2 : public QgsSymbolLayerV2
268 {
269  public:
270  virtual void renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context ) = 0;
271 
272  void drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size );
273 
274  void setAngle( double angle ) { mAngle = angle; }
275  double angle() const { return mAngle; }
276 
277  protected:
278  QgsFillSymbolLayerV2( bool locked = false );
280  void _renderPolygon( QPainter* p, const QPolygonF& points, const QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context );
281 
282  double mAngle;
283 };
284 
285 class QgsSymbolLayerV2Widget; // why does SIP fail, when this isn't here
286 
287 #endif