Skip to content

Commit

Permalink
[pal] Add api to allow setting the outer bounds for a label feature
Browse files Browse the repository at this point in the history
The outer bounds can represent the extreme margins of a label, including
non-text parts like borders or padded backgrounds. During the label
solving the outer bounds will be used when detecting whether
labels are in conflict.
  • Loading branch information
nyalldawson committed Nov 8, 2022
1 parent 8a121e3 commit 5093ae9
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 121 deletions.
20 changes: 20 additions & 0 deletions src/core/labeling/qgslabelfeature.h
Expand Up @@ -111,6 +111,24 @@ class CORE_EXPORT QgsLabelFeature
//! Size of the label (in map units)
QSizeF size( double angle = 0.0 ) const;

/**
* Returns the extreme outer bounds of the label feature, including any surrounding content like
* borders or background shapes.
*
* \see setOuterBounds()
* \since QGIS 3.30
*/
QRectF outerBounds() const { return mOuterBounds; }

/**
* Sets the extreme outer \a bounds of the label feature, including any surrounding content like
* borders or background shapes.
*
* \see outerBounds()
* \since QGIS 3.30
*/
void setOuterBounds( const QRectF &bounds ) { mOuterBounds = bounds; }

/**
* Sets the visual margin for the label feature. The visual margin represents a margin
* within the label which should not be considered when calculating the positions of candidates
Expand Down Expand Up @@ -627,6 +645,8 @@ class CORE_EXPORT QgsLabelFeature
QSizeF mSize;
//! Width and height of the label when rotated between 45 to 135 and 235 to 315 degrees;
QSizeF mRotatedSize;
//! Extreme outer bounds of the label feature, including any surrounding content like borders or background shapes.
QRectF mOuterBounds;
//! Visual margin of label contents
QgsMargins mVisualMargin;
//! Size of associated rendered symbol, if applicable
Expand Down
21 changes: 21 additions & 0 deletions src/core/labeling/qgsvectorlayerlabelprovider.cpp
Expand Up @@ -569,6 +569,25 @@ void QgsVectorLayerLabelProvider::drawLabelPrivate( pal::LabelPosition *label, Q
painter->drawLine( QPointF( rect.left(), rect.bottom() + bottomMargin ), QPointF( rect.right(), rect.bottom() + bottomMargin ) );
}

const QRectF outerBounds = label->getFeaturePart()->feature()->outerBounds();
if ( !outerBounds.isNull() )
{
const QRectF mapOuterBounds = QRectF( label->getX() + outerBounds.left(),
label->getY() + outerBounds.top(),
outerBounds.width(), outerBounds.height() );

QgsPointXY outerBoundsPt1 = xform.transform( mapOuterBounds.left(), mapOuterBounds.top() );
QgsPointXY outerBoundsPt2 = xform.transform( mapOuterBounds.right(), mapOuterBounds.bottom() );

const QRectF outerBoundsPixel( outerBoundsPt1.x() - outPt.x(),
outerBoundsPt1.y() - outPt.y(),
outerBoundsPt2.x() - outerBoundsPt1.x(),
outerBoundsPt2.y() - outerBoundsPt1.y() );

painter->setPen( QColor( 255, 0, 255, 140 ) );
painter->drawRect( outerBoundsPixel );
}

if ( QgsTextLabelFeature *textFeature = dynamic_cast< QgsTextLabelFeature * >( label->getFeaturePart()->feature() ) )
{
const QgsTextDocumentMetrics &metrics = textFeature->documentMetrics();
Expand All @@ -583,6 +602,8 @@ void QgsVectorLayerLabelProvider::drawLabelPrivate( pal::LabelPosition *label, Q
painter->drawLine( QPointF( rect.left(), rect.top() + blockBaseLine ), QPointF( rect.right(), rect.top() + blockBaseLine ) );
}
}


painter->restore();
}

Expand Down
3 changes: 1 addition & 2 deletions src/core/pal/costcalculator.cpp
Expand Up @@ -16,7 +16,6 @@
#include "layer.h"
#include "pal.h"
#include "feature.h"
#include "geomfunction.h"
#include "labelposition.h"
#include "util.h"
#include "costcalculator.h"
Expand All @@ -40,7 +39,7 @@ void CostCalculator::addObstacleCostPenalty( LabelPosition *lp, FeaturePart *obs
{
case GEOS_POINT:

dist = lp->getDistanceToPoint( obstacle->x[0], obstacle->y[0] );
dist = lp->getDistanceToPoint( obstacle->x[0], obstacle->y[0], true );
if ( dist < 0 )
n = 2;
else if ( dist < distlabel )
Expand Down
2 changes: 1 addition & 1 deletion src/core/pal/feature.cpp
Expand Up @@ -2040,7 +2040,7 @@ std::size_t FeaturePart::createCandidatesOutsidePolygon( std::vector<std::unique
// here we deviate a little from R&R, and instead of just calculating the centroid distance
// to centroid of label, we calculate the distance from the centroid to the nearest point on the label

const double centroidDistance = candidate->getDistanceToPoint( cx, cy );
const double centroidDistance = candidate->getDistanceToPoint( cx, cy, false );
const double centroidCost = centroidDistance / estimateOfMaxPossibleDistanceCentroidToLabel;
candidate->setCost( centroidCost );

Expand Down

0 comments on commit 5093ae9

Please sign in to comment.