QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgsrendercontext.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrendercontext.h
3  ------------------
4  begin : March 16, 2008
5  copyright : (C) 2008 by Marco Hugentobler
6  email : marco dot hugentobler at karto dot baug dot ethz dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSRENDERCONTEXT_H
19 #define QGSRENDERCONTEXT_H
20 
21 #include "qgis_core.h"
22 #include "qgis_sip.h"
23 #include <QColor>
24 #include <QPainter>
25 #include <memory>
26 
27 #include "qgscoordinatetransform.h"
28 #include "qgsexpressioncontext.h"
30 #include "qgsmaptopixel.h"
31 #include "qgsmapunitscale.h"
32 #include "qgsrectangle.h"
34 #include "qgsdistancearea.h"
36 #include "qgspathresolver.h"
38 #include "qgstemporalrangeobject.h"
39 
40 class QPainter;
42 class QgsLabelingEngine;
43 class QgsMapSettings;
45 class QgsSymbolLayer;
46 class QgsMaskIdProvider;
48 
49 
58 class CORE_EXPORT QgsRenderContext : public QgsTemporalRangeObject
59 {
60  public:
62  ~QgsRenderContext() override;
63 
65  QgsRenderContext &operator=( const QgsRenderContext &rh );
66 
71  enum Flag
72  {
73  DrawEditingInfo = 0x01,
74  ForceVectorOutput = 0x02,
75  UseAdvancedEffects = 0x04,
76  UseRenderingOptimization = 0x08,
77  DrawSelection = 0x10,
78  DrawSymbolBounds = 0x20,
79  RenderMapTile = 0x40,
80  Antialiasing = 0x80,
81  RenderPartialOutput = 0x100,
82  RenderPreviewJob = 0x200,
83  RenderBlocking = 0x400,
84  RenderSymbolPreview = 0x800,
85  LosslessImageRendering = 0x1000,
86  ApplyScalingWorkaroundForTextRendering = 0x2000,
87  Render3DMap = 0x4000,
88  ApplyClipAfterReprojection = 0x8000,
89  };
90  Q_DECLARE_FLAGS( Flags, Flag )
91 
92 
97  {
98  // refs for below dox: https://github.com/qgis/QGIS/pull/1286#issuecomment-39806854
99  // https://github.com/qgis/QGIS/pull/8573#issuecomment-445585826
100 
118 
134  };
135 
140  void setFlags( QgsRenderContext::Flags flags );
141 
146  void setFlag( Flag flag, bool on = true );
147 
152  Flags flags() const;
153 
158  bool testFlag( Flag flag ) const;
159 
164  static QgsRenderContext fromMapSettings( const QgsMapSettings &mapSettings );
165 
172  static QgsRenderContext fromQPainter( QPainter *painter );
173 
174  //getters
175 
180  QPainter *painter() {return mPainter;}
181 
182 #ifndef SIP_RUN
183 
189  const QPainter *painter() const { return mPainter; }
190 #endif
191 
201  void setPainterFlagsUsingContext( QPainter *painter = nullptr ) const;
202 
212  QPainter *maskPainter( int id = 0 ) { return mMaskPainter.value( id, nullptr ); }
213 
223  QSet<const QgsSymbolLayer *> disabledSymbolLayers() const { return mDisabledSymbolLayers; }
224 
234  bool isSymbolLayerEnabled( const QgsSymbolLayer *layer ) const { return ! mDisabledSymbolLayers.contains( layer ); }
235 
246  QgsCoordinateTransform coordinateTransform() const {return mCoordTransform;}
247 
252  const QgsDistanceArea &distanceArea() const { return mDistanceArea; }
253 
262  QgsCoordinateTransformContext transformContext() const;
263 
272  void setTransformContext( const QgsCoordinateTransformContext &context );
273 
281  const QgsPathResolver &pathResolver() const { return mPathResolver; }
282 
290  void setPathResolver( const QgsPathResolver &resolver ) { mPathResolver = resolver; }
291 
307  const QgsRectangle &extent() const { return mExtent; }
308 
319  QgsRectangle mapExtent() const { return mOriginalMapExtent; }
320 
326  const QgsMapToPixel &mapToPixel() const {return mMapToPixel;}
327 
334  double scaleFactor() const {return mScaleFactor;}
335 
342  bool renderingStopped() const {return mRenderingStopped;}
343 
350  bool forceVectorOutput() const;
351 
357  bool useAdvancedEffects() const;
358 
364  void setUseAdvancedEffects( bool enabled );
365 
371  bool drawEditingInformation() const;
372 
378  double rendererScale() const {return mRendererScale;}
379 
384  QgsLabelingEngine *labelingEngine() const { return mLabelingEngine; } SIP_SKIP
385 
391  QColor selectionColor() const { return mSelectionColor; }
392 
400  bool showSelection() const;
401 
402  //setters
403 
414  void setCoordinateTransform( const QgsCoordinateTransform &t );
415 
421  void setMapToPixel( const QgsMapToPixel &mtp ) {mMapToPixel = mtp;}
422 
435  void setExtent( const QgsRectangle &extent ) {mExtent = extent;}
436 
447  void setMapExtent( const QgsRectangle &extent ) { mOriginalMapExtent = extent; }
448 
454  void setDrawEditingInformation( bool b );
455 
462  void setRenderingStopped( bool stopped ) {mRenderingStopped = stopped;}
463 
469  void setDistanceArea( const QgsDistanceArea &distanceArea ) {mDistanceArea = distanceArea ;}
470 
477  void setScaleFactor( double factor ) {mScaleFactor = factor;}
478 
484  void setRendererScale( double scale ) {mRendererScale = scale;}
485 
492  void setPainter( QPainter *p ) {mPainter = p;}
493 
501  void setMaskPainter( QPainter *p, int id = 0 ) { mMaskPainter[id] = p; }
502 
512  void setDisabledSymbolLayers( const QSet<const QgsSymbolLayer *> &symbolLayers ) { mDisabledSymbolLayers = symbolLayers; }
513 
520  void setForceVectorOutput( bool force );
521 
526  void setLabelingEngine( QgsLabelingEngine *engine ) { mLabelingEngine = engine; } SIP_SKIP
527 
533  void setSelectionColor( const QColor &color ) { mSelectionColor = color; }
534 
542  void setShowSelection( bool showSelection );
543 
549  bool useRenderingOptimization() const;
550 
556  void setUseRenderingOptimization( bool enabled );
557 
566  const QgsVectorSimplifyMethod &vectorSimplifyMethod() const { return mVectorSimplifyMethod; }
567 
581  void setVectorSimplifyMethod( const QgsVectorSimplifyMethod &simplifyMethod ) { mVectorSimplifyMethod = simplifyMethod; }
582 
589  void setExpressionContext( const QgsExpressionContext &context ) { mExpressionContext = context; }
590 
597  QgsExpressionContext &expressionContext() { return mExpressionContext; }
598 
606  const QgsExpressionContext &expressionContext() const { return mExpressionContext; } SIP_SKIP
607 
609  const QgsAbstractGeometry *geometry() const { return mGeometry; }
611  void setGeometry( const QgsAbstractGeometry *geometry ) { mGeometry = geometry; }
612 
619  void setFeatureFilterProvider( const QgsFeatureFilterProvider *ffp );
620 
627  const QgsFeatureFilterProvider *featureFilterProvider() const;
628 
635  void setSegmentationTolerance( double tolerance ) { mSegmentationTolerance = tolerance; }
636 
641  double segmentationTolerance() const { return mSegmentationTolerance; }
642 
649  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type ) { mSegmentationToleranceType = type; }
650 
655  QgsAbstractGeometry::SegmentationToleranceType segmentationToleranceType() const { return mSegmentationToleranceType; }
656 
657  // Conversions
658 
665  double convertToPainterUnits( double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale = QgsMapUnitScale() ) const;
666 
673  double convertToMapUnits( double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale = QgsMapUnitScale() ) const;
674 
680  double convertFromMapUnits( double sizeInMapUnits, QgsUnitTypes::RenderUnit outputUnit ) const;
681 
688  double convertMetersToMapUnits( double meters ) const;
689 
697  {
698  return mTextRenderFormat;
699  }
700 
708  {
709  mTextRenderFormat = format;
710  }
711 
717  QList<QgsRenderedFeatureHandlerInterface *> renderedFeatureHandlers() const;
718 
724  bool hasRenderedFeatureHandlers() const { return mHasRenderedFeatureHandlers; }
725 
734  void setMaskIdProvider( QgsMaskIdProvider *provider ) { mMaskIdProvider = provider; }
735 
741  const QgsMaskIdProvider *maskIdProvider() const { return mMaskIdProvider; }
742 
748  void setCurrentMaskId( int id ) { mCurrentMaskId = id; }
749 
756  int currentMaskId() const { return mCurrentMaskId; }
757 
767  void setIsGuiPreview( bool preview ) { mIsGuiPreview = preview; }
768 
779  bool isGuiPreview() const { return mIsGuiPreview; }
780 
787  QVariantMap customRenderingFlags() const { return mCustomRenderingFlags; }
788 
796  void setCustomRenderingFlag( const QString &flag, const QVariant &value ) { mCustomRenderingFlags[flag] = value; }
797 
804  void clearCustomRenderingFlag( const QString &flag ) { mCustomRenderingFlags.remove( flag ); }
805 
813  QList< QgsMapClippingRegion > clippingRegions() const;
814 
829  QgsGeometry featureClipGeometry() const;
830 
840  void setFeatureClipGeometry( const QgsGeometry &geometry );
841 
849  QPointF textureOrigin() const;
850 
858  void setTextureOrigin( const QPointF &origin );
859 
866  QgsDoubleRange zRange() const;
867 
874  void setZRange( const QgsDoubleRange &range );
875 
876  private:
877 
878  Flags mFlags;
879 
881  QPainter *mPainter = nullptr;
882 
889  QMap<int, QPainter *> mMaskPainter;
890 
896  QgsMaskIdProvider *mMaskIdProvider = nullptr;
897 
902  int mCurrentMaskId = -1;
903 
908  bool mIsGuiPreview = false;
909 
911  QgsCoordinateTransform mCoordTransform;
912 
918  QgsDistanceArea mDistanceArea;
919 
920  QgsRectangle mExtent;
921  QgsRectangle mOriginalMapExtent;
922 
923  QgsMapToPixel mMapToPixel;
924 
926  bool mRenderingStopped = false;
927 
929  double mScaleFactor = 1.0;
930 
932  double mRendererScale = 1.0;
933 
935  QgsLabelingEngine *mLabelingEngine = nullptr;
936 
938  QColor mSelectionColor;
939 
941  QgsVectorSimplifyMethod mVectorSimplifyMethod;
942 
944  QgsExpressionContext mExpressionContext;
945 
947  const QgsAbstractGeometry *mGeometry = nullptr;
948 
950  std::unique_ptr< QgsFeatureFilterProvider > mFeatureFilterProvider;
951 
952  double mSegmentationTolerance = M_PI_2 / 90;
953 
955 
956  QgsCoordinateTransformContext mTransformContext;
957 
958  QgsPathResolver mPathResolver;
959 
960  TextRenderFormat mTextRenderFormat = TextFormatAlwaysOutlines;
961  QList< QgsRenderedFeatureHandlerInterface * > mRenderedFeatureHandlers;
962  bool mHasRenderedFeatureHandlers = false;
963  QVariantMap mCustomRenderingFlags;
964 
965  QSet<const QgsSymbolLayer *> mDisabledSymbolLayers;
966 
967  QList< QgsMapClippingRegion > mClippingRegions;
968  QgsGeometry mFeatureClipGeometry;
969 
970  QPointF mTextureOrigin;
971 
972  QgsDoubleRange mZRange;
973 
974 #ifdef QGISDEBUG
975  bool mHasTransformContext = false;
976 #endif
977 };
978 
979 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRenderContext::Flags )
980 
981 #ifndef SIP_RUN
982 
995 {
996  public:
997 
1004  QgsScopedRenderContextPainterSwap( QgsRenderContext &context, QPainter *temporaryPainter )
1005  : mContext( context )
1006  , mPreviousPainter( context.painter() )
1007  {
1008  mContext.setPainter( temporaryPainter );
1009  }
1010 
1014  void reset()
1015  {
1016  if ( !mReleased )
1017  {
1018  mContext.setPainter( mPreviousPainter );
1019  mReleased = true;
1020  }
1021  }
1022 
1027  {
1028  reset();
1029  }
1030 
1031  private:
1032 
1033  QgsRenderContext &mContext;
1034  QPainter *mPreviousPainter = nullptr;
1035  bool mReleased = false;
1036 };
1037 
1038 
1051 {
1052  public:
1053 
1062  : mContext( context )
1063  {
1064  if ( mContext.painter() )
1065  mContext.painter()->scale( mContext.scaleFactor(), mContext.scaleFactor() );
1066  }
1067 
1072  {
1073  if ( mContext.painter() )
1074  mContext.painter()->scale( 1.0 / mContext.scaleFactor(), 1.0 / mContext.scaleFactor() );
1075  }
1076 
1077  private:
1078 
1079  QgsRenderContext &mContext;
1080 };
1081 
1082 
1095 {
1096  public:
1097 
1106  : mContext( context )
1107  {
1108  if ( mContext.painter() )
1109  mContext.painter()->scale( 1.0 / mContext.scaleFactor(), 1.0 / mContext.scaleFactor() );
1110  }
1111 
1116  {
1117  if ( mContext.painter() )
1118  mContext.painter()->scale( mContext.scaleFactor(), mContext.scaleFactor() );
1119  }
1120 
1121  private:
1122 
1123  QgsRenderContext &mContext;
1124 };
1125 
1126 
1139 {
1140  public:
1141 
1147  QgsScopedQPainterState( QPainter *painter )
1148  : mPainter( painter )
1149  {
1150  mPainter->save();
1151  }
1152 
1157  {
1158  mPainter->restore();
1159  }
1160 
1161  private:
1162 
1163  QPainter *mPainter = nullptr;
1164 };
1165 
1166 #endif
1167 
1168 #endif
Abstract base class for all geometries.
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle.
@ MaximumAngle
Maximum angle between generating radii (lines from arc center to output vertices)
Contains information about the context in which a coordinate transform is executed.
Class for doing transforms between two map coordinate systems.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
QgsRange which stores a range of double values.
Definition: qgsrange.h:203
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Abstract interface for use by classes that filter the features or attributes of a layer.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
The QgsLabelingEngine class provides map labeling functionality.
A map clipping region (in map coordinates and CRS).
The QgsMapSettings class contains configuration for rendering of the map.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:39
Struct for storing maximum and minimum scales for measurements in map units.
This class allows the creation of mask ids based on the different label layers and to give a mask id ...
Resolves relative paths into absolute paths and vice versa.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
void setMapExtent(const QgsRectangle &extent)
Sets the original extent of the map being rendered.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
void setRenderingStopped(bool stopped)
Sets whether the rendering operation has been stopped and any ongoing rendering should be canceled im...
void setDistanceArea(const QgsDistanceArea &distanceArea)
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
QPainter * maskPainter(int id=0)
Returns a mask QPainter for the render operation.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
bool hasRenderedFeatureHandlers() const
Returns true if the context has any rendered feature handlers.
TextRenderFormat
Options for rendering text.
@ TextFormatAlwaysText
Always render text as text objects.
@ TextFormatAlwaysOutlines
Always render text using path objects (AKA outlines/curves).
void setSegmentationToleranceType(QgsAbstractGeometry::SegmentationToleranceType type)
Sets segmentation tolerance type (maximum angle or maximum difference between curve and approximation...
void setTextRenderFormat(TextRenderFormat format)
Sets the text render format, which dictates how text is rendered (e.g.
double segmentationTolerance() const
Gets the segmentation tolerance applied when rendering curved geometries.
const QgsAbstractGeometry * geometry() const
Returns pointer to the unsegmentized geometry.
void clearCustomRenderingFlag(const QString &flag)
Clears the specified custom rendering flag.
QPainter * painter()
Returns the destination QPainter for the render operation.
double rendererScale() const
Returns the renderer map scale.
QgsExpressionContext & expressionContext()
Gets the expression context.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
void setGeometry(const QgsAbstractGeometry *geometry)
Sets pointer to original (unsegmentized) geometry.
void setVectorSimplifyMethod(const QgsVectorSimplifyMethod &simplifyMethod)
Sets the simplification setting to use when rendering vector layers.
QVariantMap customRenderingFlags() const
Gets custom rendering flags.
bool isGuiPreview() const
Returns the Gui preview mode.
void setCurrentMaskId(int id)
Stores a mask id as the "current" one.
void setDisabledSymbolLayers(const QSet< const QgsSymbolLayer * > &symbolLayers)
When rendering a map layer in a second pass (for selective masking), some symbol layers may be disabl...
~QgsRenderContext() override
void setPathResolver(const QgsPathResolver &resolver)
Sets the path resolver for conversion between relative and absolute paths during rendering operations...
void setIsGuiPreview(bool preview)
Sets GUI preview mode.
QgsRectangle mapExtent() const
Returns the original extent of the map being rendered.
QSet< const QgsSymbolLayer * > disabledSymbolLayers() const
When rendering a map layer in a second pass (for selective masking), some symbol layers may be disabl...
void setMaskIdProvider(QgsMaskIdProvider *provider)
Attaches a mask id provider to the context.
const QPainter * painter() const
Returns the const destination QPainter for the render operation.
QColor selectionColor() const
Returns the color to use when rendering selected features.
const QgsExpressionContext & expressionContext() const
Gets the expression context (const version).
void setExtent(const QgsRectangle &extent)
When rendering a map layer, calling this method sets the "clipping" extent for the layer (in the laye...
QgsLabelingEngine * labelingEngine() const
Gets access to new labeling engine (may be nullptr)
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context's map to pixel transform, which transforms between map coordinates and device coordi...
Flag
Enumeration of flags that affect rendering operations.
int currentMaskId() const
Returns the current mask id, which can be used with maskPainter()
void setCustomRenderingFlag(const QString &flag, const QVariant &value)
Sets a custom rendering flag.
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
bool isSymbolLayerEnabled(const QgsSymbolLayer *layer) const
When rendering a map layer in a second pass (for selective masking), some symbol layers may be disabl...
void setMaskPainter(QPainter *p, int id=0)
Sets a mask QPainter for the render operation.
const QgsDistanceArea & distanceArea() const
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
void setLabelingEngine(QgsLabelingEngine *engine)
Assign new labeling engine.
bool renderingStopped() const
Returns true if the rendering operation has been stopped and any ongoing rendering should be canceled...
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
void setRendererScale(double scale)
Sets the renderer map scale.
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
const QgsRectangle & extent() const
When rendering a map layer, calling this method returns the "clipping" extent for the layer (in the l...
QgsAbstractGeometry::SegmentationToleranceType segmentationToleranceType() const
Gets segmentation tolerance type (maximum angle or maximum difference between curve and approximation...
const QgsVectorSimplifyMethod & vectorSimplifyMethod() const
Returns the simplification settings to use when rendering vector layers.
TextRenderFormat textRenderFormat() const
Returns the text render format, which dictates how text is rendered (e.g.
const QgsMaskIdProvider * maskIdProvider() const
Returns the mask id provider attached to the context.
const QgsPathResolver & pathResolver() const
Returns the path resolver for conversion between relative and absolute paths during rendering operati...
void setSelectionColor(const QColor &color)
Sets the color to use when rendering selected features.
void setSegmentationTolerance(double tolerance)
Sets the segmentation tolerance applied when rendering curved geometries.
An interface for classes which provider custom handlers for features rendered as part of a map render...
Scoped object for saving and restoring a QPainter object's state.
QgsScopedQPainterState(QPainter *painter)
Constructor for QgsScopedQPainterState.
~QgsScopedQPainterState()
Restores the painter back to its original state.
Scoped object for temporary replacement of a QgsRenderContext destination painter.
QgsScopedRenderContextPainterSwap(QgsRenderContext &context, QPainter *temporaryPainter)
Constructor for QgsScopedRenderContextPainterSwap.
~QgsScopedRenderContextPainterSwap()
Returns the destination painter for the context back to the original QPainter object.
void reset()
Resets the destination painter for the context back to the original QPainter object.
Scoped object for temporary scaling of a QgsRenderContext for millimeter based rendering.
~QgsScopedRenderContextScaleToMm()
Returns the destination painter back to pixel based units.
QgsScopedRenderContextScaleToMm(QgsRenderContext &context)
Constructor for QgsScopedRenderContextScaleToMm.
Scoped object for temporary scaling of a QgsRenderContext for pixel based rendering.
QgsScopedRenderContextScaleToPixels(QgsRenderContext &context)
Constructor for QgsScopedRenderContextScaleToPixels.
~QgsScopedRenderContextScaleToPixels()
Returns the destination painter back to millimeter based units.
Base class for objects with an associated (optional) temporal range.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
This class contains information how to simplify geometries fetched from a vector layer.
#define SIP_SKIP
Definition: qgis_sip.h:126
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.