QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgslabelingenginerule_impl.h
Go to the documentation of this file.
1/***************************************************************************
2 qgslabelingenginerule_impl.h
3 ---------------------
4 Date : August 2024
5 Copyright : (C) 2024 by Nyall Dawson
6 Email : nyall dot dawson 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#ifndef QGSLABELINGENGINERULEIMPL_H
16#define QGSLABELINGENGINERULEIMPL_H
17
18#include "qgis.h"
19#include "qgis_core.h"
20#include "qgis_sip.h"
22#include "qgsmapunitscale.h"
23#include "qgsvectorlayerref.h"
24
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
29class QgsSpatialIndex;
30
31
39{
40 public:
41
44 bool prepare( QgsRenderContext &context ) override;
45 void writeXml( QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context ) const override;
46 void readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
47 void resolveReferences( const QgsProject *project ) override;
48 bool candidateIsIllegal( const pal::LabelPosition *candidate, QgsLabelingEngineContext &context ) const override SIP_SKIP;
49 void alterCandidateCost( pal::LabelPosition *candidate, QgsLabelingEngineContext &context ) const override SIP_SKIP;
50 bool isAvailable() const override;
51
58
64 void setLabeledLayer( QgsMapLayer *layer );
65
72
78 void setTargetLayer( QgsVectorLayer *layer );
79
87 double distance() const { return mDistance; }
88
96 void setDistance( double distance ) { mDistance = distance; }
97
105 Qgis::RenderUnit distanceUnit() const { return mDistanceUnit; }
106
114 void setDistanceUnit( Qgis::RenderUnit unit ) { mDistanceUnit = unit; }
115
123 const QgsMapUnitScale &distanceUnitScale() const { return mDistanceUnitScale; }
124
132 void setDistanceUnitScale( const QgsMapUnitScale &scale ) { mDistanceUnitScale = scale; }
133
142 double cost() const { return mCost; }
143
152 void setCost( double cost ) { mCost = cost; }
153
154 protected:
155
156 void copyCommonProperties( QgsAbstractLabelingEngineRule *other ) const override;
157
159 bool mMustBeDistant = true;
160
161 private:
162#ifdef SIP_RUN
164#endif
165
166 void initialize( QgsLabelingEngineContext &context );
167
169 bool candidateExceedsTolerance( const pal::LabelPosition *candidate, QgsLabelingEngineContext &context ) const;
170
172 QgsMapLayerRef mLabeledLayer;
174 QgsVectorLayerRef mTargetLayer;
176 double mDistance = 5;
180 QgsMapUnitScale mDistanceUnitScale;
182 double mCost = 10;
183
184 // cached variables
185 double mDistanceMapUnits = 0;
186 std::unique_ptr< QgsAbstractFeatureSource > mTargetLayerSource;
187 std::unique_ptr< QgsSpatialIndex > mIndex;
188 bool mInitialized = false;
189};
190
191
199{
200 public:
201
205 QString id() const override;
206 QString displayType() const override;
207 QString description() const override;
208#ifdef SIP_RUN
209 SIP_PYOBJECT __repr__();
210 % MethodCode
211 const QString str = u"<QgsLabelingEngineRuleMinimumDistanceLabelToFeature: %1>"_s.arg( sipCpp->name() );
212 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
213 % End
214#endif
215
216 private:
217#ifdef SIP_RUN
219#endif
220};
221
222
230{
231 public:
235 QString id() const override;
236 QString displayType() const override;
237 QString description() const override;
238
239#ifdef SIP_RUN
240 SIP_PYOBJECT __repr__();
241 % MethodCode
242 const QString str = u"<QgsLabelingEngineRuleMaximumDistanceLabelToFeature: %1>"_s.arg( sipCpp->name() );
243 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
244 % End
245#endif
246
247 private:
248#ifdef SIP_RUN
250#endif
251
252};
253
261{
262 public:
265
267 QString id() const override;
268 QString displayType() const override;
269 QString description() const override;
270 bool isAvailable() const override;
271 void writeXml( QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context ) const override;
272 void readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
273 void resolveReferences( const QgsProject *project ) override;
274 bool prepare( QgsRenderContext &context ) override;
275 QgsRectangle modifyCandidateConflictSearchBoundingBox( const QgsRectangle &candidateBounds ) const override SIP_SKIP;
276 bool candidatesAreConflicting( const pal::LabelPosition *lp1, const pal::LabelPosition *lp2 ) const override SIP_SKIP;
277
278#ifdef SIP_RUN
279 SIP_PYOBJECT __repr__();
280 % MethodCode
281 const QString str = u"<QgsLabelingEngineRuleMinimumDistanceLabelToLabel: %1>"_s.arg( sipCpp->name() );
282 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
283 % End
284#endif
285
291 QgsMapLayer *labeledLayer() const;
292
298 void setLabeledLayer( QgsMapLayer *layer );
299
305 QgsMapLayer *targetLayer() const;
306
312 void setTargetLayer( QgsMapLayer *layer );
313
321 double distance() const { return mDistance; }
322
330 void setDistance( double distance ) { mDistance = distance; }
331
339 Qgis::RenderUnit distanceUnit() const { return mDistanceUnit; }
340
348 void setDistanceUnit( Qgis::RenderUnit unit ) { mDistanceUnit = unit; }
349
357 const QgsMapUnitScale &distanceUnitScale() const { return mDistanceUnitScale; }
358
366 void setDistanceUnitScale( const QgsMapUnitScale &scale ) { mDistanceUnitScale = scale; }
367
368 private:
369#ifdef SIP_RUN
371#endif
372
373 QgsMapLayerRef mLabeledLayer;
374 QgsMapLayerRef mTargetLayer;
375 double mDistance = 5;
377 QgsMapUnitScale mDistanceUnitScale;
378
379 // cached variables
380 double mDistanceMapUnits = 0;
381};
382
383
391{
392 public:
393
397 QString id() const override;
398 QString displayType() const override;
399 QString description() const override;
400 bool prepare( QgsRenderContext &context ) override;
401 void writeXml( QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context ) const override;
402 void readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
403 void resolveReferences( const QgsProject *project ) override;
404 bool candidateIsIllegal( const pal::LabelPosition *candidate, QgsLabelingEngineContext &context ) const override SIP_SKIP;
405
406#ifdef SIP_RUN
407 SIP_PYOBJECT __repr__();
408 % MethodCode
409 const QString str = u"<QgsLabelingEngineRuleAvoidLabelOverlapWithFeature: %1>"_s.arg( sipCpp->name() );
410 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
411 % End
412#endif
413
419 QgsMapLayer *labeledLayer() const;
420
426 void setLabeledLayer( QgsMapLayer *layer );
427
434
440 void setTargetLayer( QgsVectorLayer *layer );
441
442 private:
443#ifdef SIP_RUN
445#endif
446 void initialize( QgsLabelingEngineContext &context );
447
448 QgsMapLayerRef mLabeledLayer;
449 QgsVectorLayerRef mTargetLayer;
450
451 // cached variables
452 std::unique_ptr< QgsAbstractFeatureSource > mTargetLayerSource;
453 std::unique_ptr< QgsSpatialIndex > mIndex;
454 bool mInitialized = false;
455};
456
457
458#endif // QGSLABELINGENGINERULEIMPL_H
RenderUnit
Rendering size units.
Definition qgis.h:5255
@ Millimeters
Millimeters.
Definition qgis.h:5256
void setTargetLayer(QgsVectorLayer *layer)
Sets the layer providing the features which labels must be distant from (or close to).
Qgis::RenderUnit distanceUnit() const
Returns the units for the distance between labels and the features from the targetLayer().
const QgsMapUnitScale & distanceUnitScale() const
Returns the scaling for the distance between labels and the features from the targetLayer().
void setDistanceUnitScale(const QgsMapUnitScale &scale)
Sets the scale for the distance between labels and the features from the targetLayer().
double distance() const
Returns the acceptable distance threshold between labels and the features from the targetLayer().
void setCost(double cost)
Sets the penalty cost incurred when the rule is violated.
QgsVectorLayer * targetLayer() const
Returns the layer providing the features which labels must be distant from (or close to).
void setDistanceUnit(Qgis::RenderUnit unit)
Sets the unit for the distance between labels and the features from the targetLayer().
void setLabeledLayer(QgsMapLayer *layer)
Sets the layer providing the labels.
double cost() const
Returns the penalty cost incurred when the rule is violated.
void setDistance(double distance)
Sets the acceptable distance threshold between labels and the features from the targetLayer().
QgsMapLayer * labeledLayer() const
Returns the layer providing the labels.
bool mMustBeDistant
true if labels must be distant from features, false if they must be close
Abstract base class for labeling engine rules.
virtual bool prepare(QgsRenderContext &context)=0
Prepares the rule.
virtual void alterCandidateCost(pal::LabelPosition *candidate, QgsLabelingEngineContext &context) const
Provides an opportunity for the rule to alter the cost for a candidate.
virtual void resolveReferences(const QgsProject *project)
Resolves reference to layers from stored layer ID.
virtual bool isAvailable() const
Returns true if the rule is available for use within the current QGIS environment.
virtual void readXml(const QDomElement &element, const QgsReadWriteContext &context)=0
Reads the rule properties from an XML element.
virtual void writeXml(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context) const =0
Writes the rule properties to an XML element.
virtual QgsAbstractLabelingEngineRule * clone() const =0
Creates a clone of this rule.
virtual bool candidateIsIllegal(const pal::LabelPosition *candidate, QgsLabelingEngineContext &context) const
Returns true if a labeling candidate violates the rule and should be eliminated.
virtual void copyCommonProperties(QgsAbstractLabelingEngineRule *other) const
Copies common properties from this object to an other.
Encapsulates the context for a labeling engine run.
void readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the rule properties from an XML element.
void writeXml(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context) const override
Writes the rule properties to an XML element.
QString displayType() const override
Returns a user-friendly, translated string representing the rule type.
bool prepare(QgsRenderContext &context) override
Prepares the rule.
void setTargetLayer(QgsVectorLayer *layer)
Sets the layer providing the features which labels must not overlap.
QString description() const override
Returns a user-friendly description of the rule.
void setLabeledLayer(QgsMapLayer *layer)
Sets the layer providing the labels.
QgsMapLayer * labeledLayer() const
Returns the layer providing the labels.
void resolveReferences(const QgsProject *project) override
Resolves reference to layers from stored layer ID.
bool candidateIsIllegal(const pal::LabelPosition *candidate, QgsLabelingEngineContext &context) const override
Returns true if a labeling candidate violates the rule and should be eliminated.
QgsVectorLayer * targetLayer() const
Returns the layer providing the features which labels must not overlap.
QString description() const override
Returns a user-friendly description of the rule.
QString displayType() const override
Returns a user-friendly, translated string representing the rule type.
QString description() const override
Returns a user-friendly description of the rule.
QString displayType() const override
Returns a user-friendly, translated string representing the rule type.
A labeling engine rule which prevents labels being placed too close to labels from a different layer.
double distance() const
Returns the minimum permitted distance between labels from the labeledLayer() and the labels from the...
const QgsMapUnitScale & distanceUnitScale() const
Returns the scaling for the distance between labels from the labeledLayer() and the labels from the t...
void setDistanceUnit(Qgis::RenderUnit unit)
Sets the unit for the distance between labels from the labeledLayer() and the labels from the targetL...
QgsMapLayer * labeledLayer() const
Returns the layer providing the labels.
QgsRectangle modifyCandidateConflictSearchBoundingBox(const QgsRectangle &candidateBounds) const override
Returns a (possibly expanded) bounding box to use when searching for conflicts for a candidate.
void setLabeledLayer(QgsMapLayer *layer)
Sets the layer providing the labels.
void writeXml(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context) const override
Writes the rule properties to an XML element.
void resolveReferences(const QgsProject *project) override
Resolves reference to layers from stored layer ID.
QgsMapLayer * targetLayer() const
Returns the layer providing the labels which labels must be distant from.
void readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the rule properties from an XML element.
void setDistance(double distance)
Sets the minimum permitted distance between labels from the labeledLayer() and the labels from the ta...
QString displayType() const override
Returns a user-friendly, translated string representing the rule type.
QString description() const override
Returns a user-friendly description of the rule.
void setTargetLayer(QgsMapLayer *layer)
Sets the layer providing the labels which labels must be distant from.
Qgis::RenderUnit distanceUnit() const
Returns the units for the distance between labels from the labeledLayer() and the labels from the tar...
bool prepare(QgsRenderContext &context) override
Prepares the rule.
bool isAvailable() const override
Returns true if the rule is available for use within the current QGIS environment.
bool candidatesAreConflicting(const pal::LabelPosition *lp1, const pal::LabelPosition *lp2) const override
Returns true if a labeling candidate lp1 conflicts with lp2 after applying the rule.
void setDistanceUnitScale(const QgsMapUnitScale &scale)
Sets the scale for the distance between labels from the labeledLayer() and the labels from the target...
Base class for all map layer types.
Definition qgsmaplayer.h:83
Struct for storing maximum and minimum scales for measurements in map units.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:112
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
Contains information about the context of a rendering operation.
A spatial index for QgsFeature objects.
Represents a vector layer which manages a vector based dataset.
LabelPosition is a candidate feature label position.
#define SIP_SKIP
Definition qgis_sip.h:134
#define SIP_FACTORY
Definition qgis_sip.h:84
_LayerRef< QgsMapLayer > QgsMapLayerRef
_LayerRef< QgsVectorLayer > QgsVectorLayerRef