QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgspallabeling.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgspallabeling.h
3  Smart labeling for vector layers
4  -------------------
5  begin : June 2009
6  copyright : (C) Martin Dobias
7  email : wonder dot sk at gmail dot com
8 
9  ***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 //Note: although this file is in the core library, it is not part of the stable API
19 //and might change at any time!
20 
21 #ifndef QGSPALLABELING_H
22 #define QGSPALLABELING_H
23 
24 #include "qgis_core.h"
25 #include "qgis_sip.h"
26 #include <QString>
27 #include <QFont>
28 #include <QFontDatabase>
29 #include <QColor>
30 #include <QHash>
31 #include <QList>
32 #include <QPainter>
33 #include <QRectF>
34 #include <QMap>
35 #include "qgsfeature.h"
36 #include "qgsgeometry.h"
37 #include "qgsfields.h"
39 #include "qgspointxy.h"
40 #include "qgsmapunitscale.h"
41 #include "qgsstringutils.h"
42 #include "qgssymbol.h"
43 #include "qgstextformat.h"
44 #include "qgspropertycollection.h"
47 #include "qgslabellinesettings.h"
48 #include "qgslabeling.h"
49 
50 class QgsTextDocument;
51 
52 namespace pal SIP_SKIP
53 {
54  class Pal;
55  class Layer;
56  class LabelPosition;
57 }
58 
60 class QgsRectangle;
61 class QgsMapToPixel;
62 class QgsFeature;
64 class QgsVectorLayer;
65 class QgsExpression;
66 class QFontMetricsF;
67 class QPainter;
68 class QPicture;
69 class QgsGeometry;
71 class QgsLabelSearchTree;
72 class QgsMapSettings;
73 class QgsLabelFeature;
74 class QgsLabelingEngine;
77 class QgsDxfExport;
80 class QgsCallout;
81 
87 class CORE_EXPORT QgsLabelPosition
88 {
89  public:
90 
109  QgsLabelPosition( QgsFeatureId id, double r, const QVector< QgsPointXY > &corners, const QgsRectangle &rect, double w, double h, const QString &layer, const QString &labeltext, const QFont &labelfont, bool upside_down, bool diagram = false, bool pinned = false, const QString &providerId = QString(),
110  const QgsGeometry &labelGeometry = QgsGeometry(), bool isUnplaced = false )
111  : featureId( id )
112  , rotation( r )
113  , cornerPoints( corners )
114  , labelRect( rect )
115  , labelGeometry( labelGeometry )
116  , width( w )
117  , height( h )
118  , layerID( layer )
119  , labelText( labeltext )
120  , labelFont( labelfont )
121  , upsideDown( upside_down )
122  , isDiagram( diagram )
123  , isPinned( pinned )
124  , providerID( providerId )
125  , isUnplaced( isUnplaced )
126  {}
127 
129  QgsLabelPosition() = default;
130 
134  QgsFeatureId featureId = FID_NULL;
135 
139  double rotation = 0;
140 
141  QVector< QgsPointXY > cornerPoints;
143 
149 
153  double width = 0;
154 
158  double height = 0;
159 
163  QString layerID;
164 
168  QString labelText;
169 
173  QFont labelFont;
174 
178  bool upsideDown = false;
179 
183  bool isDiagram = false;
184 
188  bool isPinned = false;
189 
194  QString providerID;
195 
200  bool isUnplaced = false;
201 };
202 
208 class CORE_EXPORT QgsPalLayerSettings
209 {
210  public:
214 
216  QgsPalLayerSettings &operator=( const QgsPalLayerSettings &s );
217 
218  //TODO QGIS 4.0 - move to QgsLabelingEngine
219 
224  {
234  };
235 
236  //TODO QGIS 4.0 - move to QgsLabelingEngine
239  {
252  };
253 
254  //TODO QGIS 4.0 - move to QgsLabelingEngine
255 
261  {
264  };
265 
266  //TODO QGIS 4.0 - remove, replaced by QgsLabeling::LinePlacementFlags
267 
274  {
275  OnLine = 1,
276  AboveLine = 2,
279  BelowLine = 4,
282  MapOrientation = 8,
285  };
286 
288  {
298  };
299 
301  {
304  ShowAll
305  };
306 
307  //TODO QGIS 4.0 - Remove -- moved to QgsLabelEngineObstacleSettings
308 
311  {
314  SymbolBelow
315  };
316 
318  {
319  MultiLeft = 0,
322  MultiFollowPlacement,
325  };
326 
327  //TODO QGIS 4.0 - Remove -- moved to QgsLabelEngineObstacleSettings
328 
334  {
335  PolygonInterior,
337  PolygonBoundary,
339  PolygonWhole
343  };
344 
346  enum Property
347  {
348  // text style
349  Size = 0,
350  Bold = 1,
351  Italic = 2,
352  Underline = 3,
353  Color = 4,
354  Strikeout = 5,
355  Family = 6,
356  FontStyle = 21,
357  FontSizeUnit = 22,
358  FontTransp = 18,
359  FontOpacity = 92,
360  FontCase = 27,
361  FontLetterSpacing = 28,
362  FontWordSpacing = 29,
363  FontBlendMode = 30,
364 
365  // text formatting
366  MultiLineWrapChar = 31,
367  AutoWrapLength = 101,
368  MultiLineHeight = 32,
369  MultiLineAlignment = 33,
370  TextOrientation = 110,
371  DirSymbDraw = 34,
372  DirSymbLeft = 35,
373  DirSymbRight = 36,
374  DirSymbPlacement = 37,
375  DirSymbReverse = 38,
376  NumFormat = 39,
377  NumDecimals = 40,
378  NumPlusSign = 41,
379 
380  // text buffer
381  BufferDraw = 42,
382  BufferSize = 7,
383  BufferUnit = 43,
384  BufferColor = 8,
385  BufferTransp = 19,
386  BufferOpacity = 94,
387  BufferJoinStyle = 44,
388  BufferBlendMode = 45,
389 
390  // mask buffer
391  MaskEnabled = 104,
392  MaskBufferSize = 105,
393  MaskBufferUnit = 106,
394  MaskOpacity = 107,
395  MaskJoinStyle = 108,
396 
397  // background
398  ShapeDraw = 46,
399  ShapeKind = 47,
400  ShapeSVGFile = 48,
401  ShapeSizeType = 49,
402  ShapeSizeX = 50,
403  ShapeSizeY = 85,
404  ShapeSizeUnits = 51,
405  ShapeRotationType = 52,
406  ShapeRotation = 53,
407  ShapeOffset = 54,
408  ShapeOffsetUnits = 55,
409  ShapeRadii = 56,
410  ShapeRadiiUnits = 57,
411  ShapeTransparency = 63,
412  ShapeOpacity = 93,
413  ShapeBlendMode = 64,
414  ShapeFillColor = 58,
415  ShapeStrokeColor = 59,
416  ShapeStrokeWidth = 60,
417  ShapeStrokeWidthUnits = 61,
418  ShapeJoinStyle = 62,
419 
420  // drop shadow
421  ShadowDraw = 65,
422  ShadowUnder = 66,
423  ShadowOffsetAngle = 67,
424  ShadowOffsetDist = 68,
425  ShadowOffsetUnits = 69,
426  ShadowRadius = 70,
427  ShadowRadiusUnits = 71,
428  ShadowTransparency = 72,
429  ShadowOpacity = 95,
430  ShadowScale = 73,
431  ShadowColor = 74,
432  ShadowBlendMode = 75,
433 
434  // placement
435  CentroidWhole = 76,
436  OffsetQuad = 77,
437  OffsetXY = 78,
438  OffsetUnits = 80,
439  LabelDistance = 13,
440  DistanceUnits = 81,
441  OffsetRotation = 82,
442  CurvedCharAngleInOut = 83,
443  // (data defined only)
444  PositionX = 9,
445  PositionY = 10,
446  Hali = 11,
447  Vali = 12,
448  Rotation = 14,
449  LabelRotation = 96,
450  RepeatDistance = 84,
451  RepeatDistanceUnit = 86,
452  Priority = 87,
453  PredefinedPositionOrder = 91,
454  LinePlacementOptions = 99,
455  OverrunDistance = 102,
456  LabelAllParts = 103,
457  PolygonLabelOutside = 109,
458  LineAnchorPercent = 111,
459 
460  // rendering
461  ScaleVisibility = 23,
462  MinScale = 16,
463  MinimumScale = 97,
464  MaxScale = 17,
465  MaximumScale = 98,
466  FontLimitPixel = 24,
467  FontMinPixel = 25,
468  FontMaxPixel = 26,
469  IsObstacle = 88,
470  ObstacleFactor = 89,
471  ZIndex = 90,
472  CalloutDraw = 100,
473 
474  // (data defined only)
475  Show = 15,
476  AlwaysShow = 20
477  };
478 
479 
491  bool prepare( QgsRenderContext &context, QSet<QString> &attributeNames SIP_INOUT, const QgsFields &fields, const QgsMapSettings &mapSettings, const QgsCoordinateReferenceSystem &crs );
492 
497  QSet<QString> referencedFields( const QgsRenderContext &context ) const;
498 
507  void startRender( QgsRenderContext &context );
508 
516  void stopRender( QgsRenderContext &context );
517 
522  static const QgsPropertiesDefinition &propertyDefinitions();
523 
532  bool drawLabels = true;
533 
534  //-- text style
535 
541  QString fieldName;
542 
547  bool isExpression = false;
548 
552  QgsExpression *getLabelExpression();
553 
557  Q_DECL_DEPRECATED QColor previewBkgrdColor = Qt::white;
558 
562  bool useSubstitutions = false;
563 
564  //-- text formatting
565 
570  QString wrapChar;
571 
579  int autoWrapLength = 0;
580 
591  bool useMaxLineLengthForAutoWrap = true;
592 
594  MultiLineAlign multilineAlign = MultiFollowPlacement;
595 
602  bool formatNumbers = false;
603 
609  int decimals = 3;
610 
616  bool plusSign = false;
617 
618  //-- placement
619 
620  Placement placement = AroundPoint;
621 
628  QgsLabeling::PolygonPlacementFlags polygonPlacementFlags() const { return mPolygonPlacementFlags; }
629 
636  void setPolygonPlacementFlags( QgsLabeling::PolygonPlacementFlags flags ) { mPolygonPlacementFlags = flags; }
637 
642  bool centroidWhole = false;
643 
649  bool centroidInside = false;
650 
657  QVector< PredefinedPointPosition > predefinedPositionOrder SIP_SKIP;
658 
662  bool fitInPolygonOnly = false;
663 
669  double dist = 0;
670 
677 
684 
686  OffsetType offsetType = FromPoint;
687 
693  double repeatDistance = 0;
694 
701 
708 
712  QuadrantPosition quadOffset = QuadrantOver;
713 
720  double xOffset = 0;
721 
728  double yOffset = 0;
729 
737 
745 
747  double angleOffset = 0;
748 
750  bool preserveRotation = true;
751 
756  double maxCurvedCharAngleIn = 25.0;
757 
762  double maxCurvedCharAngleOut = -25.0;
763 
768  int priority = 5;
769 
770  //-- rendering
771 
777  bool scaleVisibility = false;
778 
789  double maximumScale = 0;
790 
801  double minimumScale = 0;
802 
808  bool fontLimitPixelSize = false;
809 
815  int fontMinPixelSize = 0;
816 
822  int fontMaxPixelSize = 10000;
823 
825  bool displayAll = false;
826 
828  UpsideDownLabels upsidedownLabels = Upright;
829 
834  bool labelPerPart = false;
835 
836  // TODO QGIS 4.0 - remove this junk
837 
838 #ifdef SIP_RUN
839  SIP_PROPERTY( name = limitNumLabels, get = _limitNumLabels, set = _setLimitNumLabels )
840  SIP_PROPERTY( name = maxNumLabels, get = _maxNumLabels, set = _setMaxNumLabels )
841  SIP_PROPERTY( name = minFeatureSize, get = _minFeatureSize, set = _setMinFeatureSize )
842  SIP_PROPERTY( name = obstacle, get = _getIsObstacle, set = _setIsObstacle )
843  SIP_PROPERTY( name = obstacleFactor, get = _getObstacleFactor, set = _setObstacleFactor )
844  SIP_PROPERTY( name = obstacleType, get = _getObstacleType, set = _setObstacleType )
845  SIP_PROPERTY( name = placementFlags, get = _getLinePlacementFlags, set = _setLinePlacementFlags )
846  SIP_PROPERTY( name = mergeLines, get = _getMergeLines, set = _setMergeLines )
847  SIP_PROPERTY( name = addDirectionSymbol, get = _getAddDirectionSymbol, set = _setAddDirectionSymbol )
848  SIP_PROPERTY( name = leftDirectionSymbol, get = _getLeftDirectionSymbol, set = _setLeftDirectionSymbol )
849  SIP_PROPERTY( name = rightDirectionSymbol, get = _getRightDirectionSymbol, set = _setRightDirectionSymbol )
850  SIP_PROPERTY( name = reverseDirectionSymbol, get = _getReverseDirectionSymbol, set = _setReverseDirectionSymbol )
851  SIP_PROPERTY( name = placeDirectionSymbol, get = _getPlaceDirectionSymbol, set = _setPlaceDirectionSymbol )
852 
853  SIP_PROPERTY( name = overrunDistance, get = _getOverrunDistance, set = _setOverrunDistance )
854  SIP_PROPERTY( name = overrunDistanceUnit, get = _getOverrunDistanceUnit, set = _setOverrunDistanceUnit )
855  SIP_PROPERTY( name = overrunDistanceMapUnitScale, get = _getOverrunDistanceMapUnitScale, set = _setOverrunDistanceMapUnitScale )
856 #endif
857 
859  bool _limitNumLabels() const { return mThinningSettings.limitNumberOfLabelsEnabled(); }
860  void _setLimitNumLabels( bool limit ) { mThinningSettings.setLimitNumberLabelsEnabled( limit ); }
861  int _maxNumLabels() const { return mThinningSettings.maximumNumberLabels(); }
862  void _setMaxNumLabels( int max ) { mThinningSettings.setMaximumNumberLabels( max ); }
863  double _minFeatureSize() const { return mThinningSettings.minimumFeatureSize(); }
864  void _setMinFeatureSize( double size ) { mThinningSettings.setMinimumFeatureSize( size ); }
865  bool _getIsObstacle() const { return mObstacleSettings.isObstacle(); }
866  void _setIsObstacle( bool obstacle ) { mObstacleSettings.setIsObstacle( obstacle ); }
867  double _getObstacleFactor() const { return mObstacleSettings.factor(); }
868  void _setObstacleFactor( double factor ) { mObstacleSettings.setFactor( factor ); }
869  ObstacleType _getObstacleType() const { return static_cast< ObstacleType>( mObstacleSettings.type() ); }
870  void _setObstacleType( ObstacleType type ) { mObstacleSettings.setType( static_cast< QgsLabelObstacleSettings::ObstacleType>( type ) ); }
871  unsigned int _getLinePlacementFlags() const { return static_cast< unsigned int >( mLineSettings.placementFlags() ); }
872  void _setLinePlacementFlags( unsigned int flags ) { mLineSettings.setPlacementFlags( static_cast< QgsLabeling::LinePlacementFlags >( flags ) ); }
873  bool _getMergeLines() const { return mLineSettings.mergeLines(); }
874  void _setMergeLines( bool merge ) { mLineSettings.setMergeLines( merge ); }
875  bool _getAddDirectionSymbol() const { return mLineSettings.addDirectionSymbol(); }
876  void _setAddDirectionSymbol( bool add ) { mLineSettings.setAddDirectionSymbol( add ); }
877  QString _getLeftDirectionSymbol() const { return mLineSettings.leftDirectionSymbol(); }
878  void _setLeftDirectionSymbol( const QString &symbol ) { mLineSettings.setLeftDirectionSymbol( symbol ); }
879  QString _getRightDirectionSymbol() const { return mLineSettings.rightDirectionSymbol(); }
880  void _setRightDirectionSymbol( const QString &symbol ) { mLineSettings.setRightDirectionSymbol( symbol ); }
881  bool _getReverseDirectionSymbol() const { return mLineSettings.reverseDirectionSymbol(); }
882  void _setReverseDirectionSymbol( bool reverse ) { mLineSettings.setReverseDirectionSymbol( reverse ); }
884  DirectionSymbols _getPlaceDirectionSymbol() const { return static_cast< DirectionSymbols>( mLineSettings.directionSymbolPlacement() ); }
885  void _setPlaceDirectionSymbol( DirectionSymbols placement ) { mLineSettings.setDirectionSymbolPlacement( static_cast< QgsLabelLineSettings::DirectionSymbolPlacement>( placement ) ); }
887  double _getOverrunDistance() const { return mLineSettings.overrunDistance(); }
888  void _setOverrunDistance( double distance ) { mLineSettings.setOverrunDistance( distance ); }
889  QgsUnitTypes::RenderUnit _getOverrunDistanceUnit() const { return mLineSettings.overrunDistanceUnit(); }
890  void _setOverrunDistanceUnit( QgsUnitTypes::RenderUnit unit ) { mLineSettings.setOverrunDistanceUnit( unit ); }
891  QgsMapUnitScale _getOverrunDistanceMapUnitScale() const { return mLineSettings.overrunDistanceMapUnitScale(); }
892  void _setOverrunDistanceMapUnitScale( const QgsMapUnitScale &scale ) { mLineSettings.setOverrunDistanceMapUnitScale( scale ); }
894 
896  double zIndex = 0;
897 
900 
902  QgsWkbTypes::GeometryType geometryGeneratorType = QgsWkbTypes::GeometryType::PointGeometry;
903 
905  bool geometryGeneratorEnabled = false;
906 
912 
919 #ifndef SIP_RUN
920  void calculateLabelSize( const QFontMetricsF *fm, const QString &text, double &labelX, double &labelY, const QgsFeature *f = nullptr, QgsRenderContext *context = nullptr, double *rotatedLabelX SIP_OUT = nullptr, double *rotatedLabelY SIP_OUT = nullptr,
921  QgsTextDocument *document = nullptr );
922 #else
923  void calculateLabelSize( const QFontMetricsF *fm, const QString &text, double &labelX, double &labelY, const QgsFeature *f = nullptr, QgsRenderContext *context = nullptr, double *rotatedLabelX SIP_OUT = nullptr, double *rotatedLabelY SIP_OUT = nullptr );
924 #endif
925 
940  void registerFeature( const QgsFeature &f, QgsRenderContext &context,
941  QgsLabelFeature **labelFeature SIP_PYARGREMOVE = nullptr,
942  QgsGeometry obstacleGeometry SIP_PYARGREMOVE = QgsGeometry(), const QgsSymbol *symbol SIP_PYARGREMOVE = nullptr );
943 
948  void readXml( const QDomElement &elem, const QgsReadWriteContext &context );
949 
954  QDomElement writeXml( QDomDocument &doc, const QgsReadWriteContext &context ) const;
955 
961  QgsPropertyCollection &dataDefinedProperties() { return mDataDefinedProperties; }
962 
970  const QgsPropertyCollection &dataDefinedProperties() const SIP_SKIP { return mDataDefinedProperties; }
971 
979  void setDataDefinedProperties( const QgsPropertyCollection &collection ) { mDataDefinedProperties = collection; }
980 
986  const QgsTextFormat &format() const { return mFormat; }
987 
994  void setFormat( const QgsTextFormat &format ) { mFormat = format; }
995 
1004  QgsCallout *callout() const { return mCallout.get(); }
1005 
1014  void setCallout( QgsCallout *callout SIP_TRANSFER );
1015 
1024  const QgsLabelLineSettings &lineSettings() const { return mLineSettings; } SIP_SKIP
1025 
1033  QgsLabelLineSettings &lineSettings() { return mLineSettings; }
1034 
1042  void setLineSettings( const QgsLabelLineSettings &settings ) { mLineSettings = settings; }
1043 
1050  const QgsLabelObstacleSettings &obstacleSettings() const { return mObstacleSettings; } SIP_SKIP
1051 
1057  QgsLabelObstacleSettings &obstacleSettings() { return mObstacleSettings; }
1058 
1064  void setObstacleSettings( const QgsLabelObstacleSettings &settings ) { mObstacleSettings = settings; }
1065 
1072  const QgsLabelThinningSettings &thinningSettings() const { return mThinningSettings; } SIP_SKIP
1073 
1079  QgsLabelThinningSettings &thinningSettings() { return mThinningSettings; }
1080 
1086  void setThinningSettings( const QgsLabelThinningSettings &settings ) { mThinningSettings = settings; }
1087 
1096  static QPixmap labelSettingsPreviewPixmap( const QgsPalLayerSettings &settings, QSize size, const QString &previewText = QString(), int padding = 0 );
1097 
1098  // temporary stuff: set when layer gets prepared or labeled
1099  const QgsFeature *mCurFeat = nullptr;
1101  int fieldIndex = 0;
1102  const QgsMapToPixel *xform = nullptr;
1104 
1108  int mFeaturesToLabel = 0; // total features that will probably be labeled, may be less (figured before PAL)
1109  int mFeatsSendingToPal = 0; // total features tested for sending into PAL (relative to maxNumLabels)
1110  int mFeatsRegPal = 0; // number of features registered in PAL, when using limitNumLabels
1111  private:
1112 
1113  friend class QgsVectorLayer; // to allow calling readFromLayerCustomProperties()
1114 
1119  void readFromLayerCustomProperties( QgsVectorLayer *layer );
1120 
1124  void readOldDataDefinedPropertyMap( QgsVectorLayer *layer, QDomElement *parentElem );
1125 
1129  void readOldDataDefinedProperty( QgsVectorLayer *layer, QgsPalLayerSettings::Property p );
1130 
1131  enum DataDefinedValueType
1132  {
1133  DDBool,
1134  DDInt,
1135  DDIntPos,
1136  DDDouble,
1137  DDDoublePos,
1138  DDRotation180,
1139  DDOpacity,
1140  DDString,
1141  DDUnits,
1142  DDColor,
1143  DDJoinStyle,
1144  DDBlendMode,
1145  DDPointF,
1146  DDSizeF,
1147  };
1148 
1149  // convenience data defined evaluation function
1150  bool dataDefinedValEval( DataDefinedValueType valType,
1152  QVariant &exprVal, QgsExpressionContext &context, const QVariant &originalValue = QVariant() );
1153 
1154  void parseTextStyle( QFont &labelFont,
1155  QgsUnitTypes::RenderUnit fontunits,
1156  QgsRenderContext &context );
1157 
1158  void parseTextBuffer( QgsRenderContext &context );
1159 
1160  void parseTextMask( QgsRenderContext &context );
1161 
1162  void parseTextFormatting( QgsRenderContext &context );
1163 
1164  void parseShapeBackground( QgsRenderContext &context );
1165 
1166  void parseDropShadow( QgsRenderContext &context );
1167 
1172  bool checkMinimumSizeMM( const QgsRenderContext &ct, const QgsGeometry &geom, double minSize ) const;
1173 
1177  void registerObstacleFeature( const QgsFeature &f, QgsRenderContext &context, QgsLabelFeature **obstacleFeature, const QgsGeometry &obstacleGeometry = QgsGeometry() );
1178 
1179  QMap<Property, QVariant> dataDefinedValues;
1180 
1182  QgsPropertyCollection mDataDefinedProperties;
1183 
1184  QgsExpression *expression = nullptr;
1185 
1186  QFontDatabase mFontDB;
1187 
1188  QgsTextFormat mFormat;
1189 
1190  std::unique_ptr< QgsCallout > mCallout;
1191 
1192  QgsLabelLineSettings mLineSettings;
1193  QgsLabelObstacleSettings mObstacleSettings;
1194  QgsLabelThinningSettings mThinningSettings;
1195 
1196  QgsLabeling::PolygonPlacementFlags mPolygonPlacementFlags = QgsLabeling::PolygonPlacementFlag::AllowPlacementInsideOfPolygon;
1197 
1198  QgsExpression mGeometryGeneratorExpression;
1199 
1200  bool mRenderStarted = false;
1201 
1202  static void initPropertyDefinitions();
1203 };
1204 
1209 class CORE_EXPORT QgsLabelCandidate
1210 {
1211  public:
1212  QgsLabelCandidate( const QRectF &r, double c ): rect( r ), cost( c ) {}
1213 
1214  QRectF rect;
1215  double cost;
1216 };
1217 
1218 
1219 
1220 
1226 class CORE_EXPORT QgsLabelingResults
1227 {
1228  public:
1231 
1236 
1238  QList<QgsLabelPosition> labelsAtPosition( const QgsPointXY &p ) const;
1240  QList<QgsLabelPosition> labelsWithinRect( const QgsRectangle &r ) const;
1241 
1246  void setMapSettings( const QgsMapSettings &settings );
1247 
1248  private:
1249 #ifdef SIP_RUN
1251 #endif
1252 
1253  std::unique_ptr< QgsLabelSearchTree > mLabelSearchTree;
1254 
1255  friend class QgsPalLabeling;
1258 };
1259 
1265 class CORE_EXPORT QgsPalLabeling
1266 {
1267  public:
1268 
1273  static bool staticWillUseLayer( const QgsMapLayer *layer );
1274 
1276  static void drawLabelCandidateRect( pal::LabelPosition *lp, QPainter *painter, const QgsMapToPixel *xform, QList<QgsLabelCandidate> *candidates = nullptr ) SIP_SKIP;
1277 
1288  static QgsGeometry prepareGeometry( const QgsGeometry &geometry, QgsRenderContext &context, const QgsCoordinateTransform &ct, const QgsGeometry &clipGeometry = QgsGeometry(), bool mergeLines = false ) SIP_FACTORY;
1289 
1300  static bool geometryRequiresPreparation( const QgsGeometry &geometry, QgsRenderContext &context, const QgsCoordinateTransform &ct, const QgsGeometry &clipGeometry = QgsGeometry(), bool mergeLines = false );
1301 
1314  static QStringList splitToLines( const QString &text, const QString &wrapCharacter, int autoWrapLength = 0, bool useMaxLineLengthWhenAutoWrapping = true );
1315 
1324  static QStringList splitToGraphemes( const QString &text );
1325 
1326  private:
1328  static void dataDefinedTextStyle( QgsPalLayerSettings &tmpLyr,
1329  const QMap< QgsPalLayerSettings::Property, QVariant > &ddValues );
1330 
1332  static void dataDefinedTextFormatting( QgsPalLayerSettings &tmpLyr,
1333  const QMap< QgsPalLayerSettings::Property, QVariant > &ddValues );
1334 
1336  static void dataDefinedTextBuffer( QgsPalLayerSettings &tmpLyr,
1337  const QMap< QgsPalLayerSettings::Property, QVariant > &ddValues );
1338 
1340  static void dataDefinedTextMask( QgsPalLayerSettings &tmpLyr,
1341  const QMap< QgsPalLayerSettings::Property, QVariant > &ddValues );
1342 
1344  static void dataDefinedShapeBackground( QgsPalLayerSettings &tmpLyr,
1345  const QMap< QgsPalLayerSettings::Property, QVariant > &ddValues );
1346 
1348  static void dataDefinedDropShadow( QgsPalLayerSettings &tmpLyr,
1349  const QMap< QgsPalLayerSettings::Property, QVariant > &ddValues );
1350 
1351  friend class QgsVectorLayerLabelProvider; // to allow calling the static methods above
1352  friend class QgsDxfExport; // to allow calling the static methods above
1353 
1362  static bool checkMinimumSizeMM( const QgsRenderContext &context, const QgsGeometry &geom, double minSize );
1363 
1364  friend class QgsPalLayerSettings;
1365 };
1366 
1367 
1368 #endif // QGSPALLABELING_H
Abstract base class for callout renderers.
Definition: qgscallout.h:47
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
Stores the settings for rendering of all diagrams for a layer.
Exports QGIS layers to the DXF format.
Definition: qgsdxfexport.h:64
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
Represents a label candidate.
QgsLabelCandidate(const QRectF &r, double c)
The QgsLabelFeature class describes a feature that should be used within the labeling engine.
Contains settings related to how the label engine places and formats labels for line features (or pol...
DirectionSymbolPlacement
Placement options for direction symbols.
Contains settings related to how the label engine treats features as obstacles.
ObstacleType
Valid obstacle types, which affect how features within the layer will act as obstacles for labels.
Represents the calculated placement of a map label.
QgsGeometry labelGeometry
A polygon geometry representing the label's bounds in map coordinates.
QgsLabelPosition(QgsFeatureId id, double r, const QVector< QgsPointXY > &corners, const QgsRectangle &rect, double w, double h, const QString &layer, const QString &labeltext, const QFont &labelfont, bool upside_down, bool diagram=false, bool pinned=false, const QString &providerId=QString(), const QgsGeometry &labelGeometry=QgsGeometry(), bool isUnplaced=false)
Constructor for QgsLabelPosition.
QString labelText
String shown in label.
QString providerID
ID of the associated label provider.
QgsLabelPosition()=default
Constructor for QgsLabelPosition.
QFont labelFont
Font which the label is rendered using.
QgsRectangle labelRect
QVector< QgsPointXY > cornerPoints
QString layerID
ID of associated map layer.
A class to query the labeling structure at a given point (small wrapper around pal RTree class)
Contains settings related to how the label engine removes candidate label positions and reduces the n...
The QgsLabelingEngine class provides map labeling functionality.
Class that stores computed placement from labeling engine.
QgsLabelingResults(const QgsLabelingResults &)=delete
QgsLabelingResults cannot be copied.
QgsLabelingResults & operator=(const QgsLabelingResults &rh)=delete
QgsLabelingResults cannot be copied.
Base class for all map layer types.
Definition: qgsmaplayer.h:85
The QgsMapSettings class contains configuration for rendering of the map.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:39
Struct for storing maximum and minimum scales for measurements in map units.
PAL labeling utilities.
Contains settings for how a map layer will be labeled.
QgsMapUnitScale labelOffsetMapUnitScale
Map unit scale for label offset.
void setObstacleSettings(const QgsLabelObstacleSettings &settings)
Sets the label obstacle settings.
void setFormat(const QgsTextFormat &format)
Sets the label text formatting settings, e.g., font settings, buffer settings, etc.
QString wrapChar
Wrapping character string.
@ SymbolAbove
Place direction symbols on above label.
@ SymbolLeftRight
Place direction symbols on left/right of label.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the label's property collection, used for data defined overrides.
QgsCoordinateTransform ct
@ ShowDefined
Show upside down when rotation is layer- or data-defined.
@ Upright
Upside-down labels (90 <= angle < 270) are shown upright.
void setLineSettings(const QgsLabelLineSettings &settings)
Sets the label line settings, which contain settings related to how the label engine places and forma...
QgsCallout * callout() const
Returns the label callout renderer, responsible for drawing label callouts.
Placement
Placement modes which determine how label candidates are generated for a feature.
@ PerimeterCurved
Arranges candidates following the curvature of a polygon's boundary. Applies to polygon layers only.
@ Curved
Arranges candidates following the curvature of a line feature. Applies to line layers only.
@ Horizontal
Arranges horizontal candidates scattered throughout a polygon feature. Applies to polygon layers only...
@ Free
Arranges candidates scattered throughout a polygon feature. Candidates are rotated to respect the pol...
@ OverPoint
Arranges candidates over a point (or centroid of a polygon), or at a preset offset from the point....
@ AroundPoint
Arranges candidates in a circle around a point (or centroid of a polygon). Applies to point or polygo...
@ Line
Arranges candidates parallel to a generalised line representing the feature or parallel to a polygon'...
@ OrderedPositionsAroundPoint
Candidates are placed in predefined positions around a point. Preference is given to positions with g...
@ OutsidePolygons
Candidates are placed outside of polygon boundaries. Applies to polygon layers only....
@ MultiJustify
Justified.
PredefinedPointPosition
Positions for labels when using the QgsPalLabeling::OrderedPositionsAroundPoint placement mode.
@ BottomSlightlyLeft
Label below point, slightly left of center.
@ BottomMiddle
Label directly below point.
@ BottomSlightlyRight
Label below point, slightly right of center.
@ MiddleLeft
Label on left of point.
@ TopSlightlyRight
Label on top of point, slightly right of center.
@ TopSlightlyLeft
Label on top of point, slightly left of center.
@ MiddleRight
Label on right of point.
@ TopMiddle
Label directly above point.
@ BottomRight
Label on bottom right of point.
@ BottomLeft
Label on bottom-left of point.
@ TopRight
Label on top-right of point.
@ TopLeft
Label on top-left of point.
const QgsPropertyCollection & dataDefinedProperties() const
Returns a reference to the label's property collection, used for data defined overrides.
Property
Data definable properties.
const QgsLabelLineSettings & lineSettings() const
Returns the label line settings, which contain settings related to how the label engine places and fo...
void setPolygonPlacementFlags(QgsLabeling::PolygonPlacementFlags flags)
Sets the polygon placement flags, which dictate how polygon labels can be placed.
const QgsLabelObstacleSettings & obstacleSettings() const
Returns the label obstacle settings.
OffsetType
Behavior modifier for label offset and distance, only applies in some label placement modes.
@ FromPoint
Offset distance applies from point geometry.
@ FromSymbolBounds
Offset distance applies from rendered symbol bounds.
void setThinningSettings(const QgsLabelThinningSettings &settings)
Sets the label thinning settings.
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the label's property collection, used for data defined overrides.
QgsLabeling::PolygonPlacementFlags polygonPlacementFlags() const
Returns the polygon placement flags, which dictate how polygon labels can be placed.
QString geometryGenerator
The geometry generator expression. Null if disabled.
QgsMapUnitScale distMapUnitScale
Map unit scale for label feature distance.
QgsStringReplacementCollection substitutions
Substitution collection for automatic text substitution with labels.
LinePlacementFlags
Line placement flags, which control how candidates are generated for a linear feature.
QgsMapUnitScale repeatDistanceMapUnitScale
Map unit scale for repeating labels for a single feature.
const QgsTextFormat & format() const
Returns the label text formatting settings, e.g., font settings, buffer settings, etc.
QString fieldName
Name of field (or an expression) to use for label text.
QgsLabelLineSettings & lineSettings()
Returns the label line settings, which contain settings related to how the label engine places and fo...
QgsLabelObstacleSettings & obstacleSettings()
Returns the label obstacle settings.
const QgsLabelThinningSettings & thinningSettings() const
Returns the label thinning settings.
ObstacleType
Valid obstacle types, which affect how features within the layer will act as obstacles for labels.
QgsLabelThinningSettings & thinningSettings()
Returns the label thinning settings.
A class to represent a 2D point.
Definition: qgspointxy.h:44
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
A collection of string replacements (specified using QgsStringReplacement objects).
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:65
Represents a document consisting of one or more QgsTextBlock objects.
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
Class that adds extra information to QgsLabelFeature for text labels.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
The QgsVectorLayerDiagramProvider class implements support for diagrams within the labeling engine.
The QgsVectorLayerLabelProvider class implements a label provider for vector layers.
Represents a vector layer which manages a vector based data sets.
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:141
LabelPosition is a candidate feature label position.
Definition: labelposition.h:56
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:798
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:797
#define SIP_PROPERTY(name, getter, setter)
Definition: qgis_sip.h:262
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:146
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_INOUT
Definition: qgis_sip.h:71
#define FID_NULL
Definition: qgsfeatureid.h:29
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.
const QgsCoordinateReferenceSystem & crs