33 #include <QDomDocument> 34 #include <QDomElement> 41 , mScaleMinDenom( scaleMinDenom )
42 , mScaleMaxDenom( scaleMaxDenom )
43 , mFilterExp( filterExp )
45 , mDescription( description )
46 , mElseRule( elseRule )
170 QString msg = off +
QString(
"RULE %1 - scale [%2,%3] - filter %4 - symbol %5\n" )
179 msg += lst.
join(
"\n" );
208 lst += rule->
symbols( context );
244 if ( currentLevel != -1 )
265 return res.
toInt() != 0;
298 int symbolIndex = symbolMap.
size();
327 if (
symbols( context ).isEmpty() )
332 if ( !props.
value(
"filter",
"" ).isEmpty() )
333 props[
"filter" ] +=
" AND ";
340 int parentScaleMinDenom = props.
value(
"scaleMinDenom",
"0" ).toInt( &ok );
341 if ( !ok || parentScaleMinDenom <= 0 )
350 int parentScaleMaxDenom = props.
value(
"scaleMaxDenom",
"0" ).toInt( &ok );
351 if ( !ok || parentScaleMaxDenom <= 0 )
386 if ( !props.
value(
"filter",
"" ).isEmpty() )
391 if ( !props.
value(
"scaleMinDenom",
"" ).isEmpty() )
398 if ( !props.
value(
"scaleMaxDenom",
"" ).isEmpty() )
411 rule->
toSld( doc, element, props );
444 if ( rule->
startRender( context, fields , subfilter ) )
448 subfilters.
append( subfilter );
456 if ( subfilters.
length() > 1 || !subfilters.
value( 0 ).isEmpty() )
458 if ( subfilters.
contains(
"TRUE" ) )
513 return symbolZLevelsSet;
540 bool rendered =
false;
554 bool willrendersomething =
false;
570 if ( !willrendersomething )
665 if ( symbolMap.
contains( symbolIdx ) )
667 symbol = symbolMap.
take( symbolIdx );
671 QgsDebugMsg(
"symbol for rule " + symbolIdx +
" not found!" );
681 Rule* rule =
new Rule( symbol, scaleMinDenom, scaleMaxDenom, filterExp, label, description );
689 while ( !childRuleElem.
isNull() )
691 Rule* childRule =
create( childRuleElem, symbolMap );
720 while ( !childElem.
isNull() )
729 else if ( childElem.
localName() ==
"Description" )
733 if ( !titleElem.
isNull() )
739 if ( !abstractElem.
isNull() )
744 else if ( childElem.
localName() ==
"Abstract" )
749 else if ( childElem.
localName() ==
"Title" )
754 else if ( childElem.
localName() ==
"Filter" )
770 else if ( childElem.
localName() ==
"MinScaleDenominator" )
777 else if ( childElem.
localName() ==
"MaxScaleDenominator" )
818 return new Rule( symbol, scaleMinDenom,
scaleMaxDenom, filterExp, label, description );
852 bool drawVertexMarker )
871 qSort( symbolZLevels );
876 int maxNormLevel = -1;
877 Q_FOREACH (
int zLevel, symbolZLevels )
879 zLevelsToNormLevels[zLevel] = ++maxNormLevel;
905 for (
int i = 0; i < count; i++ )
949 Q_ASSERT( origDescendants.
count() == clonedDescendants.
count() );
950 for (
int i = 0; i < origDescendants.
count(); ++i )
951 clonedDescendants[i]->setRuleKey( origDescendants[i]->ruleKey() );
1009 lst << qMakePair( pair.first, pix );
1022 return rule ? rule->
active() :
true;
1056 if ( symbolsElem.
isNull() )
1078 Rule* root =
nullptr;
1081 while ( !ruleElem.
isNull() )
1088 root =
new Rule(
nullptr );
1125 if ( cat.
value().
type() == QVariant::Int )
1127 else if ( cat.
value().
type() == QVariant::Double )
1150 else if ( !testExpr.
isField() )
1156 bool firstRange =
true;
1176 Q_FOREACH (
int scale, scales )
1180 if ( maxDenom != 0 && maxDenom <= scale )
1191 QString msg(
"Rule-based renderer:\n" );
1218 if ( renderer->
type() ==
"RuleRenderer" )
1223 if ( renderer->
type() ==
"singleSymbol" )
1226 if ( !singleSymbolRenderer )
1234 if ( renderer->
type() ==
"categorizedSymbol" )
1237 if ( !categorizedRenderer )
1255 for (
int i = 0; i < categorizedRenderer->
categories().
size(); ++i )
1264 if (
QVariant( category.
value() ).convert( QVariant::Double ) )
1274 if ( value ==
"''" )
1276 expression =
"ELSE";
1280 expression =
QString(
"%1 = %2" ).
arg( attr, value );
1298 if ( renderer->
type() ==
"graduatedSymbol" )
1301 if ( !graduatedRenderer )
1313 else if ( !testExpr.
isField() )
1323 for (
int i = 0; i < graduatedRenderer->
ranges().
size();++i )
1355 if ( renderer->
type() ==
"pointDisplacement" )
1358 if ( pointDisplacementRenderer )
1361 if ( renderer->
type() ==
"invertedPolygonRenderer" )
1364 if ( invertedPolygonRenderer )
1374 switch ( symbol->
type() )
1380 if ( ! sizeScaleField.
isEmpty() )
1382 sizeExpression =
QString(
"%1*(%2)" ).
arg( msl->
size() ).arg( sizeScaleField );
1385 if ( ! rotationField.
isEmpty() )
1392 if ( ! sizeScaleField.
isEmpty() )
1399 sizeExpression =
QString(
"%1*(%2)" ).
arg( lsl->
width() ).arg( sizeScaleField );
1408 sizeExpression =
QString(
"%1*(%2)" ).
arg( msl->
size() ).arg( sizeScaleField );
QString dump(int indent=0) const
Dump for debug purpose.
static QDomElement saveSymbols(QgsSymbolV2Map &symbols, const QString &tagName, QDomDocument &doc)
Class for parsing and evaluation of expressions (formerly called "search strings").
void setLabel(const QString &label)
QString description() const
A human readable description for this rule.
static QgsSymbolV2Map loadSymbols(QDomElement &element)
void setNormZLevels(const QMap< int, int > &zLevelsToNormLevels)
assign normalized z-levels [0..N-1] for this rule's symbol for quick access during rendering ...
#define RENDERER_TAG_NAME
void CORE_EXPORT save(QDomElement &elem) const
Serialize to XML.
QString & append(QChar ch)
virtual QString filter(const QgsFields &fields=QgsFields()) override
If a renderer does not require all the features this method may be overridden and return an expressio...
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
QStringList referencedColumns() const
Get list of columns referenced by the expression.
QgsSymbolV2 * symbol() const
A container class for data source field mapping or expression.
const QgsCategoryList & categories() const
bool contains(const Key &key) const
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
Q_DECL_DEPRECATED QVariant evaluate(const QgsFeature *f)
Evaluate the feature and return the result.
virtual double width() const
QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, const QString &rule="") const
QString & fill(QChar ch, int size)
QgsFeatureRequest::OrderBy orderBy() const
Get the order in which features shall be processed by this renderer.
QSet< int > collectZLevels()
get all used z-levels from this rule and children
QDomNode appendChild(const QDomNode &newChild)
static QgsFeatureRendererV2 * create(QDomElement &element)
virtual QSet< QString > legendKeysForFeature(QgsFeature &feature, QgsRenderContext &context) override
Return legend keys matching a specified feature.
QSet< int > mSymbolNormZLevels
QSet< QString > usedAttributes() const
Return a list of attributes required to render this feature.
QString attribute(const QString &name, const QString &defValue) const
void setTagName(const QString &name)
QString nodeValue() const
Q_DECL_DEPRECATED bool prepare(const QgsFields &fields)
Get the expression ready for evaluation - find out column indexes.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
void setActive(bool state)
Sets if this rule is active.
QString classAttribute() const
QString & prepend(QChar ch)
double rendererScale() const
bool isFilterOK(QgsFeature &f, QgsRenderContext *context=nullptr) const
Check if a given feature shall be rendered by this rule.
virtual QgsSymbolV2 * clone() const =0
static QgsFeatureRendererV2 * createFromSld(QDomElement &element, QGis::GeometryType geomType)
QgsSymbolV2List symbols(const QgsRenderContext &context=QgsRenderContext()) const
static bool isDefaultStack(QgsPaintEffect *effect)
Tests whether a paint effect matches the default effects stack.
bool contains(const QString &str, Qt::CaseSensitivity cs) const
bool isScaleOK(double scale) const
Check if this rule applies for a given scale.
QDomElement nextSiblingElement(const QString &tagName) const
Q_DECL_DEPRECATED bool startRender(QgsRenderContext &context, const QgsFields &fields)
Prepare the rule for rendering and its children (build active children array)
Rule * clone() const
clone this rule, return new instance
~QgsRuleBasedRendererV2()
Container of fields for a vector layer.
void removeChild(Rule *rule)
delete child rule
virtual void setLegendSymbolItem(const QString &key, QgsSymbolV2 *symbol) override
Sets the symbol to be used for a legend symbol item.
QString join(const QString &separator) const
const_iterator insert(const T &value)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
virtual bool legendSymbolItemsCheckable() const override
items of symbology items in legend should be checkable
void setIsElse(bool iselse)
Sets if this rule is an ELSE rule.
bool isElse()
Check if this rule is an ELSE rule.
QMap< QString, QString > QgsStringMap
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
QgsPaintEffect * mPaintEffect
Rule * takeChild(Rule *rule)
take child rule out, set parent as null
void removeChildAt(int i)
delete child rule
virtual QgsSymbolV2List originalSymbolsForFeature(QgsFeature &feat, QgsRenderContext &context) override
Equivalent of originalSymbolsForFeature() call extended to support renderers that may use more symbol...
virtual bool legendSymbolItemChecked(const QString &key) override
items of symbology items in legend is checked
Rule * findRuleByKey(const QString &key)
Try to find a rule given its unique key.
void renderFeatureWithSymbol(QgsFeature &feature, QgsSymbolV2 *symbol, QgsRenderContext &context, int layer, bool selected, bool drawVertexMarker)
double upperValue() const
virtual void checkLegendSymbolItem(const QString &key, bool state=true) override
item in symbology was checked
static bool createSymbolLayerV2ListFromSld(QDomElement &element, QGis::GeometryType geomType, QgsSymbolLayerV2List &layers)
void stopRender(QgsRenderContext &context)
Stop a rendering process.
virtual QgsFeatureRendererV2 * clone() const =0
QString number(int n, int base)
int count(const T &value) const
virtual QDomElement save(QDomDocument &doc) override
store renderer info to XML element
void toSld(QDomDocument &doc, QDomElement &element, QgsStringMap props) const
void append(const T &value)
static void refineRuleCategories(Rule *initialRule, QgsCategorizedSymbolRendererV2 *r)
take a rule and create a list of new rules based on the categories from categorized symbol renderer ...
QString localName() const
void startRender(QgsRenderContext &context, const QgsFields *fields=nullptr)
int scaleMinDenom() const
int toInt(bool *ok) const
QList< T > values() const
virtual void stopRender(QgsRenderContext &context) override
Needs to be called when a render cycle has finished to clean up.
QgsInvertedPolygonRenderer is a polygon-only feature renderer used to display features inverted...
void toSld(QDomDocument &doc, QDomElement &element, QgsStringMap props) const
void setAttribute(const QString &name, const QString &value)
int toInt(bool *ok, int base) const
virtual Q_DECL_DEPRECATED QString rotationField() const
return rotation field name (or empty string if not set or not supported by renderer) ...
void appendChild(Rule *rule)
add child rule, take ownership, sets this as parent
static Rule * create(QDomElement &ruleElem, QgsSymbolV2Map &symbolMap)
Create a rule from an XML definition.
int removeAll(const T &value)
RenderResult renderFeature(FeatureToRender &featToRender, QgsRenderContext &context, RenderQueue &renderQueue)
Render a given feature, will recursively call subclasses and only render if the constraints apply...
QgsSymbolV2 * symbol() const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QSet< QString > legendKeysForFeature(QgsFeature &feat, QgsRenderContext *context=nullptr)
Returns which legend keys match the feature.
int symbolLayerCount()
Returns total number of symbol layers contained in the symbol.
This class keeps data about a rules for rule-based renderer.
void setRuleKey(const QString &key)
Override the assigned rule key (should be used just internally by rule-based renderer) ...
QgsExpression * filter() const
A filter that will check if this rule applies.
virtual void toSld(QDomDocument &doc, QDomElement &element) const override
used from subclasses to create SLD Rule elements following SLD v1.1 specs
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
bool isField() const
Checks whether an expression consists only of a single field reference.
virtual QgsLegendSymbologyList legendSymbologyItems(QSize iconSize) override
return a list of symbology items for the legend
QString ruleKey() const
Unique rule identifier (for identification of rule within renderer)
bool willRenderFeature(QgsFeature &feat, QgsRenderContext *context=nullptr)
only tell whether a feature will be rendered without actually rendering it
QList< RenderJob * > jobs
virtual Q_DECL_DEPRECATED QgsSymbolV2List symbols()
For symbol levels.
Rule * mRootRule
the root node with hierarchical list of rules
virtual bool renderFeature(QgsFeature &feature, QgsRenderContext &context, int layer=-1, bool selected=false, bool drawVertexMarker=false) override
Render a feature using this renderer in the given context.
static void refineRuleRanges(Rule *initialRule, QgsGraduatedSymbolRendererV2 *r)
take a rule and create a list of new rules based on the ranges from graduated symbol renderer ...
Rule * takeChildAt(int i)
take child rule out, set parent as null
RuleList descendants() const
Returns all children, grand-children, grand-grand-children, grand-gra...
QgsFeatureRendererV2 * embeddedRenderer() const
QDomText createTextNode(const QString &value)
QgsSymbolV2List symbolsForFeature(QgsFeature &feat, QgsRenderContext *context=nullptr)
tell which symbols will be used to render the feature
void updateElseRules()
Check which child rules are else rules and update the internal list of else rules.
QgsLegendSymbolListV2 legendSymbolItemsV2(int currentLevel=-1) const
int renderingPass() const
virtual QString layerType() const =0
Returns a string that represents this layer type.
QgsExpressionContext & expressionContext()
Gets the expression context.
void copyRendererData(QgsFeatureRendererV2 *destRenderer) const
Clones generic renderer data to another renderer.
A renderer that automatically displaces points with the same position.
virtual QgsSymbolV2 * subSymbol()
QgsRuleBasedRendererV2(QgsRuleBasedRendererV2::Rule *root)
Constructs the renderer from given tree of rules (takes ownership)
void setUsingSymbolLevels(bool usingSymbolLevels)
virtual QString dump() const override
for debugging
virtual bool saveProperties(QDomDocument &doc, QDomElement &element) const
Saves the current state of the effect to a DOM element.
int scaleMaxDenom() const
QString sizeScaleField() const
Contains information about the context of a rendering operation.
const QgsFeatureRendererV2 * embeddedRenderer() const
virtual QgsSymbolV2List symbolsForFeature(QgsFeature &feat, QgsRenderContext &context) override
return list of symbols used for rendering the feature.
virtual QgsSymbolV2 * symbolForFeature(QgsFeature &feature, QgsRenderContext &context) override
return symbol for current feature. Should not be used individually: there could be more symbols for a...
QDomNode firstChild() const
static void convertToDataDefinedSymbology(QgsSymbolV2 *symbol, const QString &sizeScaleField, const QString &rotationField=QString())
helper function to convert the size scale and rotation fields present in some other renderers to data...
RenderResult
The result of rendering a rule.
void stopRender(QgsRenderContext &context)
static bool createFunctionElement(QDomDocument &doc, QDomElement &element, const QString &function)
QSet< T > & unite(const QSet< T > &other)
Rule(QgsSymbolV2 *symbol, int scaleMinDenom=0, int scaleMaxDenom=0, const QString &filterExp=QString(), const QString &label=QString(), const QString &description=QString(), bool elseRule=false)
Constructor takes ownership of the symbol.
void insert(int i, const T &value)
static QPixmap symbolPreviewPixmap(QgsSymbolV2 *symbol, QSize size, QgsRenderContext *customContext=nullptr)
double lowerValue() const
const QgsRangeList & ranges() const
QgsFeatureRequest::OrderBy mOrderBy
QString sizeScaleField() const
When drawing a vector layer with rule-based renderer, it goes through the rules and draws features wi...
static Rule * createFromSld(QDomElement &element, QGis::GeometryType geomType)
RuleList rulesForFeature(QgsFeature &feat, QgsRenderContext *context=nullptr)
tell which rules will be used to render the feature
QDomElement firstChildElement(const QString &tagName) const
bool usingSymbolLevels() const
QString sizeScaleField() const
static QgsExpression * expressionFromOgcFilter(const QDomElement &element)
Parse XML with OGC filter into QGIS expression.
virtual void startRender(QgsRenderContext &context, const QgsFields &fields) override
Needs to be called when a new render cycle is started.
virtual QgsRuleBasedRendererV2 * clone() const override
static QString quotedString(QString text)
Returns a quoted version of a string (in single quotes)
virtual QgsLegendSymbolListV2 legendSymbolItemsV2() const override
Return a list of symbology items for the legend.
QString expression() const
Return the original, unmodified expression string.
QList< T > toList() const
QString classAttribute() const
static void clearSymbolMap(QgsSymbolV2Map &symbols)
void setSymbol(QgsSymbolV2 *sym)
set a new symbol (or NULL). Deletes old symbol.
virtual QList< QString > usedAttributes() override
Returns a set of attributes required for this renderer.
double toDouble(bool *ok) const
static QgsRuleBasedRendererV2 * convertFromRenderer(const QgsFeatureRendererV2 *renderer)
creates a QgsRuleBasedRendererV2 from an existing renderer.
QSet< QString > usedAttributes() const
Return the attributes used to evaluate the expression of this rule.
QgsSymbolLayerV2 * symbolLayer(int layer)
Returns a specific symbol layers contained in the symbol.
const_iterator constEnd() const
void insertChild(int i, Rule *rule)
add child rule, take ownership, sets this as parent
void setFilterExpression(const QString &filterExp)
Set the expression used to check if a given feature shall be rendered with this rule.
QDomElement createElement(const QString &tagName)
const_iterator constBegin() const
int compare(const QString &other) const
QString parserErrorString() const
Returns parser error.
bool active() const
Returns if this rule is active.
The class stores information about one class/rule of a vector layer renderer in a unified way that ca...
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QgsSymbolV2 * symbol() const
virtual QgsLegendSymbolList legendSymbolItems(double scaleDenominator=-1, const QString &rule="") override
return a list of item text / symbol
QList< FeatureToRender > mCurrentFeatures
QDomElement save(QDomDocument &doc, QgsSymbolV2Map &symbolMap) const
virtual bool willRenderFeature(QgsFeature &feat, QgsRenderContext &context) override
return whether the renderer will render a feature or not.
static void refineRuleScales(Rule *initialRule, QList< int > scales)
take a rule and create a list of new rules with intervals of scales given by the passed scale denomin...
const T value(const Key &key) const
virtual Q_DECL_DEPRECATED void setDataDefinedProperty(const QString &property, const QString &expressionString)
Sets a data defined expression for a property.