QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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 "feature.h"
21#include "labelposition.h"
22#include "pal/layer.h"
24#include "qgsgeometry.h"
25#include "qgslabelingresults.h"
26#include "qgslabelsearchtree.h"
27#include "qgslinestring.h"
28#include "qgslogger.h"
29#include "qgsmarkersymbol.h"
30#include "qgsmaskidprovider.h"
31#include "qgsmeshlayer.h"
32#include "qgsmultipolygon.h"
33#include "qgspallabeling.h"
34#include "qgspolygon.h"
35#include "qgsrenderer.h"
36#include "qgssymbol.h"
38#include "qgstextfragment.h"
39#include "qgstextlabelfeature.h"
40#include "qgstextrenderer.h"
41#include "qgstriangularmesh.h"
42#include "qgsvectorlayer.h"
43
44#include <QPicture>
45#include <QTextDocument>
46#include <QTextFragment>
47
48using namespace pal;
49
54 , mCrs( layer->crs() )
55{
56 mName = layerName.isEmpty() ? layer->id() : layerName;
57
58 init();
59}
60
62{
63 mPlacement = mSettings.placement;
64
65 mFlags = Flags();
66 if ( mSettings.drawLabels )
68 if ( mSettings.lineSettings().mergeLines() && !mSettings.lineSettings().addDirectionSymbol() )
70 if ( mSettings.centroidInside )
72
73 mPriority = 1 - mSettings.priority / 10.0; // convert 0..10 --> 1..0
74
75 mVectorLabelProvider = std::make_unique<QgsVectorLayerLabelProvider>(
77 QgsFields(),
78 mCrs,
79 QString(),
80 &mSettings,
81 mLayer );
82
83 if ( mLabelFaces )
84 {
85 //override obstacle type to treat any intersection of a label with the point symbol as a high cost conflict
87 }
88 else
89 {
90 mObstacleType = mSettings.obstacleSettings().type();
91 }
92
93 mUpsidedownLabels = mSettings.upsidedownLabels;
94}
95
96
101
102bool QgsMeshLayerLabelProvider::prepare( QgsRenderContext &context, QSet<QString> &attributeNames )
103{
104 const QgsMapSettings &mapSettings = mEngine->mapSettings();
105
106 mVectorLabelProvider->setEngine( mEngine );
107
108 return mSettings.prepare( context, attributeNames, QgsFields(), mapSettings, mCrs );
109}
110
112{
114 mSettings.startRender( context );
115}
116
118{
120 mSettings.stopRender( context );
121}
122
124{
125 Q_UNUSED( ctx );
126 return mLabels;
127}
128
129QList< QgsLabelFeature * > QgsMeshLayerLabelProvider::registerFeature( const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry, const QgsSymbol *symbol )
130{
131 std::unique_ptr< QgsLabelFeature > label = mSettings.registerFeatureWithDetails( feature, context, obstacleGeometry, symbol );
132 QList< QgsLabelFeature * > res;
133 if ( label )
134 {
135 res << label.get();
136 mLabels << label.release();
137 }
138 return res;
139}
140
145
147{
148 mVectorLabelProvider->drawLabel( context, label );
149}
@ Point
Points.
Definition qgis.h:359
@ Polygon
Polygons.
Definition qgis.h:361
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. 0 = highest priority, 1 = lowest priority.
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.
QString providerId() const
Returns provider ID - useful in case there is more than one label provider within a layer (e....
QgsAbstractLabelProvider(QgsMapLayer *layer, const QString &providerId=QString())
Construct the provider with default values.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Container of fields for a vector layer.
Definition qgsfields.h:46
A geometry is the spatial representation of a feature.
@ PolygonWhole
Avoid placing labels over ANY part of polygon. Where PolygonInterior will prefer to place labels with...
Contains configuration for rendering maps.
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.
bool labelFaces() const
Returns false if labeling mesh vertices, true if labeling mesh faces.
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.
Contains settings for how a map layer will be labeled.
Contains information about the context of a rendering operation.
Abstract base class for all rendered symbols.
Definition qgssymbol.h:231
LabelPosition is a candidate feature label position.