QGIS API Documentation  3.23.0-Master (eb871beae0)
qgsvectorlayerlabelprovider.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayerlabelprovider.cpp
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 
17 
18 #include "qgsgeometry.h"
19 #include "qgslabelsearchtree.h"
20 #include "qgspallabeling.h"
21 #include "qgstextlabelfeature.h"
22 #include "qgsvectorlayer.h"
24 #include "qgsrenderer.h"
25 #include "qgspolygon.h"
26 #include "qgslinestring.h"
27 #include "qgsmultipolygon.h"
28 #include "qgslogger.h"
30 #include "qgsmaskidprovider.h"
31 #include "qgstextcharacterformat.h"
32 #include "qgstextfragment.h"
33 #include "qgslabelingresults.h"
34 
35 #include "feature.h"
36 #include "labelposition.h"
37 #include "callouts/qgscallout.h"
38 #include "qgssymbol.h"
39 #include "qgsmarkersymbol.h"
40 
41 #include "pal/layer.h"
42 
43 #include <QPicture>
44 #include <QTextDocument>
45 #include <QTextFragment>
46 
47 using namespace pal;
48 
49 QgsVectorLayerLabelProvider::QgsVectorLayerLabelProvider( QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings, const QString &layerName )
50  : QgsAbstractLabelProvider( layer, providerId )
51  , mSettings( settings ? * settings : QgsPalLayerSettings() ) // TODO: all providers should have valid settings?
52  , mLayerGeometryType( layer->geometryType() )
53  , mRenderer( layer->renderer() )
54  , mFields( layer->fields() )
55  , mCrs( layer->crs() )
56 {
57  mName = layerName.isEmpty() ? layer->id() : layerName;
58 
59  if ( withFeatureLoop )
60  {
61  mSource = std::make_unique<QgsVectorLayerFeatureSource>( layer );
62  }
63 
64  init();
65 }
66 
67 QgsVectorLayerLabelProvider::QgsVectorLayerLabelProvider( QgsWkbTypes::GeometryType geometryType, const QgsFields &fields, const QgsCoordinateReferenceSystem &crs, const QString &providerId, const QgsPalLayerSettings *settings, QgsMapLayer *layer, const QString &layerName )
68  : QgsAbstractLabelProvider( layer, providerId )
69  , mSettings( settings ? * settings : QgsPalLayerSettings() ) // TODO: all providers should have valid settings?
70  , mLayerGeometryType( geometryType )
71  , mRenderer( nullptr )
72  , mFields( fields )
73  , mCrs( crs )
74 {
75  mName = layerName.isEmpty() ? layer->id() : layerName;
76 
77  init();
78 }
79 
81 {
83  mFlags = Flags();
84  if ( mSettings.drawLabels )
85  mFlags |= DrawLabels;
86  if ( mSettings.displayAll )
92 
93  mPriority = 1 - mSettings.priority / 10.0; // convert 0..10 --> 1..0
94 
96  {
97  //override obstacle type to treat any intersection of a label with the point symbol as a high cost conflict
99  }
100  else
101  {
103  }
104 
106 }
107 
108 
110 {
111  qDeleteAll( mLabels );
112 }
113 
114 
115 bool QgsVectorLayerLabelProvider::prepare( QgsRenderContext &context, QSet<QString> &attributeNames )
116 {
117  const QgsMapSettings &mapSettings = mEngine->mapSettings();
118 
119  return mSettings.prepare( context, attributeNames, mFields, mapSettings, mCrs );
120 }
121 
123 {
125  mSettings.startRender( context );
126 }
127 
129 {
131  mSettings.stopRender( context );
132 }
133 
135 {
136  if ( !mSource )
137  {
138  // we have created the provider with "own feature loop" == false
139  // so it is assumed that prepare() has been already called followed by registerFeature() calls
140  return mLabels;
141  }
142 
143  QSet<QString> attrNames;
144  if ( !prepare( ctx, attrNames ) )
145  return QList<QgsLabelFeature *>();
146 
147  if ( mRenderer )
148  mRenderer->startRender( ctx, mFields );
149 
150  QgsRectangle layerExtent = ctx.extent();
152  layerExtent = mSettings.ct.transformBoundingBox( ctx.extent(), Qgis::TransformDirection::Reverse );
153 
154  QgsFeatureRequest request;
155  request.setFilterRect( layerExtent );
156  request.setSubsetOfAttributes( attrNames, mFields );
157  QgsFeatureIterator fit = mSource->getFeatures( request );
158 
160  ctx.expressionContext().appendScope( symbolScope );
161  QgsFeature fet;
162  while ( fit.nextFeature( fet ) )
163  {
164  QgsGeometry obstacleGeometry;
165  const QgsSymbol *symbol = nullptr;
166  if ( mRenderer )
167  {
168  QgsSymbolList symbols = mRenderer->originalSymbolsForFeature( fet, ctx );
169  if ( !symbols.isEmpty() && fet.geometry().type() == QgsWkbTypes::PointGeometry )
170  {
171  //point feature, use symbol bounds as obstacle
172  obstacleGeometry = QgsVectorLayerLabelProvider::getPointObstacleGeometry( fet, ctx, symbols );
173  }
174  if ( !symbols.isEmpty() )
175  {
176  symbol = symbols.at( 0 );
177  symbolScope = QgsExpressionContextUtils::updateSymbolScope( symbol, symbolScope );
178  }
179  }
180  ctx.expressionContext().setFeature( fet );
181  registerFeature( fet, ctx, obstacleGeometry, symbol );
182  }
183 
184  if ( ctx.expressionContext().lastScope() == symbolScope )
185  delete ctx.expressionContext().popScope();
186 
187  if ( mRenderer )
188  mRenderer->stopRender( ctx );
189 
190  return mLabels;
191 }
192 
193 QList< QgsLabelFeature * > QgsVectorLayerLabelProvider::registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry, const QgsSymbol *symbol )
194 {
195  std::unique_ptr< QgsLabelFeature > label = mSettings.registerFeatureWithDetails( feature, context, obstacleGeometry, symbol );
196  QList< QgsLabelFeature * > res;
197  if ( label )
198  {
199  res << label.get();
200  mLabels << label.release();
201  }
202  return res;
203 }
204 
206 {
207  if ( !fet.hasGeometry() || fet.geometry().type() != QgsWkbTypes::PointGeometry )
208  return QgsGeometry();
209 
210  bool isMultiPoint = fet.geometry().constGet()->nCoordinates() > 1;
211  std::unique_ptr< QgsAbstractGeometry > obstacleGeom;
212  if ( isMultiPoint )
213  obstacleGeom = std::make_unique< QgsMultiPolygon >();
214 
215  // for each point
216  for ( int i = 0; i < fet.geometry().constGet()->nCoordinates(); ++i )
217  {
218  QRectF bounds;
219  QgsPoint p = fet.geometry().constGet()->vertexAt( QgsVertexId( i, 0, 0 ) );
220  double x = p.x();
221  double y = p.y();
222  double z = 0; // dummy variable for coordinate transforms
223 
224  //transform point to pixels
225  if ( context.coordinateTransform().isValid() )
226  {
227  try
228  {
229  context.coordinateTransform().transformInPlace( x, y, z );
230  }
231  catch ( QgsCsException & )
232  {
233  return QgsGeometry();
234  }
235  }
236  context.mapToPixel().transformInPlace( x, y );
237 
238  QPointF pt( x, y );
239  const auto constSymbols = symbols;
240  for ( QgsSymbol *symbol : constSymbols )
241  {
242  if ( symbol->type() == Qgis::SymbolType::Marker )
243  {
244  if ( bounds.isValid() )
245  bounds = bounds.united( static_cast< QgsMarkerSymbol * >( symbol )->bounds( pt, context, fet ) );
246  else
247  bounds = static_cast< QgsMarkerSymbol * >( symbol )->bounds( pt, context, fet );
248  }
249  }
250 
251  //convert bounds to a geometry
252  QVector< double > bX;
253  bX << bounds.left() << bounds.right() << bounds.right() << bounds.left();
254  QVector< double > bY;
255  bY << bounds.top() << bounds.top() << bounds.bottom() << bounds.bottom();
256  std::unique_ptr< QgsLineString > boundLineString = std::make_unique< QgsLineString >( bX, bY );
257 
258  //then transform back to map units
259  //TODO - remove when labeling is refactored to use screen units
260  for ( int i = 0; i < boundLineString->numPoints(); ++i )
261  {
262  QgsPointXY point = context.mapToPixel().toMapCoordinates( static_cast<int>( boundLineString->xAt( i ) ),
263  static_cast<int>( boundLineString->yAt( i ) ) );
264  boundLineString->setXAt( i, point.x() );
265  boundLineString->setYAt( i, point.y() );
266  }
267  if ( context.coordinateTransform().isValid() )
268  {
269  try
270  {
271  boundLineString->transform( context.coordinateTransform(), Qgis::TransformDirection::Reverse );
272  }
273  catch ( QgsCsException & )
274  {
275  return QgsGeometry();
276  }
277  }
278  boundLineString->close();
279 
280  if ( context.coordinateTransform().isValid() )
281  {
282  // coordinate transforms may have resulted in nan coordinates - if so, strip these out
283  boundLineString->filterVertices( []( const QgsPoint & point )->bool
284  {
285  return std::isfinite( point.x() ) && std::isfinite( point.y() );
286  } );
287  if ( !boundLineString->isRing() )
288  return QgsGeometry();
289  }
290 
291  std::unique_ptr< QgsPolygon > obstaclePolygon = std::make_unique< QgsPolygon >();
292  obstaclePolygon->setExteriorRing( boundLineString.release() );
293 
294  if ( isMultiPoint )
295  {
296  static_cast<QgsMultiPolygon *>( obstacleGeom.get() )->addGeometry( obstaclePolygon.release() );
297  }
298  else
299  {
300  obstacleGeom = std::move( obstaclePolygon );
301  }
302  }
303 
304  return QgsGeometry( std::move( obstacleGeom ) );
305 }
306 
308 {
309  if ( !mSettings.drawLabels )
310  return;
311 
312  // render callout
314  {
315  drawCallout( context, label );
316  }
317 }
318 
319 void QgsVectorLayerLabelProvider::drawCallout( QgsRenderContext &context, pal::LabelPosition *label ) const
320 {
321  bool enabled = mSettings.callout()->enabled();
323  {
324  context.expressionContext().setOriginalValueVariable( enabled );
326  }
327  if ( enabled )
328  {
329  QgsMapToPixel xform = context.mapToPixel();
330  xform.setMapRotation( 0, 0, 0 );
331  QPointF outPt = xform.transform( label->getX(), label->getY() ).toQPointF();
332  QgsPointXY outPt2 = xform.transform( label->getX() + label->getWidth(), label->getY() + label->getHeight() );
333  QRectF rect( outPt.x(), outPt.y(), outPt2.x() - outPt.x(), outPt2.y() - outPt.y() );
334 
336  g.transform( xform.transform() );
337  QgsCallout::QgsCalloutContext calloutContext;
338  calloutContext.allFeaturePartsLabeled = label->getFeaturePart()->feature()->labelAllParts();
339  calloutContext.originalFeatureCrs = label->getFeaturePart()->feature()->originalFeatureCrs();
340  mSettings.callout()->render( context, rect, label->getAlpha() * 180 / M_PI, g, calloutContext );
341 
342  const QList< QgsCalloutPosition > renderedPositions = calloutContext.positions();
343 
344  for ( QgsCalloutPosition position : renderedPositions )
345  {
346  position.layerID = mLayerId;
347  position.featureId = label->getFeaturePart()->featureId();
348  position.providerID = mProviderId;
349  mEngine->results()->mLabelSearchTree->insertCallout( position );
350  }
351  }
352 }
353 
355 {
356  if ( !mSettings.drawLabels )
357  return;
358 
359  QgsTextLabelFeature *lf = dynamic_cast<QgsTextLabelFeature *>( label->getFeaturePart()->feature() );
360 
361  // Copy to temp, editable layer settings
362  // these settings will be changed by any data defined values, then used for rendering label components
363  // settings may be adjusted during rendering of components
364  QgsPalLayerSettings tmpLyr( mSettings );
365 
366  // apply any previously applied data defined settings for the label
367  const QMap< QgsPalLayerSettings::Property, QVariant > &ddValues = lf->dataDefinedValues();
368 
369  //font
370  QFont dFont = lf->definedFont();
371  QgsDebugMsgLevel( QStringLiteral( "PAL font tmpLyr: %1, Style: %2" ).arg( tmpLyr.format().font().toString(), tmpLyr.format().font().styleName() ), 4 );
372  QgsDebugMsgLevel( QStringLiteral( "PAL font definedFont: %1, Style: %2" ).arg( dFont.toString(), dFont.styleName() ), 4 );
373 
374  QgsTextFormat format = tmpLyr.format();
375  format.setFont( dFont );
376 
377  // size has already been calculated and stored in the defined font - this calculated size
378  // is in pixels
379  format.setSize( dFont.pixelSize() );
381  tmpLyr.setFormat( format );
382 
384  {
385  //calculate font alignment based on label quadrant
386  switch ( label->getQuadrant() )
387  {
388  case LabelPosition::QuadrantAboveLeft:
389  case LabelPosition::QuadrantLeft:
390  case LabelPosition::QuadrantBelowLeft:
392  break;
393  case LabelPosition::QuadrantAbove:
394  case LabelPosition::QuadrantOver:
395  case LabelPosition::QuadrantBelow:
397  break;
398  case LabelPosition::QuadrantAboveRight:
399  case LabelPosition::QuadrantRight:
400  case LabelPosition::QuadrantBelowRight:
402  break;
403  }
404  }
405 
406  // update tmpLyr with any data defined text style values
407  QgsPalLabeling::dataDefinedTextStyle( tmpLyr, ddValues );
408 
409  // update tmpLyr with any data defined text buffer values
410  QgsPalLabeling::dataDefinedTextBuffer( tmpLyr, ddValues );
411 
412  // update tmpLyr with any data defined text mask values
413  QgsPalLabeling::dataDefinedTextMask( tmpLyr, ddValues );
414 
415  // update tmpLyr with any data defined text formatting values
416  QgsPalLabeling::dataDefinedTextFormatting( tmpLyr, ddValues );
417 
418  // update tmpLyr with any data defined shape background values
419  QgsPalLabeling::dataDefinedShapeBackground( tmpLyr, ddValues );
420 
421  // update tmpLyr with any data defined drop shadow values
422  QgsPalLabeling::dataDefinedDropShadow( tmpLyr, ddValues );
423 
424  // Render the components of a label in reverse order
425  // (backgrounds -> text)
426 
427  // render callout
429  {
430  drawCallout( context, label );
431  }
432 
434  {
435  QgsTextFormat format = tmpLyr.format();
436 
437  if ( tmpLyr.format().background().enabled() && tmpLyr.format().background().type() != QgsTextBackgroundSettings::ShapeMarkerSymbol ) // background shadows not compatible with marker symbol backgrounds
438  {
440  }
441  else if ( tmpLyr.format().buffer().enabled() )
442  {
444  }
445  else
446  {
448  }
449 
450  tmpLyr.setFormat( format );
451  }
452 
453  if ( tmpLyr.format().background().enabled() )
454  {
455  drawLabelPrivate( label, context, tmpLyr, QgsTextRenderer::Background );
456  }
457 
458  if ( tmpLyr.format().buffer().enabled() )
459  {
460  drawLabelPrivate( label, context, tmpLyr, QgsTextRenderer::Buffer );
461  }
462 
463  drawLabelPrivate( label, context, tmpLyr, QgsTextRenderer::Text );
464 
465  // add to the results
466  QString labeltext = label->getFeaturePart()->feature()->labelText();
467  mEngine->results()->mLabelSearchTree->insertLabel( label, label->getFeaturePart()->featureId(), mLayerId, labeltext, dFont, false, lf->hasFixedPosition(), mProviderId );
468 }
469 
471 {
472  QgsTextLabelFeature *lf = dynamic_cast<QgsTextLabelFeature *>( label->getFeaturePart()->feature() );
473 
474  QgsTextFormat format = mSettings.format();
475  if ( mSettings.drawLabels
478  {
479  QgsPalLayerSettings tmpLyr( mSettings );
480  format = tmpLyr.format();
482  tmpLyr.setFormat( format );
483  drawLabelPrivate( label, context, tmpLyr, QgsTextRenderer::Text );
484  }
485 
486  // add to the results
487  QString labeltext = label->getFeaturePart()->feature()->labelText();
488  mEngine->results()->mLabelSearchTree->insertLabel( label, label->getFeaturePart()->featureId(), mLayerId, labeltext, format.font(), false, lf->hasFixedPosition(), mProviderId, true );
489 }
490 
492 {
493  // NOTE: this is repeatedly called for multi-part labels
494  QPainter *painter = context.painter();
495 
496  // features are pre-rotated but not scaled/translated,
497  // so we only disable rotation here. Ideally, they'd be
498  // also pre-scaled/translated, as suggested here:
499  // https://github.com/qgis/QGIS/issues/20071
500  QgsMapToPixel xform = context.mapToPixel();
501  xform.setMapRotation( 0, 0, 0 );
502 
503  QPointF outPt = xform.transform( label->getX(), label->getY() ).toQPointF();
504 
505  if ( mEngine->engineSettings().testFlag( QgsLabelingEngineSettings::DrawLabelRectOnly ) ) // TODO: this should get directly to labeling engine
506  {
507  //debugging rect
508  if ( drawType != QgsTextRenderer::Text )
509  return;
510 
511  QgsPointXY outPt2 = xform.transform( label->getX() + label->getWidth(), label->getY() + label->getHeight() );
512  QRectF rect( 0, 0, outPt2.x() - outPt.x(), outPt2.y() - outPt.y() );
513  painter->save();
514  painter->setRenderHint( QPainter::Antialiasing, false );
515  painter->translate( QPointF( outPt.x(), outPt.y() ) );
516  painter->rotate( -label->getAlpha() * 180 / M_PI );
517 
518  if ( label->conflictsWithObstacle() )
519  {
520  painter->setBrush( QColor( 255, 0, 0, 100 ) );
521  painter->setPen( QColor( 255, 0, 0, 150 ) );
522  }
523  else
524  {
525  painter->setBrush( QColor( 0, 255, 0, 100 ) );
526  painter->setPen( QColor( 0, 255, 0, 150 ) );
527  }
528 
529  painter->drawRect( rect );
530  painter->restore();
531 
532  if ( label->nextPart() )
533  drawLabelPrivate( label->nextPart(), context, tmpLyr, drawType, dpiRatio );
534 
535  return;
536  }
537 
538  QgsTextRenderer::Component component;
539  component.dpiRatio = dpiRatio;
540  component.origin = outPt;
541  component.rotation = label->getAlpha();
542 
543  if ( drawType == QgsTextRenderer::Background )
544  {
545  // get rotated label's center point
546  QPointF centerPt( outPt );
547  QgsPointXY outPt2 = xform.transform( label->getX() + label->getWidth() / 2,
548  label->getY() + label->getHeight() / 2 );
549 
550  double xc = outPt2.x() - outPt.x();
551  double yc = outPt2.y() - outPt.y();
552 
553  double angle = -component.rotation;
554  double xd = xc * std::cos( angle ) - yc * std::sin( angle );
555  double yd = xc * std::sin( angle ) + yc * std::cos( angle );
556 
557  centerPt.setX( centerPt.x() + xd );
558  centerPt.setY( centerPt.y() + yd );
559 
560  component.center = centerPt;
561 
562  {
563  // label size has already been calculated using any symbology reference scale factor -- we need
564  // to temporarily remove the reference scale here or we'll be applying the scaling twice
565  QgsScopedRenderContextReferenceScaleOverride referenceScaleOverride( context, -1.0 );
566 
567  // convert label size to render units
568  double labelWidthPx = context.convertToPainterUnits( label->getWidth(), QgsUnitTypes::RenderMapUnits, QgsMapUnitScale() );
569  double labelHeightPx = context.convertToPainterUnits( label->getHeight(), QgsUnitTypes::RenderMapUnits, QgsMapUnitScale() );
570 
571  component.size = QSizeF( labelWidthPx, labelHeightPx );
572  }
573 
574  QgsTextRenderer::drawBackground( context, component, tmpLyr.format(), QgsTextDocument(), QgsTextRenderer::Label );
575  }
576 
577  else if ( drawType == QgsTextRenderer::Buffer
578  || drawType == QgsTextRenderer::Text )
579  {
580 
581  // TODO: optimize access :)
582  QgsTextLabelFeature *lf = static_cast<QgsTextLabelFeature *>( label->getFeaturePart()->feature() );
583  QString txt = lf->text( label->getPartId() );
584  QFontMetricsF *labelfm = lf->labelFontMetrics();
585 
586  if ( auto *lMaskIdProvider = context.maskIdProvider() )
587  {
588  int maskId = lMaskIdProvider->maskId( label->getFeaturePart()->layer()->provider()->layerId(),
589  label->getFeaturePart()->layer()->provider()->providerId() );
590  context.setCurrentMaskId( maskId );
591  }
592 
593  //add the direction symbol if needed
594  if ( !txt.isEmpty() && tmpLyr.placement == QgsPalLayerSettings::Line &&
595  tmpLyr.lineSettings().addDirectionSymbol() )
596  {
597  bool prependSymb = false;
598  QString symb = tmpLyr.lineSettings().rightDirectionSymbol();
599 
600  if ( label->getReversed() )
601  {
602  prependSymb = true;
603  symb = tmpLyr.lineSettings().leftDirectionSymbol();
604  }
605 
606  if ( tmpLyr.lineSettings().reverseDirectionSymbol() )
607  {
608  if ( symb == tmpLyr.lineSettings().rightDirectionSymbol() )
609  {
610  prependSymb = true;
611  symb = tmpLyr.lineSettings().leftDirectionSymbol();
612  }
613  else
614  {
615  prependSymb = false;
616  symb = tmpLyr.lineSettings().rightDirectionSymbol();
617  }
618  }
619 
620  switch ( tmpLyr.lineSettings().directionSymbolPlacement() )
621  {
623  prependSymb = true;
624  symb = symb + QStringLiteral( "\n" );
625  break;
626 
628  prependSymb = false;
629  symb = QStringLiteral( "\n" ) + symb;
630  break;
631 
633  break;
634  }
635 
636  if ( prependSymb )
637  {
638  txt.prepend( symb );
639  }
640  else
641  {
642  txt.append( symb );
643  }
644  }
645 
649  else if ( tmpLyr.multilineAlign == QgsPalLayerSettings::MultiRight )
653 
654  QgsTextRenderer::Component component;
655  component.origin = outPt;
656  component.rotation = label->getAlpha();
657 
658  QgsTextDocument document;
659  if ( !tmpLyr.format().allowHtmlFormatting() || tmpLyr.placement == QgsPalLayerSettings::Curved )
660  {
661  const QgsTextCharacterFormat c = lf->characterFormat( label->getPartId() );
662  const QStringList multiLineList = QgsPalLabeling::splitToLines( txt, tmpLyr.wrapChar, tmpLyr.autoWrapLength, tmpLyr.useMaxLineLengthForAutoWrap );
663  for ( const QString &line : multiLineList )
664  document.append( QgsTextBlock( QgsTextFragment( line, c ) ) );
665  }
666  else
667  {
668  document = lf->document();
669  }
670 
671  QgsTextRenderer::drawTextInternal( drawType, context, tmpLyr.format(), component, document, labelfm,
673 
674  }
675  if ( label->nextPart() )
676  drawLabelPrivate( label->nextPart(), context, tmpLyr, drawType, dpiRatio );
677 }
678 
680 {
681  return mSettings;
682 }
@ NeverShow
Never show unplaced labels, regardless of the engine setting.
@ Marker
Marker symbol.
virtual int nCoordinates() const
Returns the number of nodes contained in the geometry.
virtual QgsPoint vertexAt(QgsVertexId id) const =0
Returns the point corresponding to a specified vertex id.
The QgsAbstractLabelProvider class is an interface class.
QgsLabelObstacleSettings::ObstacleType mObstacleType
Type of the obstacle of feature geometries.
QString mName
Name of the layer.
virtual void stopRender(QgsRenderContext &context)
To be called after rendering is complete.
QgsMapLayer * layer() const
Returns the associated layer, or nullptr if no layer is associated with the provider.
QString mLayerId
Associated layer's ID, if applicable.
QgsPalLayerSettings::UpsideDownLabels mUpsidedownLabels
How to handle labels that would be upside down.
double mPriority
Default priority of labels.
const QgsLabelingEngine * mEngine
Associated labeling engine.
Flags mFlags
Flags altering drawing and registration of features.
virtual void startRender(QgsRenderContext &context)
To be called before rendering of labels begins.
@ DrawAllLabels
Whether all features will be labelled even though overlaps occur.
@ MergeConnectedLines
Whether adjacent lines (with the same label text) should be merged.
@ DrawLabels
Whether the labels should be rendered.
@ CentroidMustBeInside
Whether location of centroid must be inside of polygons.
QString layerId() const
Returns ID of associated layer, or empty string if no layer is associated with the provider.
QString providerId() const
Returns provider ID - useful in case there is more than one label provider within a layer (e....
QgsPalLayerSettings::Placement mPlacement
Placement strategy.
QString mProviderId
Associated provider ID (one layer may have multiple providers, e.g. in rule-based labeling)
bool valueAsBool(int key, const QgsExpressionContext &context, bool defaultValue=false, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as an boolean.
Represents the calculated placement of a map label callout line.
Contains additional contextual information about the context in which a callout is being rendered.
Definition: qgscallout.h:246
bool allFeaturePartsLabeled
true if all parts of associated feature were labeled
Definition: qgscallout.h:249
QList< QgsCalloutPosition > positions() const
Returns the list of rendered callout positions.
Definition: qgscallout.h:283
QgsCoordinateReferenceSystem originalFeatureCrs
Contains the CRS of the original feature associated with this callout.
Definition: qgscallout.h:256
void render(QgsRenderContext &context, const QRectF &rect, const double angle, const QgsGeometry &anchor, QgsCalloutContext &calloutContext)
Renders the callout onto the specified render context.
Definition: qgscallout.cpp:148
@ OrderBelowIndividualLabels
Render callouts below their individual associated labels, some callouts may be drawn over other label...
Definition: qgscallout.h:107
@ OrderBelowAllLabels
Render callouts below all labels.
Definition: qgscallout.h:106
virtual DrawOrder drawOrder() const
Returns the desired drawing order (stacking) to use while rendering this callout.
Definition: qgscallout.cpp:143
bool enabled() const
Returns true if the the callout is enabled.
Definition: qgscallout.h:319
This class represents a coordinate reference system (CRS).
bool isShortCircuited() const
Returns true if the transform short circuits because the source and destination are equivalent.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
void transformInPlace(double &x, double &y, double &z, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const SIP_THROW(QgsCsException)
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const SIP_THROW(QgsCsException)
Transforms a rectangle from the source CRS to the destination CRS.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
Single scope for storing variables and functions for use within a QgsExpressionContext.
static QgsExpressionContextScope * updateSymbolScope(const QgsSymbol *symbol, QgsExpressionContextScope *symbolScope=nullptr)
Updates a symbol scope related to a QgsSymbol to an expression context.
QgsExpressionContextScope * popScope()
Removes the last scope from the expression context and return it.
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
virtual void stopRender(QgsRenderContext &context)
Must be called when a render cycle has finished, to allow the renderer to clean up.
virtual void startRender(QgsRenderContext &context, const QgsFields &fields)
Must be called when a new render cycle is started.
Definition: qgsrenderer.cpp:96
virtual QgsSymbolList originalSymbolsForFeature(const QgsFeature &feature, QgsRenderContext &context) const
Equivalent of originalSymbolsForFeature() call extended to support renderers that may use more symbol...
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsGeometry geometry
Definition: qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:205
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:125
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsWkbTypes::GeometryType type
Definition: qgsgeometry.h:128
static std::unique_ptr< QgsAbstractGeometry > fromGeos(const GEOSGeometry *geos)
Create a geometry from a GEOSGeometry.
Definition: qgsgeos.cpp:1231
QgsCoordinateReferenceSystem originalFeatureCrs() const
Returns the original layer CRS of the feature associated with the label.
bool hasFixedPosition() const
Whether the label should use a fixed position instead of being automatically placed.
QString labelText() const
Text of the label.
GEOSGeometry * geometry() const
Gets access to the associated geometry.
bool labelAllParts() const
Returns true if all parts of the feature should be labeled.
bool reverseDirectionSymbol() const
Returns true if direction symbols should be reversed.
DirectionSymbolPlacement directionSymbolPlacement() const
Returns the placement for direction symbols.
QString leftDirectionSymbol() const
Returns the string to use for left direction arrows.
@ SymbolLeftRight
Place direction symbols on left/right of label.
@ SymbolAbove
Place direction symbols on above label.
@ SymbolBelow
Place direction symbols on below label.
QString rightDirectionSymbol() const
Returns the string to use for right direction arrows.
bool addDirectionSymbol() const
Returns true if '<' or '>' (or custom strings set via leftDirectionSymbol and rightDirectionSymbol) w...
bool mergeLines() const
Returns true if connected line features with identical label text should be merged prior to generatin...
ObstacleType type() const
Returns how features act as obstacles for labels.
bool testFlag(Flag f) const
Test whether a particular flag is enabled.
Flags flags() const
Gets flags of the labeling engine.
@ DrawUnplacedLabels
Whether to render unplaced labels as an indicator/warning for users.
@ DrawLabelRectOnly
Whether to only draw the label rect and not the actual label text (used for unit tests)
QColor unplacedLabelColor() const
Returns the color to use when rendering unplaced labels.
const QgsLabelingEngineSettings & engineSettings() const
Gets associated labeling engine settings.
const QgsMapSettings & mapSettings() const
Gets associated map settings.
QgsLabelingResults * results() const
For internal use by the providers.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
The QgsMapSettings class contains configuration for rendering of the map.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:39
void setMapRotation(double degrees, double cx, double cy)
Sets map rotation in degrees (clockwise).
QgsPointXY toMapCoordinates(int x, int y) const
Transforms device coordinates to map (world) coordinates.
QgsPointXY transform(const QgsPointXY &p) const
Transforms a point p from map (world) coordinates to device coordinates.
Definition: qgsmaptopixel.h:90
void transformInPlace(double &x, double &y) const
Transforms device coordinates to map coordinates.
Struct for storing maximum and minimum scales for measurements in map units.
A marker symbol type, for rendering Point and MultiPoint geometries.
Multi polygon geometry collection.
static QStringList splitToLines(const QString &text, const QString &wrapCharacter, int autoWrapLength=0, bool useMaxLineLengthWhenAutoWrapping=true)
Splits a text string to a list of separate lines, using a specified wrap character (wrapCharacter).
Contains settings for how a map layer will be labeled.
std::unique_ptr< QgsLabelFeature > registerFeatureWithDetails(const QgsFeature &feature, QgsRenderContext &context, QgsGeometry obstacleGeometry=QgsGeometry(), const QgsSymbol *symbol=nullptr)
Registers a feature for labeling.
void setFormat(const QgsTextFormat &format)
Sets the label text formatting settings, e.g., font settings, buffer settings, etc.
void startRender(QgsRenderContext &context)
Prepares the label settings for rendering.
bool displayAll
If true, all features will be labelled even when overlaps occur.
QString wrapChar
Wrapping character string.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the label's property collection, used for data defined overrides.
QgsCoordinateTransform ct
bool drawLabels
Whether to draw labels for this layer.
QgsCallout * callout() const
Returns the label callout renderer, responsible for drawing label callouts.
@ Curved
Arranges candidates following the curvature of a line feature. Applies to line layers only.
@ Line
Arranges candidates parallel to a generalised line representing the feature or parallel to a polygon'...
bool centroidInside
true if centroid positioned labels must be placed inside their corresponding feature polygon,...
int priority
Label priority.
@ MultiJustify
Justified.
MultiLineAlign multilineAlign
Horizontal alignment of multi-line labels.
@ CalloutDraw
Show callout.
const QgsLabelLineSettings & lineSettings() const
Returns the label line settings, which contain settings related to how the label engine places and fo...
const QgsLabelObstacleSettings & obstacleSettings() const
Returns the label obstacle settings.
bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames, const QgsFields &fields, const QgsMapSettings &mapSettings, const QgsCoordinateReferenceSystem &crs)
Prepare for registration of features.
const QgsTextFormat & format() const
Returns the label text formatting settings, e.g., font settings, buffer settings, etc.
UpsideDownLabels upsidedownLabels
Controls whether upside down labels are displayed and how they are handled.
int autoWrapLength
If non-zero, indicates that label text should be automatically wrapped to (ideally) the specified num...
Qgis::UnplacedLabelVisibility unplacedVisibility() const
Returns the layer's unplaced label visibility.
bool useMaxLineLengthForAutoWrap
If true, indicates that when auto wrapping label text the autoWrapLength length indicates the maximum...
void stopRender(QgsRenderContext &context)
Finalises the label settings after use.
A class to represent a 2D point.
Definition: qgspointxy.h:59
double y
Definition: qgspointxy.h:63
Q_GADGET double x
Definition: qgspointxy.h:62
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspointxy.h:169
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
Q_GADGET double x
Definition: qgspoint.h:52
double y
Definition: qgspoint.h:53
bool isActive(int key) const override
Returns true if the collection contains an active property with the specified key.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
const QgsMapToPixel & mapToPixel() const
Returns the context's map to pixel transform, which transforms between map coordinates and device coo...
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
void setCurrentMaskId(int id)
Stores a mask id as the "current" one.
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
const QgsRectangle & extent() const
When rendering a map layer, calling this method returns the "clipping" extent for the layer (in the l...
const QgsMaskIdProvider * maskIdProvider() const
Returns the mask id provider attached to the context.
Scoped object for temporary override of the symbologyReferenceScale property of a QgsRenderContext.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
bool enabled() const
Returns whether the background is enabled.
ShapeType type() const
Returns the type of background shape (e.g., square, ellipse, SVG).
Represents a block of text consisting of one or more QgsTextFragment objects.
Definition: qgstextblock.h:36
bool enabled() const
Returns whether the buffer is enabled.
Stores information relating to individual character formatting.
Represents a document consisting of one or more QgsTextBlock objects.
void append(const QgsTextBlock &block)
Appends a block to the document.
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
void setColor(const QColor &color)
Sets the color that text will be rendered in.
void setSize(double size)
Sets the size for rendered text.
void setFont(const QFont &font)
Sets the font used for rendering text.
QgsTextBackgroundSettings & background()
Returns a reference to the text background settings.
bool allowHtmlFormatting() const
Returns true if text should be treated as a HTML document and HTML tags should be used for formatting...
void setSizeUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the size of rendered text.
QgsTextShadowSettings & shadow()
Returns a reference to the text drop shadow settings.
QFont font() const
Returns the font used for rendering text.
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
Stores a fragment of text along with formatting overrides to be used when rendering the fragment.
Class that adds extra information to QgsLabelFeature for text labels.
const QMap< QgsPalLayerSettings::Property, QVariant > & dataDefinedValues() const
Gets data-defined values.
QgsTextDocument document() const
Returns the document for the label.
QFont definedFont()
Font to be used for rendering.
QgsTextCharacterFormat characterFormat(int partId) const
Returns the character format corresponding to the specified label part.
QString text(int partId) const
Returns the text component corresponding to a specified label part.
QFontMetricsF * labelFontMetrics()
Metrics of the font for rendering.
@ AlignTop
Align to top.
TextPart
Components of text.
@ Text
Text component.
@ Buffer
Buffer component.
@ Background
Background shape.
HAlignment
Horizontal alignment.
@ AlignLeft
Left align.
@ AlignRight
Right align.
@ AlignCenter
Center align.
@ AlignJustify
Justify align.
@ Label
Label-specific draw mode.
bool enabled() const
Returns whether the shadow is enabled.
void setShadowPlacement(QgsTextShadowSettings::ShadowPlacement placement)
Sets the placement for the drop shadow.
@ ShadowBuffer
Draw shadow under buffer.
@ ShadowShape
Draw shadow under background shape.
@ ShadowLowest
Draw shadow below all text components.
@ ShadowText
Draw shadow under text.
QgsTextShadowSettings::ShadowPlacement shadowPlacement() const
Returns the placement for the drop shadow.
@ RenderPixels
Pixels.
Definition: qgsunittypes.h:171
@ RenderMapUnits
Map units.
Definition: qgsunittypes.h:170
void stopRender(QgsRenderContext &context) override
To be called after rendering is complete.
QgsCoordinateReferenceSystem mCrs
Layer's CRS.
void drawLabelPrivate(pal::LabelPosition *label, QgsRenderContext &context, QgsPalLayerSettings &tmpLyr, QgsTextRenderer::TextPart drawType, double dpiRatio=1.0) const
Internal label drawing method.
virtual bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames)
Prepare for registration of features.
void drawUnplacedLabel(QgsRenderContext &context, pal::LabelPosition *label) const override
Draw an unplaced label.
QList< QgsLabelFeature * > mLabels
List of generated.
QgsVectorLayerLabelProvider(QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings, const QString &layerName=QString())
Convenience constructor to initialize the provider from given vector layer.
void startRender(QgsRenderContext &context) override
To be called before rendering of labels begins.
const QgsPalLayerSettings & settings() const
Returns the layer's settings.
static QgsGeometry getPointObstacleGeometry(QgsFeature &fet, QgsRenderContext &context, const QgsSymbolList &symbols)
Returns the geometry for a point feature which should be used as an obstacle for labels.
QgsWkbTypes::GeometryType mLayerGeometryType
Geometry type of layer.
QgsPalLayerSettings mSettings
Layer's labeling configuration.
std::unique_ptr< QgsAbstractFeatureSource > mSource
Layer's feature source.
QList< QgsLabelFeature * > labelFeatures(QgsRenderContext &context) override
Returns list of label features (they are owned by the provider and thus deleted on its destruction)
virtual QList< QgsLabelFeature * > registerFeature(const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry=QgsGeometry(), const QgsSymbol *symbol=nullptr)
Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels.
void init()
initialization method - called from constructors
void drawLabel(QgsRenderContext &context, pal::LabelPosition *label) const override
Draw this label at the position determined by the labeling engine.
void drawLabelBackground(QgsRenderContext &context, pal::LabelPosition *label) const override
Draw the background for the specified label.
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
QgsFeatureId featureId() const
Returns the unique ID of the feature.
Definition: feature.cpp:161
Layer * layer()
Returns the layer that feature belongs to.
Definition: feature.cpp:156
QgsLabelFeature * feature()
Returns the parent feature.
Definition: feature.h:94
LabelPosition is a candidate feature label position.
Definition: labelposition.h:56
double getAlpha() const
Returns the angle to rotate text (in rad).
double getHeight() const
bool getReversed() const
Quadrant getQuadrant() const
bool conflictsWithObstacle() const
Returns whether the position is marked as conflicting with an obstacle feature.
double getWidth() const
LabelPosition * nextPart() const
Returns the next part of this label position (i.e.
FeaturePart * getFeaturePart() const
Returns the feature corresponding to this labelposition.
int getPartId() const
double getX(int i=0) const
Returns the down-left x coordinate.
double getY(int i=0) const
Returns the down-left y coordinate.
QgsAbstractLabelProvider * provider() const
Returns pointer to the associated provider.
Definition: layer.h:166
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
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 QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QList< QgsSymbol * > QgsSymbolList
Definition: qgsrenderer.h:44
const QgsCoordinateReferenceSystem & crs
Utility class for identifying a unique vertex within a geometry.
Definition: qgsvertexid.h:31