QGIS API Documentation  3.20.0-Odense (decaadbb31)
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"
37 #include "qgstemporalrangeobject.h"
38 
39 class QPainter;
41 class QgsLabelingEngine;
42 class QgsMapSettings;
44 class QgsSymbolLayer;
45 class QgsMaskIdProvider;
47 
48 
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  double dpiTarget() const {return mDpiTarget;}
342 
349  bool renderingStopped() const {return mRenderingStopped;}
350 
357  bool forceVectorOutput() const;
358 
364  bool useAdvancedEffects() const;
365 
371  void setUseAdvancedEffects( bool enabled );
372 
378  bool drawEditingInformation() const;
379 
385  double rendererScale() const {return mRendererScale;}
386 
391  QgsLabelingEngine *labelingEngine() const { return mLabelingEngine; } SIP_SKIP
392 
398  QColor selectionColor() const { return mSelectionColor; }
399 
407  bool showSelection() const;
408 
409  //setters
410 
421  void setCoordinateTransform( const QgsCoordinateTransform &t );
422 
428  void setMapToPixel( const QgsMapToPixel &mtp ) {mMapToPixel = mtp;}
429 
442  void setExtent( const QgsRectangle &extent ) {mExtent = extent;}
443 
454  void setMapExtent( const QgsRectangle &extent ) { mOriginalMapExtent = extent; }
455 
461  void setDrawEditingInformation( bool b );
462 
469  void setRenderingStopped( bool stopped ) {mRenderingStopped = stopped;}
470 
476  void setDistanceArea( const QgsDistanceArea &distanceArea ) {mDistanceArea = distanceArea ;}
477 
484  void setScaleFactor( double factor ) {mScaleFactor = factor;}
485 
492  void setDpiTarget( double dpi ) {mDpiTarget = dpi;}
493 
499  void setRendererScale( double scale ) {mRendererScale = scale;}
500 
507  void setPainter( QPainter *p ) {mPainter = p;}
508 
516  void setMaskPainter( QPainter *p, int id = 0 ) { mMaskPainter[id] = p; }
517 
527  void setDisabledSymbolLayers( const QSet<const QgsSymbolLayer *> &symbolLayers ) { mDisabledSymbolLayers = symbolLayers; }
528 
535  void setForceVectorOutput( bool force );
536 
541  void setLabelingEngine( QgsLabelingEngine *engine ) { mLabelingEngine = engine; } SIP_SKIP
542 
548  void setSelectionColor( const QColor &color ) { mSelectionColor = color; }
549 
557  void setShowSelection( bool showSelection );
558 
564  bool useRenderingOptimization() const;
565 
571  void setUseRenderingOptimization( bool enabled );
572 
581  const QgsVectorSimplifyMethod &vectorSimplifyMethod() const { return mVectorSimplifyMethod; }
582 
596  void setVectorSimplifyMethod( const QgsVectorSimplifyMethod &simplifyMethod ) { mVectorSimplifyMethod = simplifyMethod; }
597 
604  void setExpressionContext( const QgsExpressionContext &context ) { mExpressionContext = context; }
605 
612  QgsExpressionContext &expressionContext() { return mExpressionContext; }
613 
621  const QgsExpressionContext &expressionContext() const { return mExpressionContext; } SIP_SKIP
622 
624  const QgsAbstractGeometry *geometry() const { return mGeometry; }
626  void setGeometry( const QgsAbstractGeometry *geometry ) { mGeometry = geometry; }
627 
634  void setFeatureFilterProvider( const QgsFeatureFilterProvider *ffp );
635 
642  const QgsFeatureFilterProvider *featureFilterProvider() const;
643 
650  void setSegmentationTolerance( double tolerance ) { mSegmentationTolerance = tolerance; }
651 
656  double segmentationTolerance() const { return mSegmentationTolerance; }
657 
664  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type ) { mSegmentationToleranceType = type; }
665 
670  QgsAbstractGeometry::SegmentationToleranceType segmentationToleranceType() const { return mSegmentationToleranceType; }
671 
672  // Conversions
673 
680  double convertToPainterUnits( double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale = QgsMapUnitScale() ) const;
681 
688  double convertToMapUnits( double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale = QgsMapUnitScale() ) const;
689 
695  double convertFromMapUnits( double sizeInMapUnits, QgsUnitTypes::RenderUnit outputUnit ) const;
696 
703  double convertMetersToMapUnits( double meters ) const;
704 
712  {
713  return mTextRenderFormat;
714  }
715 
723  {
724  mTextRenderFormat = format;
725  }
726 
732  QList<QgsRenderedFeatureHandlerInterface *> renderedFeatureHandlers() const;
733 
739  bool hasRenderedFeatureHandlers() const { return mHasRenderedFeatureHandlers; }
740 
749  void setMaskIdProvider( QgsMaskIdProvider *provider ) { mMaskIdProvider = provider; }
750 
756  const QgsMaskIdProvider *maskIdProvider() const { return mMaskIdProvider; }
757 
763  void setCurrentMaskId( int id ) { mCurrentMaskId = id; }
764 
771  int currentMaskId() const { return mCurrentMaskId; }
772 
782  void setIsGuiPreview( bool preview ) { mIsGuiPreview = preview; }
783 
794  bool isGuiPreview() const { return mIsGuiPreview; }
795 
802  QVariantMap customRenderingFlags() const { return mCustomRenderingFlags; }
803 
811  void setCustomRenderingFlag( const QString &flag, const QVariant &value ) { mCustomRenderingFlags[flag] = value; }
812 
819  void clearCustomRenderingFlag( const QString &flag ) { mCustomRenderingFlags.remove( flag ); }
820 
828  QList< QgsMapClippingRegion > clippingRegions() const;
829 
844  QgsGeometry featureClipGeometry() const;
845 
855  void setFeatureClipGeometry( const QgsGeometry &geometry );
856 
864  QPointF textureOrigin() const;
865 
873  void setTextureOrigin( const QPointF &origin );
874 
881  QgsDoubleRange zRange() const;
882 
889  void setZRange( const QgsDoubleRange &range );
890 
891  private:
892 
893  Flags mFlags;
894 
896  QPainter *mPainter = nullptr;
897 
904  QMap<int, QPainter *> mMaskPainter;
905 
911  QgsMaskIdProvider *mMaskIdProvider = nullptr;
912 
917  int mCurrentMaskId = -1;
918 
923  bool mIsGuiPreview = false;
924 
926  QgsCoordinateTransform mCoordTransform;
927 
933  QgsDistanceArea mDistanceArea;
934 
935  QgsRectangle mExtent;
936  QgsRectangle mOriginalMapExtent;
937 
938  QgsMapToPixel mMapToPixel;
939 
941  bool mRenderingStopped = false;
942 
944  double mScaleFactor = 1.0;
945 
947  double mDpiTarget = -1.0;
948 
950  double mRendererScale = 1.0;
951 
953  QgsLabelingEngine *mLabelingEngine = nullptr;
954 
956  QColor mSelectionColor;
957 
959  QgsVectorSimplifyMethod mVectorSimplifyMethod;
960 
962  QgsExpressionContext mExpressionContext;
963 
965  const QgsAbstractGeometry *mGeometry = nullptr;
966 
968  std::unique_ptr< QgsFeatureFilterProvider > mFeatureFilterProvider;
969 
970  double mSegmentationTolerance = M_PI_2 / 90;
971 
973 
974  QgsCoordinateTransformContext mTransformContext;
975 
976  QgsPathResolver mPathResolver;
977 
978  TextRenderFormat mTextRenderFormat = TextFormatAlwaysOutlines;
979  QList< QgsRenderedFeatureHandlerInterface * > mRenderedFeatureHandlers;
980  bool mHasRenderedFeatureHandlers = false;
981  QVariantMap mCustomRenderingFlags;
982 
983  QSet<const QgsSymbolLayer *> mDisabledSymbolLayers;
984 
985  QList< QgsMapClippingRegion > mClippingRegions;
986  QgsGeometry mFeatureClipGeometry;
987 
988  QPointF mTextureOrigin;
989 
990  QgsDoubleRange mZRange;
991 
992 #ifdef QGISDEBUG
993  bool mHasTransformContext = false;
994 #endif
995 };
996 
997 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRenderContext::Flags )
998 
999 #ifndef SIP_RUN
1000 
1013 {
1014  public:
1015 
1022  QgsScopedRenderContextPainterSwap( QgsRenderContext &context, QPainter *temporaryPainter )
1023  : mContext( context )
1024  , mPreviousPainter( context.painter() )
1025  {
1026  mContext.setPainter( temporaryPainter );
1027  }
1028 
1032  void reset()
1033  {
1034  if ( !mReleased )
1035  {
1036  mContext.setPainter( mPreviousPainter );
1037  mReleased = true;
1038  }
1039  }
1040 
1045  {
1046  reset();
1047  }
1048 
1049  private:
1050 
1051  QgsRenderContext &mContext;
1052  QPainter *mPreviousPainter = nullptr;
1053  bool mReleased = false;
1054 };
1055 
1056 
1069 {
1070  public:
1071 
1080  : mContext( context )
1081  {
1082  if ( mContext.painter() )
1083  mContext.painter()->scale( mContext.scaleFactor(), mContext.scaleFactor() );
1084  }
1085 
1090  {
1091  if ( mContext.painter() )
1092  mContext.painter()->scale( 1.0 / mContext.scaleFactor(), 1.0 / mContext.scaleFactor() );
1093  }
1094 
1095  private:
1096 
1097  QgsRenderContext &mContext;
1098 };
1099 
1100 
1113 {
1114  public:
1115 
1124  : mContext( context )
1125  {
1126  if ( mContext.painter() )
1127  mContext.painter()->scale( 1.0 / mContext.scaleFactor(), 1.0 / mContext.scaleFactor() );
1128  }
1129 
1134  {
1135  if ( mContext.painter() )
1136  mContext.painter()->scale( mContext.scaleFactor(), mContext.scaleFactor() );
1137  }
1138 
1139  private:
1140 
1141  QgsRenderContext &mContext;
1142 };
1143 
1144 
1157 {
1158  public:
1159 
1165  QgsScopedQPainterState( QPainter *painter )
1166  : mPainter( painter )
1167  {
1168  mPainter->save();
1169  }
1170 
1175  {
1176  mPainter->restore();
1177  }
1178 
1179  private:
1180 
1181  QPainter *mPainter = nullptr;
1182 };
1183 
1184 #endif
1185 
1186 #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...
void setDpiTarget(double dpi)
Sets the targeted dpi for rendering.
~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.
double dpiTarget() const
Returns the targeted DPI for rendering.
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:168
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.