QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgsfields.h"
24 #include "qgsfeaturerequest.h"
25 #include "qgsconfig.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;
41 
42 typedef QMap<QString, QString> QgsStringMap SIP_SKIP;
43 
44 typedef QList<QgsSymbol *> QgsSymbolList;
45 typedef QMap<QString, QgsSymbol * > QgsSymbolMap SIP_SKIP;
46 
47 #include "qgslegendsymbolitem.h"
48 
49 
50 #define RENDERER_TAG_NAME "renderer-v2"
51 
53 // symbol levels
54 
59 class CORE_EXPORT QgsSymbolLevelItem
60 {
61  public:
62  QgsSymbolLevelItem( QgsSymbol *symbol, int layer )
63  : mSymbol( symbol )
64  , mLayer( layer )
65  {}
66 
70  QgsSymbol *symbol() const;
71 
75  int layer() const;
76 
77  // TODO QGIS 4.0 -> make private
78  protected:
79  QgsSymbol *mSymbol = nullptr;
80  int mLayer;
81 };
82 
83 // every level has list of items: symbol + symbol layer num
84 typedef QList< QgsSymbolLevelItem > QgsSymbolLevel;
85 
86 // this is a list of levels
87 #ifndef SIP_RUN
88 typedef QList< QgsSymbolLevel > QgsSymbolLevelOrder;
89 #else
90 typedef QList< QList< QgsSymbolLevelItem > > QgsSymbolLevelOrder;
91 #endif
92 
93 
95 // renderers
96 
101 class CORE_EXPORT QgsFeatureRenderer
102 {
103 
104 #ifdef SIP_RUN
106 
107  const QString type = sipCpp->type();
108 
109  if ( type == QLatin1String( "singleSymbol" ) )
110  sipType = sipType_QgsSingleSymbolRenderer;
111  else if ( type == QLatin1String( "categorizedSymbol" ) )
112  sipType = sipType_QgsCategorizedSymbolRenderer;
113  else if ( type == QLatin1String( "graduatedSymbol" ) )
114  sipType = sipType_QgsGraduatedSymbolRenderer;
115  else if ( type == QLatin1String( "RuleRenderer" ) )
116  sipType = sipType_QgsRuleBasedRenderer;
117  else if ( type == QLatin1String( "heatmapRenderer" ) )
118  sipType = sipType_QgsHeatmapRenderer;
119  else if ( type == QLatin1String( "invertedPolygonRenderer" ) )
120  sipType = sipType_QgsInvertedPolygonRenderer;
121  else if ( type == QLatin1String( "pointCluster" ) )
122  sipType = sipType_QgsPointClusterRenderer;
123  else if ( type == QLatin1String( "pointDisplacement" ) )
124  sipType = sipType_QgsPointDisplacementRenderer;
125  else if ( type == QLatin1String( "25dRenderer" ) )
126  sipType = sipType_Qgs25DRenderer;
127  else if ( type == QLatin1String( "nullSymbol" ) )
128  sipType = sipType_QgsNullSymbolRenderer;
129  else if ( type == QLatin1String( "embeddedSymbol" ) )
130  sipType = sipType_QgsEmbeddedSymbolRenderer;
131  else
132  sipType = 0;
133  SIP_END
134 #endif
135 
136  public:
137  // renderer takes ownership of its symbols!
138 
140  static QgsFeatureRenderer *defaultRenderer( QgsWkbTypes::GeometryType geomType ) SIP_FACTORY;
141 
142  QString type() const { return mType; }
143 
153  virtual QgsSymbol *symbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const = 0;
154 
161  virtual QgsSymbol *originalSymbolForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
162 
167  virtual QSet< QString > legendKeysForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
168 
181  virtual void startRender( QgsRenderContext &context, const QgsFields &fields );
182 
193  virtual void stopRender( QgsRenderContext &context );
194 
206  virtual QString filter( const QgsFields &fields = QgsFields() ) { Q_UNUSED( fields ) return QString(); }
207 
214  virtual QSet<QString> usedAttributes( const QgsRenderContext &context ) const = 0;
215 
222  virtual bool usesEmbeddedSymbols() const;
223 
227  virtual bool filterNeedsGeometry() const;
228 
229  virtual ~QgsFeatureRenderer();
230 
237  virtual QgsFeatureRenderer *clone() const = 0 SIP_FACTORY;
238 
252  virtual bool renderFeature( const QgsFeature &feature, QgsRenderContext &context, int layer = -1, bool selected = false, bool drawVertexMarker = false ) SIP_THROW( QgsCsException );
253 
255  virtual QString dump() const;
256 
262  {
263  SymbolLevels = 1,
264  MoreSymbolsPerFeature = 1 << 2,
265  Filter = 1 << 3,
266  ScaleDependent = 1 << 4
267  };
268 
269  Q_DECLARE_FLAGS( Capabilities, Capability )
270 
271 
283  virtual QgsFeatureRenderer::Capabilities capabilities() { return QgsFeatureRenderer::Capabilities(); }
284 
290  virtual QgsSymbolList symbols( QgsRenderContext &context ) const;
291 
292  bool usingSymbolLevels() const { return mUsingSymbolLevels; }
293  void setUsingSymbolLevels( bool usingSymbolLevels ) { mUsingSymbolLevels = usingSymbolLevels; }
294 
296  static QgsFeatureRenderer *load( QDomElement &symbologyElem, const QgsReadWriteContext &context ) SIP_FACTORY;
297 
304  virtual QDomElement save( QDomDocument &doc, const QgsReadWriteContext &context );
305 
310  virtual QDomElement writeSld( QDomDocument &doc, const QString &styleName, const QVariantMap &props = QVariantMap() ) const;
311 
323  static QgsFeatureRenderer *loadSld( const QDomNode &node, QgsWkbTypes::GeometryType geomType, QString &errorMessage ) SIP_FACTORY;
324 
326  virtual void toSld( QDomDocument &doc, QDomElement &element, const QVariantMap &props = QVariantMap() ) const
327  {
328  element.appendChild( doc.createComment( QStringLiteral( "FeatureRenderer %1 not implemented yet" ).arg( type() ) ) );
329  ( void ) props; // warning avoidance
330  }
331 
336  virtual bool legendSymbolItemsCheckable() const;
337 
342  virtual bool legendSymbolItemChecked( const QString &key );
343 
348  virtual void checkLegendSymbolItem( const QString &key, bool state = true );
349 
356  virtual void setLegendSymbolItem( const QString &key, QgsSymbol *symbol SIP_TRANSFER );
357 
370  virtual QString legendKeyToExpression( const QString &key, QgsVectorLayer *layer, bool &ok SIP_OUT ) const;
371 
376  virtual QgsLegendSymbolList legendSymbolItems() const;
377 
382  virtual QString legendClassificationAttribute() const { return QString(); }
383 
385  void setVertexMarkerAppearance( Qgis::VertexMarkerType type, double size );
386 
393  virtual bool willRenderFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
394 
401  virtual QgsSymbolList symbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
402 
408  virtual QgsSymbolList originalSymbolsForFeature( const QgsFeature &feature, QgsRenderContext &context ) const;
409 
417  virtual void modifyRequestExtent( QgsRectangle &extent, QgsRenderContext &context );
418 
425  QgsPaintEffect *paintEffect() const;
426 
433  void setPaintEffect( QgsPaintEffect *effect SIP_TRANSFER );
434 
440  bool forceRasterRender() const { return mForceRaster; }
441 
450  void setForceRasterRender( bool forceRaster ) { mForceRaster = forceRaster; }
451 
466  double referenceScale() const { return mReferenceScale; }
467 
482  void setReferenceScale( double scale ) { mReferenceScale = scale; }
483 
490  QgsFeatureRequest::OrderBy orderBy() const;
491 
498  void setOrderBy( const QgsFeatureRequest::OrderBy &orderBy );
499 
506  bool orderByEnabled() const;
507 
515  void setOrderByEnabled( bool enabled );
516 
524  virtual void setEmbeddedRenderer( QgsFeatureRenderer *subRenderer SIP_TRANSFER );
525 
532  virtual const QgsFeatureRenderer *embeddedRenderer() const;
533 
543  virtual bool accept( QgsStyleEntityVisitorInterface *visitor ) const;
544 
559  void copyRendererData( QgsFeatureRenderer *destRenderer ) const;
560 
561  protected:
562  QgsFeatureRenderer( const QString &type );
563 
570  void renderFeatureWithSymbol( const QgsFeature &feature, QgsSymbol *symbol, QgsRenderContext &context, int layer, bool selected, bool drawVertexMarker ) SIP_THROW( QgsCsException );
571 
573  void renderVertexMarker( QPointF pt, QgsRenderContext &context );
575  void renderVertexMarkerPolyline( QPolygonF &pts, QgsRenderContext &context );
577  void renderVertexMarkerPolygon( QPolygonF &pts, QList<QPolygonF> *rings, QgsRenderContext &context );
578 
583  static QPointF _getPoint( QgsRenderContext &context, const QgsPoint &point );
584 
593  void saveRendererData( QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context );
594 
595  QString mType;
596 
597  bool mUsingSymbolLevels = false;
598 
601 
603  double mCurrentVertexMarkerSize = 2;
604 
605  QgsPaintEffect *mPaintEffect = nullptr;
606 
607  bool mForceRaster = false;
608 
609  double mReferenceScale = -1.0;
610 
615  static void convertSymbolSizeScale( QgsSymbol *symbol, Qgis::ScaleMethod method, const QString &field );
616 
621  static void convertSymbolRotation( QgsSymbol *symbol, const QString &field );
622 
624 
625  bool mOrderByEnabled = false;
626 
627  private:
628 #ifdef SIP_RUN
630  QgsFeatureRenderer &operator=( const QgsFeatureRenderer & );
631 #endif
632 
633 #ifdef QGISDEBUG
634  QThread *mThread = nullptr;
636 #endif
637 
638  Q_DISABLE_COPY( QgsFeatureRenderer )
639 };
640 
641 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRenderer::Capabilities )
642 
643 // for some reason SIP compilation fails if these lines are not included:
644 class QgsRendererWidget;
646 
647 #endif // QGSRENDERER_H
qgsfields.h
qgsfeaturerequest.h
QgsSymbolLevel
QList< QgsSymbolLevelItem > QgsSymbolLevel
Definition: qgsrenderer.h:84
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
qgsrectangle.h
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsSymbolMap
QMap< QString, QgsSymbol * > QgsSymbolMap
Definition: qgsrenderer.h:45
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsFeatureRenderer::type
QString type() const
Definition: qgsrenderer.h:142
qgis.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsStyleEntityVisitorInterface
An interface for classes which can visit style entity (e.g. symbol) nodes (using the visitor pattern)...
Definition: qgsstyleentityvisitor.h:33
QgsFeatureRenderer::setForceRasterRender
void setForceRasterRender(bool forceRaster)
Sets whether the renderer should be rendered to a raster destination.
Definition: qgsrenderer.h:450
QgsSymbol
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:92
field
const QgsField & field
Definition: qgsfield.h:463
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsFeatureRenderer::legendClassificationAttribute
virtual QString legendClassificationAttribute() const
If supported by the renderer, return classification attribute for the use in legend.
Definition: qgsrenderer.h:382
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
Qgis::ScaleMethod
ScaleMethod
Scale methods.
Definition: qgis.h:219
QgsSymbolLevelItem::QgsSymbolLevelItem
QgsSymbolLevelItem(QgsSymbol *symbol, int layer)
Definition: qgsrenderer.h:62
QgsFeatureRenderer::referenceScale
double referenceScale() const
Returns the symbology reference scale.
Definition: qgsrenderer.h:466
QgsFeatureRenderer::filter
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:206
QgsFeatureRenderer::Capability
Capability
Used to specify details about a renderer.
Definition: qgsrenderer.h:261
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:198
QgsFeatureRenderer::setUsingSymbolLevels
void setUsingSymbolLevels(bool usingSymbolLevels)
Definition: qgsrenderer.h:293
Qgis::VertexMarkerType
VertexMarkerType
Editing vertex markers, used for showing vertices during a edit operation.
Definition: qgis.h:806
QgsSymbolList
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:44
QgsFeatureRenderer::toSld
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:326
QgsSymbolLevelItem
Definition: qgsrenderer.h:59
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
qgslegendsymbolitem.h
Q_DECLARE_OPERATORS_FOR_FLAGS
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.
QgsSymbolLevelItem::mLayer
int mLayer
Definition: qgsrenderer.h:80
QgsFeatureRenderer::usingSymbolLevels
bool usingSymbolLevels() const
Definition: qgsrenderer.h:292
QgsRendererWidget
Base class for renderer settings widgets.
Definition: qgsrendererwidget.h:45
QgsFeatureRenderer::mOrderBy
QgsFeatureRequest::OrderBy mOrderBy
Definition: qgsrenderer.h:623
QgsPaintEffectWidget
Base class for effect properties widgets.
Definition: qgspainteffectwidget.h:40
QgsFeatureRenderer::forceRasterRender
bool forceRasterRender() const
Returns whether the renderer must render as a raster.
Definition: qgsrenderer.h:440
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:2781
QgsFeatureRenderer
Definition: qgsrenderer.h:101
QgsWkbTypes::GeometryType
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:140
QgsFeatureRequest::OrderBy
Represents a list of OrderByClauses, with the most important first and the least important last.
Definition: qgsfeaturerequest.h:264
QgsFeatureRenderer::mType
QString mType
Definition: qgsrenderer.h:595
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsFeatureRenderer::setReferenceScale
void setReferenceScale(double scale)
Sets the symbology reference scale.
Definition: qgsrenderer.h:482
QgsPaintEffect
Base class for visual effects which can be applied to QPicture drawings.
Definition: qgspainteffect.h:52
Qgis::VertexMarkerType::Cross
@ Cross
Cross marker.
QgsSymbolLevelOrder
QList< QgsSymbolLevel > QgsSymbolLevelOrder
Definition: qgsrenderer.h:88
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsLegendSymbolList
QList< QgsLegendSymbolItem > QgsLegendSymbolList
Definition: qgslegendsymbolitem.h:144
SIP_END
#define SIP_END
Definition: qgis_sip.h:203