QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
44 return std::get< 1 >(
mRules->rootRule()->registerFeature( feature, context,
mSubProviders, obstacleGeometry, symbol ) );
49 QList<QgsAbstractLabelProvider *> lst;
59 : mSettings( settings )
60 , mMaximumScale( scaleMinDenom )
61 , mMinimumScale( scaleMaxDenom )
62 , mFilterExp( filterExp )
63 , mDescription( description )
64 , mElseRule( elseRule )
67 mFilterExp = QStringLiteral(
"ELSE" );
74 qDeleteAll( mChildren );
89 for (
Rule *
c : mChildren )
92 l +=
c->descendants();
97 void QgsRuleBasedLabeling::Rule::initFilter()
99 if ( mFilterExp.trimmed().compare( QLatin1String(
"ELSE" ), Qt::CaseInsensitive ) == 0 )
104 else if ( mFilterExp.trimmed().isEmpty() )
112 mFilter = std::make_unique< QgsExpression >( mFilterExp );
116 void QgsRuleBasedLabeling::Rule::updateElseRules()
119 for ( Rule *rule : std::as_const( mChildren ) )
121 if ( rule->isElse() )
128 if ( mSettings && mSettings->format().containsAdvancedEffects() )
131 for (
Rule *rule : std::as_const( mChildren ) )
133 if ( rule->requiresAdvancedEffects() )
153 if ( !mChildren.empty() )
155 for (
const Rule *rule : mChildren )
157 if ( !rule->accept( visitor ) )
170 for (
const Rule *rule : std::as_const( mChildren ) )
172 if ( rule->settings() )
173 list << rule->ruleKey();
175 rule->subProviderIds( list );
182 mChildren.append( rule );
183 rule->mParent =
this;
189 mChildren.insert( i, rule );
190 rule->mParent =
this;
196 delete mChildren.at( i );
197 mChildren.removeAt( i );
205 if ( key == mRuleKey )
208 for (
Rule *rule : mChildren )
219 if ( key == mRuleKey )
222 for (
Rule *rule : std::as_const( mChildren ) )
234 Rule *newrule =
new Rule( s, mMaximumScale, mMinimumScale, mFilterExp, mDescription );
237 for (
Rule *rule : mChildren )
245 QDomElement settingsElem = ruleElem.firstChildElement( QStringLiteral(
"settings" ) );
246 if ( !settingsElem.isNull() )
252 QString filterExp = ruleElem.attribute( QStringLiteral(
"filter" ) );
253 QString description = ruleElem.attribute( QStringLiteral(
"description" ) );
254 int scaleMinDenom = ruleElem.attribute( QStringLiteral(
"scalemindenom" ), QStringLiteral(
"0" ) ).toInt();
255 int scaleMaxDenom = ruleElem.attribute( QStringLiteral(
"scalemaxdenom" ), QStringLiteral(
"0" ) ).toInt();
256 QString ruleKey = ruleElem.attribute( QStringLiteral(
"key" ) );
257 Rule *rule =
new Rule(
settings, scaleMinDenom, scaleMaxDenom, filterExp, description );
259 if ( !ruleKey.isEmpty() )
260 rule->mRuleKey = ruleKey;
262 rule->
setActive( ruleElem.attribute( QStringLiteral(
"active" ), QStringLiteral(
"1" ) ).toInt() );
264 QDomElement childRuleElem = ruleElem.firstChildElement( QStringLiteral(
"rule" ) );
265 while ( !childRuleElem.isNull() )
267 Rule *childRule =
create( childRuleElem, context );
276 childRuleElem = childRuleElem.nextSiblingElement( QStringLiteral(
"rule" ) );
284 QDomElement ruleElem = doc.createElement( QStringLiteral(
"rule" ) );
288 ruleElem.appendChild( mSettings->writeXml( doc, context ) );
290 if ( !mFilterExp.isEmpty() )
291 ruleElem.setAttribute( QStringLiteral(
"filter" ), mFilterExp );
293 ruleElem.setAttribute( QStringLiteral(
"scalemindenom" ), mMaximumScale );
295 ruleElem.setAttribute( QStringLiteral(
"scalemaxdenom" ), mMinimumScale );
296 if ( !mDescription.isEmpty() )
297 ruleElem.setAttribute( QStringLiteral(
"description" ), mDescription );
299 ruleElem.setAttribute( QStringLiteral(
"active" ), 0 );
300 ruleElem.setAttribute( QStringLiteral(
"key" ), mRuleKey );
302 for ( RuleList::const_iterator it = mChildren.constBegin(); it != mChildren.constEnd(); ++it )
321 for (
Rule *rule : std::as_const( mChildren ) )
332 if ( !p->
prepare( context, attributeNames ) )
341 attributeNames.unite( mFilter->referencedColumns() );
346 for (
Rule *rule : std::as_const( mChildren ) )
354 QList< QgsLabelFeature * > labels;
355 if ( !isFilterOK( feature, context )
358 return { Filtered, labels };
361 bool registered =
false;
370 bool matchedAChild =
false;
373 for (
Rule *rule : std::as_const( mChildren ) )
376 if ( !rule->isElse() )
379 QList< QgsLabelFeature * > added;
380 std::tie( res, added ) = rule->registerFeature( feature, context,
subProviders, obstacleGeometry );
381 labels.append( added );
383 matchedAChild |= ( res == Registered || res == Inactive );
384 registered |= matchedAChild;
389 if ( !matchedAChild )
391 for (
Rule *rule : std::as_const( mElseRules ) )
394 QList< QgsLabelFeature * > added;
395 std::tie( res, added ) = rule->registerFeature( feature, context,
subProviders, obstacleGeometry, symbol ) ;
396 matchedAChild |= ( res == Registered || res == Inactive );
397 registered |= res != Filtered;
398 labels.append( added );
402 if ( !mIsActive || ( matchedAChild && !registered ) )
403 return { Inactive, labels };
404 else if ( registered )
405 return { Registered, labels };
407 return { Filtered, labels };
412 if ( ! mFilter || mElseRule )
420 bool QgsRuleBasedLabeling::Rule::isScaleOK(
double scale )
const
426 if ( !
qgsDoubleNear( mMaximumScale, 0.0 ) && mMaximumScale > scale )
428 if ( !
qgsDoubleNear( mMinimumScale, 0.0 ) && mMinimumScale < scale )
449 Q_ASSERT( origDescendants.count() == clonedDescendants.count() );
450 for (
int i = 0; i < origDescendants.count(); ++i )
451 clonedDescendants[i]->setRuleKey( origDescendants[i]->ruleKey() );
473 QDomElement rulesElem = element.firstChildElement( QStringLiteral(
"rules" ) );
485 return QStringLiteral(
"rule-based" );
490 QDomElement elem = doc.createElement( QStringLiteral(
"labeling" ) );
491 elem.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"rule-based" ) );
493 QDomElement rulesElem =
mRootRule->save( doc, context );
494 rulesElem.setTagName( QStringLiteral(
"rules" ) );
495 elem.appendChild( rulesElem );
528 return mRootRule->requiresAdvancedEffects();
549 for (
Rule *rule : rules )
555 QDomDocument doc = parent.ownerDocument();
557 QDomElement ruleElement = doc.createElement( QStringLiteral(
"se:Rule" ) );
558 parent.appendChild( ruleElement );
560 if ( !rule->filterExpression().isEmpty() )
567 QVariantMap localProps = QVariantMap( props );
std::tuple< RegisterResult, QList< QgsLabelFeature * > > registerFeature(const QgsFeature &feature, QgsRenderContext &context, RuleToProviderMap &subProviders, const QgsGeometry &obstacleGeometry=QgsGeometry(), const QgsSymbol *symbol=nullptr)
Register individual features.
bool scaleVisibility
Set to true to limit label visibility to a range of scales.
void subProviderIds(QStringList &list) const
append rule keys of descendants that contain valid settings (i.e.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
void setRuleKey(const QString &key)
Override the assigned rule key (should be used just internally by rule-based labeling)
QgsRuleBasedLabeling::Rule * rootRule()
A child rule for QgsRuleBasedLabeling.
QgsExpressionContext & expressionContext()
Gets the expression context.
The class is used as a container of context for various read/write operations on other objects.
Contains settings for how a map layer will be labeled.
void setSettings(QgsPalLayerSettings *settings)
Sets new settings (or nullptr). Deletes old settings if any.
std::unique_ptr< QgsRuleBasedLabeling > mRules
owned copy
virtual QgsVectorLayerLabelProvider * createProvider(QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings)
create a label provider
void setActive(bool state)
Sets if this rule is active.
QStringList subProviders() const override
Gets list of sub-providers within the layer's labeling.
static void mergeScaleDependencies(double mScaleMinDenom, double mScaleMaxDenom, QVariantMap &props)
Merges the local scale limits, if any, with the ones already in the map, if any.
virtual void writeTextSymbolizer(QDomNode &parent, QgsPalLayerSettings &settings, const QVariantMap &props) const
Writes a TextSymbolizer element contents based on the provided labeling settings.
const QgsRuleBasedLabeling::Rule * findRuleByKey(const QString &key) const
Try to find a rule given its unique key.
Contains information about the context of a rendering operation.
QgsRuleBasedLabeling * clone() const override
Returns a new copy of the object.
An interface for classes which can visit style entity (e.g. symbol) nodes (using the visitor pattern)...
virtual bool visitExit(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor stops visiting a node.
double minimumScale
The minimum map scale (i.e.
Abstract base class for all rendered symbols.
bool requiresAdvancedEffects() const override
Returns true if drawing labels requires advanced effects like composition modes, which could prevent ...
Rule(QgsPalLayerSettings *settings, double maximumScale=0, double minimumScale=0, const QString &filterExp=QString(), const QString &description=QString(), bool elseRule=false)
takes ownership of settings, settings may be nullptr
QgsRuleBasedLabelProvider(const QgsRuleBasedLabeling &rules, QgsVectorLayer *layer, bool withFeatureLoop=true)
void createSubProviders(QgsVectorLayer *layer, RuleToProviderMap &subProviders, QgsRuleBasedLabelProvider *provider)
add providers
double maximumScale
The maximum map scale (i.e.
bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all child rules associated with the rule...
void toSld(QDomNode &parent, const QVariantMap &props) const override
Writes the SE 1.1 TextSymbolizer element based on the current layer labeling settings.
The QgsVectorLayerLabelProvider class implements a label provider for vector layers....
Rule based labeling for a vector layer.
Contains information relating to the style entity currently being visited.
void setSettings(QgsPalLayerSettings *settings, const QString &providerId=QString()) override
Set pal settings for a specific provider (takes ownership).
void removeChildAt(int i)
delete child rule
~QgsRuleBasedLabeling() override
const QgsPalLayerSettings & settings() const
Returns the layer's settings.
QMap< QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider * > RuleToProviderMap
QList< QgsAbstractLabelProvider * > subProviders() override
Returns subproviders.
QgsVectorLayerLabelProvider * provider(QgsVectorLayer *layer) const override
QgsRuleBasedLabeling(QgsRuleBasedLabeling::Rule *root)
Constructs the labeling from given tree of rules (takes ownership)
QgsPalLayerSettings * settings() const
Returns the labeling settings.
std::unique_ptr< Rule > mRootRule
QgsRuleBasedLabeling::RuleToProviderMap mSubProviders
label providers are owned by labeling engine
QString type() const override
Unique type string of the labeling configuration implementation.
static void applyScaleDependency(QDomDocument &doc, QDomElement &ruleElem, QVariantMap &props)
Checks if the properties contain scaleMinDenom and scaleMaxDenom, if available, they are added into t...
bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames) override
Prepare for registration of features.
void prepare(QgsRenderContext &context, QSet< QString > &attributeNames, RuleToProviderMap &subProviders)
call prepare() on sub-providers and populate attributeNames
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const
store labeling info to XML element
void insertChild(int i, QgsRuleBasedLabeling::Rule *rule)
add child rule, take ownership, sets this as parent
bool drawLabels
Whether to draw labels for this layer.
void registerFeature(const QgsFeature &f, QgsRenderContext &context)
Registers a feature for labeling.
QList< QgsRuleBasedLabeling::Rule * > RuleList
@ SymbolRule
Rule based symbology or label child rule.
bool requiresAdvancedEffects() const
Returns true if this rule or any of its children requires advanced composition effects to render.
Contains information relating to a node (i.e.
const QgsLabelingEngine * mEngine
Associated labeling engine.
RegisterResult
The result of registering a rule.
QString providerId() const
Returns provider ID - useful in case there is more than one label provider within a layer (e....
A label settings entity for QgsStyle databases.
QList< QgsLabelFeature * > registerFeature(const QgsFeature &feature, QgsRenderContext &context, const QgsGeometry &obstacleGeometry=QgsGeometry(), const QgsSymbol *symbol=nullptr) override
Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels.
virtual bool visitEnter(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor starts visiting a node.
QgsRuleBasedLabeling::RuleList descendants() const
Returns all children, grand-children, grand-grand-children, grand-gra...
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
A geometry is the spatial representation of a feature.
QgsRuleBasedLabeling::Rule * clone() const
clone this rule, return new instance
Represents a vector layer which manages a vector based data sets.
void appendChild(QgsRuleBasedLabeling::Rule *rule)
add child rule, take ownership, sets this as parent
QgsMapLayer * layer() const
Returns the associated layer, or nullptr if no layer is associated with the provider.
static QgsRuleBasedLabeling::Rule * create(const QDomElement &ruleElem, const QgsReadWriteContext &context)
Create a rule from an XML definition.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
virtual bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames)
Prepare for registration of features.
QgsVectorLayerLabelProvider(QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings, const QString &layerName=QString())
Convenience constructor to initialize the provider from given vector layer.
double rendererScale() const
Returns the renderer map scale.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the labeling...
QgsPalLayerSettings settings(const QString &providerId=QString()) const override
Gets associated label settings.
static QgsRuleBasedLabeling * create(const QDomElement &element, const QgsReadWriteContext &context)
Create the instance from a DOM element with saved configuration.
Label provider for rule based labeling.
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const override
Returns labeling configuration as XML element.
static bool createFunctionElement(QDomDocument &doc, QDomElement &element, const QString &function)
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.