QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgssymbol.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssymbol.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 #ifndef QGSSYMBOL_H
17 #define QGSSYMBOL_H
18 
19 #include "qgis_core.h"
20 #include "qgis.h"
21 #include "qgsrendercontext.h"
22 #include "qgspropertycollection.h"
23 
24 class QgsSymbolLayer;
27 class QgsLineSymbolLayer;
28 
29 typedef QList<QgsSymbolLayer *> QgsSymbolLayerList;
30 
37 class CORE_EXPORT QgsSymbol
38 {
39 
40 #ifdef SIP_RUN
42  switch ( sipCpp->type() )
43  {
44  case Qgis::SymbolType::Marker: sipType = sipType_QgsMarkerSymbol; break;
45  case Qgis::SymbolType::Line: sipType = sipType_QgsLineSymbol; break;
46  case Qgis::SymbolType::Fill: sipType = sipType_QgsFillSymbol; break;
47  default: sipType = 0; break;
48  }
49  SIP_END
50 #endif
51 
52  friend class QgsFeatureRenderer;
53 
54  public:
55 
61  static QString symbolTypeToString( Qgis::SymbolType type );
62 
68  static Qgis::SymbolType symbolTypeForGeometryType( QgsWkbTypes::GeometryType type );
69 
74  enum Property
75  {
77  };
78 
83  static const QgsPropertiesDefinition &propertyDefinitions();
84 
85  virtual ~QgsSymbol();
86 
92  static QgsSymbol *defaultSymbol( QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
93 
97  Qgis::SymbolType type() const { return mType; }
98 
99  // symbol layers handling
100 
108  QgsSymbolLayerList symbolLayers() { return mLayers; }
109 
110 #ifndef SIP_RUN
111 
118  QgsSymbolLayer *symbolLayer( int layer );
119 
126  const QgsSymbolLayer *symbolLayer( int layer ) const;
127 #else
128 
138  SIP_PYOBJECT symbolLayer( int layer ) SIP_TYPEHINT( QgsSymbolLayer );
139  % MethodCode
140  const int count = sipCpp->symbolLayerCount();
141  if ( a0 < 0 || a0 >= count )
142  {
143  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
144  sipIsErr = 1;
145  }
146  else
147  {
148  sipRes = sipConvertFromType( sipCpp->symbolLayer( a0 ), sipType_QgsSymbolLayer, NULL );
149  }
150  % End
151 #endif
152 
160  int symbolLayerCount() const { return mLayers.count(); }
161 
162 #ifdef SIP_RUN
163 
167  int __len__() const;
168  % MethodCode
169  sipRes = sipCpp->symbolLayerCount();
170  % End
171 
173  int __bool__() const;
174  % MethodCode
175  sipRes = true;
176  % End
177 
188  SIP_PYOBJECT __getitem__( int index ) SIP_TYPEHINT( QgsSymbolLayer );
189  % MethodCode
190  const int count = sipCpp->symbolLayerCount();
191  if ( a0 < -count || a0 >= count )
192  {
193  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
194  sipIsErr = 1;
195  }
196  else if ( a0 >= 0 )
197  {
198  return sipConvertFromType( sipCpp->symbolLayer( a0 ), sipType_QgsSymbolLayer, NULL );
199  }
200  else
201  {
202  return sipConvertFromType( sipCpp->symbolLayer( count + a0 ), sipType_QgsSymbolLayer, NULL );
203  }
204  % End
205 
216  void __delitem__( int index );
217  % MethodCode
218  const int count = sipCpp->symbolLayerCount();
219  if ( a0 >= 0 && a0 < count )
220  sipCpp->deleteSymbolLayer( a0 );
221  else if ( a0 < 0 && a0 >= -count )
222  sipCpp->deleteSymbolLayer( count + a0 );
223  else
224  {
225  PyErr_SetString( PyExc_IndexError, QByteArray::number( a0 ) );
226  sipIsErr = 1;
227  }
228  % End
229 #endif
230 
238  bool insertSymbolLayer( int index, QgsSymbolLayer *layer SIP_TRANSFER );
239 
246  bool appendSymbolLayer( QgsSymbolLayer *layer SIP_TRANSFER );
247 
251  bool deleteSymbolLayer( int index );
252 
259  QgsSymbolLayer *takeSymbolLayer( int index ) SIP_TRANSFERBACK;
260 
268  bool changeSymbolLayer( int index, QgsSymbolLayer *layer SIP_TRANSFER );
269 
279  void startRender( QgsRenderContext &context, const QgsFields &fields = QgsFields() );
280 
287  void stopRender( QgsRenderContext &context );
288 
299  void setColor( const QColor &color );
300 
309  QColor color() const;
310 
328  void drawPreviewIcon( QPainter *painter, QSize size, QgsRenderContext *customContext = nullptr, bool selected = false, const QgsExpressionContext *expressionContext = nullptr,
329  const QgsLegendPatchShape *patchShape = nullptr );
330 
340  void exportImage( const QString &path, const QString &format, QSize size );
341 
351  QImage asImage( QSize size, QgsRenderContext *customContext = nullptr );
352 
363  QImage bigSymbolPreviewImage( QgsExpressionContext *expressionContext = nullptr, Qgis::SymbolPreviewFlags flags = Qgis::SymbolPreviewFlag::FlagIncludeCrosshairsForMarkerSymbols ) SIP_PYNAME( bigSymbolPreviewImageV2 );
364 
368  Q_DECL_DEPRECATED QImage bigSymbolPreviewImage( QgsExpressionContext *expressionContext = nullptr, int flags = static_cast< int >( Qgis::SymbolPreviewFlag::FlagIncludeCrosshairsForMarkerSymbols ) ) SIP_DEPRECATED;
369 
373  QString dump() const;
374 
380  virtual QgsSymbol *clone() const = 0 SIP_FACTORY;
381 
385  void toSld( QDomDocument &doc, QDomElement &element, QVariantMap props ) const;
386 
395  QgsUnitTypes::RenderUnit outputUnit() const;
396 
402  bool usesMapUnits() const;
403 
412  void setOutputUnit( QgsUnitTypes::RenderUnit unit );
413 
423  QgsMapUnitScale mapUnitScale() const;
424 
433  void setMapUnitScale( const QgsMapUnitScale &scale );
434 
440  qreal opacity() const { return mOpacity; }
441 
447  void setOpacity( qreal opacity ) { mOpacity = opacity; }
448 
453  void setRenderHints( Qgis::SymbolRenderHints hints ) { mRenderHints = hints; }
454 
459  Qgis::SymbolRenderHints renderHints() const { return mRenderHints; }
460 
467  void setFlags( Qgis::SymbolFlags flags ) { mSymbolFlags = flags; }
468 
475  Qgis::SymbolFlags flags() const { return mSymbolFlags; }
476 
486  void setClipFeaturesToExtent( bool clipFeaturesToExtent ) { mClipFeaturesToExtent = clipFeaturesToExtent; }
487 
497  bool clipFeaturesToExtent() const { return mClipFeaturesToExtent; }
498 
508  void setForceRHR( bool force ) { mForceRHR = force; }
509 
519  bool forceRHR() const { return mForceRHR; }
520 
526  QSet<QString> usedAttributes( const QgsRenderContext &context ) const;
527 
535  void setDataDefinedProperty( Property key, const QgsProperty &property );
536 
543  QgsPropertyCollection &dataDefinedProperties() { return mDataDefinedProperties; }
544 
550  const QgsPropertyCollection &dataDefinedProperties() const { return mDataDefinedProperties; } SIP_SKIP
551 
558  void setDataDefinedProperties( const QgsPropertyCollection &collection ) { mDataDefinedProperties = collection; }
559 
564  bool hasDataDefinedProperties() const;
565 
575  bool canCauseArtifactsBetweenAdjacentTiles() const;
576 
581  Q_DECL_DEPRECATED void setLayer( const QgsVectorLayer *layer ) SIP_DEPRECATED;
582 
586  Q_DECL_DEPRECATED const QgsVectorLayer *layer() const SIP_DEPRECATED;
587 
592  void renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false, int currentVertexMarkerType = 0, double currentVertexMarkerSize = 0.0 ) SIP_THROW( QgsCsException );
593 
599  QgsSymbolRenderContext *symbolRenderContext();
600 
612  void startFeatureRender( const QgsFeature &feature, QgsRenderContext &context, int layer = -1 );
613 
625  void stopFeatureRender( const QgsFeature &feature, QgsRenderContext &context, int layer = -1 );
626 
627  protected:
628 
634  QgsSymbol( Qgis::SymbolType type, const QgsSymbolLayerList &layers SIP_TRANSFER ); // can't be instantiated
635 
639  static inline QPointF _getPoint( QgsRenderContext &context, const QgsPoint &point )
640  {
641  QPointF pt;
642  if ( context.coordinateTransform().isValid() )
643  {
644  double x = point.x();
645  double y = point.y();
646  double z = 0.0;
647  context.coordinateTransform().transformInPlace( x, y, z );
648  pt = QPointF( x, y );
649 
650  }
651  else
652  pt = point.toQPointF();
653 
654  context.mapToPixel().transformInPlace( pt.rx(), pt.ry() );
655  return pt;
656  }
657 
661  static QPolygonF _getLineString( QgsRenderContext &context, const QgsCurve &curve, bool clipToExtent = true );
662 
669  static QPolygonF _getPolygonRing( QgsRenderContext &context, const QgsCurve &curve, bool clipToExtent, bool isExteriorRing = false, bool correctRingOrientation = false );
670 
678  static void _getPolygon( QPolygonF &pts, QVector<QPolygonF> &holes, QgsRenderContext &context, const QgsPolygon &polygon, bool clipToExtent = true, bool correctRingOrientation = false );
679 
684  QgsSymbolLayerList cloneLayers() const SIP_FACTORY;
685 
695  void renderUsingLayer( QgsSymbolLayer *layer, QgsSymbolRenderContext &context );
696 
701  void renderVertexMarker( QPointF pt, QgsRenderContext &context, int currentVertexMarkerType, double currentVertexMarkerSize );
702 
703  Qgis::SymbolType mType;
705 
707  qreal mOpacity = 1.0;
708 
709  Qgis::SymbolRenderHints mRenderHints;
710 
716  Qgis::SymbolFlags mSymbolFlags = Qgis::SymbolFlags();
717 
718  bool mClipFeaturesToExtent = true;
719  bool mForceRHR = false;
720 
721  Q_DECL_DEPRECATED const QgsVectorLayer *mLayer = nullptr; //current vectorlayer
722 
723  private:
724 #ifdef SIP_RUN
725  QgsSymbol( const QgsSymbol & );
726 #endif
727 
728  static void initPropertyDefinitions();
729 
731  static QgsPropertiesDefinition sPropertyDefinitions;
732 
737  bool mStarted = false;
738 
740  std::unique_ptr< QgsSymbolRenderContext > mSymbolRenderContext;
741 
742  QgsPropertyCollection mDataDefinedProperties;
743 
744  Q_DISABLE_COPY( QgsSymbol )
745 
746 };
747 
748 #endif
749 
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:62
@ FlagIncludeCrosshairsForMarkerSymbols
Include a crosshairs reference image in the background of marker symbol previews.
SymbolType
Symbol types.
Definition: qgis.h:168
@ Marker
Marker symbol.
@ Line
Line symbol.
@ Fill
Fill symbol.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
Abstract base class for curved geometry type.
Definition: qgscurve.h:36
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static QPointF _getPoint(QgsRenderContext &context, const QgsPoint &point)
Creates a point in screen coordinates from a wkb string in map coordinates.
Definition: qgsrenderer.cpp:47
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
virtual void toSld(QDomDocument &doc, QDomElement &element, const QVariantMap &props=QVariantMap()) const
used from subclasses to create SLD Rule elements following SLD v1.1 specs
Definition: qgsrenderer.h:320
QString type() const
Definition: qgsrenderer.h:141
virtual QString dump() const
Returns debug information about this renderer.
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Returns a list of attributes required by this renderer.
void renderVertexMarker(QPointF pt, QgsRenderContext &context)
render editing vertex marker at specified point
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:94
virtual bool renderFeature(const QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false) SIP_THROW(QgsCsException)
Render a feature using this renderer in the given context.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Container of fields for a vector layer.
Definition: qgsfields.h:45
Represents a patch shape for use in map legends.
Struct for storing maximum and minimum scales for measurements in map units.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
Polygon geometry type.
Definition: qgspolygon.h:34
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
A store for object properties.
Definition: qgsproperty.h:232
Contains information about the context of a rendering operation.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the symbol's property collection, used for data defined overrides.
Definition: qgssymbol.h:558
void setRenderHints(Qgis::SymbolRenderHints hints)
Sets rendering hint flags for the symbol.
Definition: qgssymbol.h:453
Qgis::SymbolRenderHints renderHints() const
Returns the rendering hint flags for the symbol.
Definition: qgssymbol.h:459
Property
Data definable properties.
Definition: qgssymbol.h:75
@ PropertyOpacity
Opacity.
Definition: qgssymbol.h:76
Qgis::SymbolFlags flags() const
Returns flags for the symbol.
Definition: qgssymbol.h:475
qreal opacity() const
Returns the opacity for the symbol.
Definition: qgssymbol.h:440
bool clipFeaturesToExtent() const
Returns whether features drawn by the symbol will be clipped to the render context's extent.
Definition: qgssymbol.h:497
void setFlags(Qgis::SymbolFlags flags)
Sets flags for the symbol.
Definition: qgssymbol.h:467
const QgsPropertyCollection & dataDefinedProperties() const
Returns a reference to the symbol's property collection, used for data defined overrides.
Definition: qgssymbol.h:550
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the symbol's property collection, used for data defined overrides.
Definition: qgssymbol.h:543
QgsSymbolLayerList symbolLayers()
Returns the list of symbol layers contained in the symbol.
Definition: qgssymbol.h:108
void setOpacity(qreal opacity)
Sets the opacity for the symbol.
Definition: qgssymbol.h:447
int symbolLayerCount() const
Returns the total number of symbol layers contained in the symbol.
Definition: qgssymbol.h:160
Qgis::SymbolType type() const
Returns the symbol's type.
Definition: qgssymbol.h:97
virtual QgsSymbol * clone() const =0
Returns a deep copy of this symbol.
bool forceRHR() const
Returns true if polygon features drawn by the symbol will be reoriented to follow the standard right-...
Definition: qgssymbol.h:519
void setClipFeaturesToExtent(bool clipFeaturesToExtent)
Sets whether features drawn by the symbol should be clipped to the render context's extent.
Definition: qgssymbol.h:486
void setForceRHR(bool force)
Sets whether polygon features drawn by the symbol should be reoriented to follow the standard right-h...
Definition: qgssymbol.h:508
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:168
Represents a vector layer which manages a vector based data sets.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
#define SIP_THROW(name)
Definition: qgis_sip.h:189
#define SIP_TYPEHINT(type)
Definition: qgis_sip.h:218
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:194
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.
QList< QgsSymbolLayer * > QgsSymbolLayerList
Definition: qgssymbol.h:27