QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
QgsVectorLayerLabelProvider Class Reference

The QgsVectorLayerLabelProvider class implements a label provider for vector layers. More...

#include <qgsvectorlayerlabelprovider.h>

Inheritance diagram for QgsVectorLayerLabelProvider:
Inheritance graph
[legend]

Public Member Functions

 QgsVectorLayerLabelProvider (Qgis::GeometryType geometryType, const QgsFields &fields, const QgsCoordinateReferenceSystem &crs, const QString &providerId, const QgsPalLayerSettings *settings, QgsMapLayer *layer, const QString &layerName=QString())
 Constructor to initialize the provider from any map layer (e.g. vector tile layer)
 
 QgsVectorLayerLabelProvider (QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings, const QString &layerName=QString())
 Convenience constructor to initialize the provider from given vector layer.
 
 ~QgsVectorLayerLabelProvider () override
 
void drawLabel (QgsRenderContext &context, pal::LabelPosition *label) const override
 Draw this label at the position determined by the labeling engine.
 
void drawLabelBackground (QgsRenderContext &context, pal::LabelPosition *label) const override
 Draw the background for the specified label.
 
void drawUnplacedLabel (QgsRenderContext &context, pal::LabelPosition *label) const override
 Draw an unplaced label.
 
QList< QgsLabelFeature * > labelFeatures (QgsRenderContext &context) override
 Returns list of label features (they are owned by the provider and thus deleted on its destruction)
 
virtual bool prepare (QgsRenderContext &context, QSet< QString > &attributeNames)
 Prepare for registration of features.
 
virtual QList< QgsLabelFeature * > registerFeature (const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry=QgsGeometry(), const QgsSymbol *symbol=nullptr)
 Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels.
 
void setFields (const QgsFields &fields)
 Sets fields of this label provider.
 
const QgsPalLayerSettingssettings () const
 Returns the layer's settings.
 
void startRender (QgsRenderContext &context) override
 To be called before rendering of labels begins.
 
void stopRender (QgsRenderContext &context) override
 To be called after rendering is complete.
 
- Public Member Functions inherited from QgsAbstractLabelProvider
 QgsAbstractLabelProvider (QgsMapLayer *layer, const QString &providerId=QString())
 Construct the provider with default values.
 
virtual ~QgsAbstractLabelProvider ()=default
 
Flags flags () const
 Flags associated with the provider.
 
QgsMapLayerlayer () const
 Returns the associated layer, or nullptr if no layer is associated with the provider.
 
QgsExpressionContextScopelayerExpressionContextScope () const
 Returns the expression context scope created from the layer associated with this provider.
 
QString layerId () const
 Returns ID of associated layer, or empty string if no layer is associated with the provider.
 
double layerReferenceScale () const
 Returns the symbology reference scale of the layer associated with this provider.
 
QString name () const
 Name of the layer (for statistics, debugging etc.) - does not need to be unique.
 
QgsLabelObstacleSettings::ObstacleType obstacleType () const
 How the feature geometries will work as obstacles.
 
Qgis::LabelPlacement placement () const
 What placement strategy to use for the labels.
 
double priority () const
 Default priority of labels (may be overridden by individual labels)
 
QString providerId () const
 Returns provider ID - useful in case there is more than one label provider within a layer (e.g.
 
void setEngine (const QgsLabelingEngine *engine)
 Associate provider with a labeling engine (should be only called internally from QgsLabelingEngine)
 
virtual QList< QgsAbstractLabelProvider * > subProviders ()
 Returns list of child providers - useful if the provider needs to put labels into more layers with different configuration.
 
Qgis::UpsideDownLabelHandling upsidedownLabels () const
 How to handle labels that would be upside down.
 

Static Public Member Functions

static QgsGeometry getPointObstacleGeometry (QgsFeature &fet, QgsRenderContext &context, const QgsSymbolList &symbols)
 Returns the geometry for a point feature which should be used as an obstacle for labels.
 

Protected Member Functions

void drawLabelPrivate (pal::LabelPosition *label, QgsRenderContext &context, QgsPalLayerSettings &tmpLyr, Qgis::TextComponent drawType, double dpiRatio=1.0) const
 Internal label drawing method.
 
void init ()
 initialization method - called from constructors
 

Protected Attributes

QgsCoordinateReferenceSystem mCrs
 Layer's CRS.
 
QgsFields mFields
 Layer's fields.
 
QList< QgsLabelFeature * > mLabels
 List of generated.
 
Qgis::GeometryType mLayerGeometryType
 Geometry type of layer.
 
QgsFeatureRenderermRenderer = nullptr
 
QgsPalLayerSettings mSettings
 Layer's labeling configuration.
 
std::unique_ptr< QgsAbstractFeatureSourcemSource
 Layer's feature source.
 
- Protected Attributes inherited from QgsAbstractLabelProvider
const QgsLabelingEnginemEngine = nullptr
 Associated labeling engine.
 
Flags mFlags = DrawLabels
 Flags altering drawing and registration of features.
 
QgsWeakMapLayerPointer mLayer
 Weak pointer to source layer.
 
QString mLayerId
 Associated layer's ID, if applicable.
 
QString mName
 Name of the layer.
 
QgsLabelObstacleSettings::ObstacleType mObstacleType = QgsLabelObstacleSettings::ObstacleType::PolygonBoundary
 Type of the obstacle of feature geometries.
 
Qgis::LabelPlacement mPlacement = Qgis::LabelPlacement::AroundPoint
 Placement strategy.
 
double mPriority = 0.5
 Default priority of labels.
 
QString mProviderId
 Associated provider ID (one layer may have multiple providers, e.g. in rule-based labeling)
 
Qgis::UpsideDownLabelHandling mUpsidedownLabels = Qgis::UpsideDownLabelHandling::FlipUpsideDownLabels
 How to handle labels that would be upside down.
 

Friends

class QgsVectorTileBasicLabelProvider
 
class TestQgsLabelingEngine
 

Additional Inherited Members

- Public Types inherited from QgsAbstractLabelProvider
enum  Flag { DrawLabels = 1 << 1 , MergeConnectedLines = 1 << 3 , CentroidMustBeInside = 1 << 4 }
 
typedef QFlags< FlagFlags
 

Detailed Description

The QgsVectorLayerLabelProvider class implements a label provider for vector layers.

Parameters for the labeling are taken from the layer's custom properties or from the given settings.

Note
this class is not a part of public API yet. See notes in QgsLabelingEngine
not available in Python bindings

Definition at line 39 of file qgsvectorlayerlabelprovider.h.

Constructor & Destructor Documentation

◆ QgsVectorLayerLabelProvider() [1/2]

QgsVectorLayerLabelProvider::QgsVectorLayerLabelProvider ( QgsVectorLayer layer,
const QString &  providerId,
bool  withFeatureLoop,
const QgsPalLayerSettings settings,
const QString &  layerName = QString() 
)
explicit

Convenience constructor to initialize the provider from given vector layer.

Definition at line 50 of file qgsvectorlayerlabelprovider.cpp.

◆ QgsVectorLayerLabelProvider() [2/2]

QgsVectorLayerLabelProvider::QgsVectorLayerLabelProvider ( Qgis::GeometryType  geometryType,
const QgsFields fields,
const QgsCoordinateReferenceSystem crs,
const QString &  providerId,
const QgsPalLayerSettings settings,
QgsMapLayer layer,
const QString &  layerName = QString() 
)
explicit

Constructor to initialize the provider from any map layer (e.g. vector tile layer)

Definition at line 68 of file qgsvectorlayerlabelprovider.cpp.

◆ ~QgsVectorLayerLabelProvider()

QgsVectorLayerLabelProvider::~QgsVectorLayerLabelProvider ( )
override

Definition at line 109 of file qgsvectorlayerlabelprovider.cpp.

Member Function Documentation

◆ drawLabel()

void QgsVectorLayerLabelProvider::drawLabel ( QgsRenderContext context,
pal::LabelPosition label 
) const
overridevirtual

Draw this label at the position determined by the labeling engine.

Before any calls to drawLabel(), a provider should be prepared for rendering by a call to startRender() and a corresponding call to stopRender().

Implements QgsAbstractLabelProvider.

Definition at line 358 of file qgsvectorlayerlabelprovider.cpp.

◆ drawLabelBackground()

void QgsVectorLayerLabelProvider::drawLabelBackground ( QgsRenderContext context,
pal::LabelPosition label 
) const
overridevirtual

Draw the background for the specified label.

This is called in turn for each label provider before any actual labels are rendered, and allows the provider to render content which should be drawn below ALL map labels (such as background rectangles or callout lines).

Before any calls to drawLabelBackground(), a provider should be prepared for rendering by a call to startRender() and a corresponding call to stopRender().

Since
QGIS 3.10

Reimplemented from QgsAbstractLabelProvider.

Definition at line 311 of file qgsvectorlayerlabelprovider.cpp.

◆ drawLabelPrivate()

void QgsVectorLayerLabelProvider::drawLabelPrivate ( pal::LabelPosition label,
QgsRenderContext context,
QgsPalLayerSettings tmpLyr,
Qgis::TextComponent  drawType,
double  dpiRatio = 1.0 
) const
protected

Internal label drawing method.

Definition at line 495 of file qgsvectorlayerlabelprovider.cpp.

◆ drawUnplacedLabel()

void QgsVectorLayerLabelProvider::drawUnplacedLabel ( QgsRenderContext context,
pal::LabelPosition label 
) const
overridevirtual

Draw an unplaced label.

These correspond to features which were registered for labeling, but which could not be labeled (e.g. due to conflicting labels).

The default behavior is to draw nothing for these labels.

Note
This method is only used if the Qgis::Qgis::LabelingFlag::DrawUnplacedLabels flag is set on the labeling engine.
Since
QGIS 3.10

Reimplemented from QgsAbstractLabelProvider.

Definition at line 474 of file qgsvectorlayerlabelprovider.cpp.

◆ getPointObstacleGeometry()

QgsGeometry QgsVectorLayerLabelProvider::getPointObstacleGeometry ( QgsFeature fet,
QgsRenderContext context,
const QgsSymbolList symbols 
)
static

Returns the geometry for a point feature which should be used as an obstacle for labels.

This obstacle geometry will respect the dimensions and offsets of the symbol used to render the point, and ensures that labels will not overlap large or offset points.

Parameters
fetpoint feature
contextrender context
symbolssymbols rendered for point feature

Definition at line 209 of file qgsvectorlayerlabelprovider.cpp.

◆ init()

void QgsVectorLayerLabelProvider::init ( )
protected

initialization method - called from constructors

Definition at line 81 of file qgsvectorlayerlabelprovider.cpp.

◆ labelFeatures()

QList< QgsLabelFeature * > QgsVectorLayerLabelProvider::labelFeatures ( QgsRenderContext context)
overridevirtual

Returns list of label features (they are owned by the provider and thus deleted on its destruction)

Implements QgsAbstractLabelProvider.

Definition at line 134 of file qgsvectorlayerlabelprovider.cpp.

◆ prepare()

bool QgsVectorLayerLabelProvider::prepare ( QgsRenderContext context,
QSet< QString > &  attributeNames 
)
virtual

Prepare for registration of features.

Must be called after provider has been added to engine (uses its map settings)

Parameters
contextrender context.
attributeNameslist of attribute names to which additional required attributes shall be added
Returns
Whether the preparation was successful - if not, the provider shall not be used

Reimplemented in QgsRuleBasedLabelProvider, and QgsVectorTileBasicLabelProvider.

Definition at line 115 of file qgsvectorlayerlabelprovider.cpp.

◆ registerFeature()

QList< QgsLabelFeature * > QgsVectorLayerLabelProvider::registerFeature ( const QgsFeature feature,
QgsRenderContext context,
const QgsGeometry obstacleGeometry = QgsGeometry(),
const QgsSymbol symbol = nullptr 
)
virtual

Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels.

Parameters
featurefeature to label
contextrender context. The QgsExpressionContext contained within the render context must have already had the feature and fields sets prior to calling this method.
obstacleGeometryoptional obstacle geometry, if a different geometry to the feature's geometry should be used as an obstacle for labels (e.g., if the feature has been rendered with an offset point symbol, the obstacle geometry should represent the bounds of the offset symbol). If not set, the feature's original geometry will be used as an obstacle for labels.
symbolfeature symbol to label (ownership is not transferred - the symbol must exist until after labeling is complete)
Returns
a list of the newly generated label features. Ownership of these label features is not transferred (it has already been assigned to the label provider).

Reimplemented in QgsRuleBasedLabelProvider.

Definition at line 197 of file qgsvectorlayerlabelprovider.cpp.

◆ setFields()

void QgsVectorLayerLabelProvider::setFields ( const QgsFields fields)

Sets fields of this label provider.

Normally this is not needed, but when used for vector tiles, fields are not known at the time of creation of label providers. It should be called before a call to prepare() which uses the list of fields.

Since
QGIS 3.14

Definition at line 841 of file qgsvectorlayerlabelprovider.cpp.

◆ settings()

const QgsPalLayerSettings & QgsVectorLayerLabelProvider::settings ( ) const

Returns the layer's settings.

Since
QGIS 3.10

Definition at line 836 of file qgsvectorlayerlabelprovider.cpp.

◆ startRender()

void QgsVectorLayerLabelProvider::startRender ( QgsRenderContext context)
overridevirtual

To be called before rendering of labels begins.

Must be accompanied by a corresponding call to stopRender()

Since
QGIS 3.10

Reimplemented from QgsAbstractLabelProvider.

Definition at line 122 of file qgsvectorlayerlabelprovider.cpp.

◆ stopRender()

void QgsVectorLayerLabelProvider::stopRender ( QgsRenderContext context)
overridevirtual

To be called after rendering is complete.

See also
startRender()
Since
QGIS 3.10

Reimplemented from QgsAbstractLabelProvider.

Definition at line 128 of file qgsvectorlayerlabelprovider.cpp.

Friends And Related Symbol Documentation

◆ QgsVectorTileBasicLabelProvider

friend class QgsVectorTileBasicLabelProvider
friend

Definition at line 148 of file qgsvectorlayerlabelprovider.h.

◆ TestQgsLabelingEngine

friend class TestQgsLabelingEngine
friend

Definition at line 147 of file qgsvectorlayerlabelprovider.h.

Member Data Documentation

◆ mCrs

QgsCoordinateReferenceSystem QgsVectorLayerLabelProvider::mCrs
protected

Layer's CRS.

Definition at line 138 of file qgsvectorlayerlabelprovider.h.

◆ mFields

QgsFields QgsVectorLayerLabelProvider::mFields
protected

Layer's fields.

Definition at line 136 of file qgsvectorlayerlabelprovider.h.

◆ mLabels

QList<QgsLabelFeature *> QgsVectorLayerLabelProvider::mLabels
protected

List of generated.

Definition at line 143 of file qgsvectorlayerlabelprovider.h.

◆ mLayerGeometryType

Qgis::GeometryType QgsVectorLayerLabelProvider::mLayerGeometryType
protected

Geometry type of layer.

Definition at line 129 of file qgsvectorlayerlabelprovider.h.

◆ mRenderer

QgsFeatureRenderer* QgsVectorLayerLabelProvider::mRenderer = nullptr
protected

Definition at line 131 of file qgsvectorlayerlabelprovider.h.

◆ mSettings

QgsPalLayerSettings QgsVectorLayerLabelProvider::mSettings
protected

Layer's labeling configuration.

Definition at line 127 of file qgsvectorlayerlabelprovider.h.

◆ mSource

std::unique_ptr<QgsAbstractFeatureSource> QgsVectorLayerLabelProvider::mSource
protected

Layer's feature source.

Definition at line 140 of file qgsvectorlayerlabelprovider.h.


The documentation for this class was generated from the following files: