QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgslabelingengine.h
Go to the documentation of this file.
1/***************************************************************************
2 qgslabelingengine.h
3 --------------------------------------
4 Date : September 2015
5 Copyright : (C) 2015 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSLABELINGENGINE_H
17#define QGSLABELINGENGINE_H
18
19#include "qgis_core.h"
20#include "qgsfeedback.h"
22#include "qgsmapsettings.h"
23
26class QgsLabelFeature;
28
29#ifndef SIP_RUN
30namespace pal
31{
32 class Problem;
33 class Pal;
34 class LabelPosition;
35}
36#endif
37
42class CORE_EXPORT QgsLabelCandidate
43{
44 public:
45 QgsLabelCandidate( const QRectF &r, double c ): rect( r ), cost( c ) {}
46
47 QRectF rect;
48 double cost;
49};
50
51
52#ifndef SIP_RUN
53
66class CORE_EXPORT QgsAbstractLabelProvider
67{
68
69 public:
71 QgsAbstractLabelProvider( QgsMapLayer *layer, const QString &providerId = QString() );
72
73 virtual ~QgsAbstractLabelProvider() = default;
74
76 void setEngine( const QgsLabelingEngine *engine ) { mEngine = engine; }
77
78 enum Flag
79 {
80 DrawLabels = 1 << 1,
83 };
84 Q_DECLARE_FLAGS( Flags, Flag )
85
86
87 virtual QList<QgsLabelFeature *> labelFeatures( QgsRenderContext &context ) = 0;
88
95 virtual void drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const = 0;
96
108 virtual void drawUnplacedLabel( QgsRenderContext &context, pal::LabelPosition *label ) const;
109
122 virtual void drawLabelBackground( QgsRenderContext &context, pal::LabelPosition *label ) const;
123
129 virtual void startRender( QgsRenderContext &context );
130
136 virtual void stopRender( QgsRenderContext &context );
137
139 virtual QList<QgsAbstractLabelProvider *> subProviders() { return QList<QgsAbstractLabelProvider *>(); }
140
142 QString name() const { return mName; }
143
145 QString layerId() const { return mLayerId; }
146
152 QgsMapLayer *layer() const { return mLayer.data(); }
153
159 QString providerId() const { return mProviderId; }
160
162 Flags flags() const { return mFlags; }
163
166
172 double priority() const { return mPriority; }
173
176
179
185 QgsExpressionContextScope *layerExpressionContextScope() const;
186
192 double layerReferenceScale() const { return mLayerReferenceScale; }
193
194 protected:
196 const QgsLabelingEngine *mEngine = nullptr;
197
199 QString mName;
201 QString mLayerId;
205 QString mProviderId;
211 double mPriority = 0.5;
216
217 private:
218
219 std::unique_ptr< QgsExpressionContextScope > mLayerExpressionContextScope;
220 double mLayerReferenceScale = -1;
221};
222
224
225
226
327
361class CORE_EXPORT QgsLabelingEngine
362{
363 public:
367 virtual ~QgsLabelingEngine();
368
371
375 const QgsMapSettings &mapSettings() const { return mMapSettings; }
376
378 const QgsLabelingEngineSettings &engineSettings() const { return mMapSettings.labelingEngineSettings(); }
379
387 bool prepare( QgsRenderContext &context );
388
392 QList< QgsMapLayer * > participatingLayers() const;
393
398 QStringList participatingLayerIds() const;
399
408 QString addProvider( QgsAbstractLabelProvider *provider );
409
415 QgsAbstractLabelProvider *providerById( const QString &id );
416
418 void removeProvider( QgsAbstractLabelProvider *provider );
419
427 virtual void run( QgsRenderContext &context ) = 0;
428
431
433 QgsLabelingResults *results() const { return mResults.get(); }
434
440 static void drawLabelCandidateRect( pal::LabelPosition *lp, QgsRenderContext &context, const QgsMapToPixel *xform, QList<QgsLabelCandidate> *candidates = nullptr );
441
447 static void drawLabelMetrics( pal::LabelPosition *label, const QgsMapToPixel &xform, QgsRenderContext &context, const QPointF &renderPoint );
448
449 protected:
450 void processProvider( QgsAbstractLabelProvider *provider, QgsRenderContext &context, pal::Pal &p );
451
452 protected:
453
460 void registerLabels( QgsRenderContext &context );
461
470 void solve( QgsRenderContext &context );
471
482 void drawLabels( QgsRenderContext &context, const QString &layerId = QString() );
483
488 void cleanup();
489
492
494 QList<QgsAbstractLabelProvider *> mProviders;
495 QHash<QString, QgsAbstractLabelProvider *> mProvidersById;
496 QList<QgsAbstractLabelProvider *> mSubProviders;
497
499 std::vector< std::unique_ptr< QgsAbstractLabelingEngineRule > > mEngineRules;
500
502 std::unique_ptr< QgsLabelingResults > mResults;
503
504 std::unique_ptr< pal::Pal > mPal;
505 std::unique_ptr< pal::Problem > mProblem;
506 QList<pal::LabelPosition *> mUnlabeled;
507 QList<pal::LabelPosition *> mLabels;
508
509 private:
510
511 QStringList mLayerRenderingOrderIds;
512
513};
514
525{
526 public:
529
532
533 void run( QgsRenderContext &context ) override;
534
535};
536
549{
550 public:
553
556
557 void run( QgsRenderContext &context ) override;
558
563 void renderLabelsForLayer( QgsRenderContext &context, const QString &layerId );
564
569 void finalize();
570};
571
572
580class CORE_EXPORT QgsLabelingUtils
581{
582 public:
583
590 static QString encodePredefinedPositionOrder( const QVector< Qgis::LabelPredefinedPointPosition > &positions );
591
598 static QVector< Qgis::LabelPredefinedPointPosition > decodePredefinedPositionOrder( const QString &positionString );
599
605
610 static Qgis::LabelLinePlacementFlags decodeLinePlacementFlags( const QString &string );
611
612};
613
614#endif
615
616#endif // QGSLABELINGENGINE_H
LabelPlacement
Placement modes which determine how label candidates are generated for a feature.
Definition qgis.h:1194
@ AroundPoint
Arranges candidates in a circle around a point (or centroid of a polygon). Applies to point or polygo...
Definition qgis.h:1195
QFlags< LabelLinePlacementFlag > LabelLinePlacementFlags
Line placement flags, which control how candidates are generated for a linear feature.
Definition qgis.h:1290
UpsideDownLabelHandling
Handling techniques for upside down labels.
Definition qgis.h:1323
@ FlipUpsideDownLabels
Upside-down labels (90 <= angle < 270) are shown upright.
Definition qgis.h:1324
An abstract interface class for label providers.
virtual QList< QgsLabelFeature * > labelFeatures(QgsRenderContext &context)=0
Returns list of label features (they are owned by the provider and thus deleted on its destruction).
QgsLabelObstacleSettings::ObstacleType mObstacleType
Type of the obstacle of feature geometries.
QString mName
Name of the layer.
virtual void drawUnplacedLabel(QgsRenderContext &context, pal::LabelPosition *label) const
Draw an unplaced label.
virtual void stopRender(QgsRenderContext &context)
To be called after rendering is complete.
virtual QList< QgsAbstractLabelProvider * > subProviders()
Returns list of child providers - useful if the provider needs to put labels into more layers with di...
virtual ~QgsAbstractLabelProvider()=default
Qgis::LabelPlacement placement() const
What placement strategy to use for the labels.
void setEngine(const QgsLabelingEngine *engine)
Associate provider with a labeling engine (should be only called internally from QgsLabelingEngine).
virtual void drawLabel(QgsRenderContext &context, pal::LabelPosition *label) const =0
Draw this label at the position determined by the labeling engine.
QString mLayerId
Associated layer's ID, if applicable.
double priority() const
Default priority of labels (may be overridden by individual labels).
virtual void drawLabelBackground(QgsRenderContext &context, pal::LabelPosition *label) const
Draw the background for the specified label.
QString name() const
Name of the layer (for statistics, debugging etc.) - does not need to be unique.
double mPriority
Default priority of labels. 0 = highest priority, 1 = lowest priority.
double layerReferenceScale() const
Returns the symbology reference scale of the layer associated with this provider.
const QgsLabelingEngine * mEngine
Associated labeling engine.
QgsMapLayer * layer() const
Returns the associated layer, or nullptr if no layer is associated with the provider.
Flags mFlags
Flags altering drawing and registration of features.
virtual void startRender(QgsRenderContext &context)
To be called before rendering of labels begins.
Qgis::LabelPlacement mPlacement
Placement strategy.
Flags flags() const
Flags associated with the provider.
QgsLabelObstacleSettings::ObstacleType obstacleType() const
How the feature geometries will work as obstacles.
@ MergeConnectedLines
Whether adjacent lines (with the same label text) should be merged.
@ DrawLabels
Whether the labels should be rendered.
@ CentroidMustBeInside
Whether location of centroid must be inside of polygons.
QString layerId() const
Returns ID of associated layer, or empty string if no layer is associated with the provider.
QgsWeakMapLayerPointer mLayer
Weak pointer to source layer.
Qgis::UpsideDownLabelHandling mUpsidedownLabels
How to handle labels that would be upside down.
QString providerId() const
Returns provider ID - useful in case there is more than one label provider within a layer (e....
Qgis::UpsideDownLabelHandling upsidedownLabels() const
How to handle labels that would be upside down.
QgsAbstractLabelProvider(QgsMapLayer *layer, const QString &providerId=QString())
Construct the provider with default values.
QString mProviderId
Associated provider ID (one layer may have multiple providers, e.g. in rule-based labeling).
QgsDefaultLabelingEngine()
Construct the labeling engine with default settings.
QgsDefaultLabelingEngine(const QgsDefaultLabelingEngine &rh)=delete
QgsDefaultLabelingEngine & operator=(const QgsDefaultLabelingEngine &rh)=delete
void run(QgsRenderContext &context) override
Runs the labeling job.
Single scope for storing variables and functions for use within a QgsExpressionContext.
QgsFeedback(QObject *parent=nullptr)
Construct a feedback object.
Definition qgsfeedback.h:48
QgsLabelCandidate(const QRectF &r, double c)
Describes a feature that should be used within the labeling engine.
ObstacleType
Valid obstacle types, which affect how features within the layer will act as obstacles for labels.
@ PolygonBoundary
Avoid placing labels over boundary of polygon (prefer placing outside or completely inside polygon).
void obstacleCostingAboutToBegin()
Emitted when the obstacle costing is about to begin.
void solvingPlacementAboutToBegin()
Emitted when the problem solving step is about to begin.
void calculatingConflictsAboutToBegin()
Emitted when the conflict handling step is about to begin.
QgsLabelingEngineFeedback(QObject *parent=nullptr)
Constructor for QgsLabelingEngineFeedback, with the specified parent object.
void reductionAboutToBegin()
Emitted when the candidate reduction step is about to begin.
void finalizingCandidatesFinished()
Emitted when the label candidates are finalized.
void labelRegistrationAboutToBegin()
Emitted when the label registration is about to begin.
void labelRegistrationFinished()
Emitted when the label registration has completed for all providers.
void solvingPlacementFinished()
Emitted when the problem solving step is finished.
void finalizingCandidatesAboutToBegin()
Emitted when the label candidates are about to be finalized.
void obstacleCostingFinished()
Emitted when the obstacle costing has completed.
void reductionFinished()
Emitted when the candidate reduction step is finished.
void calculatingConflictsFinished()
Emitted when the conflict handling step has completed.
void candidateCreationFinished(QgsAbstractLabelProvider *provider)
Emitted when the label candidate creation has completed for a provider.
void candidateCreationAboutToBegin(QgsAbstractLabelProvider *provider)
Emitted when the label candidate creation is about to begin for a provider.
void providerRegistrationFinished(QgsAbstractLabelProvider *provider)
Emitted when the label registration has completed for a provider.
void providerRegistrationAboutToBegin(QgsAbstractLabelProvider *provider)
Emitted when the label registration is about to begin for a provider.
Stores global configuration for labeling engine.
Provides map labeling functionality.
std::unique_ptr< pal::Pal > mPal
const QgsLabelingEngineSettings & engineSettings() const
Gets associated labeling engine settings.
std::unique_ptr< QgsLabelingResults > mResults
Resulting labeling layout.
QgsMapSettings mMapSettings
Associated map settings instance.
QList< pal::LabelPosition * > mUnlabeled
std::vector< std::unique_ptr< QgsAbstractLabelingEngineRule > > mEngineRules
std::unique_ptr< pal::Problem > mProblem
const QgsMapSettings & mapSettings() const
Gets associated map settings.
QList< pal::LabelPosition * > mLabels
QgsLabelingResults * takeResults()
Returns pointer to recently computed results and pass the ownership of results to the caller.
void setMapSettings(const QgsMapSettings &mapSettings)
Associate map settings instance.
QList< QgsAbstractLabelProvider * > mSubProviders
List of labeling engine rules (owned by the labeling engine).
virtual void run(QgsRenderContext &context)=0
Runs the labeling job.
QgsLabelingResults * results() const
For internal use by the providers.
QgsLabelingEngine()
Construct the labeling engine with default settings.
QHash< QString, QgsAbstractLabelProvider * > mProvidersById
QgsLabelingEngine(const QgsLabelingEngine &rh)=delete
QList< QgsAbstractLabelProvider * > mProviders
List of providers (the are owned by the labeling engine).
QgsLabelingEngine & operator=(const QgsLabelingEngine &rh)=delete
Stores computed placement from labeling engine.
Contains helper utilities for working with QGIS' labeling engine.
static QString encodePredefinedPositionOrder(const QVector< Qgis::LabelPredefinedPointPosition > &positions)
Encodes an ordered list of predefined point label positions to a string.
static QVector< Qgis::LabelPredefinedPointPosition > decodePredefinedPositionOrder(const QString &positionString)
Decodes a string to an ordered list of predefined point label positions.
static Qgis::LabelLinePlacementFlags decodeLinePlacementFlags(const QString &string)
Decodes a string to set of line placement flags.
static QString encodeLinePlacementFlags(Qgis::LabelLinePlacementFlags flags)
Encodes line placement flags to a string.
Base class for all map layer types.
Definition qgsmaplayer.h:80
Contains configuration for rendering maps.
Perform transforms between map coordinates and device coordinates.
Contains information about the context of a rendering operation.
void finalize()
Finalizes and cleans up the engine following the rendering of labels for the last layer to be labeled...
void run(QgsRenderContext &context) override
Runs the labeling job.
QgsStagedRenderLabelingEngine & operator=(const QgsStagedRenderLabelingEngine &rh)=delete
QgsStagedRenderLabelingEngine()
Construct the labeling engine with default settings.
QgsStagedRenderLabelingEngine(const QgsStagedRenderLabelingEngine &rh)=delete
void renderLabelsForLayer(QgsRenderContext &context, const QString &layerId)
Renders all the labels which belong only to the layer with matching layerId to the specified render c...
LabelPosition is a candidate feature label position.
Main Pal labeling class.
Definition pal.h:84
Representation of a labeling problem.
Definition problem.h:75
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)