QGIS API Documentation  3.15.0-Master (a49cb7c9f3)
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  };
87  Q_DECLARE_FLAGS( Flags, Flag )
88 
89 
94  {
95  // refs for below dox: https://github.com/qgis/QGIS/pull/1286#issuecomment-39806854
96  // https://github.com/qgis/QGIS/pull/8573#issuecomment-445585826
97 
115 
131  };
132 
137  void setFlags( QgsRenderContext::Flags flags );
138 
143  void setFlag( Flag flag, bool on = true );
144 
149  Flags flags() const;
150 
155  bool testFlag( Flag flag ) const;
156 
161  static QgsRenderContext fromMapSettings( const QgsMapSettings &mapSettings );
162 
169  static QgsRenderContext fromQPainter( QPainter *painter );
170 
171  //getters
172 
177  QPainter *painter() {return mPainter;}
178 
179 #ifndef SIP_RUN
180 
186  const QPainter *painter() const { return mPainter; }
187 #endif
188 
198  void setPainterFlagsUsingContext( QPainter *painter = nullptr ) const;
199 
209  QPainter *maskPainter( int id = 0 ) { return mMaskPainter.value( id, nullptr ); }
210 
220  QSet<const QgsSymbolLayer *> disabledSymbolLayers() const { return mDisabledSymbolLayers; }
221 
231  bool isSymbolLayerEnabled( const QgsSymbolLayer *layer ) const { return ! mDisabledSymbolLayers.contains( layer ); }
232 
243  QgsCoordinateTransform coordinateTransform() const {return mCoordTransform;}
244 
249  const QgsDistanceArea &distanceArea() const { return mDistanceArea; }
250 
259  QgsCoordinateTransformContext transformContext() const;
260 
269  void setTransformContext( const QgsCoordinateTransformContext &context );
270 
278  const QgsPathResolver &pathResolver() const { return mPathResolver; }
279 
287  void setPathResolver( const QgsPathResolver &resolver ) { mPathResolver = resolver; }
288 
304  const QgsRectangle &extent() const { return mExtent; }
305 
316  QgsRectangle mapExtent() const { return mOriginalMapExtent; }
317 
323  const QgsMapToPixel &mapToPixel() const {return mMapToPixel;}
324 
331  double scaleFactor() const {return mScaleFactor;}
332 
339  bool renderingStopped() const {return mRenderingStopped;}
340 
347  bool forceVectorOutput() const;
348 
354  bool useAdvancedEffects() const;
355 
361  void setUseAdvancedEffects( bool enabled );
362 
368  bool drawEditingInformation() const;
369 
375  double rendererScale() const {return mRendererScale;}
376 
381  QgsLabelingEngine *labelingEngine() const { return mLabelingEngine; } SIP_SKIP
382 
388  QColor selectionColor() const { return mSelectionColor; }
389 
397  bool showSelection() const;
398 
399  //setters
400 
411  void setCoordinateTransform( const QgsCoordinateTransform &t );
412 
418  void setMapToPixel( const QgsMapToPixel &mtp ) {mMapToPixel = mtp;}
419 
432  void setExtent( const QgsRectangle &extent ) {mExtent = extent;}
433 
444  void setMapExtent( const QgsRectangle &extent ) { mOriginalMapExtent = extent; }
445 
451  void setDrawEditingInformation( bool b );
452 
459  void setRenderingStopped( bool stopped ) {mRenderingStopped = stopped;}
460 
466  void setDistanceArea( const QgsDistanceArea &distanceArea ) {mDistanceArea = distanceArea ;}
467 
474  void setScaleFactor( double factor ) {mScaleFactor = factor;}
475 
481  void setRendererScale( double scale ) {mRendererScale = scale;}
482 
489  void setPainter( QPainter *p ) {mPainter = p;}
490 
498  void setMaskPainter( QPainter *p, int id = 0 ) { mMaskPainter[id] = p; }
499 
509  void setDisabledSymbolLayers( const QSet<const QgsSymbolLayer *> &symbolLayers ) { mDisabledSymbolLayers = symbolLayers; }
510 
517  void setForceVectorOutput( bool force );
518 
523  void setLabelingEngine( QgsLabelingEngine *engine ) { mLabelingEngine = engine; } SIP_SKIP
524 
530  void setSelectionColor( const QColor &color ) { mSelectionColor = color; }
531 
539  void setShowSelection( bool showSelection );
540 
546  bool useRenderingOptimization() const;
547 
553  void setUseRenderingOptimization( bool enabled );
554 
563  const QgsVectorSimplifyMethod &vectorSimplifyMethod() const { return mVectorSimplifyMethod; }
564 
578  void setVectorSimplifyMethod( const QgsVectorSimplifyMethod &simplifyMethod ) { mVectorSimplifyMethod = simplifyMethod; }
579 
586  void setExpressionContext( const QgsExpressionContext &context ) { mExpressionContext = context; }
587 
594  QgsExpressionContext &expressionContext() { return mExpressionContext; }
595 
603  const QgsExpressionContext &expressionContext() const { return mExpressionContext; } SIP_SKIP
604 
606  const QgsAbstractGeometry *geometry() const { return mGeometry; }
608  void setGeometry( const QgsAbstractGeometry *geometry ) { mGeometry = geometry; }
609 
616  void setFeatureFilterProvider( const QgsFeatureFilterProvider *ffp );
617 
624  const QgsFeatureFilterProvider *featureFilterProvider() const;
625 
632  void setSegmentationTolerance( double tolerance ) { mSegmentationTolerance = tolerance; }
633 
638  double segmentationTolerance() const { return mSegmentationTolerance; }
639 
646  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type ) { mSegmentationToleranceType = type; }
647 
652  QgsAbstractGeometry::SegmentationToleranceType segmentationToleranceType() const { return mSegmentationToleranceType; }
653 
654  // Conversions
655 
662  double convertToPainterUnits( double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale = QgsMapUnitScale() ) const;
663 
670  double convertToMapUnits( double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale = QgsMapUnitScale() ) const;
671 
677  double convertFromMapUnits( double sizeInMapUnits, QgsUnitTypes::RenderUnit outputUnit ) const;
678 
685  double convertMetersToMapUnits( double meters ) const;
686 
694  {
695  return mTextRenderFormat;
696  }
697 
705  {
706  mTextRenderFormat = format;
707  }
708 
714  QList<QgsRenderedFeatureHandlerInterface *> renderedFeatureHandlers() const;
715 
721  bool hasRenderedFeatureHandlers() const { return mHasRenderedFeatureHandlers; }
722 
731  void setMaskIdProvider( QgsMaskIdProvider *provider ) { mMaskIdProvider = provider; }
732 
738  const QgsMaskIdProvider *maskIdProvider() const { return mMaskIdProvider; }
739 
745  void setCurrentMaskId( int id ) { mCurrentMaskId = id; }
746 
753  int currentMaskId() const { return mCurrentMaskId; }
754 
764  void setIsGuiPreview( bool preview ) { mIsGuiPreview = preview; }
765 
776  bool isGuiPreview() const { return mIsGuiPreview; }
777 
784  QVariantMap customRenderingFlags() const { return mCustomRenderingFlags; }
785 
793  void setCustomRenderingFlag( const QString &flag, const QVariant &value ) { mCustomRenderingFlags[flag] = value; }
794 
801  void clearCustomRenderingFlag( const QString &flag ) { mCustomRenderingFlags.remove( flag ); }
802 
810  QList< QgsMapClippingRegion > clippingRegions() const;
811 
826  QgsGeometry featureClipGeometry() const;
827 
837  void setFeatureClipGeometry( const QgsGeometry &geometry );
838 
839  private:
840 
841  Flags mFlags;
842 
844  QPainter *mPainter = nullptr;
845 
852  QMap<int, QPainter *> mMaskPainter;
853 
859  QgsMaskIdProvider *mMaskIdProvider = nullptr;
860 
865  int mCurrentMaskId = -1;
866 
871  bool mIsGuiPreview = false;
872 
874  QgsCoordinateTransform mCoordTransform;
875 
881  QgsDistanceArea mDistanceArea;
882 
883  QgsRectangle mExtent;
884  QgsRectangle mOriginalMapExtent;
885 
886  QgsMapToPixel mMapToPixel;
887 
889  bool mRenderingStopped = false;
890 
892  double mScaleFactor = 1.0;
893 
895  double mRendererScale = 1.0;
896 
898  QgsLabelingEngine *mLabelingEngine = nullptr;
899 
901  QColor mSelectionColor;
902 
904  QgsVectorSimplifyMethod mVectorSimplifyMethod;
905 
907  QgsExpressionContext mExpressionContext;
908 
910  const QgsAbstractGeometry *mGeometry = nullptr;
911 
913  std::unique_ptr< QgsFeatureFilterProvider > mFeatureFilterProvider;
914 
915  double mSegmentationTolerance = M_PI_2 / 90;
916 
918 
919  QgsCoordinateTransformContext mTransformContext;
920 
921  QgsPathResolver mPathResolver;
922 
923  TextRenderFormat mTextRenderFormat = TextFormatAlwaysOutlines;
924  QList< QgsRenderedFeatureHandlerInterface * > mRenderedFeatureHandlers;
925  bool mHasRenderedFeatureHandlers = false;
926  QVariantMap mCustomRenderingFlags;
927 
928  QSet<const QgsSymbolLayer *> mDisabledSymbolLayers;
929 
930  QList< QgsMapClippingRegion > mClippingRegions;
931  QgsGeometry mFeatureClipGeometry;
932 
933 #ifdef QGISDEBUG
934  bool mHasTransformContext = false;
935 #endif
936 };
937 
938 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRenderContext::Flags )
939 
940 #ifndef SIP_RUN
941 
954 {
955  public:
956 
963  QgsScopedRenderContextPainterSwap( QgsRenderContext &context, QPainter *temporaryPainter )
964  : mContext( context )
965  , mPreviousPainter( context.painter() )
966  {
967  mContext.setPainter( temporaryPainter );
968  }
969 
973  void reset()
974  {
975  if ( !mReleased )
976  {
977  mContext.setPainter( mPreviousPainter );
978  mReleased = true;
979  }
980  }
981 
986  {
987  reset();
988  }
989 
990  private:
991 
992  QgsRenderContext &mContext;
993  QPainter *mPreviousPainter = nullptr;
994  bool mReleased = false;
995 };
996 
997 
1010 {
1011  public:
1012 
1021  : mContext( context )
1022  {
1023  if ( mContext.painter() )
1024  mContext.painter()->scale( mContext.scaleFactor(), mContext.scaleFactor() );
1025  }
1026 
1031  {
1032  if ( mContext.painter() )
1033  mContext.painter()->scale( 1.0 / mContext.scaleFactor(), 1.0 / mContext.scaleFactor() );
1034  }
1035 
1036  private:
1037 
1038  QgsRenderContext &mContext;
1039 };
1040 
1041 
1054 {
1055  public:
1056 
1065  : mContext( context )
1066  {
1067  if ( mContext.painter() )
1068  mContext.painter()->scale( 1.0 / mContext.scaleFactor(), 1.0 / mContext.scaleFactor() );
1069  }
1070 
1075  {
1076  if ( mContext.painter() )
1077  mContext.painter()->scale( mContext.scaleFactor(), mContext.scaleFactor() );
1078  }
1079 
1080  private:
1081 
1082  QgsRenderContext &mContext;
1083 };
1084 
1085 
1098 {
1099  public:
1100 
1106  QgsScopedQPainterState( QPainter *painter )
1107  : mPainter( painter )
1108  {
1109  mPainter->save();
1110  }
1111 
1116  {
1117  mPainter->restore();
1118  }
1119 
1120  private:
1121 
1122  QPainter *mPainter = nullptr;
1123 };
1124 
1125 #endif
1126 
1127 #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...
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:122
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()