Skip to content
Permalink
Browse files

Fix labeling offset

  • Loading branch information
m-kuhn committed Dec 24, 2019
1 parent 4f625ce commit f3e309406f60d4e9948c63343cb806021f70bbc2
@@ -1265,21 +1265,28 @@ void QgsDxfExport::writeText( const QString &layer, const QString &text, pal::La
double lblX = label->getX();
double lblY = label->getY();

QgsLabelFeature *labelFeature = label->getFeaturePart()->feature();
if ( labelFeature->hasFixedPosition() )
{
lblX = labelFeature->fixedPosition().x();
lblY = labelFeature->fixedPosition().y();
}

HAlign hali = HAlign::Undefined;
VAlign vali = VAlign::Undefined;

const QgsPropertyCollection &props = layerSettings.dataDefinedProperties();

if ( props.isActive( QgsPalLayerSettings::OffsetQuad ) )
{
lblX -= label->dX();
lblY -= label->dY();

const QVariant exprVal = props.value( QgsPalLayerSettings::OffsetQuad, expressionContext );
if ( exprVal.isValid() )
{
int offsetQuad = exprVal.toInt();

lblX -= label->dX();
lblY -= label->dY();

switch ( offsetQuad )
{
case 0: // Above Left
@@ -1328,6 +1335,9 @@ void QgsDxfExport::writeText( const QString &layer, const QString &text, pal::La

if ( props.isActive( QgsPalLayerSettings::Hali ) )
{
lblX -= label->dX();
lblY -= label->dY();

hali = HAlign::HLeft;
QVariant exprVal = props.value( QgsPalLayerSettings::Hali, expressionContext );
if ( exprVal.isValid() )
@@ -1344,8 +1354,6 @@ void QgsDxfExport::writeText( const QString &layer, const QString &text, pal::La
}
}

std::unique_ptr<QFontMetricsF> labelFontMetrics( new QFontMetricsF( layerSettings.format().font() ) );

//vertical alignment
if ( props.isActive( QgsPalLayerSettings::Vali ) )
{
@@ -114,3 +114,23 @@ void QgsLabelFeature::setObstacleSettings( const QgsLabelObstacleSettings &setti
{
mObstacleSettings = settings;
}

double QgsLabelFeature::dX() const
{
return mDX;
}

void QgsLabelFeature::setDX( double dX )
{
mDX = dX;
}

double QgsLabelFeature::dY() const
{
return mDY;
}

void QgsLabelFeature::setDY( double dY )
{
mDY = dY;
}
@@ -434,6 +434,47 @@ class CORE_EXPORT QgsLabelFeature
*/
void setObstacleSettings( const QgsLabelObstacleSettings &settings );

/**
* The deltas (dX and dY) are the difference by which the fixed position deviates
* from the original position because of alignment.
*
* This can be used to restore the original position.
*
* \since QGIS 3.12
*/
double dX() const;

/**
* The deltas (dX and dY) are the difference by which the fixed position deviates
* from the original position because of alignment.
*
* This can be used to restore the original position.
*
* \since QGIS 3.12
*/
void setDX( double dX );

/**
* The deltas (dX and dY) are the difference by which the fixed position deviates
* from the original position because of alignment.
*
* This can be used to restore the original position.
*
* \since QGIS 3.12
*/
double dY() const;


/**
* The deltas (dX and dY) are the difference by which the fixed position deviates
* from the original position because of alignment.
*
* This can be used to restore the original position.
*
* \since QGIS 3.12
*/
void setDY( double dY );

protected:
//! Pointer to PAL layer (assigned when registered to PAL)
pal::Layer *mLayer = nullptr;
@@ -508,6 +549,8 @@ class CORE_EXPORT QgsLabelFeature

QgsLabelObstacleSettings mObstacleSettings;

double mDX = 0.0;
double mDY = 0.0;
};

#endif // QGSLABELFEATURE_H
@@ -2013,6 +2013,10 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
double quadOffsetX = 0.0, quadOffsetY = 0.0;
double offsetX = 0.0, offsetY = 0.0;

//x/y shift in case of alignment
double xdiff = 0.0;
double ydiff = 0.0;

//data defined quadrant offset?
bool ddFixedQuad = false;
QuadrantPosition quadOff = quadOffset;
@@ -2168,10 +2172,6 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
angle = 0.0;
}

//x/y shift in case of alignment
double xdiff = 0.0;
double ydiff = 0.0;

//horizontal alignment
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::Hali ) )
{
@@ -2331,6 +2331,8 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext

// feature to the layer
QgsTextLabelFeature *lf = new QgsTextLabelFeature( feature.id(), std::move( geos_geom_clone ), QSizeF( labelX, labelY ) );
lf->setDX( xdiff );
lf->setDY( ydiff );
lf->setFeature( feature );
lf->setSymbol( symbol );
if ( !qgsDoubleNear( rotatedLabelX, 0.0 ) && !qgsDoubleNear( rotatedLabelY, 0.0 ) )
@@ -23,7 +23,6 @@

QgsTextLabelFeature::QgsTextLabelFeature( QgsFeatureId id, geos::unique_ptr geometry, QSizeF size )
: QgsLabelFeature( id, std::move( geometry ), size )

{
mDefinedFont = QFont();
}
@@ -1615,7 +1615,7 @@ std::vector< std::unique_ptr< LabelPosition > > FeaturePart::createCandidates( P

if ( mLF->hasFixedPosition() )
{
lPos.emplace_back( qgis::make_unique< LabelPosition> ( 0, mLF->fixedPosition().x(), mLF->fixedPosition().y(), getLabelWidth( angle ), getLabelHeight( angle ), angle, 0.0, this ) );
lPos.emplace_back( qgis::make_unique< LabelPosition> ( 0, mLF->fixedPosition().x(), mLF->fixedPosition().y(), getLabelWidth( angle ), getLabelHeight( angle ), angle, 0.0, this, false, LabelPosition::Quadrant::QuadrantOver, mLF->dX(), mLF->dY() ) );
}
else
{
@@ -560,7 +560,7 @@ void TestQgsDxfExport::testTextAlign()
valignProp.setStaticValue( vali );
props.setProperty( QgsPalLayerSettings::Vali, valignProp );
QgsProperty posYProp = QgsProperty();
posXProp.setExpressionString( QStringLiteral( "y($geometry)" ) );
posYProp.setExpressionString( QStringLiteral( "y($geometry)" ) );
props.setProperty( QgsPalLayerSettings::PositionY, posYProp );
settings.setDataDefinedProperties( props );

0 comments on commit f3e3094

Please sign in to comment.
You can’t perform that action at this time.