21 #include <QtConcurrentMap> 45 : mSettings( settings )
69 QHash<QgsMapLayer *, int> result;
73 result.insert( it.key(), it.value() );
88 QSet< QgsMapLayer * > labeledLayers;
131 static const double SPLIT_COORD = 180.0;
146 .arg( std::fabs( 1.0 - extent2.
width() / extent.
width() ) )
147 .arg( std::fabs( 1.0 - extent2.
height() / extent.
height() ) )
150 if ( std::fabs( 1.0 - extent2.
width() / extent.
width() ) < 0.5 &&
151 std::fabs( 1.0 - extent2.
height() / extent.
height() ) < 0.5 )
176 if ( ll.
x() > ur.
x() )
203 extent =
QgsRectangle( -DBL_MAX, -DBL_MAX, DBL_MAX, DBL_MAX );
212 extent =
QgsRectangle( -DBL_MAX, -DBL_MAX, DBL_MAX, DBL_MAX );
213 r2 =
QgsRectangle( -DBL_MAX, -DBL_MAX, DBL_MAX, DBL_MAX );
223 LayerRenderJobs layerJobs;
232 Q_UNUSED( cacheValid );
238 while ( li.hasPrevious() )
242 QgsDebugMsgLevel( QString(
"layer %1: minscale:%2 maxscale:%3 scaledepvis:%4 blendmode:%5" )
252 QgsDebugMsgLevel(
"Layer not rendered because it is not within the defined visibility scale range", 3 );
262 reprojectToLayerExtent( ml, ct, r1, r2 );
267 mErrors.append( Error( ml->
id(), tr(
"There was a problem transforming the layer's extent. Layer skipped." ) ) );
276 bool requiresLabeling =
false;
284 layerJobs.append( LayerRenderJob() );
285 LayerRenderJob &job = layerJobs.last();
295 job.renderingTime = -1;
299 job.context.setPainter( painter );
300 job.context.setLabelingEngine( labelingEngine2 );
301 job.context.setCoordinateTransform( ct );
302 job.context.setExtent( r1 );
304 if ( mFeatureFilterProvider )
305 job.context.setFeatureFilterProvider( mFeatureFilterProvider );
311 job.imageInitialized =
true;
313 job.renderer =
nullptr;
314 job.context.setPainter(
nullptr );
321 if (
mCache || !painter || needTemporaryImage( ml ) )
324 QImage *mypFlattenedImage =
nullptr;
328 if ( mypFlattenedImage->isNull() )
331 delete mypFlattenedImage;
332 layerJobs.removeLast();
336 job.img = mypFlattenedImage;
337 QPainter *mypPainter =
new QPainter( job.img );
339 job.context.setPainter( mypPainter );
343 if ( hasStyleOverride )
349 job.renderingTime = layerTime.elapsed();
351 if ( hasStyleOverride )
363 job.context.setPainter( painter );
364 job.context.setLabelingEngine( labelingEngine2 );
374 job.context.setPainter(
nullptr );
378 if ( canUseLabelCache && (
mCache || !painter ) )
381 QImage *mypFlattenedImage =
nullptr;
385 if ( mypFlattenedImage->isNull() )
388 delete mypFlattenedImage;
392 job.img = mypFlattenedImage;
403 for ( LayerRenderJobs::iterator it = jobs.begin(); it != jobs.end(); ++it )
405 LayerRenderJob &job = *it;
408 delete job.context.painter();
409 job.context.setPainter(
nullptr );
411 if (
mCache && !job.cached && !job.context.renderingStopped() && job.layer )
413 QgsDebugMsg(
"caching image for " + ( job.layer ? job.layer->id() : QString() ) );
423 Q_FOREACH (
const QString &message, job.renderer->errors() )
424 mErrors.append( Error( job.renderer->layerId(), message ) );
427 job.renderer =
nullptr;
441 if (
mCache && !job.cached && !job.context.renderingStopped() )
458 QPainter painter( &image );
461 for ( LayerRenderJobs::const_iterator it = jobs.constBegin(); it != jobs.constEnd(); ++it )
463 const LayerRenderJob &job = *it;
465 if ( job.layer && job.layer->customProperty( QStringLiteral(
"rendering/renderAboveLabels" ) ).toBool() )
468 if ( !job.imageInitialized )
471 painter.setCompositionMode( job.blendMode );
472 painter.setOpacity( job.opacity );
476 painter.drawImage( 0, 0, *job.img );
482 if ( labelJob.img && labelJob.complete )
484 painter.setCompositionMode( QPainter::CompositionMode_SourceOver );
485 painter.setOpacity( 1.0 );
486 painter.drawImage( 0, 0, *labelJob.img );
490 for ( LayerRenderJobs::const_iterator it = jobs.constBegin(); it != jobs.constEnd(); ++it )
492 const LayerRenderJob &job = *it;
494 if ( !job.layer || !job.layer->customProperty( QStringLiteral(
"rendering/renderAboveLabels" ) ).toBool() )
497 if ( !job.imageInitialized )
500 painter.setCompositionMode( job.blendMode );
501 painter.setOpacity( job.opacity );
505 painter.drawImage( 0, 0, *job.img );
515 if ( !settings.
value( QStringLiteral(
"Map/logCanvasRefreshEvent" ),
false ).toBool() )
518 QMultiMap<int, QString> elapsed;
519 Q_FOREACH (
const LayerRenderJob &job, jobs )
520 elapsed.insert( job.renderingTime, job.layer ? job.layer->id() : QString() );
522 elapsed.insert( labelJob.renderingTime, tr(
"Labeling" ) );
524 QList<int> tt( elapsed.uniqueKeys() );
525 std::sort( tt.begin(), tt.end(), std::greater<int>() );
526 Q_FOREACH (
int t, tt )
528 QgsMessageLog::logMessage( tr(
"%1 ms: %2" ).arg( t ).arg( QStringList( elapsed.values( t ) ).join( QStringLiteral(
", " ) ) ), tr(
"Rendering" ) );
bool restoreOverrideStyle()
Restore the original store after a call to setOverrideStyle()
QList< QgsMapLayer *> dependentLayers(const QString &cacheKey) const
Returns a list of map layers on which an image in the cache depends.
bool labelsEnabled() const
Returns whether the layer contains labels which are enabled and should be drawn.
A rectangle specified with double values.
Base class for all map layer types.
const LayerRenderJobs & jobs() const
Abstract base class for map rendering implementations.
void setXMaximum(double x)
Set the maximum x value.
This class is a composition of two QSettings instances:
void cleanupJobs(LayerRenderJobs &jobs)
A class to represent a 2D point.
QColor backgroundColor() const
Get the background color of the map.
void setCacheImage(const QString &cacheKey, const QImage &image, const QList< QgsMapLayer * > &dependentLayers=QList< QgsMapLayer * >())
Set the cached image for a particular cacheKey.
Errors errors() const
List of errors that happened during the rendering job - available when the rendering has been finishe...
bool isInScaleRange(double scale) const
Tests whether the layer should be visible at the specified scale.
QString toString(int precision=-1) const
Returns a string representation of the point (x, y) with a preset precision.
QList< QgsMapLayer * > layers() const
Get list of layers for map rendering The layers are stored in the reverse order of how they are rende...
static const QString LABEL_CACHE_ID
QgsMapRendererCache ID string for cached label image.
bool isEditable() const override
Returns true if the provider is in editing mode.
QgsMapLayer::LayerType type() const
Returns the type of the layer.
QgsRectangle visibleExtent() const
Return the actual extent derived from requested extent that takes takes output image size into accoun...
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning)
add a message to the instance (and create it if necessary)
void setCache(QgsMapRendererCache *cache)
Assign a cache to be used for reading and storing rendered images of individual layers.
The QgsMapSettings class contains configuration for rendering of the map.
QgsMapLayerStyleManager * styleManager() const
Get access to the layer's style manager.
static bool staticWillUseLayer(QgsVectorLayer *layer)
called to find out whether the layer is used for labeling
QgsCoordinateTransform layerTransform(const QgsMapLayer *layer) const
Return coordinate transform from layer's CRS to destination CRS.
static QImage composeImage(const QgsMapSettings &settings, const LayerRenderJobs &jobs, const LabelRenderJob &labelJob)
LabelRenderJob prepareLabelingJob(QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache=true)
Prepares a labeling job.
bool prepareLabelCache() const
Prepares the cache for storing the result of labeling.
virtual bool requiresAdvancedEffects() const =0
Returns true if drawing labels requires advanced effects like composition modes, which could prevent ...
QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime
Render time (in ms) per layer, by layer ID.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
#define QgsDebugMsgLevel(str, level)
bool init(const QgsRectangle &extent, double scale)
Initialize cache: set new parameters and clears the cache if any parameters have changed since last i...
double scale() const
Returns the calculated map scale.
double width() const
Returns the width of the rectangle.
Enable anti-aliasing for map rendering.
QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
QImage::Format outputImageFormat() const
format of internal QImage, default QImage::Format_ARGB32_Premultiplied
QgsCoordinateReferenceSystem crs() const
Returns the layer's spatial reference system.
QImage cacheImage(const QString &cacheKey) const
Returns the cached image for the specified cacheKey.
bool isGeographic() const
Returns whether the CRS is a geographic CRS (using lat/lon coordinates)
const QgsAbstractVectorLayerLabeling * labeling() const
Access to const labeling configuration.
double minimumScale() const
Returns the minimum map scale (i.e.
double maximumScale() const
Returns the maximum map scale (i.e.
QHash< QgsMapLayer *, int > perLayerRenderingTime() const
Returns the render time (in ms) per layer.
bool isFinite() const
Returns true if the rectangle has finite boundaries.
void cleanupLabelJob(LabelRenderJob &job)
Handles clean up tasks for a label job, including deletion of images and storing cached label results...
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
bool hasCacheImage(const QString &cacheKey) const
Returns true if the cache contains an image with the specified cacheKey.
The QgsLabelingEngine class provides map labeling functionality.
QMap< QString, QString > layerStyleOverrides() const
Get map of map layer style overrides (key: layer ID, value: style name) where a different style shoul...
QList< QgsMapRendererJob::Error > Errors
LayerRenderJobs prepareJobs(QPainter *painter, QgsLabelingEngine *labelingEngine2)
void logRenderingTime(const LayerRenderJobs &jobs, const LabelRenderJob &labelJob)
bool hasScaleBasedVisibility() const
Returns whether scale based visibility is enabled for the layer.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
void clearCacheImage(const QString &cacheKey)
Removes an image from the cache with matching cacheKey.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
virtual QgsMapLayerRenderer * createMapRenderer(QgsRenderContext &rendererContext)=0
Return new instance of QgsMapLayerRenderer that will be used for rendering of given context...
QgsMapRendererQImageJob(const QgsMapSettings &settings)
double yMaximum() const
Returns the y maximum value (top side of rectangle).
This class is responsible for keeping cache of rendered images resulting from a map rendering job...
bool testFlag(Flag flag) const
Check whether a particular flag is enabled.
bool setOverrideStyle(const QString &styleDef)
Temporarily apply a different style to the layer.
Custom exception class for Coordinate Reference System related exceptions.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
Represents a vector layer which manages a vector based data sets.
const QgsMapSettings & mapSettings() const
Return map settings with which this job was started.
QgsMapRendererCache * mCache
QSize outputSize() const
Return the size of the resulting map image.
QgsMapRendererJob(const QgsMapSettings &settings)
void setXMinimum(double x)
Set the minimum x value.
QPainter::CompositionMode blendMode() const
Returns the current blending mode for a layer.
double height() const
Returns the height of the rectangle.