Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix labeling offset
  • Loading branch information
m-kuhn committed Dec 24, 2019
1 parent 4f625ce commit f3e3094
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 12 deletions.
18 changes: 13 additions & 5 deletions src/core/dxf/qgsdxfexport.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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() )
Expand All @@ -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 ) )
{
Expand Down
20 changes: 20 additions & 0 deletions src/core/labeling/qgslabelfeature.cpp
Expand Up @@ -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;
}
43 changes: 43 additions & 0 deletions src/core/labeling/qgslabelfeature.h
Expand Up @@ -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;
Expand Down Expand Up @@ -508,6 +549,8 @@ class CORE_EXPORT QgsLabelFeature

QgsLabelObstacleSettings mObstacleSettings;

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

#endif // QGSLABELFEATURE_H
10 changes: 6 additions & 4 deletions src/core/labeling/qgspallabeling.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ) )
{
Expand Down Expand Up @@ -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 ) )
Expand Down
1 change: 0 additions & 1 deletion src/core/labeling/qgstextlabelfeature.cpp
Expand Up @@ -23,7 +23,6 @@

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

{
mDefinedFont = QFont();
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/pal/feature.cpp
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgsdxfexport.cpp
Expand Up @@ -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 );

Expand Down

0 comments on commit f3e3094

Please sign in to comment.