QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsmeshlayerlabelprovider.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmeshlayerlabelprovider.cpp
3 ---------------------
4 begin : November 2023
5 copyright : (C) 2023 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
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
19
20#include "qgsgeometry.h"
21#include "qgslabelsearchtree.h"
22#include "qgspallabeling.h"
23#include "qgstextlabelfeature.h"
24#include "qgsmeshlayer.h"
25#include "qgsvectorlayer.h"
26#include "qgsrenderer.h"
27#include "qgspolygon.h"
28#include "qgslinestring.h"
29#include "qgsmultipolygon.h"
30#include "qgslogger.h"
32#include "qgsmaskidprovider.h"
34#include "qgstextfragment.h"
35#include "qgslabelingresults.h"
36#include "qgstextrenderer.h"
37#include "qgstriangularmesh.h"
38
39#include "feature.h"
40#include "labelposition.h"
41#include "qgssymbol.h"
42#include "qgsmarkersymbol.h"
43
44#include "pal/layer.h"
45
46#include <QPicture>
47#include <QTextDocument>
48#include <QTextFragment>
49
50using namespace pal;
51
52QgsMeshLayerLabelProvider::QgsMeshLayerLabelProvider( QgsMeshLayer *layer, const QString &providerId, const QgsPalLayerSettings *settings, const QString &layerName, bool labelFaces )
53 : QgsAbstractLabelProvider( layer, providerId )
54 , mSettings( settings ? * settings : QgsPalLayerSettings() )
55 , mLabelFaces( labelFaces )
56 , mCrs( layer->crs() )
57{
58 mName = layerName.isEmpty() ? layer->id() : layerName;
59
60 init();
61}
62
64{
66
67 mFlags = Flags();
74
75 mPriority = 1 - mSettings.priority / 10.0; // convert 0..10 --> 1..0
76
77 mVectorLabelProvider = std::make_unique<QgsVectorLayerLabelProvider>(
79 QgsFields(),
80 mCrs,
81 QString(),
82 &mSettings,
83 mLayer );
84
85 if ( mLabelFaces )
86 {
87 //override obstacle type to treat any intersection of a label with the point symbol as a high cost conflict
89 }
90 else
91 {
93 }
94
96}
97
98
100{
101 qDeleteAll( mLabels );
102}
103
104bool QgsMeshLayerLabelProvider::prepare( QgsRenderContext &context, QSet<QString> &attributeNames )
105{
106 const QgsMapSettings &mapSettings = mEngine->mapSettings();
107
108 mVectorLabelProvider->setEngine( mEngine );
109
110 return mSettings.prepare( context, attributeNames, QgsFields(), mapSettings, mCrs );
111}
112
114{
116 mSettings.startRender( context );
117}
118
120{
122 mSettings.stopRender( context );
123}
124
126{
127 Q_UNUSED( ctx );
128 return mLabels;
129}
130
131QList< QgsLabelFeature * > QgsMeshLayerLabelProvider::registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry, const QgsSymbol *symbol )
132{
133 std::unique_ptr< QgsLabelFeature > label = mSettings.registerFeatureWithDetails( feature, context, obstacleGeometry, symbol );
134 QList< QgsLabelFeature * > res;
135 if ( label )
136 {
137 res << label.get();
138 mLabels << label.release();
139 }
140 return res;
141}
142
144{
145 return mSettings;
146}
147
149{
150 mVectorLabelProvider->drawLabel( context, label );
151}
@ Polygon
Polygons.
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.
double mPriority
Default priority of labels.
const QgsLabelingEngine * mEngine
Associated labeling engine.
QgsMapLayer * layer() const
Returns the associated layer, or nullptr if no layer is associated with the provider.
Flags mFlags
Flags altering drawing and registration of features.
virtual void startRender(QgsRenderContext &context)
To be called before rendering of labels begins.
Qgis::LabelPlacement mPlacement
Placement strategy.
@ 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.
QgsWeakMapLayerPointer mLayer
Weak pointer to source layer.
Qgis::UpsideDownLabelHandling mUpsidedownLabels
How to handle labels that would be upside down.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
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:162
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.
@ PolygonWhole
Avoid placing labels over ANY part of polygon. Where PolygonInterior will prefer to place labels with...
const QgsMapSettings & mapSettings() const
Gets associated map settings.
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.
const QgsPalLayerSettings & settings() const
Returns the layer's settings.
QgsCoordinateReferenceSystem mCrs
Layer's CRS.
QgsMeshLayerLabelProvider(QgsMeshLayer *layer, const QString &providerId, const QgsPalLayerSettings *settings, const QString &layerName=QString(), bool labelFaces=false)
Convenience constructor to initialize the provider from given mesh layer.
void stopRender(QgsRenderContext &context) override
To be called after rendering is complete.
void init()
initialization method - called from constructors
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.
QList< QgsLabelFeature * > mLabels
List of generated.
QgsPalLayerSettings mSettings
Layer's labeling configuration.
void startRender(QgsRenderContext &context) override
To be called before rendering of labels begins.
QList< QgsLabelFeature * > labelFeatures(QgsRenderContext &context) override
Returns list of label features (they are owned by the provider and thus deleted on its destruction)
void drawLabel(QgsRenderContext &context, pal::LabelPosition *label) const override
Draw this label at the position determined by the labeling engine.
virtual bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames)
Prepare for registration of features.
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:101
Contains settings for how a map layer will be labeled.
const QgsLabelObstacleSettings & obstacleSettings() const
Returns the label obstacle settings.
std::unique_ptr< QgsLabelFeature > registerFeatureWithDetails(const QgsFeature &feature, QgsRenderContext &context, QgsGeometry obstacleGeometry=QgsGeometry(), const QgsSymbol *symbol=nullptr)
Registers a feature for labeling.
void startRender(QgsRenderContext &context)
Prepares the label settings for rendering.
Qgis::LabelPlacement placement
Label placement mode.
bool drawLabels
Whether to draw labels for this layer.
bool centroidInside
true if centroid positioned labels must be placed inside their corresponding feature polygon,...
int priority
Label priority.
bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames, const QgsFields &fields, const QgsMapSettings &mapSettings, const QgsCoordinateReferenceSystem &crs)
Prepare for registration of features.
const QgsLabelLineSettings & lineSettings() const
Returns the label line settings, which contain settings related to how the label engine places and fo...
Qgis::UpsideDownLabelHandling upsidedownLabels
Controls whether upside down labels are displayed and how they are handled.
void stopRender(QgsRenderContext &context)
Finalises the label settings after use.
Contains information about the context of a rendering operation.
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:94
LabelPosition is a candidate feature label position.
Definition: labelposition.h:56
const QgsCoordinateReferenceSystem & crs