QGIS API Documentation  2.99.0-Master (23ddace)
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 
26 #define POINTS_TO_MM 2.83464567
27 #define INCH_TO_MM 25.4
28 
30  : mFlags( DrawEditingInfo | UseAdvancedEffects | DrawSelection | UseRenderingOptimization )
31 {
33 }
34 
36  : mFlags( rh.mFlags )
37  , mPainter( rh.mPainter )
38  , mCoordTransform( rh.mCoordTransform )
39  , mExtent( rh.mExtent )
40  , mMapToPixel( rh.mMapToPixel )
41  , mRenderingStopped( rh.mRenderingStopped )
42  , mScaleFactor( rh.mScaleFactor )
43  , mRendererScale( rh.mRendererScale )
44  , mLabelingEngine( rh.mLabelingEngine )
45  , mSelectionColor( rh.mSelectionColor )
46  , mVectorSimplifyMethod( rh.mVectorSimplifyMethod )
47  , mExpressionContext( rh.mExpressionContext )
48  , mGeometry( rh.mGeometry )
49  , mFeatureFilterProvider( rh.mFeatureFilterProvider ? rh.mFeatureFilterProvider->clone() : nullptr )
50  , mSegmentationTolerance( rh.mSegmentationTolerance )
51  , mSegmentationToleranceType( rh.mSegmentationToleranceType )
52 {
53 }
54 
56 {
57  mFlags = rh.mFlags;
58  mPainter = rh.mPainter;
59  mCoordTransform = rh.mCoordTransform;
60  mExtent = rh.mExtent;
61  mMapToPixel = rh.mMapToPixel;
62  mRenderingStopped = rh.mRenderingStopped;
63  mScaleFactor = rh.mScaleFactor;
64  mRendererScale = rh.mRendererScale;
65  mLabelingEngine = rh.mLabelingEngine;
66  mSelectionColor = rh.mSelectionColor;
67  mVectorSimplifyMethod = rh.mVectorSimplifyMethod;
68  mExpressionContext = rh.mExpressionContext;
69  mGeometry = rh.mGeometry;
70  mFeatureFilterProvider.reset( rh.mFeatureFilterProvider ? rh.mFeatureFilterProvider->clone() : nullptr );
71  mSegmentationTolerance = rh.mSegmentationTolerance;
72  mSegmentationToleranceType = rh.mSegmentationToleranceType;
73  return *this;
74 }
75 
77 {
78  QgsRenderContext context;
79  context.setPainter( painter );
80  if ( painter && painter->device() )
81  {
82  context.setScaleFactor( painter->device()->logicalDpiX() / 25.4 );
83  }
84  else
85  {
86  context.setScaleFactor( 3.465 ); //assume 88 dpi as standard value
87  }
88  return context;
89 }
90 
91 void QgsRenderContext::setFlags( QgsRenderContext::Flags flags )
92 {
93  mFlags = flags;
94 }
95 
97 {
98  if ( on )
99  mFlags |= flag;
100  else
101  mFlags &= ~flag;
102 }
103 
104 QgsRenderContext::Flags QgsRenderContext::flags() const
105 {
106  return mFlags;
107 }
108 
110 {
111  return mFlags.testFlag( flag );
112 }
113 
115 {
116  QgsRenderContext ctx;
117  ctx.setMapToPixel( mapSettings.mapToPixel() );
118  ctx.setExtent( mapSettings.visibleExtent() );
124  ctx.setSelectionColor( mapSettings.selectionColor() );
130  ctx.setScaleFactor( mapSettings.outputDpi() / 25.4 ); // = pixels per mm
131  ctx.setRendererScale( mapSettings.scale() );
132  ctx.setExpressionContext( mapSettings.expressionContext() );
133  ctx.setSegmentationTolerance( mapSettings.segmentationTolerance() );
135 
136  //this flag is only for stopping during the current rendering progress,
137  //so must be false at every new render operation
138  ctx.setRenderingStopped( false );
139 
140  return ctx;
141 }
142 
144 {
145  return mFlags.testFlag( ForceVectorOutput );
146 }
147 
149 {
150  return mFlags.testFlag( UseAdvancedEffects );
151 }
152 
154 {
155  setFlag( UseAdvancedEffects, enabled );
156 }
157 
159 {
160  return mFlags.testFlag( DrawEditingInfo );
161 }
162 
164 {
165  return mFlags.testFlag( DrawSelection );
166 }
167 
169 {
170  mCoordTransform = t;
171 }
172 
174 {
175  setFlag( DrawEditingInfo, b );
176 }
177 
179 {
180  setFlag( ForceVectorOutput, force );
181 }
182 
184 {
185  setFlag( DrawSelection, showSelection );
186 }
187 
189 {
190  return mFlags.testFlag( UseRenderingOptimization );
191 }
192 
194 {
195  setFlag( UseRenderingOptimization, enabled );
196 }
197 
199 {
200  if ( ffp )
201  {
202  mFeatureFilterProvider.reset( ffp->clone() );
203  }
204  else
205  {
206  mFeatureFilterProvider.reset( nullptr );
207  }
208 }
209 
211 {
212  return mFeatureFilterProvider.get();
213 }
214 
216 {
217  double conversionFactor = 1.0;
218  switch ( unit )
219  {
221  conversionFactor = mScaleFactor;
222  break;
223 
225  conversionFactor = mScaleFactor / POINTS_TO_MM;
226  break;
227 
229  conversionFactor = mScaleFactor * INCH_TO_MM;
230  break;
231 
233  {
234  double mup = scale.computeMapUnitsPerPixel( *this );
235  if ( mup > 0 )
236  {
237  conversionFactor = 1.0 / mup;
238  }
239  else
240  {
241  conversionFactor = 1.0;
242  }
243  break;
244  }
246  conversionFactor = 1.0;
247  break;
248 
251  //no sensible value
252  conversionFactor = 1.0;
253  break;
254  }
255 
256  double convertedSize = size * conversionFactor;
257 
258  if ( unit == QgsUnitTypes::RenderMapUnits )
259  {
260  //check max/min size
261  if ( scale.minSizeMMEnabled )
262  convertedSize = qMax( convertedSize, scale.minSizeMM * mScaleFactor );
263  if ( scale.maxSizeMMEnabled )
264  convertedSize = qMin( convertedSize, scale.maxSizeMM * mScaleFactor );
265  }
266 
267  return convertedSize;
268 }
269 
270 double QgsRenderContext::convertToMapUnits( double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale ) const
271 {
272  double mup = mMapToPixel.mapUnitsPerPixel();
273 
274  switch ( unit )
275  {
277  {
278  // check scale
279  double minSizeMU = -DBL_MAX;
280  if ( scale.minSizeMMEnabled )
281  {
282  minSizeMU = scale.minSizeMM * mScaleFactor * mup;
283  }
284  if ( !qgsDoubleNear( scale.minScale, 0.0 ) )
285  {
286  minSizeMU = qMax( minSizeMU, size * ( mRendererScale / scale.minScale ) );
287  }
288  size = qMax( size, minSizeMU );
289 
290  double maxSizeMU = DBL_MAX;
291  if ( scale.maxSizeMMEnabled )
292  {
293  maxSizeMU = scale.maxSizeMM * mScaleFactor * mup;
294  }
295  if ( !qgsDoubleNear( scale.maxScale, 0.0 ) )
296  {
297  maxSizeMU = qMin( maxSizeMU, size * ( mRendererScale / scale.maxScale ) );
298  }
299  size = qMin( size, maxSizeMU );
300 
301  return size;
302  }
304  {
305  return size * mScaleFactor * mup;
306  }
308  {
309  return size * mScaleFactor * mup / POINTS_TO_MM;
310  }
312  {
313  return size * mScaleFactor * mup * INCH_TO_MM;
314  }
316  {
317  return size * mup;
318  }
319 
322  //no sensible value
323  return 0.0;
324  }
325  return 0.0;
326 }
327 
328 double QgsRenderContext::convertFromMapUnits( double sizeInMapUnits, QgsUnitTypes::RenderUnit outputUnit ) const
329 {
330  double mup = mMapToPixel.mapUnitsPerPixel();
331 
332  switch ( outputUnit )
333  {
335  {
336  return sizeInMapUnits;
337  }
339  {
340  return sizeInMapUnits / ( mScaleFactor * mup );
341  }
343  {
344  return sizeInMapUnits / ( mScaleFactor * mup / POINTS_TO_MM );
345  }
347  {
348  return sizeInMapUnits / ( mScaleFactor * mup * INCH_TO_MM );
349  }
351  {
352  return sizeInMapUnits / mup;
353  }
354 
357  //no sensible value
358  return 0.0;
359  }
360  return 0.0;
361 }
void setForceVectorOutput(bool force)
double convertToMapUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to map units.
Enable layer opacity and blending effects.
void setRenderingStopped(bool stopped)
double minSizeMM
The minimum size in millimeters, or 0.0 if unset.
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.
double convertFromMapUnits(double sizeInMapUnits, QgsUnitTypes::RenderUnit outputUnit) const
Converts a size from map units to the specified units.
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...
double computeMapUnitsPerPixel(const QgsRenderContext &c) const
Computes a map units per pixel scaling factor, respecting the minimum and maximum scales set for the ...
Flags flags() const
Return combination of flags used for rendering.
Percentage of another measurement (e.g., canvas size, feature size)
Definition: qgsunittypes.h:100
Enable layer opacity 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...
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:203
double maxScale
The maximum scale, or 0.0 if unset.
const QgsFeatureFilterProvider * featureFilterProvider() const
Get the filter feature provider used for additional filtering of rendered features.
bool minSizeMMEnabled
Whether the minimum size in mm should be respected.
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)
#define INCH_TO_MM
Whether vector selections should be shown in the rendered map.
double scale() const
Returns the calculated map scale.
void setDrawEditingInformation(bool b)
Enable anti-aliasing for map rendering.
points (e.g., for font sizes)
Definition: qgsunittypes.h:102
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...
double mapUnitsPerPixel() const
Return current map units per pixel.
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 convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
#define POINTS_TO_MM
double segmentationTolerance() const
Gets the segmentation tolerance applied when rendering curved geometries.
QPainter * painter()
Returns the destination QPainter for the render operation.
double maxSizeMM
The maximum size in millimeters, or 0.0 if unset.
Struct for storing maximum and minimum scales for measurements in map units.
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.
double minScale
The minimum scale, or 0.0 if unset.
bool maxSizeMMEnabled
Whether the maximum size in mm should be respected.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:95
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.