QGIS API Documentation  2.12.0-Lyon
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 
28 class QgsRenderContext;
29 class QgsGeometry;
30 
31 namespace pal
32 {
33  class LabelInfo;
34 }
35 
51 class CORE_EXPORT QgsLabelFeature
52 {
53  public:
55  QgsLabelFeature( QgsFeatureId id, GEOSGeometry* geometry, const QSizeF& size );
57  virtual ~QgsLabelFeature();
58 
60  QgsFeatureId id() const { return mId; }
61 
63  GEOSGeometry* geometry() const { return mGeometry; }
64 
66  QSizeF size() const { return mSize; }
67 
73  double priority() const { return mPriority; }
80  void setPriority( double priority ) { mPriority = priority; }
81 
83  bool hasFixedPosition() const { return mHasFixedPosition; }
85  void setHasFixedPosition( bool enabled ) { mHasFixedPosition = enabled; }
87  QgsPoint fixedPosition() const { return mFixedPosition; }
89  void setFixedPosition( const QgsPoint& point ) { mFixedPosition = point; }
90 
92  bool hasFixedAngle() const { return mHasFixedAngle; }
94  void setHasFixedAngle( bool enabled ) { mHasFixedAngle = enabled; }
96  double fixedAngle() const { return mFixedAngle; }
98  void setFixedAngle( double angle ) { mFixedAngle = angle; }
99 
105  bool hasFixedQuadrant() const { return mHasFixedQuadrant; }
111  void setHasFixedQuadrant( bool enabled ) { mHasFixedQuadrant = enabled; }
116  QPointF quadOffset() const { return mQuadOffset; }
119  void setQuadOffset( const QPointF& quadOffset ) { mQuadOffset = quadOffset; }
122  QgsPoint positionOffset() const { return mPositionOffset; }
125  void setPositionOffset( const QgsPoint& offset ) { mPositionOffset = offset; }
128  double distLabel() const { return mDistLabel; }
131  void setDistLabel( double dist ) { mDistLabel = dist; }
132 
135  double repeatDistance() const { return mRepeatDistance; }
138  void setRepeatDistance( double dist ) { mRepeatDistance = dist; }
139 
141  bool alwaysShow() const { return mAlwaysShow; }
143  void setAlwaysShow( bool enabled ) { mAlwaysShow = enabled; }
144 
149  bool isObstacle() const { return mIsObstacle; }
154  void setIsObstacle( bool enabled ) { mIsObstacle = enabled; }
159  double obstacleFactor() const { return mObstacleFactor; }
167  void setObstacleFactor( double factor ) { mObstacleFactor = factor; }
168 
174  QString labelText() const { return mLabelText; }
176  void setLabelText( const QString& text ) { mLabelText = text; }
177 
179  pal::LabelInfo* curvedLabelInfo() const { return mInfo; }
181  void setCurvedLabelInfo( pal::LabelInfo* info ) { mInfo = info; }
182 
184  pal::Layer* layer() const { return mLayer; }
186  void setLayer( pal::Layer* layer ) { mLayer = layer; }
187 
189  QgsAbstractLabelProvider* provider() const;
190 
191  protected:
194 
198  GEOSGeometry* mGeometry;
202  double mPriority;
210  double mFixedAngle;
218  double mDistLabel;
231 };
232 
233 
234 
235 class QgsLabelingEngineV2;
236 
237 
248 class CORE_EXPORT QgsAbstractLabelProvider
249 {
250 
251  public:
256 
258  void setEngine( const QgsLabelingEngineV2* engine ) { mEngine = engine; }
259 
260  enum Flag
261  {
262  DrawLabels = 1 << 1,
263  DrawAllLabels = 1 << 2,
264  MergeConnectedLines = 1 << 3,
265  CentroidMustBeInside = 1 << 4,
266  FitInPolygonOnly = 1 << 5,
267  LabelPerFeaturePart = 1 << 6,
268  };
269  Q_DECLARE_FLAGS( Flags, Flag )
270 
271 
272  virtual QList<QgsLabelFeature*> labelFeatures( QgsRenderContext& context ) = 0;
273 
275  virtual void drawLabel( QgsRenderContext& context, pal::LabelPosition* label ) const = 0;
276 
278  virtual QList<QgsAbstractLabelProvider*> subProviders() { return QList<QgsAbstractLabelProvider*>(); }
279 
281  QString name() const { return mName; }
282 
284  Flags flags() const { return mFlags; }
285 
287  QgsPalLayerSettings::Placement placement() const { return mPlacement; }
288 
290  unsigned int linePlacementFlags() const { return mLinePlacementFlags; }
291 
293  double priority() const { return mPriority; }
294 
296  QgsPalLayerSettings::ObstacleType obstacleType() const { return mObstacleType; }
297 
299  QgsPalLayerSettings::UpsideDownLabels upsidedownLabels() const { return mUpsidedownLabels; }
300 
301  protected:
304 
308  Flags mFlags;
312  unsigned int mLinePlacementFlags;
314  double mPriority;
319 };
320 
321 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsAbstractLabelProvider::Flags )
322 
323 
324 
325 
356 class CORE_EXPORT QgsLabelingEngineV2
357 {
358  public:
360  QgsLabelingEngineV2();
362  ~QgsLabelingEngineV2();
363 
364  enum Flag
365  {
366  UseAllLabels = 1 << 1,
367  UsePartialCandidates = 1 << 2,
368  RenderOutlineLabels = 1 << 3,
369  DrawLabelRectOnly = 1 << 4,
370  DrawCandidates = 1 << 5,
371  DrawShadowRects = 1 << 6,
372  };
373  Q_DECLARE_FLAGS( Flags, Flag )
374 
375 
376  void setMapSettings( const QgsMapSettings& mapSettings ) { mMapSettings = mapSettings; }
378  const QgsMapSettings& mapSettings() const { return mMapSettings; }
379 
381  void addProvider( QgsAbstractLabelProvider* provider );
382 
384  void removeProvider( QgsAbstractLabelProvider* provider );
385 
387  void run( QgsRenderContext& context );
388 
390  QgsLabelingResults* takeResults();
391 
393  QgsLabelingResults* results() const { return mResults; }
394 
396  void setFlags( const Flags& flags ) { mFlags = flags; }
398  Flags flags() const { return mFlags; }
400  bool testFlag( Flag f ) const { return mFlags.testFlag( f ); }
402  void setFlag( Flag f, bool enabled = true ) { if ( enabled ) mFlags |= f; else mFlags &= ~f; }
403 
405  void numCandidatePositions( int& candPoint, int& candLine, int& candPolygon ) { candPoint = mCandPoint; candLine = mCandLine; candPolygon = mCandPolygon; }
407  void setNumCandidatePositions( int candPoint, int candLine, int candPolygon ) { mCandPoint = candPoint; mCandLine = candLine; mCandPolygon = candPolygon; }
408 
410  void setSearchMethod( QgsPalLabeling::Search s ) { mSearchMethod = s; }
412  QgsPalLabeling::Search searchMethod() const { return mSearchMethod; }
413 
415  void readSettingsFromProject();
417  void writeSettingsToProject();
418 
419  protected:
420  void processProvider( QgsAbstractLabelProvider* provider, QgsRenderContext& context, pal::Pal& p );
421 
422  protected:
429  Flags mFlags;
433  int mCandPoint, mCandLine, mCandPolygon;
434 
437 };
438 
439 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsLabelingEngineV2::Flags )
440 
441 #endif // QGSLABELINGENGINEV2_H
pal::Layer * layer() const
Get PAL layer of the label feature. Should be only used internally in PAL.
QString name() const
Name of the layer (for statistics, debugging etc.) - does not need to be unique.
void setQuadOffset(const QPointF &quadOffset)
Set which side of the point to use.
QgsPalLayerSettings::Placement mPlacement
Placement strategy.
unsigned int linePlacementFlags() const
For layers with linestring geometries - extra placement flags (or-ed combination of QgsPalLayerSettin...
double repeatDistance() const
Applies only to linestring features - after what distance (in map units) the labels should be repeate...
bool alwaysShow() const
Whether label should be always shown (sets very high label priority)
double mPriority
Default priority of labels.
A layer of spacial entites.
Definition: layer.h:57
QString mLabelText
text of the label
void setFlags(const Flags &flags)
Set flags of the labeling engine.
pal::LabelInfo * mInfo
extra information for curved labels (may be null)
bool mIsObstacle
whether the feature geometry acts as an obstacle for labels
Pal main class.
Definition: pal.h:111
The QgsLabelingEngineV2 class provides map labeling functionality.
void setObstacleFactor(double factor)
Sets the obstacle factor for the feature.
QPointF quadOffset() const
Applies to "offset from point" placement strategy and "around point" (in case hasFixedQuadrant() retu...
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
void setCurvedLabelInfo(pal::LabelInfo *info)
takes ownership of the instance
bool mHasFixedQuadrant
whether mQuadOffset should be respected (only for "around point" placement)
double priority() const
Returns the feature's labeling priority.
double mRepeatDistance
distance after which label should be repeated (only for linestrings)
double mObstacleFactor
how strong is the geometry acting as obstacle
QgsFeatureId mId
Associated ID unique within the parent label provider.
void setFixedPosition(const QgsPoint &point)
Set coordinates of the fixed position (relevant only if hasFixedPosition() returns true) ...
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.
QgsPoint mFixedPosition
fixed position for the label (instead of automatic placement)
void setAlwaysShow(bool enabled)
Set whether label should be always shown (sets very high label priority)
void setLabelText(const QString &text)
Set text of the label.
bool mHasFixedPosition
whether mFixedPosition should be respected
bool isObstacle() const
Returns whether the feature will act as an obstacle for labels.
void setHasFixedPosition(bool enabled)
Set whether the label should use a fixed position instead of being automatically placed.
The QgsMapSettings class contains configuration for rendering of the map.
GEOSGeometry * geometry() const
Get access to the associated geometry.
QgsLabelingResults * results() const
For internal use by the providers.
pal::LabelInfo * curvedLabelInfo() const
Get additional infor required for curved label placement. Returns null if not set.
QSizeF size() const
Size of the label (in map units)
QSizeF mSize
Width and height of the label.
bool hasFixedPosition() const
Whether the label should use a fixed position instead of being automatically placed.
void setSearchMethod(QgsPalLabeling::Search s)
Set which search method to use for removal collisions between labels.
double mFixedAngle
fixed rotation for the label (instead of automatic choice)
void setIsObstacle(bool enabled)
Sets whether the feature will act as an obstacle for 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.
void setLayer(pal::Layer *layer)
Assign PAL layer to the label feature. Should be only used internally in PAL.
QgsPoint fixedPosition() const
Coordinates of the fixed position (relevant only if hasFixedPosition() returns true) ...
bool hasFixedQuadrant() const
Returns whether the quadrant for the label is fixed.
Flags flags() const
Get flags of the labeling engine.
Optional additional info about label (for curved labels)
Definition: feature.h:47
void setFixedAngle(double angle)
Set angle in degrees of the fixed angle (relevant only if hasFixedAngle() returns true) ...
Flags mFlags
Flags altering drawing and registration of features.
GEOSGeometry * mGeometry
Geometry of the feature to be labelled.
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)...
void setDistLabel(double dist)
Applies to "around point" placement strategy or linestring features.
bool mAlwaysShow
whether to always show label - even in case of collisions
A class to represent a point.
Definition: qgspoint.h:63
The QgsAbstractLabelProvider class is an interface class.
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
bool testFlag(Flag f) const
Test whether a particular flag is enabled.
bool mHasFixedAngle
whether mFixedAngle should be respected
QgsPoint positionOffset() const
Applies only to "offset from point" placement strategy.
Contains information about the context of a rendering operation.
double obstacleFactor() const
Returns the obstacle factor for the feature.
The QgsLabelFeature class describes a feature that should be used within the labeling engine...
double mPriority
Priority of the label.
QString mName
Name of the layer.
QgsPalLayerSettings::ObstacleType obstacleType() const
How the feature geometries will work as obstacles.
void setHasFixedAngle(bool enabled)
Set whether the label should use a fixed angle instead of using angle from automatic placement...
void setRepeatDistance(double dist)
Applies only to linestring features - set after what distance (in map units) the labels should be rep...
virtual ~QgsAbstractLabelProvider()
Vritual destructor.
QgsPoint mPositionOffset
offset of label from the feature (only for "offset from point" placement)
double distLabel() const
Applies to "around point" placement strategy or linestring features.
QgsPalLabeling::Search searchMethod() const
Which search method to use for removal collisions between labels.
void setHasFixedQuadrant(bool enabled)
Sets whether the quadrant for the label must be respected.
QgsPalLayerSettings::ObstacleType mObstacleType
Type of the obstacle of feature geometries.
qint64 QgsFeatureId
Definition: qgsfeature.h:31
QgsPalLayerSettings::UpsideDownLabels mUpsidedownLabels
How to handle labels that would be upside down.
pal::Layer * mLayer
Pointer to PAL layer (assigned when registered to PAL)
void setPositionOffset(const QgsPoint &offset)
Applies only to "offset from point" placement strategy.
Class that stores computed placement from labeling engine.
bool hasFixedAngle() const
Whether the label should use a fixed angle instead of using angle from automatic placement.
double mDistLabel
distance of label from the feature (only for "around point" placement or linestrings) ...
void setFlag(Flag f, bool enabled=true)
Set whether a particual flag is enabled.
QString labelText() const
Text of the label.
QList< QgsAbstractLabelProvider * > mSubProviders
double fixedAngle() const
Angle in degrees of the fixed angle (relevant only if hasFixedAngle() returns true) ...
void numCandidatePositions(int &candPoint, int &candLine, int &candPolygon)
Get number of candidate positions that will be generated for each label feature (default to 8) ...
void setPriority(double priority)
Sets the priority for labeling the feature.
QgsFeatureId id() const
Identifier of the label (unique within the parent label provider)
ObstacleType
Valid obstacle types, which affect how features within the layer will act as obstacles for labels...
QPointF mQuadOffset
whether the side of the label is fixed (only for "around point" placement)