QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #define SIP_NO_FILE
20 
21 #include "qgis_core.h"
22 #include "qgsmapsettings.h"
23 
25 #include "qgslabeling.h"
26 #include "qgsfeedback.h"
28 
29 class QgsLabelingEngine;
30 class QgsLabelingResults;
31 class QgsLabelFeature;
32 
33 namespace pal
34 {
35  class Problem;
36  class Pal;
37  class LabelPosition;
38 }
39 
52 class CORE_EXPORT QgsAbstractLabelProvider
53 {
54 
55  public:
57  QgsAbstractLabelProvider( QgsMapLayer *layer, const QString &providerId = QString() );
58 
59  virtual ~QgsAbstractLabelProvider() = default;
60 
62  void setEngine( const QgsLabelingEngine *engine ) { mEngine = engine; }
63 
64  enum Flag
65  {
66  DrawLabels = 1 << 1,
67  MergeConnectedLines = 1 << 3,
68  CentroidMustBeInside = 1 << 4,
69  };
70  Q_DECLARE_FLAGS( Flags, Flag )
71 
72 
73  virtual QList<QgsLabelFeature *> labelFeatures( QgsRenderContext &context ) = 0;
74 
81  virtual void drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const = 0;
82 
94  virtual void drawUnplacedLabel( QgsRenderContext &context, pal::LabelPosition *label ) const;
95 
108  virtual void drawLabelBackground( QgsRenderContext &context, pal::LabelPosition *label ) const;
109 
115  virtual void startRender( QgsRenderContext &context );
116 
122  virtual void stopRender( QgsRenderContext &context );
123 
125  virtual QList<QgsAbstractLabelProvider *> subProviders() { return QList<QgsAbstractLabelProvider *>(); }
126 
128  QString name() const { return mName; }
129 
131  QString layerId() const { return mLayerId; }
132 
138  QgsMapLayer *layer() const { return mLayer.data(); }
139 
145  QString providerId() const { return mProviderId; }
146 
148  Flags flags() const { return mFlags; }
149 
151  Qgis::LabelPlacement placement() const { return mPlacement; }
152 
154  double priority() const { return mPriority; }
155 
157  QgsLabelObstacleSettings::ObstacleType obstacleType() const { return mObstacleType; }
158 
160  Qgis::UpsideDownLabelHandling upsidedownLabels() const { return mUpsidedownLabels; }
161 
167  QgsExpressionContextScope *layerExpressionContextScope() const;
168 
174  double layerReferenceScale() const { return mLayerReferenceScale; }
175 
176  protected:
178  const QgsLabelingEngine *mEngine = nullptr;
179 
181  QString mName;
183  QString mLayerId;
187  QString mProviderId;
189  Flags mFlags = DrawLabels;
193  double mPriority = 0.5;
197  Qgis::UpsideDownLabelHandling mUpsidedownLabels = Qgis::UpsideDownLabelHandling::FlipUpsideDownLabels;
198 
199  private:
200 
201  std::unique_ptr< QgsExpressionContextScope > mLayerExpressionContextScope;
202  double mLayerReferenceScale = -1;
203 };
204 
205 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsAbstractLabelProvider::Flags )
206 
207 
208 
214 class CORE_EXPORT QgsLabelingEngineFeedback : public QgsFeedback
215 {
216  Q_OBJECT
217 
218  public:
219 
223  QgsLabelingEngineFeedback( QObject *parent SIP_TRANSFERTHIS = nullptr )
224  : QgsFeedback( parent )
225  {}
226 
227  signals:
228 
232  void labelRegistrationAboutToBegin();
233 
237  void labelRegistrationFinished();
238 
242  void providerRegistrationAboutToBegin( QgsAbstractLabelProvider *provider );
243 
247  void providerRegistrationFinished( QgsAbstractLabelProvider *provider );
248 
252  void candidateCreationAboutToBegin( QgsAbstractLabelProvider *provider );
253 
257  void candidateCreationFinished( QgsAbstractLabelProvider *provider );
258 
262  void obstacleCostingAboutToBegin();
263 
267  void obstacleCostingFinished();
268 
272  void calculatingConflictsAboutToBegin();
273 
277  void calculatingConflictsFinished();
278 
282  void finalizingCandidatesAboutToBegin();
283 
287  void finalizingCandidatesFinished();
288 
292  void reductionAboutToBegin();
293 
297  void reductionFinished();
298 
302  void solvingPlacementAboutToBegin();
303 
307  void solvingPlacementFinished();
308 };
309 
343 class CORE_EXPORT QgsLabelingEngine
344 {
345  public:
349  virtual ~QgsLabelingEngine();
350 
352  QgsLabelingEngine( const QgsLabelingEngine &rh ) = delete;
354  QgsLabelingEngine &operator=( const QgsLabelingEngine &rh ) = delete;
355 
357  void setMapSettings( const QgsMapSettings &mapSettings );
359  const QgsMapSettings &mapSettings() const { return mMapSettings; }
360 
362  const QgsLabelingEngineSettings &engineSettings() const { return mMapSettings.labelingEngineSettings(); }
363 
368  QList< QgsMapLayer * > participatingLayers() const;
369 
374  QStringList participatingLayerIds() const;
375 
377  void addProvider( QgsAbstractLabelProvider *provider );
378 
380  void removeProvider( QgsAbstractLabelProvider *provider );
381 
389  virtual void run( QgsRenderContext &context ) = 0;
390 
392  QgsLabelingResults *takeResults();
393 
395  QgsLabelingResults *results() const { return mResults.get(); }
396 
397  protected:
398  void processProvider( QgsAbstractLabelProvider *provider, QgsRenderContext &context, pal::Pal &p );
399 
400  protected:
401 
408  void registerLabels( QgsRenderContext &context );
409 
418  void solve( QgsRenderContext &context );
419 
430  void drawLabels( QgsRenderContext &context, const QString &layerId = QString() );
431 
436  void cleanup();
437 
440 
442  QList<QgsAbstractLabelProvider *> mProviders;
443  QList<QgsAbstractLabelProvider *> mSubProviders;
444 
446  std::unique_ptr< QgsLabelingResults > mResults;
447 
448  std::unique_ptr< pal::Pal > mPal;
449  std::unique_ptr< pal::Problem > mProblem;
450  QList<pal::LabelPosition *> mUnlabeled;
451  QList<pal::LabelPosition *> mLabels;
452 
453 };
454 
464 class CORE_EXPORT QgsDefaultLabelingEngine : public QgsLabelingEngine
465 {
466  public:
469 
474 
475  void run( QgsRenderContext &context ) override;
476 
477 };
478 
491 {
492  public:
495 
500 
501  void run( QgsRenderContext &context ) override;
502 
507  void renderLabelsForLayer( QgsRenderContext &context, const QString &layerId );
508 
513  void finalize();
514 };
515 
516 
525 class CORE_EXPORT QgsLabelingUtils
526 {
527  public:
528 
535  static QString encodePredefinedPositionOrder( const QVector< Qgis::LabelPredefinedPointPosition > &positions );
536 
543  static QVector< Qgis::LabelPredefinedPointPosition > decodePredefinedPositionOrder( const QString &positionString );
544 
549  static QString encodeLinePlacementFlags( QgsLabeling::LinePlacementFlags flags );
550 
555  static QgsLabeling::LinePlacementFlags decodeLinePlacementFlags( const QString &string );
556 
557 };
558 
559 #endif // QGSLABELINGENGINE_H
QgsLabelingUtils
Contains helper utilities for working with QGIS' labeling engine.
Definition: qgslabelingengine.h:525
QgsAbstractLabelProvider::layerId
QString layerId() const
Returns ID of associated layer, or empty string if no layer is associated with the provider.
Definition: qgslabelingengine.h:131
QgsLabelingEngine::mPal
std::unique_ptr< pal::Pal > mPal
Definition: qgslabelingengine.h:448
Qgis::UpsideDownLabelHandling
UpsideDownLabelHandling
Handling techniques for upside down labels.
Definition: qgis.h:642
qgslabelingenginesettings.h
QgsLabelingEngine::mProviders
QList< QgsAbstractLabelProvider * > mProviders
List of providers (the are owned by the labeling engine)
Definition: qgslabelingengine.h:442
QgsAbstractLabelProvider::placement
Qgis::LabelPlacement placement() const
What placement strategy to use for the labels.
Definition: qgslabelingengine.h:151
QgsStagedRenderLabelingEngine
A QgsLabelingEngine implementation, which only calculates the labeling solution during its run() meth...
Definition: qgslabelingengine.h:490
QgsAbstractLabelProvider::mName
QString mName
Name of the layer.
Definition: qgslabelingengine.h:181
pal::LabelPosition
LabelPosition is a candidate feature label position.
Definition: labelposition.h:55
QgsLabelingResults
Class that stores computed placement from labeling engine.
Definition: qgslabelingresults.h:32
QgsAbstractLabelProvider::obstacleType
QgsLabelObstacleSettings::ObstacleType obstacleType() const
How the feature geometries will work as obstacles.
Definition: qgslabelingengine.h:157
QgsLabelingEngine::mUnlabeled
QList< pal::LabelPosition * > mUnlabeled
Definition: qgslabelingengine.h:450
QgsAbstractLabelProvider::Flag
Flag
Definition: qgslabelingengine.h:64
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsDefaultLabelingEngine
Default QgsLabelingEngine implementation, which completes the whole labeling operation (including lab...
Definition: qgslabelingengine.h:464
QgsLabelingEngine::mSubProviders
QList< QgsAbstractLabelProvider * > mSubProviders
Definition: qgslabelingengine.h:443
qgsmapsettings.h
QgsAbstractLabelProvider::priority
double priority() const
Default priority of labels (may be overridden by individual labels)
Definition: qgslabelingengine.h:154
QgsAbstractLabelProvider::mProviderId
QString mProviderId
Associated provider ID (one layer may have multiple providers, e.g. in rule-based labeling)
Definition: qgslabelingengine.h:187
pal
Definition: qgsdiagramrenderer.h:50
QgsLabelingEngine::engineSettings
const QgsLabelingEngineSettings & engineSettings() const
Gets associated labeling engine settings.
Definition: qgslabelingengine.h:362
QgsAbstractLabelProvider::mLayerId
QString mLayerId
Associated layer's ID, if applicable.
Definition: qgslabelingengine.h:183
QgsAbstractLabelProvider
The QgsAbstractLabelProvider class is an interface class. Implementations return list of labels and t...
Definition: qgslabelingengine.h:52
QgsLabelingEngineSettings
Stores global configuration for labeling engine.
Definition: qgslabelingenginesettings.h:31
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsAbstractLabelProvider::setEngine
void setEngine(const QgsLabelingEngine *engine)
Associate provider with a labeling engine (should be only called internally from QgsLabelingEngine)
Definition: qgslabelingengine.h:62
QgsAbstractLabelProvider::name
QString name() const
Name of the layer (for statistics, debugging etc.) - does not need to be unique.
Definition: qgslabelingengine.h:128
Q_DECLARE_OPERATORS_FOR_FLAGS
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QgsLabelingEngine::run
virtual void run(QgsRenderContext &context)=0
Runs the labeling job.
QgsAbstractLabelProvider::subProviders
virtual QList< QgsAbstractLabelProvider * > subProviders()
Returns list of child providers - useful if the provider needs to put labels into more layers with di...
Definition: qgslabelingengine.h:125
QgsAbstractLabelProvider::layerReferenceScale
double layerReferenceScale() const
Returns the symbology reference scale of the layer associated with this provider.
Definition: qgslabelingengine.h:174
QgsLabelingEngine::operator=
QgsLabelingEngine & operator=(const QgsLabelingEngine &rh)=delete
QgsLabelingEngine cannot be copied.
QgsWeakMapLayerPointer
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:2146
QgsAbstractLabelProvider::providerId
QString providerId() const
Returns provider ID - useful in case there is more than one label provider within a layer (e....
Definition: qgslabelingengine.h:145
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext....
Definition: qgsexpressioncontext.h:113
QgsAbstractLabelProvider::mLayer
QgsWeakMapLayerPointer mLayer
Weak pointer to source layer.
Definition: qgslabelingengine.h:185
QgsLabelingEngine::mProblem
std::unique_ptr< pal::Problem > mProblem
Definition: qgslabelingengine.h:449
QgsLabelingEngine
The QgsLabelingEngine class provides map labeling functionality. The input for the engine is a list o...
Definition: qgslabelingengine.h:343
qgslabeling.h
QgsLabelingEngine::mapSettings
const QgsMapSettings & mapSettings() const
Gets associated map settings.
Definition: qgslabelingengine.h:359
QgsLabelingEngine::results
QgsLabelingResults * results() const
For internal use by the providers.
Definition: qgslabelingengine.h:395
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsLabelObstacleSettings::PolygonBoundary
@ PolygonBoundary
Definition: qgslabelobstaclesettings.h:46
pal::Pal
Main Pal labeling class.
Definition: pal.h:79
QgsLabelingEngine::mResults
std::unique_ptr< QgsLabelingResults > mResults
Resulting labeling layout.
Definition: qgslabelingengine.h:446
QgsLabelingEngine::mMapSettings
QgsMapSettings mMapSettings
Associated map settings instance.
Definition: qgslabelingengine.h:439
QgsAbstractLabelProvider::layer
QgsMapLayer * layer() const
Returns the associated layer, or nullptr if no layer is associated with the provider.
Definition: qgslabelingengine.h:138
QgsAbstractLabelProvider::flags
Flags flags() const
Flags associated with the provider.
Definition: qgslabelingengine.h:148
QgsAbstractLabelProvider::upsidedownLabels
Qgis::UpsideDownLabelHandling upsidedownLabels() const
How to handle labels that would be upside down.
Definition: qgslabelingengine.h:160
QgsLabelObstacleSettings::ObstacleType
ObstacleType
Valid obstacle types, which affect how features within the layer will act as obstacles for labels.
Definition: qgslabelobstaclesettings.h:42
QgsLabelFeature
The QgsLabelFeature class describes a feature that should be used within the labeling engine....
Definition: qgslabelfeature.h:57
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
Definition: qgsmapsettings.h:88
qgsfeedback.h
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsLabelingEngineFeedback::QgsLabelingEngineFeedback
QgsLabelingEngineFeedback(QObject *parent=nullptr)
Constructor for QgsLabelingEngineFeedback, with the specified parent object.
Definition: qgslabelingengine.h:223
QgsLabelingEngineFeedback
QgsFeedback subclass for granular reporting of labeling engine progress.
Definition: qgslabelingengine.h:214
Qgis::LabelPlacement::AroundPoint
@ AroundPoint
Arranges candidates in a circle around a point (or centroid of a polygon). Applies to point or polygo...
QgsLabelingEngine::mLabels
QList< pal::LabelPosition * > mLabels
Definition: qgslabelingengine.h:451
Qgis::LabelPlacement
LabelPlacement
Placement modes which determine how label candidates are generated for a feature.
Definition: qgis.h:560
qgslabelobstaclesettings.h