Skip to content

Commit

Permalink
[processing] Add buffer details to extract labels algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Jan 7, 2022
1 parent 3720518 commit 5ff8ff1
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion src/analysis/processing/qgsalgorithmextractlabels.cpp
Expand Up @@ -237,6 +237,10 @@ class ExtractLabelSink : public QgsLabelSink
const double fontWordSpacing = font.wordSpacing();

QgsTextFormat format = labelSettings.format();
if ( dataDefinedValues.contains( QgsPalLayerSettings::Size ) )
{
format.setSize( dataDefinedValues.value( QgsPalLayerSettings::Size ).toDouble() );
}
if ( dataDefinedValues.contains( QgsPalLayerSettings::Color ) )
{
format.setColor( dataDefinedValues.value( QgsPalLayerSettings::Color ).value<QColor>() );
Expand All @@ -254,11 +258,44 @@ class ExtractLabelSink : public QgsLabelSink
const double formatOpacity = format.opacity() * 100;
const double formatLineHeight = format.lineHeight();

QgsTextBufferSettings buffer = format.buffer();
if ( dataDefinedValues.contains( QgsPalLayerSettings::BufferDraw ) )
{
buffer.setEnabled( dataDefinedValues.value( QgsPalLayerSettings::BufferDraw ).toBool() );
}
const bool bufferDraw = buffer.enabled();
double bufferSize = 0.0;
QString bufferColor;
double bufferOpacity = 0.0;
if ( bufferDraw )
{
if ( dataDefinedValues.contains( QgsPalLayerSettings::BufferSize ) )
{
buffer.setSize( dataDefinedValues.value( QgsPalLayerSettings::BufferSize ).toDouble() );
}
if ( dataDefinedValues.contains( QgsPalLayerSettings::BufferColor ) )
{
buffer.setColor( dataDefinedValues.value( QgsPalLayerSettings::BufferColor ).value<QColor>() );
}
if ( dataDefinedValues.contains( QgsPalLayerSettings::BufferOpacity ) )
{
buffer.setOpacity( dataDefinedValues.value( QgsPalLayerSettings::BufferOpacity ).toDouble() / 100.0 );
}

bufferSize = buffer.sizeUnit() == QgsUnitTypes::RenderPercentage
? context.convertToPainterUnits( format.size(), format.sizeUnit(), format.sizeMapUnitScale() ) * buffer.size() / 100
: context.convertToPainterUnits( buffer.size(), buffer.sizeUnit(), buffer.sizeMapUnitScale() );
bufferSize = bufferSize * 72 / context.painter()->device()->logicalDpiX();
bufferColor = buffer.color().name();
bufferOpacity = buffer.opacity() * 100;
}

QgsAttributes attributes;
attributes << mMapLayerNames.value( layerId ) << fid
<< labelText << label->getWidth() << label->getHeight() << labelRotation << unplacedLabel
<< fontFamily << fontSize << fontItalic << fontBold << fontUnderline << fontStyle << fontLetterSpacing << fontWordSpacing
<< labelAlignment << formatLineHeight << formatColor << formatOpacity;
<< labelAlignment << formatLineHeight << formatColor << formatOpacity
<< bufferDraw << bufferSize << bufferColor << bufferOpacity;

double x = label->getX();
double y = label->getY();
Expand Down Expand Up @@ -319,6 +356,10 @@ QVariantMap QgsExtractLabelsAlgorithm::processAlgorithm( const QVariantMap &para
fields.append( QgsField( QStringLiteral( "MultiLineHeight" ), QVariant::Double, QString(), 20, 2 ) );
fields.append( QgsField( QStringLiteral( "Color" ), QVariant::String, QString(), 7, 0 ) );
fields.append( QgsField( QStringLiteral( "FontOpacity" ), QVariant::Double, QString(), 20, 1 ) );
fields.append( QgsField( QStringLiteral( "BufferDraw" ), QVariant::Bool, QString(), 1, 0 ) );
fields.append( QgsField( QStringLiteral( "BufferSize" ), QVariant::Double, QString(), 20, 4 ) );
fields.append( QgsField( QStringLiteral( "BufferColor" ), QVariant::String, QString(), 7, 0 ) );
fields.append( QgsField( QStringLiteral( "BufferOpacity" ), QVariant::Double, QString(), 20, 1 ) );

QString dest;
std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::Point, mCrs, QgsFeatureSink::RegeneratePrimaryKey ) );
Expand Down Expand Up @@ -395,6 +436,11 @@ QVariantMap QgsExtractLabelsAlgorithm::processAlgorithm( const QVariantMap &para
textFormat.setSize( 9 );
textFormat.setSizeUnit( QgsUnitTypes::RenderPoints );
textFormat.setColor( QColor( 0, 0, 0 ) );

QgsTextBufferSettings buffer = textFormat.buffer();
buffer.setSizeUnit( QgsUnitTypes::RenderPoints );

textFormat.setBuffer( buffer );
settings.setFormat( textFormat );

settingsProperties.setProperty( QgsPalLayerSettings::Color, QgsProperty::fromExpression( QStringLiteral( "if(\"LabelUnplaced\",'255,0,0',\"Color\")" ) ) );
Expand All @@ -409,6 +455,10 @@ QVariantMap QgsExtractLabelsAlgorithm::processAlgorithm( const QVariantMap &para
settingsProperties.setProperty( QgsPalLayerSettings::MultiLineAlignment, QgsProperty::fromField( QStringLiteral( "MultiLineAlignment" ) ) );
settingsProperties.setProperty( QgsPalLayerSettings::MultiLineHeight, QgsProperty::fromField( QStringLiteral( "MultiLineHeight" ) ) );
settingsProperties.setProperty( QgsPalLayerSettings::LabelRotation, QgsProperty::fromField( QStringLiteral( "LabelRotation" ) ) );
settingsProperties.setProperty( QgsPalLayerSettings::BufferDraw, QgsProperty::fromField( QStringLiteral( "BufferDraw" ) ) );
settingsProperties.setProperty( QgsPalLayerSettings::BufferSize, QgsProperty::fromField( QStringLiteral( "BufferSize" ) ) );
settingsProperties.setProperty( QgsPalLayerSettings::BufferColor, QgsProperty::fromField( QStringLiteral( "BufferColor" ) ) );
settingsProperties.setProperty( QgsPalLayerSettings::BufferOpacity, QgsProperty::fromField( QStringLiteral( "BufferOpacity" ) ) );
settingsProperties.setProperty( QgsPalLayerSettings::Show, QgsProperty::fromExpression( QStringLiteral( "\"LabelUnplaced\"=false" ) ) );
settings.setDataDefinedProperties( settingsProperties );

Expand Down

0 comments on commit 5ff8ff1

Please sign in to comment.