QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsrenderer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrenderer.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 QGSRENDERER_H
17 #define QGSRENDERER_H
18 
19 #include "qgis_core.h"
20 #include "qgis_sip.h"
21 #include "qgis.h"
22 #include "qgsrectangle.h"
23 #include "qgsrendercontext.h"
24 #include "qgsfields.h"
25 #include "qgsfeaturerequest.h"
26 
27 #include <QList>
28 #include <QString>
29 #include <QVariant>
30 #include <QPair>
31 #include <QPixmap>
32 #include <QDomDocument>
33 #include <QDomElement>
34 
35 class QgsFeature;
36 class QgsVectorLayer;
37 class QgsPaintEffect;
40 
41 typedef QMap<QString, QString> QgsStringMap SIP_SKIP;
42 
43 typedef QList<QgsSymbol *> QgsSymbolList;
44 typedef QMap<QString, QgsSymbol * > QgsSymbolMap SIP_SKIP;
45 
46 #include "qgslegendsymbolitem.h"
47 
48 
49 #define RENDERER_TAG_NAME "renderer-v2"
50 
52 // symbol levels
53 
58 class CORE_EXPORT QgsSymbolLevelItem
59 {
60  public:
61  QgsSymbolLevelItem( QgsSymbol *symbol, int layer )
62  : mSymbol( symbol )
63  , mLayer( layer )
64  {}
65 
69  QgsSymbol *symbol() const;
70 
74  int layer() const;
75 
76  // TODO QGIS 4.0 -> make private
77  protected:
78  QgsSymbol *mSymbol = nullptr;
79  int mLayer;
80 };
81 
82 // every level has list of items: symbol + symbol layer num
83 typedef QList< QgsSymbolLevelItem > QgsSymbolLevel;
84 
85 // this is a list of levels
86 #ifndef SIP_RUN
87 typedef QList< QgsSymbolLevel > QgsSymbolLevelOrder;
88 #else
89 typedef QList< QList< QgsSymbolLevelItem > > QgsSymbolLevelOrder;
90 #endif
91 
92 
94 // renderers
95 
100 class CORE_EXPORT QgsFeatureRenderer
101 {
102 
103 #ifdef SIP_RUN
105 
106  const QString type = sipCpp->type();
107 
108  if ( type == QLatin1String( "singleSymbol" ) )
109  sipType = sipType_QgsSingleSymbolRenderer;
110  else if ( type == QLatin1String( "categorizedSymbol" ) )
111  sipType = sipType_QgsCategorizedSymbolRenderer;
112  else if ( type == QLatin1String( "graduatedSymbol" ) )
113  sipType = sipType_QgsGraduatedSymbolRenderer;
114  else if ( type == QLatin1String( "RuleRenderer" ) )
115  sipType = sipType_QgsRuleBasedRenderer;
116  else if ( type == QLatin1String( "heatmapRenderer" ) )
117  sipType = sipType_QgsHeatmapRenderer;
118  else if ( type == QLatin1String( "invertedPolygonRenderer" ) )
119  sipType = sipType_QgsInvertedPolygonRenderer;
120  else if ( type == QLatin1String( "pointCluster" ) )
121  sipType = sipType_QgsPointClusterRenderer;
122  else if ( type == QLatin1String( "pointDisplacement" ) )
123  sipType = sipType_QgsPointDisplacementRenderer;
124  else if ( type == QLatin1String( "25dRenderer" ) )
125  sipType = sipType_Qgs25DRenderer;
126  else if ( type == QLatin1String( "nullSymbol" ) )
127  sipType = sipType_QgsNullSymbolRenderer;
128  else if ( type == QLatin1String( "embeddedSymbol" ) )
129  sipType = sipType_QgsEmbeddedSymbolRenderer;
130  else
131  sipType = 0;
132  SIP_END
133 #endif
134 
135  public:
136  // renderer takes ownership of its symbols!
137 
139  static QgsFeatureRenderer *defaultRenderer( QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
140 
141  QString type() const { return mType; }
142 
152  virtual QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const = 0;
153 
160  virtual QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
161 
166  virtual QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
167 
180  virtual void startRender( QgsRenderContext &context, const QgsFields &fields );
181 
192  virtual void stopRender( QgsRenderContext &context );
193 
205  virtual QString filter( const QgsFields &fields = QgsFields() ) { Q_UNUSED( fields ) return QString(); }
206 
213  virtual QSet<QString> usedAttributes( const QgsRenderContext &context ) const = 0;
214 
221  virtual bool usesEmbeddedSymbols() const;
222 
226  virtual bool filterNeedsGeometry() const;
227 
228  virtual ~QgsFeatureRenderer();
229 
236  virtual QgsFeatureRenderer *clone() const = 0 SIP_FACTORY;
237 
251  virtual bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) SIP_THROW( QgsCsException );
252 
254  virtual QString dump() const;
255 
261  {
262  SymbolLevels = 1,
263  MoreSymbolsPerFeature = 1 << 2,
264  Filter = 1 << 3,
265  ScaleDependent = 1 << 4
266  };
267 
268  Q_DECLARE_FLAGS( Capabilities, Capability )
269 
270 
282  virtual QgsFeatureRenderer::Capabilities capabilities() { return QgsFeatureRenderer::Capabilities(); }
283 
289  virtual QgsSymbolList symbols( QgsRenderContext &context ) const;
290 
291  bool usingSymbolLevels() const { return mUsingSymbolLevels; }
292  void setUsingSymbolLevels( bool usingSymbolLevels ) { mUsingSymbolLevels = usingSymbolLevels; }
293 
295  static QgsFeatureRenderer *load( QDomElement &symbologyElem, const QgsReadWriteContext &context ) SIP_FACTORY;
296 
298  virtual QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context );
299 
304  virtual QDomElement writeSld( QDomDocument &doc, const QString &styleName, const QVariantMap &props = QVariantMap() ) const;
305 
317  static QgsFeatureRenderer *loadSld( const QDomNode &node, QgsWkbTypes::GeometryType geomType, QString &errorMessage ) SIP_FACTORY;
318 
320  virtual void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const
321  {
322  element.appendChild( doc.createComment( QStringLiteral( "FeatureRenderer %1 not implemented yet" ).arg( type() ) ) );
323  ( void ) props; // warning avoidance
324  }
325 
330  virtual bool legendSymbolItemsCheckable() const;
331 
336  virtual bool legendSymbolItemChecked( const QString &key );
337 
342  virtual void checkLegendSymbolItem( const QString &key, bool state = true );
343 
350  virtual void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER );
351 
356  virtual QgsLegendSymbolList legendSymbolItems() const;
357 
362  virtual QString legendClassificationAttribute() const { return QString(); }
363 
365  void setVertexMarkerAppearance( int type, double size );
366 
373  virtual bool willRenderFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
374 
381  virtual QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
382 
388  virtual QgsSymbolList originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
389 
397  virtual void modifyRequestExtent( QgsRectangle &extent, QgsRenderContext &context );
398 
405  QgsPaintEffect *paintEffect() const;
406 
413  void setPaintEffect( QgsPaintEffect *effect );
414 
420  bool forceRasterRender() const { return mForceRaster; }
421 
430  void setForceRasterRender( bool forceRaster ) { mForceRaster = forceRaster; }
431 
438  QgsFeatureRequest::OrderBy orderBy() const;
439 
446  void setOrderBy( const QgsFeatureRequest::OrderBy &orderBy );
447 
454  bool orderByEnabled() const;
455 
463  void setOrderByEnabled( bool enabled );
464 
472  virtual void setEmbeddedRenderer( QgsFeatureRenderer *subRenderer SIP_TRANSFER );
473 
480  virtual const QgsFeatureRenderer *embeddedRenderer() const;
481 
491  virtual bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
492 
493  protected:
494  QgsFeatureRenderer( const QString &type );
495 
502  void renderFeatureWithSymbol( const QgsFeature &feature, QgsSymbol *symbol, QgsRenderContext &context, int layer, bool selected, bool drawVertexMarker ) SIP_THROW( QgsCsException );
503 
505  void renderVertexMarker( QPointF pt, QgsRenderContext &context );
507  void renderVertexMarkerPolyline( QPolygonF &pts, QgsRenderContext &context );
509  void renderVertexMarkerPolygon( QPolygonF &pts, QList<QPolygonF> *rings, QgsRenderContext &context );
510 
515  static QPointF _getPoint( QgsRenderContext &context, const QgsPoint &point );
516 
526  void copyRendererData( QgsFeatureRenderer *destRenderer ) const;
527 
528  QString mType;
529 
530  bool mUsingSymbolLevels = false;
531 
535  double mCurrentVertexMarkerSize = 2;
536 
537  QgsPaintEffect *mPaintEffect = nullptr;
538 
539  bool mForceRaster = false;
540 
545  static void convertSymbolSizeScale( QgsSymbol *symbol, Qgis::ScaleMethod method, const QString &field );
546 
551  static void convertSymbolRotation( QgsSymbol *symbol, const QString &field );
552 
554 
555  bool mOrderByEnabled = false;
556 
557  private:
558 #ifdef SIP_RUN
560  QgsFeatureRenderer &operator=( const QgsFeatureRenderer & );
561 #endif
562 
563 #ifdef QGISDEBUG
565  QThread *mThread = nullptr;
566 #endif
567 
568  Q_DISABLE_COPY( QgsFeatureRenderer )
569 };
570 
571 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRenderer::Capabilities )
572 
573 // for some reason SIP compilation fails if these lines are not included:
574 class QgsRendererWidget;
576 
577 #endif // QGSRENDERER_H
ScaleMethod
Scale methods.
Definition: qgis.h:182
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
virtual QgsFeatureRenderer * clone() const =0
Create a deep copy of this renderer.
virtual QString filter(const QgsFields &fields=QgsFields())
If a renderer does not require all the features this method may be overridden and return an expressio...
Definition: qgsrenderer.h:205
bool forceRasterRender() const
Returns whether the renderer must render as a raster.
Definition: qgsrenderer.h:420
void setForceRasterRender(bool forceRaster)
Sets whether the renderer should be rendered to a raster destination.
Definition: qgsrenderer.h:430
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
virtual QString legendClassificationAttribute() const
If supported by the renderer, return classification attribute for the use in legend.
Definition: qgsrenderer.h:362
virtual QgsSymbol * symbolForFeature(const QgsFeature &feature, QgsRenderContext &context) const =0
To be overridden.
QString type() const
Definition: qgsrenderer.h:141
bool usingSymbolLevels() const
Definition: qgsrenderer.h:291
void setUsingSymbolLevels(bool usingSymbolLevels)
Definition: qgsrenderer.h:292
Capability
Used to specify details about a renderer.
Definition: qgsrenderer.h:261
virtual QSet< QString > usedAttributes(const QgsRenderContext &context) const =0
Returns a list of attributes required by this renderer.
int mCurrentVertexMarkerType
The current type of editing marker.
Definition: qgsrenderer.h:533
QgsFeatureRequest::OrderBy mOrderBy
Definition: qgsrenderer.h:553
Represents a list of OrderByClauses, with the most important first and the least important last.
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
Base class for effect properties widgets.
Base class for visual effects which can be applied to QPicture drawings.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
Base class for renderer settings widgets.
An interface for classes which can visit style entity (e.g.
QgsSymbolLevelItem(QgsSymbol *symbol, int layer)
Definition: qgsrenderer.h:61
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
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
QMap< QString, QString > QgsStringMap
Definition: qgis.h:1041
#define SIP_THROW(name)
Definition: qgis_sip.h:189
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:177
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_END
Definition: qgis_sip.h:194
const QgsField & field
Definition: qgsfield.h:463
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QList< QgsLegendSymbolItem > QgsLegendSymbolList
QList< QgsSymbolLevel > QgsSymbolLevelOrder
Definition: qgsrenderer.h:87
QList< QgsSymbolLevelItem > QgsSymbolLevel
Definition: qgsrenderer.h:83
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgsrenderer.h:44
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:43