QGIS API Documentation 4.1.0-Master (60fea48833c)
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} //namespace pal
36#endif
37
42class CORE_EXPORT QgsLabelCandidate
43{
44 public:
45 QgsLabelCandidate( const QRectF &r, double c )
46 : rect( r )
47 , cost( c )
48 {}
49
50 QRectF rect;
51 double cost;
52};
53
54
55#ifndef SIP_RUN
56
69class CORE_EXPORT QgsAbstractLabelProvider
70{
71 public:
73 QgsAbstractLabelProvider( QgsMapLayer *layer, const QString &providerId = QString() );
74
75 virtual ~QgsAbstractLabelProvider() = default;
76
78 void setEngine( const QgsLabelingEngine *engine ) { mEngine = engine; }
79
80 enum Flag
81 {
82 DrawLabels = 1 << 1,
85 };
86 Q_DECLARE_FLAGS( Flags, Flag )
87
88
89 virtual QList<QgsLabelFeature *> labelFeatures( QgsRenderContext &context ) = 0;
90
97 virtual void drawLabel( QgsRenderContext &context, pal::LabelPosition *label ) const = 0;
98
110 virtual void drawUnplacedLabel( QgsRenderContext &context, pal::LabelPosition *label ) const;
111
124 virtual void drawLabelBackground( QgsRenderContext &context, pal::LabelPosition *label ) const;
125
131 virtual void startRender( QgsRenderContext &context );
132
138 virtual void stopRender( QgsRenderContext &context );
139
141 virtual QList<QgsAbstractLabelProvider *> subProviders() { return QList<QgsAbstractLabelProvider *>(); }
142
144 QString name() const { return mName; }
145
147 QString layerId() const { return mLayerId; }
148
154 QgsMapLayer *layer() const { return mLayer.data(); }
155
161 QString providerId() const { return mProviderId; }
162
164 Flags flags() const { return mFlags; }
165
168
174 double priority() const { return mPriority; }
175
178
181
187 QgsExpressionContextScope *layerExpressionContextScope() const;
188
194 double layerReferenceScale() const { return mLayerReferenceScale; }
195
196 protected:
198 const QgsLabelingEngine *mEngine = nullptr;
199
201 QString mName;
203 QString mLayerId;
207 QString mProviderId;
213 double mPriority = 0.5;
218
219 private:
220 std::unique_ptr< QgsExpressionContextScope > mLayerExpressionContextScope;
221 double mLayerReferenceScale = -1;
222};
223
225
226
227
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:
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 QStringList mLayerRenderingOrderIds;
510};
511
522{
523 public:
526
529
530 void run( QgsRenderContext &context ) override;
531};
532
545{
546 public:
549
552
553 void run( QgsRenderContext &context ) override;
554
559 void renderLabelsForLayer( QgsRenderContext &context, const QString &layerId );
560
565 void finalize();
566};
567
568
576class CORE_EXPORT QgsLabelingUtils
577{
578 public:
585 static QString encodePredefinedPositionOrder( const QVector< Qgis::LabelPredefinedPointPosition > &positions );
586
593 static QVector< Qgis::LabelPredefinedPointPosition > decodePredefinedPositionOrder( const QString &positionString );
594
600
605 static Qgis::LabelLinePlacementFlags decodeLinePlacementFlags( const QString &string );
606};
607
608#endif
609
610#endif // QGSLABELINGENGINE_H
LabelPlacement
Placement modes which determine how label candidates are generated for a feature.
Definition qgis.h:1232
@ AroundPoint
Arranges candidates in a circle around a point (or centroid of a polygon). Applies to point or polygo...
Definition qgis.h:1233
QFlags< LabelLinePlacementFlag > LabelLinePlacementFlags
Line placement flags, which control how candidates are generated for a linear feature.
Definition qgis.h:1355
UpsideDownLabelHandling
Handling techniques for upside down labels.
Definition qgis.h:1388
@ FlipUpsideDownLabels
Upside-down labels (90 <= angle < 270) are shown upright.
Definition qgis.h:1389
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:83
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:87
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:52
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsProjectionSelectionWidget::CrsOptions)