QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgslabelingenginev2.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslabelingenginev2.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 QGSLABELINGENGINEV2_H
17 #define QGSLABELINGENGINEV2_H
18 
19 #include "qgsgeometry.h"
20 
21 #include "qgsmapsettings.h"
22 
23 #include "qgspallabeling.h"
24 
25 #include <QFlags>
26 
27 
29 
30 
42 class CORE_EXPORT QgsAbstractLabelProvider
43 {
44 
45  public:
47  QgsAbstractLabelProvider( const QString& layerId = QString(), const QString& providerId = QString() );
50 
52  void setEngine( const QgsLabelingEngineV2* engine ) { mEngine = engine; }
53 
54  enum Flag
55  {
56  DrawLabels = 1 << 1,
57  DrawAllLabels = 1 << 2,
58  MergeConnectedLines = 1 << 3,
59  CentroidMustBeInside = 1 << 4,
60  LabelPerFeaturePart = 1 << 6,
61  };
62  Q_DECLARE_FLAGS( Flags, Flag )
63 
64 
65  virtual QList<QgsLabelFeature*> labelFeatures( QgsRenderContext& context ) = 0;
66 
68  virtual void drawLabel( QgsRenderContext& context, pal::LabelPosition* label ) const = 0;
69 
72 
74  QString name() const { return mName; }
75 
77  QString layerId() const { return mLayerId; }
78 
82  QString providerId() const { return mProviderId; }
83 
85  Flags flags() const { return mFlags; }
86 
88  QgsPalLayerSettings::Placement placement() const { return mPlacement; }
89 
91  unsigned int linePlacementFlags() const { return mLinePlacementFlags; }
92 
94  double priority() const { return mPriority; }
95 
97  QgsPalLayerSettings::ObstacleType obstacleType() const { return mObstacleType; }
98 
100  QgsPalLayerSettings::UpsideDownLabels upsidedownLabels() const { return mUpsidedownLabels; }
101 
102  protected:
105 
113  Flags mFlags;
117  unsigned int mLinePlacementFlags;
119  double mPriority;
124 };
125 
126 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsAbstractLabelProvider::Flags )
127 
128 
129 
130 
162 class CORE_EXPORT QgsLabelingEngineV2
163 {
164  public:
166  QgsLabelingEngineV2();
168  ~QgsLabelingEngineV2();
169 
170  enum Flag
171  {
172  UseAllLabels = 1 << 1,
173  UsePartialCandidates = 1 << 2,
174  RenderOutlineLabels = 1 << 3,
175  DrawLabelRectOnly = 1 << 4,
176  DrawCandidates = 1 << 5,
177  DrawShadowRects = 1 << 6,
178  };
179  Q_DECLARE_FLAGS( Flags, Flag )
180 
181 
182  void setMapSettings( const QgsMapSettings& mapSettings ) { mMapSettings = mapSettings; }
184  const QgsMapSettings& mapSettings() const { return mMapSettings; }
185 
187  void addProvider( QgsAbstractLabelProvider* provider );
188 
190  void removeProvider( QgsAbstractLabelProvider* provider );
191 
193  void run( QgsRenderContext& context );
194 
196  QgsLabelingResults* takeResults();
197 
199  QgsLabelingResults* results() const { return mResults; }
200 
202  void setFlags( const Flags& flags ) { mFlags = flags; }
204  Flags flags() const { return mFlags; }
206  bool testFlag( Flag f ) const { return mFlags.testFlag( f ); }
208  void setFlag( Flag f, bool enabled = true ) { if ( enabled ) mFlags |= f; else mFlags &= ~f; }
209 
211  void numCandidatePositions( int& candPoint, int& candLine, int& candPolygon ) { candPoint = mCandPoint; candLine = mCandLine; candPolygon = mCandPolygon; }
213  void setNumCandidatePositions( int candPoint, int candLine, int candPolygon ) { mCandPoint = candPoint; mCandLine = candLine; mCandPolygon = candPolygon; }
214 
216  void setSearchMethod( QgsPalLabeling::Search s ) { mSearchMethod = s; }
218  QgsPalLabeling::Search searchMethod() const { return mSearchMethod; }
219 
221  void readSettingsFromProject();
223  void writeSettingsToProject();
224 
225  protected:
226  void processProvider( QgsAbstractLabelProvider* provider, QgsRenderContext& context, pal::Pal& p );
227 
228  protected:
235  Flags mFlags;
239  int mCandPoint, mCandLine, mCandPolygon;
240 
243 
244  private:
245 
246  QgsLabelingEngineV2( const QgsLabelingEngineV2& rh );
247  QgsLabelingEngineV2& operator=( const QgsLabelingEngineV2& rh );
248 };
249 
250 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLabelingEngineV2::Flags )
251 
252 
253 
261 class CORE_EXPORT QgsLabelingUtils
262 {
263  public:
264 
270  static QString encodePredefinedPositionOrder( const QVector< QgsPalLayerSettings::PredefinedPointPosition >& positions );
271 
277  static QVector< QgsPalLayerSettings::PredefinedPointPosition > decodePredefinedPositionOrder( const QString& positionString );
278 
279 };
280 
281 #endif // QGSLABELINGENGINEV2_H
QgsPalLayerSettings::Placement mPlacement
Placement strategy.
double mPriority
Default priority of labels.
Contains helper utilities for working with QGIS&#39; labeling engine.
QgsPalLayerSettings::ObstacleType obstacleType() const
How the feature geometries will work as obstacles.
void setFlags(const Flags &flags)
Set flags of the labeling engine.
Main Pal labelling class.
Definition: pal.h:84
The QgsLabelingEngineV2 class provides map labeling functionality.
void setNumCandidatePositions(int candPoint, int candLine, int candPolygon)
Set number of candidate positions that will be generated for each label feature.
const QgsMapSettings & mapSettings() const
Get associated map settings.
virtual QList< QgsAbstractLabelProvider * > subProviders()
Return list of child providers - useful if the provider needs to put labels into more layers with dif...
The QgsMapSettings class contains configuration for rendering of the map.
unsigned int linePlacementFlags() const
For layers with linestring geometries - extra placement flags (or-ed combination of QgsPalLayerSettin...
void setSearchMethod(QgsPalLabeling::Search s)
Set which search method to use for removal collisions between labels.
QgsMapSettings mMapSettings
Associated map settings instance.
QList< QgsAbstractLabelProvider * > mProviders
List of providers (the are owned by the labeling engine)
unsigned int mLinePlacementFlags
Extra placement flags for linestring geometries.
Flags mFlags
Flags altering drawing and registration of features.
QgsPalLabeling::Search mSearchMethod
search method to use for removal collisions between labels
const QgsLabelingEngineV2 * mEngine
Associated labeling engine.
QgsLabelingResults * mResults
Resulting labeling layout.
void setEngine(const QgsLabelingEngineV2 *engine)
Associate provider with a labeling engine (should be only called internally from QgsLabelingEngineV2)...
QgsPalLayerSettings::Placement placement() const
What placement strategy to use for the labels.
QgsPalLabeling::Search searchMethod() const
Which search method to use for removal collisions between labels.
The QgsAbstractLabelProvider class is an interface class.
QString layerId() const
Returns ID of associated layer, or empty string if no layer is associated with the provider...
QgsPalLayerSettings::UpsideDownLabels upsidedownLabels() const
How to handle labels that would be upside down.
QgsLabelingResults * results() const
For internal use by the providers.
Flags flags() const
Get flags of the labeling engine.
Flags flags() const
Flags associated with the provider.
Placement
Placement modes which determine how label candidates are generated for a feature. ...
Contains information about the context of a rendering operation.
QString name() const
Name of the layer (for statistics, debugging etc.) - does not need to be unique.
QString mName
Name of the layer.
virtual ~QgsAbstractLabelProvider()
Vritual destructor.
bool testFlag(Flag f) const
Test whether a particular flag is enabled.
double priority() const
Default priority of labels (may be overridden by individual labels)
QString mProviderId
Associated provider ID (one layer may have multiple providers, e.g. in rule-based labeling) ...
LabelPosition is a candidate feature label position.
Definition: labelposition.h:51
QgsPalLayerSettings::ObstacleType mObstacleType
Type of the obstacle of feature geometries.
QgsPalLayerSettings::UpsideDownLabels 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...
Class that stores computed placement from labeling engine.
QString mLayerId
Associated layer&#39;s ID, if applicable.
void setFlag(Flag f, bool enabled=true)
Set whether a particual flag is enabled.
QList< QgsAbstractLabelProvider * > mSubProviders
void numCandidatePositions(int &candPoint, int &candLine, int &candPolygon)
Get number of candidate positions that will be generated for each label feature (default to 8) ...
ObstacleType
Valid obstacle types, which affect how features within the layer will act as obstacles for labels...