Skip to content

Commit

Permalink
Add method to determine layers participating in a label engine
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 1, 2017
1 parent 0438afc commit 1e5a93b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/core/qgslabelingengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ QgsLabelingEngine::~QgsLabelingEngine()
qDeleteAll( mSubProviders );
}

QStringList QgsLabelingEngine::participatingLayerIds() const
{
QSet< QString > ids;
Q_FOREACH ( QgsAbstractLabelProvider* provider, mProviders )
{
if ( !provider->layerId().isEmpty() )
ids << provider->layerId();
}
Q_FOREACH ( QgsAbstractLabelProvider* provider, mSubProviders )
{
if ( !provider->layerId().isEmpty() )
ids << provider->layerId();
}
return ids.toList();
}

void QgsLabelingEngine::addProvider( QgsAbstractLabelProvider* provider )
{
provider->setEngine( this );
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgslabelingengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ class CORE_EXPORT QgsLabelingEngine
//! Get associated map settings
const QgsMapSettings& mapSettings() const { return mMapSettings; }

/**
* Returns a list of layer IDs for layers with providers in the engine.
* @note added in QGIS 3.0
*/
QStringList participatingLayerIds() const;

//! Add provider of label features. Takes ownership of the provider
void addProvider( QgsAbstractLabelProvider* provider );

Expand Down
30 changes: 30 additions & 0 deletions tests/src/core/testqgslabelingengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TestQgsLabelingEngine : public QObject
void testEncodeDecodePositionOrder();
void testSubstitutions();
void testCapitalization();
void testParticipatingLayers();

private:
QgsVectorLayer* vl;
Expand Down Expand Up @@ -534,6 +535,35 @@ void TestQgsLabelingEngine::testCapitalization()
QCOMPARE( provider4->mLabels.at( 0 )->labelText(), QString( "A TeSt LABEL" ) );
}

void TestQgsLabelingEngine::testParticipatingLayers()
{
QgsLabelingEngine engine;
QVERIFY( engine.participatingLayerIds().isEmpty() );

QgsPalLayerSettings settings1;
QgsVectorLayerLabelProvider* provider = new QgsVectorLayerLabelProvider( vl, QStringLiteral( "test" ), true, &settings1 );
engine.addProvider( provider );
QCOMPARE( engine.participatingLayerIds(), QStringList() << vl->id() );

QgsVectorLayer* layer2 = new QgsVectorLayer( QStringLiteral( "Point?field=col1:integer" ), QStringLiteral( "layer2" ), QStringLiteral( "memory" ) );
QgsPalLayerSettings settings2;
QgsVectorLayerLabelProvider* provider2 = new QgsVectorLayerLabelProvider( layer2, QStringLiteral( "test2" ), true, &settings2 );
engine.addProvider( provider2 );
QCOMPARE( engine.participatingLayerIds().toSet(), QSet< QString >() << vl->id() << layer2->id() );

// add a rule-based labeling node
QgsRuleBasedLabeling::Rule* root = new QgsRuleBasedLabeling::Rule( 0 );
QgsPalLayerSettings s1;
root->appendChild( new QgsRuleBasedLabeling::Rule( new QgsPalLayerSettings( s1 ) ) );
QgsPalLayerSettings s2;
root->appendChild( new QgsRuleBasedLabeling::Rule( new QgsPalLayerSettings( s2 ) ) );

QgsVectorLayer* layer3 = new QgsVectorLayer( QStringLiteral( "Point?field=col1:integer" ), QStringLiteral( "layer3" ), QStringLiteral( "memory" ) );
QgsRuleBasedLabelProvider* ruleProvider = new QgsRuleBasedLabelProvider( QgsRuleBasedLabeling( root ), layer3 );
engine.addProvider( ruleProvider );
QCOMPARE( engine.participatingLayerIds().toSet(), QSet< QString >() << vl->id() << layer2->id() << layer3->id() );
}

bool TestQgsLabelingEngine::imageCheck( const QString& testName, QImage &image, int mismatchCount )
{
//draw background
Expand Down

0 comments on commit 1e5a93b

Please sign in to comment.