QGIS API Documentation  3.21.0-Master (5b68dc587e)
qgslabelfeature.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslabelfeature.h
3  ---------------------
4  begin : December 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 #ifndef QGSLABELFEATURE_H
16 #define QGSLABELFEATURE_H
17 
18 #define SIP_NO_FILE
19 
20 #include "qgis_core.h"
21 #include "qgspallabeling.h"
22 #include "geos_c.h"
23 #include "qgsgeos.h"
24 #include "qgsmargins.h"
26 #include "qgslabeling.h"
27 
28 namespace pal
29 {
30  class Layer;
31 }
32 
34 class QgsRenderContext;
35 class QgsGeometry;
36 
37 
55 class CORE_EXPORT QgsLabelFeature
56 {
57  public:
58 
60  QgsLabelFeature( QgsFeatureId id, geos::unique_ptr geometry, QSizeF size );
62  virtual ~QgsLabelFeature();
63 
65  QgsFeatureId id() const { return mId; }
66 
68  GEOSGeometry *geometry() const { return mGeometry.get(); }
69 
79  void setPermissibleZone( const QgsGeometry &geometry );
80 
89  QgsGeometry permissibleZone() const { return mPermissibleZone; }
90 
96  //TODO - remove when QgsGeometry caches GEOS preparedness
97  const GEOSPreparedGeometry *permissibleZonePrepared() const { return mPermissibleZoneGeosPrepared.get(); }
98 
100  QSizeF size( double angle = 0.0 ) const;
101 
112  void setVisualMargin( const QgsMargins &margin ) { mVisualMargin = margin; }
113 
118  const QgsMargins &visualMargin() const { return mVisualMargin; }
119 
126  void setSymbolSize( QSizeF size ) { mSymbolSize = size; }
127 
135  const QSizeF &symbolSize() const { return mSymbolSize; }
136 
143  double priority() const { return mPriority; }
144 
152  void setPriority( double priority ) { mPriority = priority; }
153 
160  double zIndex() const { return mZIndex; }
161 
169  void setZIndex( double zIndex ) { mZIndex = zIndex; }
170 
172  bool hasFixedPosition() const { return mHasFixedPosition; }
174  void setHasFixedPosition( bool enabled ) { mHasFixedPosition = enabled; }
176  QgsPointXY fixedPosition() const { return mFixedPosition; }
177 
179  void setFixedPosition( const QgsPointXY &point ) { mFixedPosition = point; }
180 
187  QgsPointXY anchorPosition() const;
188 
195  void setAnchorPosition( const QgsPointXY &anchorPosition );
196 
198  bool hasFixedAngle() const { return mHasFixedAngle; }
200  void setHasFixedAngle( bool enabled ) { mHasFixedAngle = enabled; }
202  double fixedAngle() const { return mFixedAngle; }
204  void setFixedAngle( double angle ) { mFixedAngle = angle; }
205 
212  bool hasFixedQuadrant() const { return mHasFixedQuadrant; }
213 
220  void setHasFixedQuadrant( bool enabled ) { mHasFixedQuadrant = enabled; }
221 
228  QPointF quadOffset() const { return mQuadOffset; }
229 
234  void setQuadOffset( QPointF quadOffset ) { mQuadOffset = quadOffset; }
235 
240  QgsPointXY positionOffset() const { return mPositionOffset; }
241 
246  void setPositionOffset( const QgsPointXY &offset ) { mPositionOffset = offset; }
247 
254  QgsPalLayerSettings::OffsetType offsetType() const { return mOffsetType; }
255 
262  void setOffsetType( QgsPalLayerSettings::OffsetType type ) { mOffsetType = type; }
263 
268  double distLabel() const { return mDistLabel; }
269 
274  void setDistLabel( double dist ) { mDistLabel = dist; }
275 
281  QVector< QgsPalLayerSettings::PredefinedPointPosition > predefinedPositionOrder() const { return mPredefinedPositionOrder; }
282 
288  void setPredefinedPositionOrder( const QVector< QgsPalLayerSettings::PredefinedPointPosition > &order ) { mPredefinedPositionOrder = order; }
289 
294  double repeatDistance() const { return mRepeatDistance; }
295 
300  void setRepeatDistance( double dist ) { mRepeatDistance = dist; }
301 
303  bool alwaysShow() const { return mAlwaysShow; }
305  void setAlwaysShow( bool enabled ) { mAlwaysShow = enabled; }
306 
311  QgsLabeling::LinePlacementFlags arrangementFlags() const { return mArrangementFlags; }
312 
318  void setArrangementFlags( QgsLabeling::LinePlacementFlags flags ) { mArrangementFlags = flags; }
319 
326  QgsLabeling::PolygonPlacementFlags polygonPlacementFlags() const { return mPolygonPlacementFlags; }
327 
334  void setPolygonPlacementFlags( QgsLabeling::PolygonPlacementFlags flags ) { mPolygonPlacementFlags = flags; }
335 
342  QString labelText() const { return mLabelText; }
344  void setLabelText( const QString &text ) { mLabelText = text; }
345 
347  pal::Layer *layer() const { return mLayer; }
349  void setLayer( pal::Layer *layer ) { mLayer = layer; }
350 
352  QgsAbstractLabelProvider *provider() const;
353 
360  QgsFeature feature() const;
361 
368  void setFeature( const QgsFeature &feature );
369 
376  const QgsSymbol *symbol() { return mSymbol; }
377 
385  void setSymbol( const QgsSymbol *symbol ) { mSymbol = symbol; }
386 
395  double overrunDistance() const;
396 
405  void setOverrunDistance( double distance );
406 
415  double overrunSmoothDistance() const;
416 
425  void setOverrunSmoothDistance( double distance );
426 
439  double lineAnchorPercent() const { return mLineAnchorPercent; }
440 
453  void setLineAnchorPercent( double percent ) { mLineAnchorPercent = percent; }
454 
455 
463  QgsLabelLineSettings::AnchorType lineAnchorType() const { return mLineAnchorType; }
464 
472  void setLineAnchorType( QgsLabelLineSettings::AnchorType type ) { mLineAnchorType = type; }
473 
479  bool labelAllParts() const { return mLabelAllParts; }
480 
486  void setLabelAllParts( bool labelAllParts ) { mLabelAllParts = labelAllParts; }
487 
493  void setRotatedSize( QSizeF size ) { mRotatedSize = size; }
494 
501  const QgsLabelObstacleSettings &obstacleSettings() const;
502 
509  void setObstacleSettings( const QgsLabelObstacleSettings &settings );
510 
517  QgsCoordinateReferenceSystem originalFeatureCrs() const;
518 
525  void setOriginalFeatureCrs( const QgsCoordinateReferenceSystem &crs );
526 
534  double minimumSize() const { return mMinimumSize; }
535 
543  void setMinimumSize( double size ) { mMinimumSize = size; }
544 
545  protected:
547  pal::Layer *mLayer = nullptr;
548 
556  QSizeF mSize;
558  QSizeF mRotatedSize;
562  QSizeF mSymbolSize;
564  double mPriority = -1;
566  double mZIndex = 0;
568  bool mHasFixedPosition = false;
572  bool mHasFixedAngle = false;
574  double mFixedAngle = 0;
576  bool mHasFixedQuadrant = false;
578  QPointF mQuadOffset;
582  double mDistLabel = 0;
586  QVector< QgsPalLayerSettings::PredefinedPointPosition > mPredefinedPositionOrder;
588  double mRepeatDistance = 0;
590  bool mAlwaysShow = false;
592  QString mLabelText;
593 
595  double mOverrunDistance = 0;
597  double mOverrunSmoothDistance = 0;
598 
599  QgsLabeling::LinePlacementFlags mArrangementFlags = QgsLabeling::LinePlacementFlags();
600  QgsLabeling::PolygonPlacementFlags mPolygonPlacementFlags = QgsLabeling::PolygonPlacementFlag::AllowPlacementInsideOfPolygon;
601 
602  private:
603 
605  geos::unique_ptr mPermissibleZoneGeos;
606 
607  // TODO - not required when QgsGeometry caches geos preparedness
608  geos::prepared_unique_ptr mPermissibleZoneGeosPrepared;
609 
610  QgsFeature mFeature;
611 
612  const QgsSymbol *mSymbol = nullptr;
613 
614  bool mLabelAllParts = false;
615 
616  QgsLabelObstacleSettings mObstacleSettings{};
617 
618  QgsPointXY mAnchorPosition;
619 
620  double mLineAnchorPercent = 0.5;
622 
623  QgsCoordinateReferenceSystem mOriginalFeatureCrs;
624 
625  double mMinimumSize = 0.0;
626 
627 };
628 
629 #endif // QGSLABELFEATURE_H
The QgsAbstractLabelProvider class is an interface class.
This class represents a coordinate reference system (CRS).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
The QgsLabelFeature class describes a feature that should be used within the labeling engine.
void setMinimumSize(double size)
Sets the minimum size (in map unit) for a feature to be labelled.
QSizeF mSymbolSize
Size of associated rendered symbol, if applicable.
void setDistLabel(double dist)
Applies to "around point" placement strategy or linestring features.
void setArrangementFlags(QgsLabeling::LinePlacementFlags flags)
Sets the feature's arrangement flags.
void setAlwaysShow(bool enabled)
Sets whether label should be always shown (sets very high label priority)
QgsPointXY mFixedPosition
fixed position for the label (instead of automatic placement)
QSizeF mRotatedSize
Width and height of the label when rotated between 45 to 135 and 235 to 315 degrees;.
QVector< QgsPalLayerSettings::PredefinedPointPosition > predefinedPositionOrder() const
Returns the priority ordered list of predefined positions for label candidates.
pal::Layer * layer() const
Gets PAL layer of the label feature. Should be only used internally in PAL.
void setRepeatDistance(double dist)
Applies only to linestring features - set after what distance (in map units) the labels should be rep...
double fixedAngle() const
Angle in degrees of the fixed angle (relevant only if hasFixedAngle() returns true)
const QSizeF & symbolSize() const
Returns the size of the rendered symbol associated with this feature, if applicable.
double repeatDistance() const
Applies only to linestring features - after what distance (in map units) the labels should be repeate...
void setSymbolSize(QSizeF size)
Sets the size of the rendered symbol associated with this feature.
QgsPalLayerSettings::OffsetType offsetType() const
Returns the offset type, which determines how offsets and distance to label behaves.
QgsLabeling::PolygonPlacementFlags polygonPlacementFlags() const
Returns the polygon placement flags, which dictate how polygon labels can be placed.
QgsPointXY positionOffset() const
Applies only to "offset from point" placement strategy.
void setLayer(pal::Layer *layer)
Assign PAL layer to the label feature. Should be only used internally in PAL.
QgsMargins mVisualMargin
Visual margin of label contents.
bool hasFixedQuadrant() const
Returns whether the quadrant for the label is fixed.
void setZIndex(double zIndex)
Sets the label's z-index.
void setLineAnchorType(QgsLabelLineSettings::AnchorType type)
Sets the line anchor type, which dictates how the lineAnchorPercent() setting is handled.
bool hasFixedAngle() const
Whether the label should use a fixed angle instead of using angle from automatic placement.
QgsFeatureId mId
Associated ID unique within the parent label provider.
QgsLabeling::LinePlacementFlags arrangementFlags() const
Returns the feature's arrangement flags.
bool alwaysShow() const
Whether label should be always shown (sets very high label priority)
const QgsSymbol * symbol()
Returns the feature symbol associated with this label.
double lineAnchorPercent() const
Returns the percent along the line at which labels should be placed, for line labels only.
const QgsMargins & visualMargin() const
Returns the visual margin for the label feature.
void setLabelAllParts(bool labelAllParts)
Sets whether all parts of the feature should be labeled.
void setOffsetType(QgsPalLayerSettings::OffsetType type)
Sets the offset type, which determines how offsets and distance to label behaves.
void setSymbol(const QgsSymbol *symbol)
Sets the feature symbol associated with this label.
void setHasFixedAngle(bool enabled)
Sets whether the label should use a fixed angle instead of using angle from automatic placement.
QgsLabelLineSettings::AnchorType lineAnchorType() const
Returns the line anchor type, which dictates how the lineAnchorPercent() setting is handled.
double distLabel() const
Applies to "around point" placement strategy or linestring features.
QPointF quadOffset() const
Applies to "offset from point" placement strategy and "around point" (in case hasFixedQuadrant() retu...
QgsGeometry mPermissibleZone
Optional geometry to use for label's permissible zone.
void setPositionOffset(const QgsPointXY &offset)
Applies only to "offset from point" placement strategy.
QgsPointXY mPositionOffset
offset of label from the feature (only for "offset from point" placement)
QVector< QgsPalLayerSettings::PredefinedPointPosition > mPredefinedPositionOrder
Ordered list of predefined positions for label (only for OrderedPositionsAroundPoint placement)
void setLineAnchorPercent(double percent)
Sets the percent along the line at which labels should be placed, for line labels only.
QgsFeatureId id() const
Identifier of the label (unique within the parent label provider)
QPointF mQuadOffset
whether the side of the label is fixed (only for "around point" placement)
QSizeF mSize
Width and height of the label.
double priority() const
Returns the feature's labeling priority.
double zIndex() const
Returns the label's z-index.
void setHasFixedQuadrant(bool enabled)
Sets whether the quadrant for the label must be respected.
void setPriority(double priority)
Sets the priority for labeling the feature.
void setQuadOffset(QPointF quadOffset)
Set which side of the point to use.
double minimumSize() const
Returns the minimum size (in map unit) for a feature to be labelled.
QgsGeometry permissibleZone() const
Returns the label's permissible zone geometry.
void setPolygonPlacementFlags(QgsLabeling::PolygonPlacementFlags flags)
Sets the polygon placement flags, which dictate how polygon labels can be placed.
bool hasFixedPosition() const
Whether the label should use a fixed position instead of being automatically placed.
void setHasFixedPosition(bool enabled)
Sets whether the label should use a fixed position instead of being automatically placed.
geos::unique_ptr mGeometry
Geometry of the feature to be labelled.
void setFixedPosition(const QgsPointXY &point)
Sets coordinates of the fixed position (relevant only if hasFixedPosition() returns true)
QString labelText() const
Text of the label.
void setPredefinedPositionOrder(const QVector< QgsPalLayerSettings::PredefinedPointPosition > &order)
Sets the priority ordered list of predefined positions for label candidates.
const GEOSPreparedGeometry * permissibleZonePrepared() const
Returns a GEOS prepared geometry representing the label's permissibleZone().
GEOSGeometry * geometry() const
Gets access to the associated geometry.
void setVisualMargin(const QgsMargins &margin)
Sets the visual margin for the label feature.
void setLabelText(const QString &text)
Sets text of the label.
void setRotatedSize(QSizeF size)
Sets an alternate label size to be used when a label rotation angle is between 45 to 135 and 235 to 3...
bool labelAllParts() const
Returns true if all parts of the feature should be labeled.
QString mLabelText
text of the label
QgsPointXY fixedPosition() const
Coordinates of the fixed position (relevant only if hasFixedPosition() returns true)
void setFixedAngle(double angle)
Sets angle in degrees of the fixed angle (relevant only if hasFixedAngle() returns true)
AnchorType
Line anchor types.
@ HintOnly
Line anchor is a hint for preferred placement only, but other placements close to the hint are permit...
Contains settings related to how the label engine treats features as obstacles.
The QgsMargins class defines the four margins of a rectangle.
Definition: qgsmargins.h:38
OffsetType
Behavior modifier for label offset and distance, only applies in some label placement modes.
@ FromPoint
Offset distance applies from point geometry.
A class to represent a 2D point.
Definition: qgspointxy.h:59
Contains information about the context of a rendering operation.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
A set of features which influence the labeling process.
Definition: layer.h:62
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
std::unique_ptr< const GEOSPreparedGeometry, GeosDeleter > prepared_unique_ptr
Scoped GEOS prepared geometry pointer.
Definition: qgsgeos.h:84
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
const QgsCoordinateReferenceSystem & crs