QGIS API Documentation  3.17.0-Master (a035f434f4)
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 
57 class CORE_EXPORT QgsRenderContext : public QgsTemporalRangeObject
58 {
59  public:
61  ~QgsRenderContext() override;
62 
64  QgsRenderContext &operator=( const QgsRenderContext &rh );
65 
70  enum Flag
71  {
72  DrawEditingInfo = 0x01,
73  ForceVectorOutput = 0x02,
74  UseAdvancedEffects = 0x04,
75  UseRenderingOptimization = 0x08,
76  DrawSelection = 0x10,
77  DrawSymbolBounds = 0x20,
78  RenderMapTile = 0x40,
79  Antialiasing = 0x80,
80  RenderPartialOutput = 0x100,
81  RenderPreviewJob = 0x200,
82  RenderBlocking = 0x400,
83  RenderSymbolPreview = 0x800,
84  LosslessImageRendering = 0x1000,
85  ApplyScalingWorkaroundForTextRendering = 0x2000,
86  Render3DMap = 0x4000,
87  ApplyClipAfterReprojection = 0x8000,
88  };
89  Q_DECLARE_FLAGS( Flags, Flag )
90 
91 
96  {
97  // refs for below dox: https://github.com/qgis/QGIS/pull/1286#issuecomment-39806854
98  // https://github.com/qgis/QGIS/pull/8573#issuecomment-445585826
99 
117 
133  };
134 
139  void setFlags( QgsRenderContext::Flags flags );
140 
145  void setFlag( Flag flag, bool on = true );
146 
151  Flags flags() const;
152 
157  bool testFlag( Flag flag ) const;
158 
163  static QgsRenderContext fromMapSettings( const QgsMapSettings &mapSettings );
164 
171  static QgsRenderContext fromQPainter( QPainter *painter );
172 
173  //getters
174 
179  QPainter *painter() {return mPainter;}
180 
181 #ifndef SIP_RUN
182 
188  const QPainter *painter() const { return mPainter; }
189 #endif
190 
200  void setPainterFlagsUsingContext( QPainter *painter = nullptr ) const;
201 
211  QPainter *maskPainter( int id = 0 ) { return mMaskPainter.value( id, nullptr ); }
212 
222  QSet<const QgsSymbolLayer *> disabledSymbolLayers() const { return mDisabledSymbolLayers; }
223 
233  bool isSymbolLayerEnabled( const QgsSymbolLayer *layer ) const { return ! mDisabledSymbolLayers.contains( layer ); }
234 
245  QgsCoordinateTransform coordinateTransform() const {return mCoordTransform;}
246 
251  const QgsDistanceArea &distanceArea() const { return mDistanceArea; }
252 
261  QgsCoordinateTransformContext transformContext() const;
262 
271  void setTransformContext( const QgsCoordinateTransformContext &context );
272 
280  const QgsPathResolver &pathResolver() const { return mPathResolver; }
281 
289  void setPathResolver( const QgsPathResolver &resolver ) { mPathResolver = resolver; }
290 
306  const QgsRectangle &extent() const { return mExtent; }
307 
318  QgsRectangle mapExtent() const { return mOriginalMapExtent; }
319 
325  const QgsMapToPixel &mapToPixel() const {return mMapToPixel;}
326 
333  double scaleFactor() const {return mScaleFactor;}
334 
341  bool renderingStopped() const {return mRenderingStopped;}
342 
349  bool forceVectorOutput() const;
350 
356  bool useAdvancedEffects() const;
357 
363  void setUseAdvancedEffects( bool enabled );
364 
370  bool drawEditingInformation() const;
371 
377  double rendererScale() const {return mRendererScale;}
378 
383  QgsLabelingEngine *labelingEngine() const { return mLabelingEngine; } SIP_SKIP
384 
390  QColor selectionColor() const { return mSelectionColor; }
391 
399  bool showSelection() const;
400 
401  //setters
402 
413  void setCoordinateTransform( const QgsCoordinateTransform &t );
414 
420  void setMapToPixel( const QgsMapToPixel &mtp ) {mMapToPixel = mtp;}
421 
434  void setExtent( const QgsRectangle &extent ) {mExtent = extent;}
435 
446  void setMapExtent( const QgsRectangle &extent ) { mOriginalMapExtent = extent; }
447 
453  void setDrawEditingInformation( bool b );
454 
461  void setRenderingStopped( bool stopped ) {mRenderingStopped = stopped;}
462 
468  void setDistanceArea( const QgsDistanceArea &distanceArea ) {mDistanceArea = distanceArea ;}
469 
476  void setScaleFactor( double factor ) {mScaleFactor = factor;}
477 
483  void setRendererScale( double scale ) {mRendererScale = scale;}
484 
491  void setPainter( QPainter *p ) {mPainter = p;}
492 
500  void setMaskPainter( QPainter *p, int id = 0 ) { mMaskPainter[id] = p; }
501 
511  void setDisabledSymbolLayers( const QSet<const QgsSymbolLayer *> &symbolLayers ) { mDisabledSymbolLayers = symbolLayers; }
512 
519  void setForceVectorOutput( bool force );
520 
525  void setLabelingEngine( QgsLabelingEngine *engine ) { mLabelingEngine = engine; } SIP_SKIP
526 
532  void setSelectionColor( const QColor &color ) { mSelectionColor = color; }
533 
541  void setShowSelection( bool showSelection );
542 
548  bool useRenderingOptimization() const;
549 
555  void setUseRenderingOptimization( bool enabled );
556 
565  const QgsVectorSimplifyMethod &vectorSimplifyMethod() const { return mVectorSimplifyMethod; }
566 
580  void setVectorSimplifyMethod( const QgsVectorSimplifyMethod &simplifyMethod ) { mVectorSimplifyMethod = simplifyMethod; }
581 
588  void setExpressionContext( const QgsExpressionContext &context ) { mExpressionContext = context; }
589 
596  QgsExpressionContext &expressionContext() { return mExpressionContext; }
597 
605  const QgsExpressionContext &expressionContext() const { return mExpressionContext; } SIP_SKIP
606 
608  const QgsAbstractGeometry *geometry() const { return mGeometry; }
610  void setGeometry( const QgsAbstractGeometry *geometry ) { mGeometry = geometry; }
611 
618  void setFeatureFilterProvider( const QgsFeatureFilterProvider *ffp );
619 
626  const QgsFeatureFilterProvider *featureFilterProvider() const;
627 
634  void setSegmentationTolerance( double tolerance ) { mSegmentationTolerance = tolerance; }
635 
640  double segmentationTolerance() const { return mSegmentationTolerance; }
641 
648  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type ) { mSegmentationToleranceType = type; }
649 
654  QgsAbstractGeometry::SegmentationToleranceType segmentationToleranceType() const { return mSegmentationToleranceType; }
655 
656  // Conversions
657 
664  double convertToPainterUnits( double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale = QgsMapUnitScale() ) const;
665 
672  double convertToMapUnits( double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale = QgsMapUnitScale() ) const;
673 
679  double convertFromMapUnits( double sizeInMapUnits, QgsUnitTypes::RenderUnit outputUnit ) const;
680 
687  double convertMetersToMapUnits( double meters ) const;
688 
696  {
697  return mTextRenderFormat;
698  }
699 
707  {
708  mTextRenderFormat = format;
709  }
710 
716  QList<QgsRenderedFeatureHandlerInterface *> renderedFeatureHandlers() const;
717 
723  bool hasRenderedFeatureHandlers() const { return mHasRenderedFeatureHandlers; }
724 
733  void setMaskIdProvider( QgsMaskIdProvider *provider ) { mMaskIdProvider = provider; }
734 
740  const QgsMaskIdProvider *maskIdProvider() const { return mMaskIdProvider; }
741 
747  void setCurrentMaskId( int id ) { mCurrentMaskId = id; }
748 
755  int currentMaskId() const { return mCurrentMaskId; }
756 
766  void setIsGuiPreview( bool preview ) { mIsGuiPreview = preview; }
767 
778  bool isGuiPreview() const { return mIsGuiPreview; }
779 
786  QVariantMap customRenderingFlags() const { return mCustomRenderingFlags; }
787 
795  void setCustomRenderingFlag( const QString &flag, const QVariant &value ) { mCustomRenderingFlags[flag] = value; }
796 
803  void clearCustomRenderingFlag( const QString &flag ) { mCustomRenderingFlags.remove( flag ); }
804 
812  QList< QgsMapClippingRegion > clippingRegions() const;
813 
828  QgsGeometry featureClipGeometry() const;
829 
839  void setFeatureClipGeometry( const QgsGeometry &geometry );
840 
848  QPointF textureOrigin() const;
849 
857  void setTextureOrigin( const QPointF &origin );
858 
859  private:
860 
861  Flags mFlags;
862 
864  QPainter *mPainter = nullptr;
865 
872  QMap<int, QPainter *> mMaskPainter;
873 
879  QgsMaskIdProvider *mMaskIdProvider = nullptr;
880 
885  int mCurrentMaskId = -1;
886 
891  bool mIsGuiPreview = false;
892 
894  QgsCoordinateTransform mCoordTransform;
895 
901  QgsDistanceArea mDistanceArea;
902 
903  QgsRectangle mExtent;
904  QgsRectangle mOriginalMapExtent;
905 
906  QgsMapToPixel mMapToPixel;
907 
909  bool mRenderingStopped = false;
910 
912  double mScaleFactor = 1.0;
913 
915  double mRendererScale = 1.0;
916 
918  QgsLabelingEngine *mLabelingEngine = nullptr;
919 
921  QColor mSelectionColor;
922 
924  QgsVectorSimplifyMethod mVectorSimplifyMethod;
925 
927  QgsExpressionContext mExpressionContext;
928 
930  const QgsAbstractGeometry *mGeometry = nullptr;
931 
933  std::unique_ptr< QgsFeatureFilterProvider > mFeatureFilterProvider;
934 
935  double mSegmentationTolerance = M_PI_2 / 90;
936 
938 
939  QgsCoordinateTransformContext mTransformContext;
940 
941  QgsPathResolver mPathResolver;
942 
943  TextRenderFormat mTextRenderFormat = TextFormatAlwaysOutlines;
944  QList< QgsRenderedFeatureHandlerInterface * > mRenderedFeatureHandlers;
945  bool mHasRenderedFeatureHandlers = false;
946  QVariantMap mCustomRenderingFlags;
947 
948  QSet<const QgsSymbolLayer *> mDisabledSymbolLayers;
949 
950  QList< QgsMapClippingRegion > mClippingRegions;
951  QgsGeometry mFeatureClipGeometry;
952 
953  QPointF mTextureOrigin;
954 
955 #ifdef QGISDEBUG
956  bool mHasTransformContext = false;
957 #endif
958 };
959 
960 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRenderContext::Flags )
961 
962 #ifndef SIP_RUN
963 
976 {
977  public:
978 
985  QgsScopedRenderContextPainterSwap( QgsRenderContext &context, QPainter *temporaryPainter )
986  : mContext( context )
987  , mPreviousPainter( context.painter() )
988  {
989  mContext.setPainter( temporaryPainter );
990  }
991 
995  void reset()
996  {
997  if ( !mReleased )
998  {
999  mContext.setPainter( mPreviousPainter );
1000  mReleased = true;
1001  }
1002  }
1003 
1008  {
1009  reset();
1010  }
1011 
1012  private:
1013 
1014  QgsRenderContext &mContext;
1015  QPainter *mPreviousPainter = nullptr;
1016  bool mReleased = false;
1017 };
1018 
1019 
1032 {
1033  public:
1034 
1043  : mContext( context )
1044  {
1045  if ( mContext.painter() )
1046  mContext.painter()->scale( mContext.scaleFactor(), mContext.scaleFactor() );
1047  }
1048 
1053  {
1054  if ( mContext.painter() )
1055  mContext.painter()->scale( 1.0 / mContext.scaleFactor(), 1.0 / mContext.scaleFactor() );
1056  }
1057 
1058  private:
1059 
1060  QgsRenderContext &mContext;
1061 };
1062 
1063 
1076 {
1077  public:
1078 
1087  : mContext( context )
1088  {
1089  if ( mContext.painter() )
1090  mContext.painter()->scale( 1.0 / mContext.scaleFactor(), 1.0 / mContext.scaleFactor() );
1091  }
1092 
1097  {
1098  if ( mContext.painter() )
1099  mContext.painter()->scale( mContext.scaleFactor(), mContext.scaleFactor() );
1100  }
1101 
1102  private:
1103 
1104  QgsRenderContext &mContext;
1105 };
1106 
1107 
1120 {
1121  public:
1122 
1128  QgsScopedQPainterState( QPainter *painter )
1129  : mPainter( painter )
1130  {
1131  mPainter->save();
1132  }
1133 
1138  {
1139  mPainter->restore();
1140  }
1141 
1142  private:
1143 
1144  QPainter *mPainter = nullptr;
1145 };
1146 
1147 #endif
1148 
1149 #endif
Some rendering operations may need multiple mask images.
void setRenderingStopped(bool stopped)
Sets whether the rendering operation has been stopped and any ongoing rendering should be canceled im...
A rectangle specified with double values.
Definition: qgsrectangle.h:41
double rendererScale() const
Returns the renderer map scale.
const QgsPathResolver & pathResolver() const
Returns the path resolver for conversion between relative and absolute paths during rendering operati...
Scoped object for temporary scaling of a QgsRenderContext for millimeter based rendering.
QgsScopedRenderContextScaleToPixels(QgsRenderContext &context)
Constructor for QgsScopedRenderContextScaleToPixels.
QSet< const QgsSymbolLayer * > disabledSymbolLayers() const
When rendering a map layer in a second pass (for selective masking), some symbol layers may be disabl...
const QgsVectorSimplifyMethod & vectorSimplifyMethod() const
Returns the simplification settings to use when rendering vector layers.
QgsScopedRenderContextScaleToMm(QgsRenderContext &context)
Constructor for QgsScopedRenderContextScaleToMm.
~QgsScopedQPainterState()
Restores the painter back to its original state.
Maximum angle between generating radii (lines from arc center to output vertices) ...
QgsLabelingEngine * labelingEngine() const
Gets access to new labeling engine (may be nullptr)
Always render text as text objects.
void setRendererScale(double scale)
Sets the renderer map scale.
void setPathResolver(const QgsPathResolver &resolver)
Sets the path resolver for conversion between relative and absolute paths during rendering operations...
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.
bool renderingStopped() const
Returns true if the rendering operation has been stopped and any ongoing rendering should be canceled...
void setIsGuiPreview(bool preview)
Sets GUI preview mode.
void setVectorSimplifyMethod(const QgsVectorSimplifyMethod &simplifyMethod)
Sets the simplification setting to use when rendering vector layers.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:123
QgsRectangle mapExtent() const
Returns the original extent of the map being rendered.
QgsScopedQPainterState(QPainter *painter)
Constructor for QgsScopedQPainterState.
TextRenderFormat textRenderFormat() const
Returns the text render format, which dictates how text is rendered (e.g.
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
QgsAbstractGeometry::SegmentationToleranceType segmentationToleranceType() const
Gets 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.
void setExtent(const QgsRectangle &extent)
When rendering a map layer, calling this method sets the "clipping" extent for the layer (in the laye...
void setMapExtent(const QgsRectangle &extent)
Sets the original extent of the map being rendered.
~QgsScopedRenderContextScaleToPixels()
Returns the destination painter back to millimeter based units.
bool hasRenderedFeatureHandlers() const
Returns true if the context has any rendered feature handlers.
The QgsMapSettings class contains configuration for rendering of the map.
QgsScopedRenderContextPainterSwap(QgsRenderContext &context, QPainter *temporaryPainter)
Constructor for QgsScopedRenderContextPainterSwap.
void setCurrentMaskId(int id)
Stores a mask id as the "current" one.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:37
Scoped object for temporary scaling of a QgsRenderContext for pixel based rendering.
void setSelectionColor(const QColor &color)
Sets the color to use when rendering selected features.
Scoped object for saving and restoring a QPainter object&#39;s state.
~QgsScopedRenderContextPainterSwap()
Returns the destination painter for the context back to the original QPainter object.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
bool isGuiPreview() const
Returns the Gui preview mode.
A map clipping region (in map coordinates and CRS).
void setGeometry(const QgsAbstractGeometry *geometry)
Sets pointer to original (unsegmentized) geometry.
const QgsRectangle & extent() const
When rendering a map layer, calling this method returns the "clipping" extent for the layer (in the l...
#define SIP_SKIP
Definition: qgis_sip.h:126
const QPainter * painter() const
Returns the const destination QPainter for the render operation.
const QgsAbstractGeometry * geometry() const
Returns pointer to the unsegmentized geometry.
Always render text using path objects (AKA outlines/curves).
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
An interface for classes which provider custom handlers for features rendered as part of a map render...
QColor selectionColor() const
Returns the color to use when rendering selected features.
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
void setMaskPainter(QPainter *p, int id=0)
Sets a mask QPainter for the render operation.
Scoped object for temporary replacement of a QgsRenderContext destination painter.
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
Abstract interface for use by classes that filter the features of a layer.
void setSegmentationToleranceType(QgsAbstractGeometry::SegmentationToleranceType type)
Sets segmentation tolerance type (maximum angle or maximum difference between curve and approximation...
Abstract base class for all geometries.
Contains information about the context in which a coordinate transform is executed.
QPainter * maskPainter(int id=0)
Returns a mask QPainter for the render operation.
TextRenderFormat
Options for rendering text.
QgsExpressionContext & expressionContext()
Gets the expression context.
void setSegmentationTolerance(double tolerance)
Sets the segmentation tolerance applied when rendering curved geometries.
void setLabelingEngine(QgsLabelingEngine *engine)
Assign new labeling engine.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
The QgsLabelingEngine class provides map labeling functionality.
Base class for objects with an associated (optional) temporal range.
bool isSymbolLayerEnabled(const QgsSymbolLayer *layer) const
When rendering a map layer in a second pass (for selective masking), some symbol layers may be disabl...
This class contains information how to simplify geometries fetched from a vector layer.
Contains information about the context of a rendering operation.
double segmentationTolerance() const
Gets the segmentation tolerance applied when rendering curved geometries.
QPainter * painter()
Returns the destination QPainter for the render operation.
const QgsMapToPixel & mapToPixel() const
Returns the context&#39;s map to pixel transform, which transforms between map coordinates and device coo...
void clearCustomRenderingFlag(const QString &flag)
Clears the specified custom rendering flag.
Struct for storing maximum and minimum scales for measurements in map units.
const QgsDistanceArea & distanceArea() const
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
void setMaskIdProvider(QgsMaskIdProvider *provider)
Attaches a mask id provider to the context.
void setMapToPixel(const QgsMapToPixel &mtp)
Sets the context&#39;s map to pixel transform, which transforms between map coordinates and device coordi...
void setCustomRenderingFlag(const QString &flag, const QVariant &value)
Sets a custom rendering flag.
QVariantMap customRenderingFlags() const
Gets custom rendering flags.
Class for doing transforms between two map coordinate systems.
void setDistanceArea(const QgsDistanceArea &distanceArea)
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
void reset()
Resets the destination painter for the context back to the original QPainter object.
Resolves relative paths into absolute paths and vice versa.
Flag
Enumeration of flags that affect rendering operations.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
~QgsScopedRenderContextScaleToMm()
Returns the destination painter back to pixel based units.
const QgsExpressionContext & expressionContext() const
Gets the expression context (const version).
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...
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:166
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
const QgsMaskIdProvider * maskIdProvider() const
Returns the mask id provider attached to the context.
int currentMaskId() const
Returns the current mask id, which can be used with maskPainter()