QGIS API Documentation  2.14.0-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  FitInPolygonOnly = 1 << 5,
61  LabelPerFeaturePart = 1 << 6,
62  };
63  Q_DECLARE_FLAGS( Flags, Flag )
64 
65 
66  virtual QList<QgsLabelFeature*> labelFeatures( QgsRenderContext& context ) = 0;
67 
69  virtual void drawLabel( QgsRenderContext& context, pal::LabelPosition* label ) const = 0;
70 
72  virtual QList<QgsAbstractLabelProvider*> subProviders() { return QList<QgsAbstractLabelProvider*>(); }
73 
75  QString name() const { return mName; }
76 
78  QString layerId() const { return mLayerId; }
79 
81  Flags flags() const { return mFlags; }
82 
84  QgsPalLayerSettings::Placement placement() const { return mPlacement; }
85 
87  unsigned int linePlacementFlags() const { return mLinePlacementFlags; }
88 
90  double priority() const { return mPriority; }
91 
93  QgsPalLayerSettings::ObstacleType obstacleType() const { return mObstacleType; }
94 
96  QgsPalLayerSettings::UpsideDownLabels upsidedownLabels() const { return mUpsidedownLabels; }
97 
98  protected:
101 
107  Flags mFlags;
111  unsigned int mLinePlacementFlags;
113  double mPriority;
118 };
119 
120 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsAbstractLabelProvider::Flags )
121 
122 
123 
124 
156 class CORE_EXPORT QgsLabelingEngineV2
157 {
158  public:
160  QgsLabelingEngineV2();
162  ~QgsLabelingEngineV2();
163 
164  enum Flag
165  {
166  UseAllLabels = 1 << 1,
167  UsePartialCandidates = 1 << 2,
168  RenderOutlineLabels = 1 << 3,
169  DrawLabelRectOnly = 1 << 4,
170  DrawCandidates = 1 << 5,
171  DrawShadowRects = 1 << 6,
172  };
173  Q_DECLARE_FLAGS( Flags, Flag )
174 
175 
176  void setMapSettings( const QgsMapSettings& mapSettings ) { mMapSettings = mapSettings; }
178  const QgsMapSettings& mapSettings() const { return mMapSettings; }
179 
181  void addProvider( QgsAbstractLabelProvider* provider );
182 
184  void removeProvider( QgsAbstractLabelProvider* provider );
185 
187  void run( QgsRenderContext& context );
188 
190  QgsLabelingResults* takeResults();
191 
193  QgsLabelingResults* results() const { return mResults; }
194 
196  void setFlags( const Flags& flags ) { mFlags = flags; }
198  Flags flags() const { return mFlags; }
200  bool testFlag( Flag f ) const { return mFlags.testFlag( f ); }
202  void setFlag( Flag f, bool enabled = true ) { if ( enabled ) mFlags |= f; else mFlags &= ~f; }
203 
205  void numCandidatePositions( int& candPoint, int& candLine, int& candPolygon ) { candPoint = mCandPoint; candLine = mCandLine; candPolygon = mCandPolygon; }
207  void setNumCandidatePositions( int candPoint, int candLine, int candPolygon ) { mCandPoint = candPoint; mCandLine = candLine; mCandPolygon = candPolygon; }
208 
210  void setSearchMethod( QgsPalLabeling::Search s ) { mSearchMethod = s; }
212  QgsPalLabeling::Search searchMethod() const { return mSearchMethod; }
213 
215  void readSettingsFromProject();
217  void writeSettingsToProject();
218 
219  protected:
220  void processProvider( QgsAbstractLabelProvider* provider, QgsRenderContext& context, pal::Pal& p );
221 
222  protected:
229  Flags mFlags;
233  int mCandPoint, mCandLine, mCandPolygon;
234 
237 
238  private:
239 
240  QgsLabelingEngineV2( const QgsLabelingEngineV2& rh );
241  QgsLabelingEngineV2& operator=( const QgsLabelingEngineV2& rh );
242 };
243 
244 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLabelingEngineV2::Flags )
245 
246 
247 
255 class CORE_EXPORT QgsLabelingUtils
256 {
257  public:
258 
264  static QString encodePredefinedPositionOrder( const QVector< QgsPalLayerSettings::PredefinedPointPosition >& positions );
265 
271  static QVector< QgsPalLayerSettings::PredefinedPointPosition > decodePredefinedPositionOrder( const QString& positionString );
272 
273 };
274 
275 #endif // QGSLABELINGENGINEV2_H
QString name() const
Name of the layer (for statistics, debugging etc.) - does not need to be unique.
QgsPalLayerSettings::Placement mPlacement
Placement strategy.
unsigned int linePlacementFlags() const
For layers with linestring geometries - extra placement flags (or-ed combination of QgsPalLayerSettin...
double mPriority
Default priority of labels.
Contains helper utilities for working with QGIS&#39; labeling engine.
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.
QgsPalLayerSettings::UpsideDownLabels upsidedownLabels() const
How to handle labels that would be upside down.
void setNumCandidatePositions(int candPoint, int candLine, int candPolygon)
Set number of candidate positions that will be generated for each label feature.
The QgsMapSettings class contains configuration for rendering of the map.
QgsLabelingResults * results() const
For internal use by the providers.
void setSearchMethod(QgsPalLabeling::Search s)
Set which search method to use for removal collisions between labels.
QgsPalLayerSettings::Placement placement() const
What placement strategy to use for the 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.
const QgsMapSettings & mapSettings() const
Get associated map settings.
Flags flags() const
Get flags of the labeling engine.
Flags mFlags
Flags altering drawing and registration of features.
QgsPalLabeling::Search mSearchMethod
search method to use for removal collisions between labels
double priority() const
Default priority of labels (may be overridden by individual labels)
const QgsLabelingEngineV2 * mEngine
Associated labeling engine.
QgsLabelingResults * mResults
Resulting labeling layout.
Flags flags() const
Flags associated with the provider.
void setEngine(const QgsLabelingEngineV2 *engine)
Associate provider with a labeling engine (should be only called internally from QgsLabelingEngineV2)...
The QgsAbstractLabelProvider class is an interface class.
bool testFlag(Flag f) const
Test whether a particular flag is enabled.
QString layerId() const
Returns ID of associated layer, or empty string if no layer is 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.
The QgsLabelFeature class describes a feature that should be used within the labeling engine...
QString mName
Name of the layer.
QgsPalLayerSettings::ObstacleType obstacleType() const
How the feature geometries will work as obstacles.
virtual ~QgsAbstractLabelProvider()
Vritual destructor.
QgsPalLabeling::Search searchMethod() const
Which search method to use for removal collisions between labels.
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...