QGIS API Documentation  2.99.0-Master (0a63d1f)
qgsrendercontext.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrendercontext.cpp
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 
19 #include "qgsrendercontext.h"
20 
21 #include "qgsmapsettings.h"
22 #include "qgsexpression.h"
23 #include "qgsvectorlayer.h"
25 
27  : mFlags( DrawEditingInfo | UseAdvancedEffects | DrawSelection | UseRenderingOptimization )
28 {
30 }
31 
33  : mFlags( rh.mFlags )
34  , mPainter( rh.mPainter )
35  , mCoordTransform( rh.mCoordTransform )
36  , mExtent( rh.mExtent )
37  , mMapToPixel( rh.mMapToPixel )
38  , mRenderingStopped( rh.mRenderingStopped )
39  , mScaleFactor( rh.mScaleFactor )
40  , mRendererScale( rh.mRendererScale )
41  , mLabelingEngine( rh.mLabelingEngine )
42  , mSelectionColor( rh.mSelectionColor )
43  , mVectorSimplifyMethod( rh.mVectorSimplifyMethod )
44  , mExpressionContext( rh.mExpressionContext )
45  , mGeometry( rh.mGeometry )
46  , mFeatureFilterProvider( rh.mFeatureFilterProvider ? rh.mFeatureFilterProvider->clone() : nullptr )
47  , mSegmentationTolerance( rh.mSegmentationTolerance )
48  , mSegmentationToleranceType( rh.mSegmentationToleranceType )
49 {
50 }
51 
53 {
54  mFlags = rh.mFlags;
55  mPainter = rh.mPainter;
56  mCoordTransform = rh.mCoordTransform;
57  mExtent = rh.mExtent;
58  mMapToPixel = rh.mMapToPixel;
59  mRenderingStopped = rh.mRenderingStopped;
60  mScaleFactor = rh.mScaleFactor;
61  mRendererScale = rh.mRendererScale;
62  mLabelingEngine = rh.mLabelingEngine;
63  mSelectionColor = rh.mSelectionColor;
64  mVectorSimplifyMethod = rh.mVectorSimplifyMethod;
65  mExpressionContext = rh.mExpressionContext;
66  mGeometry = rh.mGeometry;
67  mFeatureFilterProvider.reset( rh.mFeatureFilterProvider ? rh.mFeatureFilterProvider->clone() : nullptr );
68  mSegmentationTolerance = rh.mSegmentationTolerance;
69  mSegmentationToleranceType = rh.mSegmentationToleranceType;
70  return *this;
71 }
72 
74 {
75  QgsRenderContext context;
76  context.setPainter( painter );
77  if ( painter && painter->device() )
78  {
79  context.setScaleFactor( painter->device()->logicalDpiX() / 25.4 );
80  }
81  else
82  {
83  context.setScaleFactor( 3.465 ); //assume 88 dpi as standard value
84  }
85  return context;
86 }
87 
88 void QgsRenderContext::setFlags( QgsRenderContext::Flags flags )
89 {
90  mFlags = flags;
91 }
92 
94 {
95  if ( on )
96  mFlags |= flag;
97  else
98  mFlags &= ~flag;
99 }
100 
101 QgsRenderContext::Flags QgsRenderContext::flags() const
102 {
103  return mFlags;
104 }
105 
107 {
108  return mFlags.testFlag( flag );
109 }
110 
112 {
113  QgsRenderContext ctx;
114  ctx.setMapToPixel( mapSettings.mapToPixel() );
115  ctx.setExtent( mapSettings.visibleExtent() );
121  ctx.setSelectionColor( mapSettings.selectionColor() );
127  ctx.setScaleFactor( mapSettings.outputDpi() / 25.4 ); // = pixels per mm
128  ctx.setRendererScale( mapSettings.scale() );
129  ctx.setExpressionContext( mapSettings.expressionContext() );
130  ctx.setSegmentationTolerance( mapSettings.segmentationTolerance() );
132 
133  //this flag is only for stopping during the current rendering progress,
134  //so must be false at every new render operation
135  ctx.setRenderingStopped( false );
136 
137  return ctx;
138 }
139 
141 {
142  return mFlags.testFlag( ForceVectorOutput );
143 }
144 
146 {
147  return mFlags.testFlag( UseAdvancedEffects );
148 }
149 
151 {
152  setFlag( UseAdvancedEffects, enabled );
153 }
154 
156 {
157  return mFlags.testFlag( DrawEditingInfo );
158 }
159 
161 {
162  return mFlags.testFlag( DrawSelection );
163 }
164 
166 {
167  mCoordTransform = t;
168 }
169 
171 {
172  setFlag( DrawEditingInfo, b );
173 }
174 
176 {
177  setFlag( ForceVectorOutput, force );
178 }
179 
181 {
182  setFlag( DrawSelection, showSelection );
183 }
184 
186 {
187  return mFlags.testFlag( UseRenderingOptimization );
188 }
189 
191 {
192  setFlag( UseRenderingOptimization, enabled );
193 }
194 
196 {
197  if ( ffp )
198  {
199  mFeatureFilterProvider.reset( ffp->clone() );
200  }
201  else
202  {
203  mFeatureFilterProvider.reset( nullptr );
204  }
205 }
206 
208 {
209  return mFeatureFilterProvider.data();
210 }
void setForceVectorOutput(bool force)
Enable layer transparency and blending effects.
void setRenderingStopped(bool stopped)
Enable vector simplification and other rendering optimizations.
virtual QgsFeatureFilterProvider * clone() const =0
Create a clone of the feature filter provider.
const QgsExpressionContext & expressionContext() const
Gets the expression context.
void setFlags(QgsRenderContext::Flags flags)
Set combination of flags that will be used for rendering.
Use antialiasing while drawing.
void setCoordinateTransform(const QgsCoordinateTransform &t)
Sets coordinate transformation.
void setSimplifyHints(SimplifyHints simplifyHints)
Sets the simplification hints of the vector layer managed.
void setRendererScale(double scale)
Sets the renderer map scale.
Whether to make extra effort to update map image with partially rendered layers (better for interacti...
Flags flags() const
Return combination of flags used for rendering.
Enable layer transparency and blending effects.
Whether vector selections should be shown in the rendered map.
void setExtent(const QgsRectangle &extent)
QgsRectangle visibleExtent() const
Return the actual extent derived from requested extent that takes takes output image size into accoun...
const QgsFeatureFilterProvider * featureFilterProvider() const
Get the filter feature provider used for additional filtering of rendered features.
Vector graphics should not be cached and drawn as raster images.
The QgsMapSettings class contains configuration for rendering of the map.
void setSelectionColor(const QColor &color)
No simplification can be applied.
void setUseAdvancedEffects(bool enabled)
Used to enable or disable advanced effects such as blend modes.
void setScaleFactor(double factor)
Sets the scaling factor for the render to convert painter units to physical sizes.
QgsRenderContext & operator=(const QgsRenderContext &rh)
Whether vector selections should be shown in the rendered map.
double scale() const
Return the calculated scale of the map.
void setDrawEditingInformation(bool b)
Enable anti-aliasing for map rendering.
static QgsRenderContext fromQPainter(QPainter *painter)
Creates a default render context given a pixel based QPainter destination.
bool drawEditingInformation() const
Draw bounds of symbols (for debugging/testing)
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
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...
const QgsMapToPixel & mapToPixel() const
Draw bounds of symbols (for debugging/testing)
Vector graphics should not be cached and drawn as raster images.
Enable drawing of vertex markers for layers in editing mode.
QgsAbstractGeometry::SegmentationToleranceType segmentationToleranceType() const
Gets segmentation tolerance type (maximum angle or maximum difference between curve and approximation...
bool forceVectorOutput() const
void setSegmentationTolerance(double tolerance)
Sets the segmentation tolerance applied when rendering curved geometries.
bool useRenderingOptimization() const
Returns true if the rendering optimization (geometry simplification) can be executed.
Draw map such that there are no problems between adjacent tiles.
double outputDpi() const
Return DPI used for conversion between real world units (e.g.
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.
Whether to make extra effort to update map image with partially rendered layers (better for interacti...
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
bool useAdvancedEffects() const
Returns true if advanced effects such as blend modes such be used.
void setMapToPixel(const QgsMapToPixel &mtp)
Class for doing transforms between two map coordinate systems.
void setUseRenderingOptimization(bool enabled)
bool showSelection() const
Returns true if vector selections should be shown in the rendered map.
void setFlag(Flag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
void setFeatureFilterProvider(const QgsFeatureFilterProvider *ffp)
Set a filter feature provider used for additional filtering of rendered features. ...
Enable vector simplification and other rendering optimizations.
bool testFlag(Flag flag) const
Check whether a particular flag is enabled.
QColor selectionColor() const
Get color that is used for drawing of selected vector features.
void setShowSelection(const bool showSelection)
Sets whether vector selections should be shown in the rendered map.
bool testFlag(Flag flag) const
Check whether a particular flag is enabled.
Draw map such that there are no problems between adjacent tiles.
Flag
Enumeration of flags that affect rendering operations.
Enable drawing of vertex markers for layers in editing mode.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.