Skip to content

Commit

Permalink
Always collect unplaced labels in rendered layout map items
Browse files Browse the repository at this point in the history
Provides a mechanism for retrieving statistics about unplaced
labels to plugins
  • Loading branch information
nyalldawson committed Jun 9, 2021
1 parent 643e468 commit f0a7cb5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/layout/qgslayoutitemmap.cpp
Expand Up @@ -1545,6 +1545,7 @@ QgsMapSettings QgsLayoutItemMap::mapSettings( const QgsRectangle &extent, QSizeF
// override project "show partial labels" setting with this map's setting
labelSettings.setFlag( QgsLabelingEngineSettings::UsePartialCandidates, mMapFlags & ShowPartialLabels );
labelSettings.setFlag( QgsLabelingEngineSettings::DrawUnplacedLabels, mMapFlags & ShowUnplacedLabels );
labelSettings.setFlag( QgsLabelingEngineSettings::CollectUnplacedLabels, true );
jobMapSettings.setLabelingEngineSettings( labelSettings );

// override the default text render format inherited from the labeling engine settings using the layout's render context setting
Expand Down
36 changes: 36 additions & 0 deletions tests/src/core/testqgslayoutmap.cpp
Expand Up @@ -2009,6 +2009,42 @@ void TestQgsLayoutMap::testLabelResults()
QVERIFY( !labels.at( 1 ).isUnplaced );
QCOMPARE( labels.at( 2 ).labelText, QStringLiteral( "8888" ) );
QVERIFY( !labels.at( 2 ).isUnplaced );

// with unplaced labels
QgsVectorLayer *vl3( vl2->clone() );
p.addMapLayer( vl3 );
// with unplaced labels -- all vl3 labels will be unplaced, because they are conflicting with those in vl2
settings.priority = 1;
settings.displayAll = false;
vl3->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) );
vl3->setLabelsEnabled( true );
map->setLayers( { vl2, vl3 } );

painter.begin( &im );
map->paint( &painter, nullptr, nullptr );
painter.end();

results = std::move( map->mExportLabelingResults );
QVERIFY( results );
labels = results->allLabels();
QCOMPARE( labels.count(), 6 );
std::sort( labels.begin(), labels.end(), []( const QgsLabelPosition & a, const QgsLabelPosition & b )
{
return a.isUnplaced == b.isUnplaced ? a.labelText.compare( b.labelText ) < 0 : a.isUnplaced < b.isUnplaced;
} );
QCOMPARE( labels.at( 0 ).labelText, QStringLiteral( "1" ) );
QVERIFY( !labels.at( 0 ).isUnplaced );
QCOMPARE( labels.at( 1 ).labelText, QStringLiteral( "33333" ) );
QVERIFY( !labels.at( 1 ).isUnplaced );
QCOMPARE( labels.at( 2 ).labelText, QStringLiteral( "8888" ) );
QVERIFY( !labels.at( 2 ).isUnplaced );
QCOMPARE( labels.at( 3 ).labelText, QStringLiteral( "1" ) );
QVERIFY( labels.at( 3 ).isUnplaced );
QCOMPARE( labels.at( 4 ).labelText, QStringLiteral( "33333" ) );
QVERIFY( labels.at( 4 ).isUnplaced );
QCOMPARE( labels.at( 5 ).labelText, QStringLiteral( "8888" ) );
QVERIFY( labels.at( 5 ).isUnplaced );

}

QGSTEST_MAIN( TestQgsLayoutMap )
Expand Down

0 comments on commit f0a7cb5

Please sign in to comment.