QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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:
43 bool prepare( QgsRenderContext &context ) override;
44 void writeXml( QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context ) const override;
45 void readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
46 void resolveReferences( const QgsProject *project ) override;
47 bool candidateIsIllegal( const pal::LabelPosition *candidate, QgsLabelingEngineContext &context ) const override SIP_SKIP;
48 void alterCandidateCost( pal::LabelPosition *candidate, QgsLabelingEngineContext &context ) const override SIP_SKIP;
49 bool isAvailable() const override;
50
57
63 void setLabeledLayer( QgsMapLayer *layer );
64
71
77 void setTargetLayer( QgsVectorLayer *layer );
78
86 double distance() const { return mDistance; }
87
95 void setDistance( double distance ) { mDistance = distance; }
96
104 Qgis::RenderUnit distanceUnit() const { return mDistanceUnit; }
105
113 void setDistanceUnit( Qgis::RenderUnit unit ) { mDistanceUnit = unit; }
114
122 const QgsMapUnitScale &distanceUnitScale() const { return mDistanceUnitScale; }
123
131 void setDistanceUnitScale( const QgsMapUnitScale &scale ) { mDistanceUnitScale = scale; }
132
141 double cost() const { return mCost; }
142
151 void setCost( double cost ) { mCost = cost; }
152
153 protected:
154 void copyCommonProperties( QgsAbstractLabelingEngineRule *other ) const override;
155
157 bool mMustBeDistant = true;
158
159 private:
160#ifdef SIP_RUN
162#endif
163
164 void initialize( QgsLabelingEngineContext &context );
165
167 bool candidateExceedsTolerance( const pal::LabelPosition *candidate, QgsLabelingEngineContext &context ) const;
168
170 QgsMapLayerRef mLabeledLayer;
172 QgsVectorLayerRef mTargetLayer;
174 double mDistance = 5;
178 QgsMapUnitScale mDistanceUnitScale;
180 double mCost = 10;
181
182 // cached variables
183 double mDistanceMapUnits = 0;
184 std::unique_ptr< QgsAbstractFeatureSource > mTargetLayerSource;
185 std::unique_ptr< QgsSpatialIndex > mIndex;
186 bool mInitialized = false;
187};
188
189
197{
198 public:
202 QString id() const override;
203 QString displayType() const override;
204 QString description() const override;
205#ifdef SIP_RUN
206 // clang-format off
207 SIP_PYOBJECT __repr__();
208 % MethodCode
209 const QString str = u"<QgsLabelingEngineRuleMinimumDistanceLabelToFeature: %1>"_s.arg( sipCpp->name() );
210 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
211 % End
212// clang-format on
213#endif
214
215 // clang-format off
216 private:
217 // clang-format on
218#ifdef SIP_RUN
220#endif
221};
222
223
231{
232 public:
236 QString id() const override;
237 QString displayType() const override;
238 QString description() const override;
239
240#ifdef SIP_RUN
241 // clang-format off
242 SIP_PYOBJECT __repr__();
243 % MethodCode
244 const QString str = u"<QgsLabelingEngineRuleMaximumDistanceLabelToFeature: %1>"_s.arg( sipCpp->name() );
245 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
246 % End
247// clang-format on
248#endif
249
250 // clang-format off
251 private:
252 // clang-format on
253#ifdef SIP_RUN
255#endif
256};
257
265{
266 public:
269
271 QString id() const override;
272 QString displayType() const override;
273 QString description() const override;
274 bool isAvailable() const override;
275 void writeXml( QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context ) const override;
276 void readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
277 void resolveReferences( const QgsProject *project ) override;
278 bool prepare( QgsRenderContext &context ) override;
279 QgsRectangle modifyCandidateConflictSearchBoundingBox( const QgsRectangle &candidateBounds ) const override SIP_SKIP;
280 bool candidatesAreConflicting( const pal::LabelPosition *lp1, const pal::LabelPosition *lp2 ) const override SIP_SKIP;
281
282#ifdef SIP_RUN
283 // clang-format off
284 SIP_PYOBJECT __repr__();
285 % MethodCode
286 const QString str = u"<QgsLabelingEngineRuleMinimumDistanceLabelToLabel: %1>"_s.arg( sipCpp->name() );
287 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
288 % End
289// clang-format on
290#endif
291
297 QgsMapLayer *labeledLayer() const;
298
304 void setLabeledLayer( QgsMapLayer *layer );
305
311 QgsMapLayer *targetLayer() const;
312
318 void setTargetLayer( QgsMapLayer *layer );
319
327 double distance() const { return mDistance; }
328
336 void setDistance( double distance ) { mDistance = distance; }
337
345 Qgis::RenderUnit distanceUnit() const { return mDistanceUnit; }
346
354 void setDistanceUnit( Qgis::RenderUnit unit ) { mDistanceUnit = unit; }
355
363 const QgsMapUnitScale &distanceUnitScale() const { return mDistanceUnitScale; }
364
372 void setDistanceUnitScale( const QgsMapUnitScale &scale ) { mDistanceUnitScale = scale; }
373
374 private:
375#ifdef SIP_RUN
377#endif
378
379 QgsMapLayerRef mLabeledLayer;
380 QgsMapLayerRef mTargetLayer;
381 double mDistance = 5;
383 QgsMapUnitScale mDistanceUnitScale;
384
385 // cached variables
386 double mDistanceMapUnits = 0;
387};
388
389
397{
398 public:
402 QString id() const override;
403 QString displayType() const override;
404 QString description() const override;
405 bool prepare( QgsRenderContext &context ) override;
406 void writeXml( QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context ) const override;
407 void readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
408 void resolveReferences( const QgsProject *project ) override;
409 bool candidateIsIllegal( const pal::LabelPosition *candidate, QgsLabelingEngineContext &context ) const override SIP_SKIP;
410
411#ifdef SIP_RUN
412 // clang-format off
413 SIP_PYOBJECT __repr__();
414 % MethodCode
415 const QString str = u"<QgsLabelingEngineRuleAvoidLabelOverlapWithFeature: %1>"_s.arg( sipCpp->name() );
416 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
417 % End
418// clang-format on
419#endif
420
426 QgsMapLayer *labeledLayer() const;
427
433 void setLabeledLayer( QgsMapLayer *layer );
434
441
447 void setTargetLayer( QgsVectorLayer *layer );
448
449 private:
450#ifdef SIP_RUN
452#endif
453 void initialize( QgsLabelingEngineContext &context );
454
455 QgsMapLayerRef mLabeledLayer;
456 QgsVectorLayerRef mTargetLayer;
457
458 // cached variables
459 std::unique_ptr< QgsAbstractFeatureSource > mTargetLayerSource;
460 std::unique_ptr< QgsSpatialIndex > mIndex;
461 bool mInitialized = false;
462};
463
464
465#endif // QGSLABELINGENGINERULEIMPL_H
RenderUnit
Rendering size units.
Definition qgis.h:5340
@ Millimeters
Millimeters.
Definition qgis.h:5341
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:113
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:133
#define SIP_FACTORY
Definition qgis_sip.h:83
_LayerRef< QgsMapLayer > QgsMapLayerRef
_LayerRef< QgsVectorLayer > QgsVectorLayerRef