Skip to content
Permalink
Browse files

Add method to determine layers participating in a label engine

  • Loading branch information
nyalldawson committed Feb 1, 2017
1 parent 0438afc commit 1e5a93b0f9936db3e584833bbdbd93bcbd50f628
Showing with 52 additions and 0 deletions.
  1. +16 −0 src/core/qgslabelingengine.cpp
  2. +6 −0 src/core/qgslabelingengine.h
  3. +30 −0 tests/src/core/testqgslabelingengine.cpp
@@ -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 );
@@ -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 );

@@ -45,6 +45,7 @@ class TestQgsLabelingEngine : public QObject
void testEncodeDecodePositionOrder();
void testSubstitutions();
void testCapitalization();
void testParticipatingLayers();

private:
QgsVectorLayer* vl;
@@ -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

0 comments on commit 1e5a93b

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