QGIS API Documentation 3.41.0-Master (cea29feecf2)
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_core.h"
19#include "qgis_sip.h"
20#include "qgis.h"
22#include "qgsvectorlayerref.h"
23#include "qgsmapunitscale.h"
24
25class QgsSpatialIndex;
26
27
35{
36 public:
37
40 bool prepare( QgsRenderContext &context ) override;
41 void writeXml( QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context ) const override;
42 void readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
43 void resolveReferences( const QgsProject *project ) override;
44 bool candidateIsIllegal( const pal::LabelPosition *candidate, QgsLabelingEngineContext &context ) const override SIP_SKIP;
45 void alterCandidateCost( pal::LabelPosition *candidate, QgsLabelingEngineContext &context ) const override SIP_SKIP;
46 bool isAvailable() const override;
47
53 QgsMapLayer *labeledLayer() const;
54
60 void setLabeledLayer( QgsMapLayer *layer );
61
67 QgsVectorLayer *targetLayer() const;
68
74 void setTargetLayer( QgsVectorLayer *layer );
75
83 double distance() const { return mDistance; }
84
92 void setDistance( double distance ) { mDistance = distance; }
93
101 Qgis::RenderUnit distanceUnit() const { return mDistanceUnit; }
102
110 void setDistanceUnit( Qgis::RenderUnit unit ) { mDistanceUnit = unit; }
111
119 const QgsMapUnitScale &distanceUnitScale() const { return mDistanceUnitScale; }
120
128 void setDistanceUnitScale( const QgsMapUnitScale &scale ) { mDistanceUnitScale = scale; }
129
138 double cost() const { return mCost; }
139
148 void setCost( double cost ) { mCost = cost; }
149
150 protected:
151
152 void copyCommonProperties( QgsAbstractLabelingEngineRule *other ) const override;
153
155 bool mMustBeDistant = true;
156
157 private:
158#ifdef SIP_RUN
160#endif
161
162 void initialize( QgsLabelingEngineContext &context );
163
165 bool candidateExceedsTolerance( const pal::LabelPosition *candidate, QgsLabelingEngineContext &context ) const;
166
168 QgsMapLayerRef mLabeledLayer;
170 QgsVectorLayerRef mTargetLayer;
172 double mDistance = 5;
176 QgsMapUnitScale mDistanceUnitScale;
178 double mCost = 10;
179
180 // cached variables
181 double mDistanceMapUnits = 0;
182 std::unique_ptr< QgsAbstractFeatureSource > mTargetLayerSource;
183 std::unique_ptr< QgsSpatialIndex > mIndex;
184 bool mInitialized = false;
185};
186
187
195{
196 public:
197
201 QString id() const override;
202 QString displayType() const override;
203 QString description() const override;
204#ifdef SIP_RUN
205 SIP_PYOBJECT __repr__();
206 % MethodCode
207 const QString str = QStringLiteral( "<QgsLabelingEngineRuleMinimumDistanceLabelToFeature: %1>" ).arg( sipCpp->name() );
208 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
209 % End
210#endif
211
212 private:
213#ifdef SIP_RUN
215#endif
216};
217
218
226{
227 public:
231 QString id() const override;
232 QString displayType() const override;
233 QString description() const override;
234
235#ifdef SIP_RUN
236 SIP_PYOBJECT __repr__();
237 % MethodCode
238 const QString str = QStringLiteral( "<QgsLabelingEngineRuleMaximumDistanceLabelToFeature: %1>" ).arg( sipCpp->name() );
239 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
240 % End
241#endif
242
243 private:
244#ifdef SIP_RUN
246#endif
247
248};
249
257{
258 public:
261
263 QString id() const override;
264 QString displayType() const override;
265 QString description() const override;
266 bool isAvailable() const override;
267 void writeXml( QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context ) const override;
268 void readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
269 void resolveReferences( const QgsProject *project ) override;
270 bool prepare( QgsRenderContext &context ) override;
271 QgsRectangle modifyCandidateConflictSearchBoundingBox( const QgsRectangle &candidateBounds ) const override SIP_SKIP;
272 bool candidatesAreConflicting( const pal::LabelPosition *lp1, const pal::LabelPosition *lp2 ) const override SIP_SKIP;
273
274#ifdef SIP_RUN
275 SIP_PYOBJECT __repr__();
276 % MethodCode
277 const QString str = QStringLiteral( "<QgsLabelingEngineRuleMinimumDistanceLabelToLabel: %1>" ).arg( sipCpp->name() );
278 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
279 % End
280#endif
281
287 QgsMapLayer *labeledLayer() const;
288
294 void setLabeledLayer( QgsMapLayer *layer );
295
301 QgsMapLayer *targetLayer() const;
302
308 void setTargetLayer( QgsMapLayer *layer );
309
317 double distance() const { return mDistance; }
318
326 void setDistance( double distance ) { mDistance = distance; }
327
335 Qgis::RenderUnit distanceUnit() const { return mDistanceUnit; }
336
344 void setDistanceUnit( Qgis::RenderUnit unit ) { mDistanceUnit = unit; }
345
353 const QgsMapUnitScale &distanceUnitScale() const { return mDistanceUnitScale; }
354
362 void setDistanceUnitScale( const QgsMapUnitScale &scale ) { mDistanceUnitScale = scale; }
363
364 private:
365#ifdef SIP_RUN
367#endif
368
369 QgsMapLayerRef mLabeledLayer;
370 QgsMapLayerRef mTargetLayer;
371 double mDistance = 5;
373 QgsMapUnitScale mDistanceUnitScale;
374
375 // cached variables
376 double mDistanceMapUnits = 0;
377};
378
379
387{
388 public:
389
393 QString id() const override;
394 QString displayType() const override;
395 QString description() const override;
396 bool prepare( QgsRenderContext &context ) override;
397 void writeXml( QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context ) const override;
398 void readXml( const QDomElement &element, const QgsReadWriteContext &context ) override;
399 void resolveReferences( const QgsProject *project ) override;
400 bool candidateIsIllegal( const pal::LabelPosition *candidate, QgsLabelingEngineContext &context ) const override SIP_SKIP;
401
402#ifdef SIP_RUN
403 SIP_PYOBJECT __repr__();
404 % MethodCode
405 const QString str = QStringLiteral( "<QgsLabelingEngineRuleAvoidLabelOverlapWithFeature: %1>" ).arg( sipCpp->name() );
406 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
407 % End
408#endif
409
415 QgsMapLayer *labeledLayer() const;
416
422 void setLabeledLayer( QgsMapLayer *layer );
423
429 QgsVectorLayer *targetLayer() const;
430
436 void setTargetLayer( QgsVectorLayer *layer );
437
438 private:
439#ifdef SIP_RUN
441#endif
442 void initialize( QgsLabelingEngineContext &context );
443
444 QgsMapLayerRef mLabeledLayer;
445 QgsVectorLayerRef mTargetLayer;
446
447 // cached variables
448 std::unique_ptr< QgsAbstractFeatureSource > mTargetLayerSource;
449 std::unique_ptr< QgsSpatialIndex > mIndex;
450 bool mInitialized = false;
451};
452
453
454#endif // QGSLABELINGENGINERULEIMPL_H
RenderUnit
Rendering size units.
Definition qgis.h:4892
@ Millimeters
Millimeters.
Base class for labeling engine rules which prevents labels being placed too close or to far from feat...
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.
void setDistanceUnit(Qgis::RenderUnit unit)
Sets the unit for the distance between labels and the features from the targetLayer().
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().
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.
A labeling engine rule which prevents labels being placed overlapping features from a different layer...
A labeling engine rule which prevents labels being placed too far from features from a different laye...
A labeling engine rule which prevents labels being placed too close to features from a different laye...
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...
void setDistance(double distance)
Sets the minimum permitted distance between labels from the labeledLayer() and the labels from the ta...
Qgis::RenderUnit distanceUnit() const
Returns the units for the distance between labels from the labeledLayer() and the labels from the tar...
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:76
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:107
The class is used as a container of context for various read/write operations on other 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 data sets.
LabelPosition is a candidate feature label position.
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_FACTORY
Definition qgis_sip.h:76