QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 "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
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,
81 MergeConnectedLines = 1 << 3,
82 CentroidMustBeInside = 1 << 4,
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
165 Qgis::LabelPlacement placement() const { return mPlacement; }
166
172 double priority() const { return mPriority; }
173
175 QgsLabelObstacleSettings::ObstacleType obstacleType() const { return mObstacleType; }
176
178 Qgis::UpsideDownLabelHandling upsidedownLabels() const { return mUpsidedownLabels; }
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;
207 Flags mFlags = DrawLabels;
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
360class CORE_EXPORT QgsLabelingEngine
361{
362 public:
366 virtual ~QgsLabelingEngine();
367
370
372 void setMapSettings( const QgsMapSettings &mapSettings );
374 const QgsMapSettings &mapSettings() const { return mMapSettings; }
375
377 const QgsLabelingEngineSettings &engineSettings() const { return mMapSettings.labelingEngineSettings(); }
378
386 bool prepare( QgsRenderContext &context );
387
391 QList< QgsMapLayer * > participatingLayers() const;
392
397 QStringList participatingLayerIds() const;
398
407 QString addProvider( QgsAbstractLabelProvider *provider );
408
414 QgsAbstractLabelProvider *providerById( const QString &id );
415
417 void removeProvider( QgsAbstractLabelProvider *provider );
418
426 virtual void run( QgsRenderContext &context ) = 0;
427
429 QgsLabelingResults *takeResults();
430
432 QgsLabelingResults *results() const { return mResults.get(); }
433
439 static void drawLabelCandidateRect( pal::LabelPosition *lp, QgsRenderContext &context, const QgsMapToPixel *xform, QList<QgsLabelCandidate> *candidates = nullptr );
440
446 static void drawLabelMetrics( pal::LabelPosition *label, const QgsMapToPixel &xform, QgsRenderContext &context, const QPointF &renderPoint );
447
448 protected:
449 void processProvider( QgsAbstractLabelProvider *provider, QgsRenderContext &context, pal::Pal &p );
450
451 protected:
452
459 void registerLabels( QgsRenderContext &context );
460
469 void solve( QgsRenderContext &context );
470
481 void drawLabels( QgsRenderContext &context, const QString &layerId = QString() );
482
487 void cleanup();
488
491
493 QList<QgsAbstractLabelProvider *> mProviders;
494 QHash<QString, QgsAbstractLabelProvider *> mProvidersById;
495 QList<QgsAbstractLabelProvider *> mSubProviders;
496
498 std::vector< std::unique_ptr< QgsAbstractLabelingEngineRule > > mEngineRules;
499
501 std::unique_ptr< QgsLabelingResults > mResults;
502
503 std::unique_ptr< pal::Pal > mPal;
504 std::unique_ptr< pal::Problem > mProblem;
505 QList<pal::LabelPosition *> mUnlabeled;
506 QList<pal::LabelPosition *> mLabels;
507
508 private:
509
510 QStringList mLayerRenderingOrderIds;
511
512};
513
524{
525 public:
528
531
532 void run( QgsRenderContext &context ) override;
533
534};
535
548{
549 public:
552
555
556 void run( QgsRenderContext &context ) override;
557
562 void renderLabelsForLayer( QgsRenderContext &context, const QString &layerId );
563
568 void finalize();
569};
570
571
579class CORE_EXPORT QgsLabelingUtils
580{
581 public:
582
589 static QString encodePredefinedPositionOrder( const QVector< Qgis::LabelPredefinedPointPosition > &positions );
590
597 static QVector< Qgis::LabelPredefinedPointPosition > decodePredefinedPositionOrder( const QString &positionString );
598
603 static QString encodeLinePlacementFlags( Qgis::LabelLinePlacementFlags flags );
604
609 static Qgis::LabelLinePlacementFlags decodeLinePlacementFlags( const QString &string );
610
611};
612
613#endif
614
615#endif // QGSLABELINGENGINE_H
LabelPlacement
Placement modes which determine how label candidates are generated for a feature.
Definition qgis.h:1125
@ 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:1221
UpsideDownLabelHandling
Handling techniques for upside down labels.
Definition qgis.h:1254
@ FlipUpsideDownLabels
Upside-down labels (90 <= angle < 270) are shown upright.
The QgsAbstractLabelProvider class is an interface class.
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)
The QgsLabelFeature class 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.
The QgsLabelingEngine class 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
Class that 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:76
The QgsMapSettings class contains configuration for rendering of the map.
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)