QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
qgsannotationlayerrenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsannotationlayerrenderer.cpp
3 ------------------
4 copyright : (C) 2019 by Sandro Mani
5 email : smani at sourcepole dot ch
6 ***************************************************************************/
7
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18
19#include <optional>
20
21#include "qgsannotationlayer.h"
22#include "qgsfeedback.h"
23#include "qgspainteffect.h"
24#include "qgsrendercontext.h"
26#include "qgsthreadingutils.h"
27
29 : QgsMapLayerRenderer( layer->id(), &context )
30 , mFeedback( std::make_unique< QgsFeedback >() )
31 , mLayerName( layer->name() )
32 , mLayerOpacity( layer->opacity() )
33 , mLayerBlendMode( layer->blendMode() )
34{
35 if ( QgsMapLayer *linkedLayer = layer->linkedVisibilityLayer() )
36 {
37 if ( !context.customProperties().value( QStringLiteral( "visible_layer_ids" ) ).toList().contains( linkedLayer->id() ) )
38 {
39 mReadyToCompose = true;
40 return;
41 }
42 }
43
44 // Clone items from layer which fall inside the rendered extent
45 // Because some items have scale dependent bounds, we have to accept some limitations here.
46 // first, we can use the layer's spatial index to very quickly retrieve items we know will fall within the visible
47 // extent. This will ONLY apply to items which have a non-scale-dependent bounding box though.
48
49 const QStringList itemsList = layer->queryIndex( context.extent() );
50 QSet< QString > items( itemsList.begin(), itemsList.end() );
51
52 // we also have NO choice but to clone ALL non-indexed items (i.e. those with a scale-dependent bounding box)
53 // since these won't be in the layer's spatial index, and it's too expensive to determine their actual bounding box
54 // upfront (we are blocking the main thread right now!)
55
56 // TODO -- come up with some brilliant way to avoid this and also index scale-dependent items ;)
57 items.unite( layer->mNonIndexedItems );
58
59 mItems.reserve( items.size() );
60 std::transform( items.begin(), items.end(), std::back_inserter( mItems ),
61 [layer]( const QString & id ) ->std::pair< QString, std::unique_ptr< QgsAnnotationItem > >
62 {
63 return std::make_pair( id, std::unique_ptr< QgsAnnotationItem >( layer->item( id )->clone() ) );
64 } );
65
66 std::sort( mItems.begin(), mItems.end(), [](
67 const std::pair< QString, std::unique_ptr< QgsAnnotationItem > > &a,
68 const std::pair< QString, std::unique_ptr< QgsAnnotationItem > > &b )
69 { return a.second->zIndex() < b.second->zIndex(); } );
70
71 if ( layer->paintEffect() && layer->paintEffect()->enabled() )
72 {
73 mPaintEffect.reset( layer->paintEffect()->clone() );
74 }
75}
76
78
80{
81 return mFeedback.get();
82}
83
85{
86 QgsScopedThreadName threadName( QStringLiteral( "render:%1" ).arg( mLayerName ) );
87
88 QgsRenderContext &context = *renderContext();
89
90 if ( mPaintEffect )
91 {
92 mPaintEffect->begin( context );
93 }
94
95 bool canceled = false;
96 for ( const std::pair< QString, std::unique_ptr< QgsAnnotationItem > > &item : std::as_const( mItems ) )
97 {
98 if ( mFeedback->isCanceled() )
99 {
100 canceled = true;
101 break;
102 }
103
104 if ( !item.second->enabled() )
105 continue;
106
107 std::optional< QgsScopedRenderContextReferenceScaleOverride > referenceScaleOverride;
108 if ( item.second->useSymbologyReferenceScale() && item.second->flags() & Qgis::AnnotationItemFlag::SupportsReferenceScale )
109 {
110 referenceScaleOverride.emplace( QgsScopedRenderContextReferenceScaleOverride( context, item.second->symbologyReferenceScale() ) );
111 }
112
113 const QgsRectangle bounds = item.second->boundingBox( context );
114 if ( bounds.intersects( context.extent() ) )
115 {
116 item.second->render( context, mFeedback.get() );
117 auto details = std::make_unique< QgsRenderedAnnotationItemDetails >( mLayerID, item.first );
118 details->setBoundingBox( bounds );
119 appendRenderedItemDetails( details.release() );
120 }
121 }
122
123 if ( mPaintEffect )
124 {
125 mPaintEffect->end( context );
126 }
127
128 return !canceled;
129}
130
132{
133 switch ( renderContext()->rasterizedRenderingPolicy() )
134 {
137 break;
138
140 return false;
141 }
142
143 if ( !qgsDoubleNear( mLayerOpacity, 1.0 ) )
144 return true;
145
146 if ( mLayerBlendMode != QPainter::CompositionMode_SourceOver )
147 return true;
148
149 return false;
150}
@ Default
Allow raster-based rendering in situations where it is required for correct rendering or where it wil...
Definition qgis.h:2704
@ PreferVector
Prefer vector-based rendering, when the result will still be visually near-identical to a raster-base...
Definition qgis.h:2705
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
Definition qgis.h:2706
@ SupportsReferenceScale
Item supports reference scale based rendering.
Definition qgis.h:2466
bool forceRasterRender() const override
Returns true if the renderer must be rendered to a raster paint device (e.g.
~QgsAnnotationLayerRenderer() override
bool render() override
Do the rendering (based on data stored in the class).
QgsFeedback * feedback() const override
Access to feedback object of the layer renderer (may be nullptr).
QgsAnnotationLayerRenderer(QgsAnnotationLayer *layer, QgsRenderContext &context)
Constructor for a QgsAnnotationLayerRenderer, for the specified layer.
Represents a map layer containing a set of georeferenced annotations, e.g.
QgsMapLayer * linkedVisibilityLayer()
Returns a linked layer, where the items in this annotation layer will only be visible when the linked...
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
void appendRenderedItemDetails(QgsRenderedItemDetails *details)
Appends the details of a rendered item to the renderer.
QgsRenderContext * renderContext()
Returns the render context associated with the renderer.
QgsMapLayerRenderer(const QString &layerID, QgsRenderContext *context=nullptr)
Constructor for QgsMapLayerRenderer, with the associated layerID and render context.
Base class for all map layer types.
Definition qgsmaplayer.h:80
A rectangle specified with double values.
bool intersects(const QgsRectangle &rect) const
Returns true when rectangle intersects with other rectangle.
Contains information about the context of a rendering operation.
double symbologyReferenceScale() const
Returns the symbology reference scale.
const QgsRectangle & extent() const
When rendering a map layer, calling this method returns the "clipping" extent for the layer (in the l...
Scoped object for temporary override of the symbologyReferenceScale property of a QgsRenderContext.
Scoped object for setting the current thread name.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6607