Skip to content

Commit

Permalink
Introduce label provider ID to uniquely identify label's settings
Browse files Browse the repository at this point in the history
Before rule-based labeling, layer ID would identify label settings
of a label as there could be only one configuration per layer.
  • Loading branch information
wonder-sk committed Apr 27, 2016
1 parent a64ea0b commit 03f201d
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 33 deletions.
4 changes: 3 additions & 1 deletion python/core/qgsmaprenderer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class QgsLabelPosition
#include <qgsmaprenderer.h>
%End
public:
QgsLabelPosition( int id, double r, const QVector< QgsPoint >& corners, const QgsRectangle& rect, double w, double h, const QString& layer, const QString& labeltext, const QFont& labelfont, bool upside_down, bool diagram = false, bool pinned = false );
QgsLabelPosition( int id, double r, const QVector< QgsPoint >& corners, const QgsRectangle& rect, double w, double h, const QString& layer, const QString& labeltext, const QFont& labelfont, bool upside_down, bool diagram = false, bool pinned = false, const QString& providerId = QString() );
QgsLabelPosition();
int featureId;
double rotation;
Expand All @@ -20,6 +20,8 @@ class QgsLabelPosition
bool upsideDown;
bool isDiagram;
bool isPinned;
//! @note added in 2.14
QString providerID;
};

/** Labeling engine interface. */
Expand Down
2 changes: 1 addition & 1 deletion src/core/dxf/qgsdxfexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ void QgsDxfExport::writeEntities()
}
else
{
lp = new QgsDxfLabelProvider( vl, this, nullptr );
lp = new QgsDxfLabelProvider( vl, QString(), this, nullptr );
engine.addProvider( lp );

if ( !lp->prepare( ctx, attributes ) )
Expand Down
8 changes: 4 additions & 4 deletions src/core/dxf/qgsdxfpallabeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#include "qgslogger.h"


QgsDxfLabelProvider::QgsDxfLabelProvider( QgsVectorLayer* layer, QgsDxfExport* dxf, const QgsPalLayerSettings *settings )
: QgsVectorLayerLabelProvider( layer, false, settings )
QgsDxfLabelProvider::QgsDxfLabelProvider( QgsVectorLayer* layer, const QString& providerId, QgsDxfExport* dxf, const QgsPalLayerSettings *settings )
: QgsVectorLayerLabelProvider( layer, providerId, false, settings )
, mDxfExport( dxf )
{
}
Expand Down Expand Up @@ -52,11 +52,11 @@ void QgsDxfRuleBasedLabelProvider::reinit( QgsVectorLayer* layer )
mRules.rootRule()->createSubProviders( layer, mSubProviders, this );
}

QgsVectorLayerLabelProvider *QgsDxfRuleBasedLabelProvider::createProvider( QgsVectorLayer *layer, bool withFeatureLoop, const QgsPalLayerSettings *settings )
QgsVectorLayerLabelProvider *QgsDxfRuleBasedLabelProvider::createProvider( QgsVectorLayer *layer, const QString& providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings )
{
QgsDebugMsg( "Entering." );
Q_UNUSED( withFeatureLoop );
return new QgsDxfLabelProvider( layer, mDxfExport, settings );
return new QgsDxfLabelProvider( layer, providerId, mDxfExport, settings );
}

void QgsDxfRuleBasedLabelProvider::drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const
Expand Down
4 changes: 2 additions & 2 deletions src/core/dxf/qgsdxfpallabeling.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class QgsDxfLabelProvider : public QgsVectorLayerLabelProvider
{
public:
//! construct the provider
explicit QgsDxfLabelProvider( QgsVectorLayer* layer, QgsDxfExport* dxf, const QgsPalLayerSettings *settings );
explicit QgsDxfLabelProvider( QgsVectorLayer* layer, const QString& providerId, QgsDxfExport* dxf, const QgsPalLayerSettings *settings );

/** Re-implementation that writes to DXF file instead of drawing with QPainter
* @param context render context
Expand Down Expand Up @@ -88,7 +88,7 @@ class QgsDxfRuleBasedLabelProvider : public QgsRuleBasedLabelProvider
void registerDxfFeature( QgsFeature& feature, QgsRenderContext &context, const QString& dxfLayerName );

//! create QgsDxfLabelProvider
virtual QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, bool withFeatureLoop, const QgsPalLayerSettings *settings ) override;
virtual QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, const QString& providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings ) override;

protected:
//! pointer to parent DXF export where this instance is used
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgslabelingenginev2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,10 @@ QgsAbstractLabelProvider*QgsLabelFeature::provider() const

}

QgsAbstractLabelProvider::QgsAbstractLabelProvider( const QString& layerId )
QgsAbstractLabelProvider::QgsAbstractLabelProvider( const QString& layerId, const QString& providerId )
: mEngine( nullptr )
, mLayerId( layerId )
, mProviderId( providerId )
, mFlags( DrawLabels )
, mPlacement( QgsPalLayerSettings::AroundPoint )
, mLinePlacementFlags( 0 )
Expand Down
9 changes: 8 additions & 1 deletion src/core/qgslabelingenginev2.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CORE_EXPORT QgsAbstractLabelProvider

public:
//! Construct the provider with default values
QgsAbstractLabelProvider( const QString& layerId = QString() );
QgsAbstractLabelProvider( const QString& layerId = QString(), const QString& providerId = QString() );
//! Vritual destructor
virtual ~QgsAbstractLabelProvider() {}

Expand Down Expand Up @@ -77,6 +77,11 @@ class CORE_EXPORT QgsAbstractLabelProvider
//! Returns ID of associated layer, or empty string if no layer is associated with the provider.
QString layerId() const { return mLayerId; }

//! Returns provider ID - useful in case there is more than one label provider within a layer
//! (e.g. in case of rule-based labeling - provider ID = rule's key). May be empty string if
//! layer ID is sufficient for identification of provider's configuration.
QString providerId() const { return mProviderId; }

//! Flags associated with the provider
Flags flags() const { return mFlags; }

Expand All @@ -103,6 +108,8 @@ class CORE_EXPORT QgsAbstractLabelProvider
QString mName;
//! Associated layer's ID, if applicable
QString mLayerId;
//! Associated provider ID (one layer may have multiple providers, e.g. in rule-based labeling)
QString mProviderId;
//! Flags altering drawing and registration of features
Flags mFlags;
//! Placement strategy
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgslabelsearchtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void QgsLabelSearchTree::labelsInRect( const QgsRectangle& r, QList<QgsLabelPosi
}
}

bool QgsLabelSearchTree::insertLabel( pal::LabelPosition* labelPos, int featureId, const QString& layerName, const QString& labeltext, const QFont& labelfont, bool diagram, bool pinned )
bool QgsLabelSearchTree::insertLabel( pal::LabelPosition* labelPos, int featureId, const QString& layerName, const QString& labeltext, const QFont& labelfont, bool diagram, bool pinned, const QString& providerId )
{
if ( !labelPos )
{
Expand All @@ -93,7 +93,7 @@ bool QgsLabelSearchTree::insertLabel( pal::LabelPosition* labelPos, int featureI
cornerPoints.push_back( QgsPoint( labelPos->getX( i ), labelPos->getY( i ) ) );
}
QgsLabelPosition* newEntry = new QgsLabelPosition( featureId, labelPos->getAlpha(), cornerPoints, QgsRectangle( c_min[0], c_min[1], c_max[0], c_max[1] ),
labelPos->getWidth(), labelPos->getHeight(), layerName, labeltext, labelfont, labelPos->getUpsideDown(), diagram, pinned );
labelPos->getWidth(), labelPos->getHeight(), layerName, labeltext, labelfont, labelPos->getUpsideDown(), diagram, pinned, providerId );
mSpatialIndex.Insert( c_min, c_max, newEntry );
mOwnedPositions << newEntry;
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgslabelsearchtree.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CORE_EXPORT QgsLabelSearchTree
* @return true in case of success
* @note not available in python bindings
*/
bool insertLabel( pal::LabelPosition* labelPos, int featureId, const QString& layerName, const QString& labeltext, const QFont& labelfont, bool diagram = false, bool pinned = false );
bool insertLabel( pal::LabelPosition* labelPos, int featureId, const QString& layerName, const QString& labeltext, const QFont& labelfont, bool diagram = false, bool pinned = false, const QString& providerId = QString() );

private:
// set as mutable because RTree template is not const-correct
Expand Down
6 changes: 4 additions & 2 deletions src/core/qgsmaprenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class QgsDiagramLayerSettings;
class CORE_EXPORT QgsLabelPosition
{
public:
QgsLabelPosition( int id, double r, const QVector< QgsPoint >& corners, const QgsRectangle& rect, double w, double h, const QString& layer, const QString& labeltext, const QFont& labelfont, bool upside_down, bool diagram = false, bool pinned = false )
: featureId( id ), rotation( r ), cornerPoints( corners ), labelRect( rect ), width( w ), height( h ), layerID( layer ), labelText( labeltext ), labelFont( labelfont ), upsideDown( upside_down ), isDiagram( diagram ), isPinned( pinned ) {}
QgsLabelPosition( int id, double r, const QVector< QgsPoint >& corners, const QgsRectangle& rect, double w, double h, const QString& layer, const QString& labeltext, const QFont& labelfont, bool upside_down, bool diagram = false, bool pinned = false, const QString& providerId = QString() )
: featureId( id ), rotation( r ), cornerPoints( corners ), labelRect( rect ), width( w ), height( h ), layerID( layer ), labelText( labeltext ), labelFont( labelfont ), upsideDown( upside_down ), isDiagram( diagram ), isPinned( pinned ), providerID( providerId ) {}
QgsLabelPosition()
: featureId( -1 ), rotation( 0 ), labelRect( QgsRectangle() ), width( 0 ), height( 0 ), layerID( "" ), labelText( "" ), labelFont( QFont() ), upsideDown( false ), isDiagram( false ), isPinned( false ) {}
int featureId;
Expand All @@ -62,6 +62,8 @@ class CORE_EXPORT QgsLabelPosition
bool upsideDown;
bool isDiagram;
bool isPinned;
//! @note added in 2.14
QString providerID;
};

/** Labeling engine interface. */
Expand Down
17 changes: 9 additions & 8 deletions src/core/qgsrulebasedlabeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


QgsRuleBasedLabelProvider::QgsRuleBasedLabelProvider( const QgsRuleBasedLabeling& rules, QgsVectorLayer* layer, bool withFeatureLoop )
: QgsVectorLayerLabelProvider( layer, withFeatureLoop )
: QgsVectorLayerLabelProvider( layer, QString(), withFeatureLoop )
, mRules( rules )
{
mRules.rootRule()->createSubProviders( layer, mSubProviders, this );
Expand All @@ -27,9 +27,9 @@ QgsRuleBasedLabelProvider::~QgsRuleBasedLabelProvider()
// sub-providers owned by labeling engine
}

QgsVectorLayerLabelProvider *QgsRuleBasedLabelProvider::createProvider( QgsVectorLayer *layer, bool withFeatureLoop, const QgsPalLayerSettings *settings )
QgsVectorLayerLabelProvider *QgsRuleBasedLabelProvider::createProvider( QgsVectorLayer *layer, const QString& providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings )
{
return new QgsVectorLayerLabelProvider( layer, withFeatureLoop, settings );
return new QgsVectorLayerLabelProvider( layer, providerId, withFeatureLoop, settings );
}

bool QgsRuleBasedLabelProvider::prepare( const QgsRenderContext& context, QStringList& attributeNames )
Expand Down Expand Up @@ -67,6 +67,7 @@ QgsRuleBasedLabeling::Rule::Rule( QgsPalLayerSettings* settings, int scaleMinDen
, mIsActive( true )
, mFilter( nullptr )
{
mRuleKey = QUuid::createUuid().toString();
initFilter();
}

Expand Down Expand Up @@ -162,11 +163,11 @@ QgsRuleBasedLabeling::Rule*QgsRuleBasedLabeling::Rule::create( const QDomElement
QString description = ruleElem.attribute( "description" );
int scaleMinDenom = ruleElem.attribute( "scalemindenom", "0" ).toInt();
int scaleMaxDenom = ruleElem.attribute( "scalemaxdenom", "0" ).toInt();
//QString ruleKey = ruleElem.attribute( "key" );
QString ruleKey = ruleElem.attribute( "key" );
Rule* rule = new Rule( settings, scaleMinDenom, scaleMaxDenom, filterExp, description );

//if ( !ruleKey.isEmpty() )
// rule->mRuleKey = ruleKey;
if ( !ruleKey.isEmpty() )
rule->mRuleKey = ruleKey;

rule->setActive( ruleElem.attribute( "active", "1" ).toInt() );

Expand Down Expand Up @@ -206,7 +207,7 @@ QDomElement QgsRuleBasedLabeling::Rule::save( QDomDocument& doc ) const
ruleElem.setAttribute( "description", mDescription );
if ( !mIsActive )
ruleElem.setAttribute( "active", 0 );
//ruleElem.setAttribute( "key", mRuleKey );
ruleElem.setAttribute( "key", mRuleKey );

for ( RuleList::const_iterator it = mChildren.constBegin(); it != mChildren.constEnd(); ++it )
{
Expand All @@ -221,7 +222,7 @@ void QgsRuleBasedLabeling::Rule::createSubProviders( QgsVectorLayer* layer, QgsR
if ( mSettings )
{
// add provider!
QgsVectorLayerLabelProvider *p = provider->createProvider( layer, false, mSettings );
QgsVectorLayerLabelProvider *p = provider->createProvider( layer, mRuleKey, false, mSettings );
Q_ASSERT( !subProviders.contains( this ) );
subProviders[this] = p;
}
Expand Down
9 changes: 8 additions & 1 deletion src/core/qgsrulebasedlabeling.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling
*/
bool isElse() const { return mElseRule; }

//! Unique rule identifier (for identification of rule within labeling, used as provider ID)
QString ruleKey() const { return mRuleKey; }

//! set new settings (or NULL). Deletes old settings if any.
void setSettings( QgsPalLayerSettings* settings );

Expand Down Expand Up @@ -160,6 +163,8 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling
*/
void setIsElse( bool iselse ) { mElseRule = iselse; }

//! Override the assigned rule key (should be used just internally by rule-based labeling)
void setRuleKey( const QString& key ) { mRuleKey = key; }

// parent / child operations

Expand Down Expand Up @@ -260,6 +265,8 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling
RuleList mElseRules;
bool mIsActive; // whether it is enabled or not

QString mRuleKey; // string used for unique identification of rule within labeling

// temporary
QgsExpression* mFilter;

Expand Down Expand Up @@ -311,7 +318,7 @@ class CORE_EXPORT QgsRuleBasedLabelProvider : public QgsVectorLayerLabelProvider
virtual void registerFeature( QgsFeature& feature, QgsRenderContext& context, QgsGeometry* obstacleGeometry = nullptr ) override;

//! create a label provider
virtual QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, bool withFeatureLoop, const QgsPalLayerSettings *settings );
virtual QgsVectorLayerLabelProvider *createProvider( QgsVectorLayer *layer, const QString& providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings );

//! return subproviders
virtual QList<QgsAbstractLabelProvider*> subProviders() override;
Expand Down
2 changes: 0 additions & 2 deletions src/core/qgsvectorlayerdiagramprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ QgsVectorLayerDiagramProvider::QgsVectorLayerDiagramProvider(
: QgsAbstractLabelProvider( layerId )
, mSettings( *diagSettings )
, mDiagRenderer( diagRenderer->clone() )
, mLayerId( layerId )
, mFields( fields )
, mLayerCrs( crs )
, mSource( source )
Expand All @@ -49,7 +48,6 @@ QgsVectorLayerDiagramProvider::QgsVectorLayerDiagramProvider( QgsVectorLayer* la
: QgsAbstractLabelProvider( layer->id() )
, mSettings( *layer->diagramLayerSettings() )
, mDiagRenderer( layer->diagramRenderer()->clone() )
, mLayerId( layer->id() )
, mFields( layer->fields() )
, mLayerCrs( layer->crs() )
, mSource( ownFeatureLoop ? new QgsVectorLayerFeatureSource( layer ) : nullptr )
Expand Down
2 changes: 0 additions & 2 deletions src/core/qgsvectorlayerdiagramprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ class CORE_EXPORT QgsVectorLayerDiagramProvider : public QgsAbstractLabelProvide
QgsDiagramLayerSettings mSettings;
//! Diagram renderer instance (owned by mSettings)
QgsDiagramRendererV2* mDiagRenderer;
//! ID of the layer
QString mLayerId;

// these are needed only if using own renderer loop

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayerlabeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ QgsAbstractVectorLayerLabeling* QgsAbstractVectorLayerLabeling::create( const QD
QgsVectorLayerLabelProvider* QgsVectorLayerSimpleLabeling::provider( QgsVectorLayer* layer ) const
{
if ( layer->customProperty( "labeling" ).toString() == QLatin1String( "pal" ) && layer->labelsEnabled() )
return new QgsVectorLayerLabelProvider( layer, false );
return new QgsVectorLayerLabelProvider( layer, QString(), false );

return nullptr;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/qgsvectorlayerlabelprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ static void _fixQPictureDPI( QPainter* p )



QgsVectorLayerLabelProvider::QgsVectorLayerLabelProvider( QgsVectorLayer* layer, bool withFeatureLoop, const QgsPalLayerSettings* settings, const QString& layerName )
: QgsAbstractLabelProvider( layer->id() )
QgsVectorLayerLabelProvider::QgsVectorLayerLabelProvider( QgsVectorLayer* layer, const QString& providerId, bool withFeatureLoop, const QgsPalLayerSettings* settings, const QString& layerName )
: QgsAbstractLabelProvider( layer->id(), providerId )
, mSettings( settings ? *settings : QgsPalLayerSettings::fromLayer( layer ) )
, mLayerGeometryType( layer->geometryType() )
, mRenderer( layer->rendererV2() )
Expand Down Expand Up @@ -490,7 +490,7 @@ void QgsVectorLayerLabelProvider::drawLabel( QgsRenderContext& context, pal::Lab

// add to the results
QString labeltext = label->getFeaturePart()->feature()->labelText();
mEngine->results()->mLabelSearchTree->insertLabel( label, label->getFeaturePart()->featureId(), mLayerId, labeltext, dFont, false, lf->hasFixedPosition() );
mEngine->results()->mLabelSearchTree->insertLabel( label, label->getFeaturePart()->featureId(), mLayerId, labeltext, dFont, false, lf->hasFixedPosition(), mProviderId );
}


Expand Down
6 changes: 5 additions & 1 deletion src/core/qgsvectorlayerlabelprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ class CORE_EXPORT QgsVectorLayerLabelProvider : public QgsAbstractLabelProvider
public:

//! Convenience constructor to initialize the provider from given vector layer
explicit QgsVectorLayerLabelProvider( QgsVectorLayer* layer, bool withFeatureLoop = true, const QgsPalLayerSettings* settings = nullptr, const QString& layerName = QString() );
explicit QgsVectorLayerLabelProvider( QgsVectorLayer* layer,
const QString& providerId,
bool withFeatureLoop = true,
const QgsPalLayerSettings* settings = nullptr,
const QString& layerName = QString() );

//! Construct diagram provider with all the necessary configuration parameters
QgsVectorLayerLabelProvider( const QgsPalLayerSettings& settings,
Expand Down

0 comments on commit 03f201d

Please sign in to comment.