31 static bool _palIsCanceled(
void *ctx )
33 return ( reinterpret_cast< QgsRenderContext * >( ctx ) )->renderingStopped();
46 : mMapSettings( mapSettings )
58 QStringList layerIds = mMapSettings.
layerIds();
61 if ( layer1Pos != layer2Pos && layer1Pos >= 0 && layer2Pos >= 0 )
62 return layer1Pos > layer2Pos;
65 return lf1->
size().width() * lf1->
size().height() > lf2->
size().width() * lf2->
size().height();
88 mResults->setMapSettings( mapSettings );
93 QSet< QgsMapLayer * > layers;
96 if ( provider->
layer() )
97 layers << provider->
layer();
101 if ( provider->
layer() )
102 layers << provider->
layer();
104 return layers.toList();
124 QgsAbstractLabelProvider::Flags flags = provider->
flags();
161 Q_ASSERT(
"unsupported upside-down label setting" &&
false );
167 QList<QgsLabelFeature *> features = provider->
labelFeatures( context );
175 catch ( std::exception &e )
178 QgsDebugMsgLevel( QStringLiteral(
"Ignoring feature %1 due PAL exception:" ).arg( feature->
id() ) + QString::fromLatin1( e.what() ), 4 );
220 int candPoint, candLine, candPolygon;
232 bool appendedLayerScope =
false;
235 appendedLayerScope =
true;
239 if ( appendedLayerScope )
246 QPainter *painter = context.
painter();
250 visiblePoly.append( visiblePoly.at( 0 ) );
270 std::unique_ptr< pal::Problem > problem;
275 catch ( std::exception &e )
278 QgsDebugMsgLevel(
"PAL EXCEPTION :-( " + QString::fromLatin1( e.what() ), 4 );
304 painter->setBrush( Qt::NoBrush );
305 for (
int i = 0; i < problem->getNumFeatures(); i++ )
307 for (
int j = 0; j < problem->getFeatureCandidateCount( i ); j++ )
319 QgsDebugMsgLevel( QStringLiteral(
"LABELING work: %1 ms ... labels# %2" ).arg( t.elapsed() ).arg( labels.size() ), 4 );
326 painter->setRenderHint( QPainter::Antialiasing );
347 painter->setCompositionMode( QPainter::CompositionMode_SourceOver );
349 QgsDebugMsgLevel( QStringLiteral(
"LABELING draw: %1 ms" ).arg( t.elapsed() ), 4 );
362 return mLayer ? mLayer->provider() :
nullptr;
367 : mLayerId( layer ? layer->id() : QString() )
369 , mProviderId( providerId )
370 , mFlags( DrawLabels )
385 QStringList predefinedOrderString;
391 predefinedOrderString << QStringLiteral(
"TL" );
394 predefinedOrderString << QStringLiteral(
"TSL" );
397 predefinedOrderString << QStringLiteral(
"T" );
400 predefinedOrderString << QStringLiteral(
"TSR" );
403 predefinedOrderString << QStringLiteral(
"TR" );
406 predefinedOrderString << QStringLiteral(
"L" );
409 predefinedOrderString << QStringLiteral(
"R" );
412 predefinedOrderString << QStringLiteral(
"BL" );
415 predefinedOrderString << QStringLiteral(
"BSL" );
418 predefinedOrderString << QStringLiteral(
"B" );
421 predefinedOrderString << QStringLiteral(
"BSR" );
424 predefinedOrderString << QStringLiteral(
"BR" );
428 return predefinedOrderString.join( QStringLiteral(
"," ) );
433 QVector<QgsPalLayerSettings::PredefinedPointPosition> result;
434 QStringList predefinedOrderList = positionString.split(
',' );
435 Q_FOREACH (
const QString &position, predefinedOrderList )
437 QString cleaned = position.trimmed().toUpper();
438 if ( cleaned == QLatin1String(
"TL" ) )
440 else if ( cleaned == QLatin1String(
"TSL" ) )
442 else if ( cleaned == QLatin1String(
"T" ) )
444 else if ( cleaned == QLatin1String(
"TSR" ) )
446 else if ( cleaned == QLatin1String(
"TR" ) )
448 else if ( cleaned == QLatin1String(
"L" ) )
450 else if ( cleaned == QLatin1String(
"R" ) )
452 else if ( cleaned == QLatin1String(
"BL" ) )
454 else if ( cleaned == QLatin1String(
"BSL" ) )
456 else if ( cleaned == QLatin1String(
"B" ) )
458 else if ( cleaned == QLatin1String(
"BSR" ) )
460 else if ( cleaned == QLatin1String(
"BR" ) )
470 parts << QStringLiteral(
"OL" );
472 parts << QStringLiteral(
"AL" );
474 parts << QStringLiteral(
"BL" );
476 parts << QStringLiteral(
"LO" );
477 return parts.join(
',' );
482 pal::LineArrangementFlags
flags =
nullptr;
483 const QStringList flagList =
string.split(
',' );
484 bool foundLineOrientationFlag =
false;
485 for (
const QString &flag : flagList )
487 QString cleaned = flag.trimmed().toUpper();
488 if ( cleaned == QLatin1String(
"OL" ) )
490 else if ( cleaned == QLatin1String(
"AL" ) )
492 else if ( cleaned == QLatin1String(
"BL" ) )
494 else if ( cleaned == QLatin1String(
"LO" ) )
495 foundLineOrientationFlag =
true;
497 if ( !foundLineOrientationFlag )
Label below point, slightly right of center.
void processProvider(QgsAbstractLabelProvider *provider, QgsRenderContext &context, pal::Pal &p)
Layer * addLayer(QgsAbstractLabelProvider *provider, const QString &layerName, QgsPalLayerSettings::Placement arrangement, double defaultPriority, bool active, bool toLabel, bool displayAll=false)
add a new layer
QString name() const
Name of the layer (for statistics, debugging etc.) - does not need to be unique.
A rectangle specified with double values.
Base class for all map layer types.
Label on bottom-left of point.
const QgsLabelingEngineSettings & labelingEngineSettings() const
Returns the global configuration of the labeling engine.
void setMapRotation(double degrees, double cx, double cy)
Set map rotation in degrees (clockwise)
void addProvider(QgsAbstractLabelProvider *provider)
Add provider of label features. Takes ownership of the provider.
QList< QgsAbstractLabelProvider * > mProviders
List of providers (the are owned by the labeling engine)
void removeProvider(QgsAbstractLabelProvider *provider)
Remove provider if the provider's initialization failed. Provider instance is deleted.
QgsLabelFeature * feature()
Returns the parent feature.
Label on top-left of point.
QgsAbstractLabelProvider(QgsMapLayer *layer, const QString &providerId=QString())
Construct the provider with default values.
static void drawLabelCandidateRect(pal::LabelPosition *lp, QPainter *painter, const QgsMapToPixel *xform, QList< QgsLabelCandidate > *candidates=nullptr)
QgsAbstractLabelProvider * provider() const
Returns provider of this instance.
A set of features which influence the labeling process.
PredefinedPointPosition
Positions for labels when using the QgsPalLabeling::OrderedPositionsAroundPoint placement mode...
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
OperationResult rotate(double rotation, const QgsPointXY ¢er)
Rotate this geometry around the Z axis.
bool operator()(pal::LabelPosition *lp1, pal::LabelPosition *lp2) const
Whether to use also label candidates that are partially outside of the map view.
QgsLabelingEngine()
Construct the labeling engine with default settings.
QList< QgsAbstractLabelProvider * > mSubProviders
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
Is slower and best than TABU, worse and faster than TABU_CHAIN.
Label on top of point, slightly right of center.
void setMapSettings(const QgsMapSettings &mapSettings)
Associate map settings instance.
A geometry is the spatial representation of a feature.
void setUpsidedownLabels(UpsideDownLabels ud)
Sets how upside down labels will be handled within the layer.
const QgsMapToPixel & mapToPixel() const
Search searchMethod() const
Which search method to use for removal collisions between labels.
void setShowPartial(bool show)
Set flag show partial label.
Whether to label each part of multi-part features separately.
double rotation() const
Returns the rotation of the resulting map image, in degrees clockwise.
FeaturePart * getFeaturePart()
Returns the feature corresponding to this labelposition.
virtual QList< QgsLabelFeature * > labelFeatures(QgsRenderContext &context)=0
Returns list of label features (they are owned by the provider and thus deleted on its destruction) ...
QgsPalLayerSettings::UpsideDownLabels upsidedownLabels() const
How to handle labels that would be upside down.
void setObstacleType(QgsPalLayerSettings::ObstacleType obstacleType)
Sets the obstacle type, which controls how features within the layer act as obstacles for labels...
void setCentroidInside(bool forceInside)
Sets whether labels placed at the centroid of features within the layer are forced to be placed insid...
Is a little bit better than CHAIN but slower.
Whether adjacent lines (with the same label text) should be merged.
std::unique_ptr< Problem > extractProblem(const QgsRectangle &extent, const QgsGeometry &mapBoundary)
Extracts the labeling problem for the specified map extent - only features within this extent will be...
The QgsMapSettings class contains configuration for rendering of the map.
QSizeF size() const
Size of the label (in map units)
Perform transforms between map coordinates and device coordinates.
Whether to draw all labels even if there would be collisions.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
static pal::LineArrangementFlags decodeLinePlacementFlags(const QString &string)
Decodes a string to set of line placement flags.
QList< QgsMapLayer * > participatingLayers() const
Returns a list of layers with providers in the engine.
QgsPalLayerSettings::Placement placement() const
What placement strategy to use for the labels.
QgsMapLayer * layer() const
Returns the associated layer, or nullptr if no layer is associated with the provider.
#define QgsDebugMsgLevel(str, level)
bool renderingStopped() const
Returns TRUE if the rendering operation has been stopped and any ongoing rendering should be canceled...
bool testFlag(Flag f) const
Test whether a particular flag is enabled.
Show upside down for all labels, including dynamic ones.
QStringList layerIds() const
Gets list of layer IDs for map rendering The layers are stored in the reverse order of how they are r...
QgsPointXY center() const
Returns the center point of the rectangle.
void setPointP(int point_p)
set # candidates to generate for points features Higher the value is, longer Pal::labeller will spend...
Whether location of centroid must be inside of polygons.
double priority() const
Default priority of labels (may be overridden by individual labels)
void setLineP(int line_p)
set maximum # candidates to generate for lines features Higher the value is, longer Pal::labeller wil...
Upside-down labels (90 <= angle < 270) are shown upright.
Flags flags() const
Flags associated with the provider.
Whether all features will be labelled even though overlaps occur.
The QgsAbstractLabelProvider class is an interface class.
Whether to draw rectangles of generated candidates (good for debugging)
static QString encodeLinePlacementFlags(pal::LineArrangementFlags flags)
Encodes line placement flags to a string.
QString layerId() const
Returns ID of associated layer, or empty string if no layer is associated with the provider...
QgsLabelSorter(const QgsMapSettings &mapSettings)
const QgsMapSettings & mapSettings() const
Gets associated map settings.
QgsExpressionContext & expressionContext()
Gets the expression context.
double zIndex() const
Returns the label's z-index.
~QgsLabelingEngine()
Clean up everything (especially the registered providers)
bool registerFeature(QgsLabelFeature *label)
Register a feature in the layer.
static QString encodePredefinedPositionOrder(const QVector< QgsPalLayerSettings::PredefinedPointPosition > &positions)
Encodes an ordered list of predefined point label positions to a string.
void run(QgsRenderContext &context)
compute the labeling with given map settings and providers
virtual QList< QgsAbstractLabelProvider * > subProviders()
Returns list of child providers - useful if the provider needs to put labels into more layers with di...
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
The QgsLabelFeature class describes a feature that should be used within the labeling engine...
std::unique_ptr< QgsLabelingResults > mResults
Resulting labeling layout.
void setPolyP(int poly_p)
set maximum # candidates to generate for polygon features Higher the value is, longer Pal::labeller w...
QgsMapSettings mMapSettings
Associated map settings instance.
void registerCancelationCallback(FnIsCanceled fnCanceled, void *context)
Register a function that returns whether this job has been canceled - PAL calls it during the computa...
QgsPalLayerSettings::ObstacleType obstacleType() const
How the feature geometries will work as obstacles.
QgsLabelingResults * takeResults()
Returns pointer to recently computed results and pass the ownership of results to the caller...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
Stores global configuration for labeling engine.
QList< LabelPosition * > solveProblem(Problem *prob, bool displayAll)
void setLabelMode(LabelMode mode)
Sets the layer's labeling mode.
Label below point, slightly left of center.
Helper class for sorting labels into correct draw order.
static QgsGeometry fromQPolygonF(const QPolygonF &polygon)
Construct geometry from a QPolygonF.
LabelPosition is a candidate feature label position.
Label on top of point, slightly left of center.
void numCandidatePositions(int &candPoint, int &candLine, int &candPolygon) const
Gets number of candidate positions that will be generated for each label feature (default to 8) ...
Whether the labels should be rendered.
Class that stores computed placement from labeling engine.
QPolygonF visiblePolygon() const
Returns the visible area as a polygon (may be rotated)
static QVector< QgsPalLayerSettings::PredefinedPointPosition > decodePredefinedPositionOrder(const QString &positionString)
Decodes a string to an ordered list of predefined point label positions.
SearchMethod
Search method to use.
QgsExpressionContextScope * popScope()
Removes the last scope from the expression context and return it.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
Show upside down when rotation is layer- or data-defined.
virtual void drawLabel(QgsRenderContext &context, pal::LabelPosition *label) const =0
draw this label at the position determined by the labeling engine
Is the worst but fastest method.
void setSearch(SearchMethod method)
Select the search method to use.
void setEngine(const QgsLabelingEngine *engine)
Associate provider with a labeling engine (should be only called internally from QgsLabelingEngine) ...
void setMergeConnectedLines(bool merge)
Sets whether connected lines should be merged before labeling.
QgsFeatureId id() const
Identifier of the label (unique within the parent label provider)