24using namespace Qt::StringLiterals;
40 for (
const auto &subprovider : std::as_const(
mSubProviders ) )
42 subprovider.second->setEngine(
mEngine );
53 return std::get< 1 >(
mRules->rootRule()->registerFeature( feature, context,
mSubProviders, obstacleGeometry, symbol ) );
58 QList<QgsAbstractLabelProvider *> lst;
59 for (
const auto &subprovider : std::as_const(
mSubProviders ) )
61 lst << subprovider.second;
71 , mMaximumScale( scaleMinDenom )
72 , mMinimumScale( scaleMaxDenom )
73 , mFilterExp( filterExp )
75 , mElseRule( elseRule )
78 mFilterExp = u
"ELSE"_s;
85 qDeleteAll( mChildren );
100 for (
Rule *
c : mChildren )
103 l +=
c->descendants();
108void QgsRuleBasedLabeling::Rule::initFilter()
110 if ( mFilterExp.trimmed().compare(
"ELSE"_L1, Qt::CaseInsensitive ) == 0 )
115 else if ( mFilterExp.trimmed().isEmpty() )
123 mFilter = std::make_unique< QgsExpression >( mFilterExp );
127void QgsRuleBasedLabeling::Rule::updateElseRules()
130 for (
Rule *rule : std::as_const( mChildren ) )
132 if ( rule->isElse() )
139 if ( mSettings && mSettings->format().containsAdvancedEffects() )
142 for (
Rule *rule : std::as_const( mChildren ) )
144 if ( rule->requiresAdvancedEffects() )
158 || mSettings->format().hasNonDefaultCompositionMode() ) )
161 for (
Rule *rule : std::as_const( mChildren ) )
163 if ( rule->hasNonDefaultCompositionMode() )
183 if ( !mChildren.empty() )
185 for (
const Rule *rule : mChildren )
187 if ( !rule->accept( visitor ) )
200 for (
const Rule *rule : std::as_const( mChildren ) )
202 if ( rule->settings() )
203 list << rule->ruleKey();
205 rule->subProviderIds( list );
212 mChildren.append( rule );
213 rule->mParent =
this;
219 mChildren.insert( i, rule );
220 rule->mParent =
this;
226 delete mChildren.at( i );
227 mChildren.removeAt( i );
235 if ( key == mRuleKey )
238 for (
Rule *rule : mChildren )
240 const Rule *r = rule->findRuleByKey( key );
249 if ( key == mRuleKey )
252 for (
Rule *rule : std::as_const( mChildren ) )
254 Rule *r = rule->findRuleByKey( key );
264 Rule *newrule =
new Rule( s, mMaximumScale, mMinimumScale, mFilterExp, mDescription );
270 for (
Rule *rule : mChildren )
271 newrule->
appendChild( rule->clone( resetRuleKey ) );
279 QDomElement settingsElem = ruleElem.firstChildElement( u
"settings"_s );
280 if ( !settingsElem.isNull() )
283 settings->readXml( settingsElem, context );
286 QString filterExp = ruleElem.attribute( u
"filter"_s );
287 QString
description = ruleElem.attribute( u
"description"_s );
288 int scaleMinDenom = ruleElem.attribute( u
"scalemindenom"_s, u
"0"_s ).toInt();
289 int scaleMaxDenom = ruleElem.attribute( u
"scalemaxdenom"_s, u
"0"_s ).toInt();
292 ruleKey = ruleElem.attribute( u
"key"_s );
294 ruleKey = QUuid::createUuid().toString();
300 rule->
setActive( ruleElem.attribute( u
"active"_s, u
"1"_s ).toInt() );
302 QDomElement childRuleElem = ruleElem.firstChildElement( u
"rule"_s );
303 while ( !childRuleElem.isNull() )
305 Rule *childRule =
create( childRuleElem, context );
314 childRuleElem = childRuleElem.nextSiblingElement( u
"rule"_s );
322 QDomElement ruleElem = doc.createElement( u
"rule"_s );
326 ruleElem.appendChild( mSettings->writeXml( doc, context ) );
328 if ( !mFilterExp.isEmpty() )
329 ruleElem.setAttribute( u
"filter"_s, mFilterExp );
331 ruleElem.setAttribute( u
"scalemindenom"_s, mMaximumScale );
333 ruleElem.setAttribute( u
"scalemaxdenom"_s, mMinimumScale );
334 if ( !mDescription.isEmpty() )
335 ruleElem.setAttribute( u
"description"_s, mDescription );
337 ruleElem.setAttribute( u
"active"_s, 0 );
338 ruleElem.setAttribute( u
"key"_s, mRuleKey );
340 for ( RuleList::const_iterator it = mChildren.constBegin(); it != mChildren.constEnd(); ++it )
343 ruleElem.appendChild( rule->
save( doc, context ) );
355 [
this](
const std::pair<QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider *> &item )
357 return item.first == this;
370 for (
Rule *rule : std::as_const( mChildren ) )
381 [
this](
const std::pair<QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider *> &item )
383 return item.first == this;
389 if ( !p->
prepare( context, attributeNames ) )
399 attributeNames.unite( mFilter->referencedColumns() );
404 for (
Rule *rule : std::as_const( mChildren ) )
412 QList< QgsLabelFeature * > labels;
413 if ( !isFilterOK( feature, context )
419 bool registered =
false;
423 [
this](
const std::pair<QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider *> &item )
425 return item.first == this;
430 labels.append( it->second->registerFeature( feature, context, obstacleGeometry, symbol ) );
434 bool matchedAChild =
false;
437 for (
Rule *rule : std::as_const( mChildren ) )
440 if ( !rule->isElse() )
443 QList< QgsLabelFeature * > added;
444 std::tie( res, added ) = rule->registerFeature( feature, context,
subProviders, obstacleGeometry );
445 labels.append( added );
448 registered |= matchedAChild;
453 if ( !matchedAChild )
455 for (
Rule *rule : std::as_const( mElseRules ) )
458 QList< QgsLabelFeature * > added;
459 std::tie( res, added ) = rule->registerFeature( feature, context,
subProviders, obstacleGeometry, symbol ) ;
462 labels.append( added );
466 if ( !mIsActive || ( matchedAChild && !registered ) )
468 else if ( registered )
476 if ( ! mFilter || mElseRule )
484bool QgsRuleBasedLabeling::Rule::isScaleOK(
double scale )
const
517 Q_ASSERT( origDescendants.count() == clonedDescendants.count() );
518 for (
int i = 0; i < origDescendants.count(); ++i )
519 clonedDescendants[i]->setRuleKey( origDescendants[i]->ruleKey() );
541 QDomElement rulesElem = element.firstChildElement( u
"rules"_s );
553 return u
"rule-based"_s;
558 QDomElement elem = doc.createElement( u
"labeling"_s );
559 elem.setAttribute( u
"type"_s, u
"rule-based"_s );
561 QDomElement rulesElem =
mRootRule->save( doc, context );
562 rulesElem.setTagName( u
"rules"_s );
563 elem.appendChild( rulesElem );
596 return mRootRule->requiresAdvancedEffects();
601 return mRootRule->hasNonDefaultCompositionMode();
618 toSld( parent, context );
629 for (
Rule *rule : rules )
635 QDomDocument doc = parent.ownerDocument();
637 QDomElement ruleElement = doc.createElement( u
"se:Rule"_s );
638 parent.appendChild( ruleElement );
640 if ( !rule->filterExpression().isEmpty() )
648 QVariantMap localProps = oldProps;
671 for (
Rule *rule : rules )
const QgsLabelingEngine * mEngine
Associated labeling engine.
QgsMapLayer * layer() const
Returns the associated layer, or nullptr if no layer is associated with the provider.
QString providerId() const
Returns provider ID - useful in case there is more than one label provider within a layer (e....
virtual Q_DECL_DEPRECATED void writeTextSymbolizer(QDomNode &parent, QgsPalLayerSettings &settings, const QVariantMap &props) const
Writes a TextSymbolizer element contents based on the provided labeling settings.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
A geometry is the spatial representation of a feature.
Contains settings for how a map layer will be labeled.
@ FontBlendMode
Text blend mode.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
double rendererScale() const
Returns the renderer map scale.
QgsExpressionContext & expressionContext()
Gets the expression context.
Label provider for rule based labeling.
bool prepare(QgsRenderContext &context, QSet< QString > &attributeNames) override
Prepare for registration of features.
std::unique_ptr< QgsRuleBasedLabeling > mRules
owned copy
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.
QgsRuleBasedLabelProvider(const QgsRuleBasedLabeling &rules, QgsVectorLayer *layer, bool withFeatureLoop=true)
std::vector< std::pair< QgsRuleBasedLabeling::Rule *, QgsVectorLayerLabelProvider * > > mSubProviders
label providers are owned by labeling engine
virtual QgsVectorLayerLabelProvider * createProvider(QgsVectorLayer *layer, const QString &providerId, bool withFeatureLoop, const QgsPalLayerSettings *settings)
create a label provider
QList< QgsAbstractLabelProvider * > subProviders() override
Returns subproviders.
A child rule for QgsRuleBasedLabeling.
void setRuleKey(const QString &key)
Override the assigned rule key (should be used just internally by rule-based labeling).
void createSubProviders(QgsVectorLayer *layer, RuleToProviderVec &subProviders, QgsRuleBasedLabelProvider *provider)
add providers
std::tuple< RegisterResult, QList< QgsLabelFeature * > > registerFeature(const QgsFeature &feature, QgsRenderContext &context, RuleToProviderVec &subProviders, const QgsGeometry &obstacleGeometry=QgsGeometry(), const QgsSymbol *symbol=nullptr)
Register individual features.
void prepare(QgsRenderContext &context, QSet< QString > &attributeNames, RuleToProviderVec &subProviders)
call prepare() on sub-providers and populate attributeNames
QgsPalLayerSettings * settings() const
Returns the labeling settings.
RegisterResult
The result of registering a rule.
@ Inactive
The rule is inactive.
@ Filtered
The rule does not apply.
@ Registered
Something was registered.
bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified symbology visitor, causing it to visit all child rules associated with the rule...
bool hasNonDefaultCompositionMode() const
Returns true if this rule or any of its children requires a non-default composition mode.
void subProviderIds(QStringList &list) const
append rule keys of descendants that contain valid settings (i.e.
bool requiresAdvancedEffects() const
Returns true if this rule or any of its children requires advanced composition effects to render.
void removeChildAt(int i)
delete child rule
void setActive(bool state)
Sets if this rule is active.
static QgsRuleBasedLabeling::Rule * create(const QDomElement &ruleElem, const QgsReadWriteContext &context, bool reuseId=true)
Create a rule from an XML definition.
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const
store labeling info to XML element
void setSettings(QgsPalLayerSettings *settings)
Sets new settings (or nullptr). Deletes old settings if any.
QgsRuleBasedLabeling::RuleList descendants() const
Returns all children, grand-children, grand-grand-children, grand-gra... you get it.
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
void insertChild(int i, QgsRuleBasedLabeling::Rule *rule)
add child rule, take ownership, sets this as parent
QgsRuleBasedLabeling::Rule * clone(bool resetRuleKey=true) const
clone this rule
QString ruleKey() const
Unique rule identifier (for identification of rule within labeling, used as provider ID).
const QgsRuleBasedLabeling::Rule * findRuleByKey(const QString &key) const
Try to find a rule given its unique key.
void appendChild(QgsRuleBasedLabeling::Rule *rule)
add child rule, take ownership, sets this as parent
QString description() const
A human readable description for this rule.
Rule based labeling for a vector layer.
QList< QgsRuleBasedLabeling::Rule * > RuleList
std::unique_ptr< Rule > mRootRule
Q_DECL_DEPRECATED void toSld(QDomNode &parent, const QVariantMap &properties) const override
Writes the SE 1.1 TextSymbolizer element based on the current layer labeling settings.
QStringList subProviders() const override
Gets list of sub-providers within the layer's labeling.
QgsRuleBasedLabeling(QgsRuleBasedLabeling::Rule *root)
Constructs the labeling from given tree of rules (takes ownership).
QgsVectorLayerLabelProvider * provider(QgsVectorLayer *layer) const override
static QgsRuleBasedLabeling * create(const QDomElement &element, const QgsReadWriteContext &context)
Create the instance from a DOM element with saved configuration.
void setSettings(QgsPalLayerSettings *settings, const QString &providerId=QString()) override
Set pal settings for a specific provider (takes ownership).
~QgsRuleBasedLabeling() override
QgsPalLayerSettings settings(const QString &providerId=QString()) const override
Gets associated label settings.
QString type() const override
Unique type string of the labeling configuration implementation.
QgsRuleBasedLabeling * clone() const override
Returns a new copy of the object.
void multiplyOpacity(double opacityFactor) override
Multiply opacity by opacityFactor.
QgsRuleBasedLabeling::Rule * rootRule()
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified symbology visitor, causing it to visit all symbols associated with the labeling...
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const override
Returns labeling configuration as XML element.
bool hasNonDefaultCompositionMode() const override
Returns true the labeling requires a non-default composition mode.
bool requiresAdvancedEffects() const override
Returns true if drawing labels requires advanced effects like composition modes, which could prevent ...
static bool equalToOrGreaterThanMinimumScale(const double scale, const double minScale)
Returns whether the scale is equal to or greater than the minScale, taking non-round numbers into acc...
static bool lessThanMaximumScale(const double scale, const double maxScale)
Returns whether the scale is less than the maxScale, taking non-round numbers into account.
Holds SLD export options and other information related to SLD export of a QGIS layer style.
void setExtraProperties(const QVariantMap &properties)
Sets the open ended set of properties that can drive/inform the SLD encoding.
QVariantMap extraProperties() const
Returns the open ended set of properties that can drive/inform the SLD encoding.
An interface for classes which can visit style entity (e.g.
@ SymbolRule
Rule based symbology or label child rule.
virtual bool visitExit(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor stops visiting a node.
virtual bool visitEnter(const QgsStyleEntityVisitorInterface::Node &node)
Called when the visitor starts visiting a node.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
A label settings entity for QgsStyle databases.
static void applyScaleDependency(QDomDocument &doc, QDomElement &ruleElem, QVariantMap &props)
Checks if the properties contain scaleMinDenom and scaleMaxDenom, if available, they are added into t...
static Q_DECL_DEPRECATED bool createFunctionElement(QDomDocument &doc, QDomElement &element, const QString &function)
Creates an OGC function element.
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.
Abstract base class for all rendered symbols.
Container for all settings relating to text rendering.
void multiplyOpacity(double opacityFactor)
Multiply opacity by opacityFactor.
Implements a label provider for vector layers.
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.
const QgsPalLayerSettings & settings() const
Returns the layer's settings.
Represents a vector layer which manages a vector based dataset.
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
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Contains information relating to a node (i.e.
Contains information relating to the style entity currently being visited.