QGIS API Documentation 3.43.0-Master (32433f7016e)
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 "qgsmapsettings.h"
21
22#include "qgsfeedback.h"
24
27class QgsLabelFeature;
29
30#ifndef SIP_RUN
31namespace pal
32{
33 class Problem;
34 class Pal;
35 class LabelPosition;
36}
37#endif
38
43class CORE_EXPORT QgsLabelCandidate
44{
45 public:
46 QgsLabelCandidate( const QRectF &r, double c ): rect( r ), cost( c ) {}
47
48 QRectF rect;
49 double cost;
50};
51
52
53#ifndef SIP_RUN
54
67class CORE_EXPORT QgsAbstractLabelProvider
68{
69
70 public:
72 QgsAbstractLabelProvider( QgsMapLayer *layer, const QString &providerId = QString() );
73
74 virtual ~QgsAbstractLabelProvider() = default;
75
77 void setEngine( const QgsLabelingEngine *engine ) { mEngine = engine; }
78
79 enum Flag
80 {
81 DrawLabels = 1 << 1,
82 MergeConnectedLines = 1 << 3,
83 CentroidMustBeInside = 1 << 4,
84 };
85 Q_DECLARE_FLAGS( Flags, Flag )
86
87
88 virtual QList<QgsLabelFeature *> labelFeatures( QgsRenderContext &context ) = 0;
89
96 virtual void drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const = 0;
97
109 virtual void drawUnplacedLabel( QgsRenderContext &context, pal::LabelPosition *label ) const;
110
123 virtual void drawLabelBackground( QgsRenderContext &context, pal::LabelPosition *label ) const;
124
130 virtual void startRender( QgsRenderContext &context );
131
137 virtual void stopRender( QgsRenderContext &context );
138
140 virtual QList<QgsAbstractLabelProvider *> subProviders() { return QList<QgsAbstractLabelProvider *>(); }
141
143 QString name() const { return mName; }
144
146 QString layerId() const { return mLayerId; }
147
153 QgsMapLayer *layer() const { return mLayer.data(); }
154
160 QString providerId() const { return mProviderId; }
161
163 Flags flags() const { return mFlags; }
164
166 Qgis::LabelPlacement placement() const { return mPlacement; }
167
173 double priority() const { return mPriority; }
174
176 QgsLabelObstacleSettings::ObstacleType obstacleType() const { return mObstacleType; }
177
179 Qgis::UpsideDownLabelHandling upsidedownLabels() const { return mUpsidedownLabels; }
180
186 QgsExpressionContextScope *layerExpressionContextScope() const;
187
193 double layerReferenceScale() const { return mLayerReferenceScale; }
194
195 protected:
197 const QgsLabelingEngine *mEngine = nullptr;
198
200 QString mName;
202 QString mLayerId;
206 QString mProviderId;
208 Flags mFlags = DrawLabels;
212 double mPriority = 0.5;
217
218 private:
219
220 std::unique_ptr< QgsExpressionContextScope > mLayerExpressionContextScope;
221 double mLayerReferenceScale = -1;
222};
223
225
226
227
328
362class CORE_EXPORT QgsLabelingEngine
363{
364 public:
368 virtual ~QgsLabelingEngine();
369
372
374 void setMapSettings( const QgsMapSettings &mapSettings );
376 const QgsMapSettings &mapSettings() const { return mMapSettings; }
377
379 const QgsLabelingEngineSettings &engineSettings() const { return mMapSettings.labelingEngineSettings(); }
380
388 bool prepare( QgsRenderContext &context );
389
393 QList< QgsMapLayer * > participatingLayers() const;
394
399 QStringList participatingLayerIds() const;
400
409 QString addProvider( QgsAbstractLabelProvider *provider );
410
416 QgsAbstractLabelProvider *providerById( const QString &id );
417
419 void removeProvider( QgsAbstractLabelProvider *provider );
420
428 virtual void run( QgsRenderContext &context ) = 0;
429
431 QgsLabelingResults *takeResults();
432
434 QgsLabelingResults *results() const { return mResults.get(); }
435
441 static void drawLabelCandidateRect( pal::LabelPosition *lp, QgsRenderContext &context, const QgsMapToPixel *xform, QList<QgsLabelCandidate> *candidates = nullptr );
442
448 static void drawLabelMetrics( pal::LabelPosition *label, const QgsMapToPixel &xform, QgsRenderContext &context, const QPointF &renderPoint );
449
450 protected:
451 void processProvider( QgsAbstractLabelProvider *provider, QgsRenderContext &context, pal::Pal &p );
452
453 protected:
454
461 void registerLabels( QgsRenderContext &context );
462
471 void solve( QgsRenderContext &context );
472
483 void drawLabels( QgsRenderContext &context, const QString &layerId = QString() );
484
489 void cleanup();
490
493
495 QList<QgsAbstractLabelProvider *> mProviders;
496 QHash<QString, QgsAbstractLabelProvider *> mProvidersById;
497 QList<QgsAbstractLabelProvider *> mSubProviders;
498
500 std::vector< std::unique_ptr< QgsAbstractLabelingEngineRule > > mEngineRules;
501
503 std::unique_ptr< QgsLabelingResults > mResults;
504
505 std::unique_ptr< pal::Pal > mPal;
506 std::unique_ptr< pal::Problem > mProblem;
507 QList<pal::LabelPosition *> mUnlabeled;
508 QList<pal::LabelPosition *> mLabels;
509
510 private:
511
512 QStringList mLayerRenderingOrderIds;
513
514};
515
526{
527 public:
530
533
534 void run( QgsRenderContext &context ) override;
535
536};
537
550{
551 public:
554
557
558 void run( QgsRenderContext &context ) override;
559
564 void renderLabelsForLayer( QgsRenderContext &context, const QString &layerId );
565
570 void finalize();
571};
572
573
581class CORE_EXPORT QgsLabelingUtils
582{
583 public:
584
591 static QString encodePredefinedPositionOrder( const QVector< Qgis::LabelPredefinedPointPosition > &positions );
592
599 static QVector< Qgis::LabelPredefinedPointPosition > decodePredefinedPositionOrder( const QString &positionString );
600
605 static QString encodeLinePlacementFlags( Qgis::LabelLinePlacementFlags flags );
606
611 static Qgis::LabelLinePlacementFlags decodeLinePlacementFlags( const QString &string );
612
613};
614
615#endif
616
617#endif // QGSLABELINGENGINE_H
LabelPlacement
Placement modes which determine how label candidates are generated for a feature.
Definition qgis.h:1158
@ AroundPoint
Arranges candidates in a circle around a point (or centroid of a polygon). Applies to point or polygo...
QFlags< LabelLinePlacementFlag > LabelLinePlacementFlags
Line placement flags, which control how candidates are generated for a linear feature.
Definition qgis.h:1254
UpsideDownLabelHandling
Handling techniques for upside down labels.
Definition qgis.h:1287
@ FlipUpsideDownLabels
Upside-down labels (90 <= angle < 270) are shown upright.
An abstract interface class for label providers.
QString mName
Name of the layer.
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)
QString mLayerId
Associated layer's ID, if applicable.
double priority() const
Default priority of labels (may be overridden by individual labels).
QString name() const
Name of the layer (for statistics, debugging etc.) - does not need to be unique.
double layerReferenceScale() const
Returns the symbology reference scale of the layer associated with this provider.
QgsMapLayer * layer() const
Returns the associated layer, or nullptr if no layer is associated with the provider.
Flags flags() const
Flags associated with the provider.
QgsLabelObstacleSettings::ObstacleType obstacleType() const
How the feature geometries will work as obstacles.
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.
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.
QString mProviderId
Associated provider ID (one layer may have multiple providers, e.g. in rule-based labeling)
Default QgsLabelingEngine implementation, which completes the whole labeling operation (including lab...
QgsDefaultLabelingEngine(const QgsDefaultLabelingEngine &rh)=delete
QgsDefaultLabelingEngine & operator=(const QgsDefaultLabelingEngine &rh)=delete
Single scope for storing variables and functions for use within a QgsExpressionContext.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Represents a label candidate.
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)
QgsFeedback subclass for granular reporting of labeling engine progress.
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
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.
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.
Base class for all map layer types.
Definition qgsmaplayer.h:77
Contains configuration for rendering maps.
Perform transforms between map coordinates and device coordinates.
Contains information about the context of a rendering operation.
A QgsLabelingEngine implementation, which only calculates the labeling solution during its run() meth...
QgsStagedRenderLabelingEngine & operator=(const QgsStagedRenderLabelingEngine &rh)=delete
QgsStagedRenderLabelingEngine(const QgsStagedRenderLabelingEngine &rh)=delete
LabelPosition is a candidate feature label position.
Main Pal labeling class.
Definition pal.h:83
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(QgsTextRendererUtils::CurvedTextFlags)