Skip to content

Commit

Permalink
Add method for a callout to obtain a transform from the original
Browse files Browse the repository at this point in the history
associated layer's crs to the destination map crs
  • Loading branch information
nyalldawson committed Feb 23, 2021
1 parent 07a21af commit d2e0e8d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
28 changes: 25 additions & 3 deletions python/core/auto_generated/callouts/qgscallout.sip.in
Expand Up @@ -183,9 +183,31 @@ Returns the desired drawing order (stacking) to use while rendering this callout
The default order is QgsCallout.OrderBelowIndividualLabels.
%End

struct QgsCalloutContext
{
bool allFeaturePartsLabeled;
class QgsCalloutContext
{
%Docstring
Contains additional contextual information about the context in which a callout is
being rendered.

.. versionadded:: 3.10
%End

%TypeHeaderCode
#include "qgscallout.h"
%End
public:
bool allFeaturePartsLabeled;

QgsCoordinateReferenceSystem originalFeatureCrs;

QgsCoordinateTransform originalFeatureToMapTransform( const QgsRenderContext &renderContext ) const;
%Docstring
Returns the coordinate transform to convert from the original layer associated with
the callout to the destination map CRS.

.. versionadded:: 3.20
%End

};

void render( QgsRenderContext &context, QRectF rect, const double angle, const QgsGeometry &anchor, QgsCalloutContext &calloutContext );
Expand Down
10 changes: 10 additions & 0 deletions src/core/callouts/qgscallout.cpp
Expand Up @@ -694,3 +694,13 @@ void QgsManhattanLineCallout::draw( QgsRenderContext &context, QRectF rect, cons
drawCalloutLine( part );
}
}

QgsCoordinateTransform QgsCallout::QgsCalloutContext::originalFeatureToMapTransform( const QgsRenderContext &renderContext ) const
{
if ( !mOriginalFeatureToMapTransform.isValid() )
{
// lazy initialization, only create if needed...
mOriginalFeatureToMapTransform = QgsCoordinateTransform( originalFeatureCrs, renderContext.coordinateTransform().destinationCrs(), renderContext.transformContext() );
}
return mOriginalFeatureToMapTransform;
}
26 changes: 23 additions & 3 deletions src/core/callouts/qgscallout.h
Expand Up @@ -211,10 +211,30 @@ class CORE_EXPORT QgsCallout
* \ingroup core
* \since QGIS 3.10
*/
struct CORE_EXPORT QgsCalloutContext
class CORE_EXPORT QgsCalloutContext
{
//! TRUE if all parts of associated feature were labeled
bool allFeaturePartsLabeled = false;
public:
//! TRUE if all parts of associated feature were labeled
bool allFeaturePartsLabeled = false;

/**
* Contains the CRS of the original feature associated with this callout.
*
* \since QGIS 3.18
*/
QgsCoordinateReferenceSystem originalFeatureCrs;

/**
* Returns the coordinate transform to convert from the original layer associated with
* the callout to the destination map CRS.
*
* \since QGIS 3.20
*/
QgsCoordinateTransform originalFeatureToMapTransform( const QgsRenderContext &renderContext ) const;

private:
//! Lazy initialized coordinate transform from original feature CRS to map CRS
mutable QgsCoordinateTransform mOriginalFeatureToMapTransform;
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/core/labeling/qgsvectorlayerlabelprovider.cpp
Expand Up @@ -330,6 +330,7 @@ void QgsVectorLayerLabelProvider::drawCallout( QgsRenderContext &context, pal::L
g.transform( xform.transform() );
QgsCallout::QgsCalloutContext calloutContext;
calloutContext.allFeaturePartsLabeled = label->getFeaturePart()->feature()->labelAllParts();
calloutContext.originalFeatureCrs = label->getFeaturePart()->feature()->originalFeatureCrs();
mSettings.callout()->render( context, rect, label->getAlpha() * 180 / M_PI, g, calloutContext );
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsmaprendererjob.cpp
Expand Up @@ -649,6 +649,9 @@ LabelRenderJob QgsMapRendererJob::prepareLabelingJob( QPainter *painter, QgsLabe
job.context.setLabelingEngine( labelingEngine2 );
job.context.setExtent( mSettings.visibleExtent() );
job.context.setFeatureFilterProvider( mFeatureFilterProvider );
QgsCoordinateTransform ct;
ct.setDestinationCrs( mSettings.destinationCrs() );
job.context.setCoordinateTransform( ct );

// if we can use the cache, let's do it and avoid rendering!
bool hasCache = canUseLabelCache && mCache && mCache->hasCacheImage( LABEL_CACHE_ID );
Expand Down

0 comments on commit d2e0e8d

Please sign in to comment.