QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include "qgsannotationlayer.h"
19 #include "qgsfeedback.h"
21 #include "qgspainteffect.h"
22 #include "qgsrendercontext.h"
23 #include <optional>
24 
26  : QgsMapLayerRenderer( layer->id(), &context )
27  , mFeedback( std::make_unique< QgsFeedback >() )
28  , mLayerOpacity( layer->opacity() )
29 {
30  // Clone items from layer which fall inside the rendered extent
31  // Because some items have scale dependent bounds, we have to accept some limitations here.
32  // first, we can use the layer's spatial index to very quickly retrieve items we know will fall within the visible
33  // extent. This will ONLY apply to items which have a non-scale-dependent bounding box though.
34 
35  QSet< QString > items = qgis::listToSet( layer->queryIndex( context.extent() ) );
36 
37  // we also have NO choice but to clone ALL non-indexed items (i.e. those with a scale-dependent bounding box)
38  // since these won't be in the layer's spatial index, and it's too expensive to determine their actual bounding box
39  // upfront (we are blocking the main thread right now!)
40 
41  // TODO -- come up with some brilliant way to avoid this and also index scale-dependent items ;)
42  items.unite( layer->mNonIndexedItems );
43 
44  mItems.reserve( items.size() );
45  std::transform( items.begin(), items.end(), std::back_inserter( mItems ),
46  [layer]( const QString & id ) ->std::pair< QString, std::unique_ptr< QgsAnnotationItem > >
47  {
48  return std::make_pair( id, std::unique_ptr< QgsAnnotationItem >( layer->item( id )->clone() ) );
49  } );
50 
51  std::sort( mItems.begin(), mItems.end(), [](
52  const std::pair< QString, std::unique_ptr< QgsAnnotationItem > > &a,
53  const std::pair< QString, std::unique_ptr< QgsAnnotationItem > > &b )
54  { return a.second->zIndex() < b.second->zIndex(); } );
55 
56  if ( layer->paintEffect() && layer->paintEffect()->enabled() )
57  {
58  mPaintEffect.reset( layer->paintEffect()->clone() );
59  }
60 }
61 
63 
65 {
66  return mFeedback.get();
67 }
68 
70 {
71  QgsRenderContext &context = *renderContext();
72 
73  if ( mPaintEffect )
74  {
75  mPaintEffect->begin( context );
76  }
77 
78  bool canceled = false;
79  for ( const std::pair< QString, std::unique_ptr< QgsAnnotationItem > > &item : std::as_const( mItems ) )
80  {
81  if ( mFeedback->isCanceled() )
82  {
83  canceled = true;
84  break;
85  }
86 
87  std::optional< QgsScopedRenderContextReferenceScaleOverride > referenceScaleOverride;
88  if ( item.second->useSymbologyReferenceScale() )
89  {
90  referenceScaleOverride.emplace( QgsScopedRenderContextReferenceScaleOverride( context, item.second->symbologyReferenceScale() ) );
91  }
92 
93  const QgsRectangle bounds = item.second->boundingBox( context );
94  if ( bounds.intersects( context.extent() ) )
95  {
96  item.second->render( context, mFeedback.get() );
97  std::unique_ptr< QgsRenderedAnnotationItemDetails > details = std::make_unique< QgsRenderedAnnotationItemDetails >( mLayerID, item.first );
98  details->setBoundingBox( bounds );
99  appendRenderedItemDetails( details.release() );
100  }
101  }
102 
103  if ( mPaintEffect )
104  {
105  mPaintEffect->end( context );
106  }
107 
108  return !canceled;
109 }
110 
112 {
113  return renderContext()->testFlag( Qgis::RenderContextFlag::UseAdvancedEffects ) && ( !qgsDoubleNear( mLayerOpacity, 1.0 ) );
114 }
QgsRectangle::intersects
bool intersects(const QgsRectangle &rect) const SIP_HOLDGIL
Returns true when rectangle intersects with other rectangle.
Definition: qgsrectangle.h:349
QgsAnnotationLayerRenderer::QgsAnnotationLayerRenderer
QgsAnnotationLayerRenderer(QgsAnnotationLayer *layer, QgsRenderContext &context)
Constructor for a QgsAnnotationLayerRenderer, for the specified layer.
Definition: qgsannotationlayerrenderer.cpp:25
QgsMapLayerRenderer::mLayerID
QString mLayerID
Definition: qgsmaplayerrenderer.h:153
QgsScopedRenderContextReferenceScaleOverride
Scoped object for temporary override of the symbologyReferenceScale property of a QgsRenderContext.
Definition: qgsrendercontext.h:1375
qgsrenderedannotationitemdetails.h
QgsRenderContext::testFlag
bool testFlag(Qgis::RenderContextFlag flag) const
Check whether a particular flag is enabled.
Definition: qgsrendercontext.cpp:229
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsMapLayerRenderer::renderContext
QgsRenderContext * renderContext()
Returns the render context associated with the renderer.
Definition: qgsmaplayerrenderer.h:111
qgsannotationlayerrenderer.h
QgsAnnotationLayerRenderer::forceRasterRender
bool forceRasterRender() const override
Returns true if the renderer must be rendered to a raster paint device (e.g.
Definition: qgsannotationlayerrenderer.cpp:111
qgspainteffect.h
QgsRenderContext::extent
const QgsRectangle & extent() const
When rendering a map layer, calling this method returns the "clipping" extent for the layer (in the l...
Definition: qgsrendercontext.h:239
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsMapLayerRenderer
Base class for utility classes that encapsulate information necessary for rendering of map layers.
Definition: qgsmaplayerrenderer.h:54
QgsAnnotationLayerRenderer::render
bool render() override
Do the rendering (based on data stored in the class).
Definition: qgsannotationlayerrenderer.cpp:69
QgsAnnotationLayerRenderer::~QgsAnnotationLayerRenderer
~QgsAnnotationLayerRenderer() override
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2265
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsPaintEffect::clone
virtual QgsPaintEffect * clone() const =0
Duplicates an effect by creating a deep copy of the effect.
QgsAnnotationLayerRenderer::feedback
QgsFeedback * feedback() const override
Access to feedback object of the layer renderer (may be nullptr)
Definition: qgsannotationlayerrenderer.cpp:64
qgsrendercontext.h
qgsannotationlayer.h
QgsAnnotationLayer::paintEffect
QgsPaintEffect * paintEffect() const
Returns the current paint effect for the layer.
Definition: qgsannotationlayer.cpp:546
QgsRenderContext::symbologyReferenceScale
double symbologyReferenceScale() const
Returns the symbology reference scale.
Definition: qgsrendercontext.h:362
QgsAnnotationLayer
Represents a map layer containing a set of georeferenced annotations, e.g. markers,...
Definition: qgsannotationlayer.h:46
QgsMapLayerRenderer::appendRenderedItemDetails
void appendRenderedItemDetails(QgsRenderedItemDetails *details)
Appends the details of a rendered item to the renderer.
Definition: qgsmaplayerrenderer.cpp:27
QgsPaintEffect::enabled
bool enabled() const
Returns whether the effect is enabled.
Definition: qgspainteffect.h:197
qgsfeedback.h
Qgis::RenderContextFlag::UseAdvancedEffects
@ UseAdvancedEffects
Enable layer opacity and blending effects.