QGIS API Documentation  2.14.11-Essen
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() );
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 
80  Flags flags() const { return mFlags; }
81 
83  QgsPalLayerSettings::Placement placement() const { return mPlacement; }
84 
86  unsigned int linePlacementFlags() const { return mLinePlacementFlags; }
87 
89  double priority() const { return mPriority; }
90 
92  QgsPalLayerSettings::ObstacleType obstacleType() const { return mObstacleType; }
93 
95  QgsPalLayerSettings::UpsideDownLabels upsidedownLabels() const { return mUpsidedownLabels; }
96 
97  protected:
100 
106  Flags mFlags;
110  unsigned int mLinePlacementFlags;
112  double mPriority;
117 };
118 
119 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsAbstractLabelProvider::Flags )
120 
121 
122 
123 
155 class CORE_EXPORT QgsLabelingEngineV2
156 {
157  public:
159  QgsLabelingEngineV2();
161  ~QgsLabelingEngineV2();
162 
163  enum Flag
164  {
165  UseAllLabels = 1 << 1,
166  UsePartialCandidates = 1 << 2,
167  RenderOutlineLabels = 1 << 3,
168  DrawLabelRectOnly = 1 << 4,
169  DrawCandidates = 1 << 5,
170  DrawShadowRects = 1 << 6,
171  };
172  Q_DECLARE_FLAGS( Flags, Flag )
173 
174 
175  void setMapSettings( const QgsMapSettings& mapSettings ) { mMapSettings = mapSettings; }
177  const QgsMapSettings& mapSettings() const { return mMapSettings; }
178 
180  void addProvider( QgsAbstractLabelProvider* provider );
181 
183  void removeProvider( QgsAbstractLabelProvider* provider );
184 
186  void run( QgsRenderContext& context );
187 
189  QgsLabelingResults* takeResults();
190 
192  QgsLabelingResults* results() const { return mResults; }
193 
195  void setFlags( const Flags& flags ) { mFlags = flags; }
197  Flags flags() const { return mFlags; }
199  bool testFlag( Flag f ) const { return mFlags.testFlag( f ); }
201  void setFlag( Flag f, bool enabled = true ) { if ( enabled ) mFlags |= f; else mFlags &= ~f; }
202 
204  void numCandidatePositions( int& candPoint, int& candLine, int& candPolygon ) { candPoint = mCandPoint; candLine = mCandLine; candPolygon = mCandPolygon; }
206  void setNumCandidatePositions( int candPoint, int candLine, int candPolygon ) { mCandPoint = candPoint; mCandLine = candLine; mCandPolygon = candPolygon; }
207 
209  void setSearchMethod( QgsPalLabeling::Search s ) { mSearchMethod = s; }
211  QgsPalLabeling::Search searchMethod() const { return mSearchMethod; }
212 
214  void readSettingsFromProject();
216  void writeSettingsToProject();
217 
218  protected:
219  void processProvider( QgsAbstractLabelProvider* provider, QgsRenderContext& context, pal::Pal& p );
220 
221  protected:
228  Flags mFlags;
232  int mCandPoint, mCandLine, mCandPolygon;
233 
236 
237  private:
238 
239  QgsLabelingEngineV2( const QgsLabelingEngineV2& rh );
240  QgsLabelingEngineV2& operator=( const QgsLabelingEngineV2& rh );
241 };
242 
243 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLabelingEngineV2::Flags )
244 
245 
246 
254 class CORE_EXPORT QgsLabelingUtils
255 {
256  public:
257 
263  static QString encodePredefinedPositionOrder( const QVector< QgsPalLayerSettings::PredefinedPointPosition >& positions );
264 
270  static QVector< QgsPalLayerSettings::PredefinedPointPosition > decodePredefinedPositionOrder( const QString& positionString );
271 
272 };
273 
274 #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)
LabelPosition is a candidate feature label position.
Definition: labelposition.h:50
QgsPalLayerSettings::ObstacleType mObstacleType
Type of the obstacle of feature geometries.
QgsPalLayerSettings::UpsideDownLabels mUpsidedownLabels
How to handle labels that would be upside down.
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...