Skip to content

Commit ad955e2

Browse files
committed
Added willRenderFeature() and symbolsForFeature() utility methods.
1 parent ccb5ed0 commit ad955e2

File tree

6 files changed

+302
-64
lines changed

6 files changed

+302
-64
lines changed

python/core/symbology-ng-core.sip

Lines changed: 108 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ public:
7777

7878
virtual QString dump();
7979

80+
enum Capabilities
81+
{
82+
SymbolLevels = 1, // rendering with symbol levels (i.e. implements symbols(), symbolForFeature())
83+
RotationField = 2, // rotate symbols by attribute value
84+
MoreSymbolsPerFeature = 4 // may use more than one symbol to render a feature: symbolsForFeature() will return them
85+
};
86+
87+
//! returns bitwise OR-ed capabilities of the renderer
88+
//! \note added in 2.0
89+
virtual int capabilities();
90+
8091
virtual QgsFeatureRendererV2* clone()=0 /Factory/;
8192

8293
virtual QgsSymbolV2List symbols()=0;
@@ -105,6 +116,18 @@ public:
105116
//! @note added in 1.9
106117
virtual void setRotationField( QString fieldName );
107118

119+
//! return whether the renderer will render a feature or not.
120+
//! Must be called between startRender() and stopRender() calls.
121+
//! Default implementation uses symbolForFeature().
122+
//! @note added in 1.9
123+
virtual bool willRenderFeature( QgsFeature& feat );
124+
125+
//! return list of symbols used for rendering the feature.
126+
//! For renderers that do not support MoreSymbolsPerFeature it is more efficient
127+
//! to use symbolForFeature()
128+
//! @note added in 1.9
129+
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
130+
108131
protected:
109132
QgsFeatureRendererV2(QString type);
110133

@@ -154,6 +177,10 @@ public:
154177

155178
virtual QString dump();
156179

180+
//! returns bitwise OR-ed capabilities of the renderer
181+
//! \note added in 2.0
182+
virtual int capabilities();
183+
157184
virtual QgsFeatureRendererV2* clone() /Factory/;
158185

159186
virtual QgsSymbolV2List symbols();
@@ -221,6 +248,10 @@ public:
221248

222249
virtual QString dump();
223250

251+
//! returns bitwise OR-ed capabilities of the renderer
252+
//! \note added in 2.0
253+
virtual int capabilities();
254+
224255
virtual QgsFeatureRendererV2* clone() /Factory/;
225256

226257
virtual QgsSymbolV2List symbols();
@@ -320,6 +351,10 @@ public:
320351

321352
virtual QString dump();
322353

354+
//! returns bitwise OR-ed capabilities of the renderer
355+
//! \note added in 2.0
356+
virtual int capabilities();
357+
323358
virtual QgsFeatureRendererV2* clone() /Factory/;
324359

325360
virtual QgsSymbolV2List symbols();
@@ -410,27 +445,76 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2
410445
class Rule
411446
{
412447
public:
413-
//! Constructor takes ownership of the symbol
414-
Rule( QgsSymbolV2* symbol /Transfer/, int scaleMinDenom = 0, int scaleMaxDenom = 0, QString filterExp = QString() );
415-
//Rule( const QgsRuleBasedRendererV2::Rule& other );
448+
449+
Rule( QgsSymbolV2* symbol /Transfer/, int scaleMinDenom = 0, int scaleMaxDenom = 0, QString filterExp = QString(),
450+
QString label = QString(), QString description = QString() );
416451
~Rule();
417-
QString dump() const;
418-
//QStringList needsFields() const;
452+
QString dump( int offset = 0 ) const;
453+
QSet<QString> usedAttributes();
454+
QgsSymbolV2List symbols();
455+
// TODO QgsLegendSymbolList legendSymbolItems();
419456
bool isFilterOK( QgsFeature& f ) const;
420457
bool isScaleOK( double scale ) const;
421458

422459
QgsSymbolV2* symbol();
460+
QString label() const;
423461
bool dependsOnScale() const;
424462
int scaleMinDenom() const;
425463
int scaleMaxDenom() const;
426-
QString filterExpression() const;
427464
QgsExpression* filter() const;
465+
QString filterExpression() const;
466+
QString description() const;
428467

468+
//! set a new symbol (or NULL). Deletes old symbol.
469+
void setSymbol( QgsSymbolV2* sym /Transfer/ );
470+
void setLabel( QString label );
429471
void setScaleMinDenom( int scaleMinDenom );
430472
void setScaleMaxDenom( int scaleMaxDenom );
431473
void setFilterExpression( QString filterExp );
474+
void setDescription( QString description );
475+
476+
//! clone this rule, return new instance
477+
QgsRuleBasedRendererV2::Rule* clone() const /Factory/;
478+
479+
QDomElement save( QDomDocument& doc, QgsSymbolV2Map& symbolMap );
480+
481+
//! prepare the rule for rendering and its children (build active children array)
482+
bool startRender( QgsRenderContext& context, const QgsVectorLayer *vlayer );
483+
//! get all used z-levels from this rule and children
484+
QSet<int> collectZLevels();
485+
//! assign normalized z-levels [0..N-1] for this rule's symbol for quick access during rendering
486+
// TODO void setNormZLevels( const QMap<int, int>& zLevelsToNormLevels );
487+
488+
// TODO bool renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );
489+
490+
//! only tell whether a feature will be rendered without actually rendering it
491+
//! @note added in 1.9
492+
bool willRenderFeature( QgsFeature& feat );
493+
494+
//! tell which symbols will be used to render the feature
495+
//! @note added in 1.9
496+
QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
497+
498+
void stopRender( QgsRenderContext& context );
499+
500+
static QgsRuleBasedRendererV2::Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap ) /Factory/;
501+
502+
QList<QgsRuleBasedRendererV2::Rule*>& children();
503+
QgsRuleBasedRendererV2::Rule* parent();
504+
505+
//! add child rule, take ownership, sets this as parent
506+
void appendChild( QgsRuleBasedRendererV2::Rule* rule /Transfer/ );
507+
//! add child rule, take ownership, sets this as parent
508+
void insertChild( int i, QgsRuleBasedRendererV2::Rule* rule /Transfer/ );
509+
//! delete child rule
510+
void removeChild( QgsRuleBasedRendererV2::Rule* rule );
511+
//! delete child rule
512+
void removeChildAt( int i );
513+
//! take child rule out, set parent as null
514+
void takeChild( QgsRuleBasedRendererV2::Rule* rule );
515+
//! take child rule out, set parent as null
516+
QgsRuleBasedRendererV2::Rule* takeChildAt( int i );
432517

433-
//Rule& operator=( const Rule& other );
434518
};
435519

436520
/////
@@ -453,6 +537,12 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2
453537

454538
virtual QList<QString> usedAttributes();
455539

540+
virtual QString dump();
541+
542+
//! returns bitwise OR-ed capabilities of the renderer
543+
//! \note added in 2.0
544+
virtual int capabilities();
545+
456546
virtual QgsFeatureRendererV2* clone() /Factory/;
457547

458548
virtual QgsSymbolV2List symbols();
@@ -463,6 +553,17 @@ class QgsRuleBasedRendererV2 : QgsFeatureRendererV2
463553
//! return a list of symbology items for the legend
464554
virtual QgsLegendSymbologyList legendSymbologyItems( QSize iconSize );
465555

556+
//! return whether the renderer will render a feature or not.
557+
//! Must be called between startRender() and stopRender() calls.
558+
//! @note added in 1.9
559+
virtual bool willRenderFeature( QgsFeature& feat );
560+
561+
//! return list of symbols used for rendering the feature.
562+
//! For renderers that do not support MoreSymbolsPerFeature it is more efficient
563+
//! to use symbolForFeature()
564+
//! @note added in 1.9
565+
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
566+
466567
/////
467568

468569

src/core/symbology-ng/qgsrendererv2.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,11 @@ void QgsFeatureRendererV2::renderVertexMarkerPolygon( QPolygonF& pts, QList<QPol
417417
}
418418
}
419419
}
420+
421+
QgsSymbolV2List QgsFeatureRendererV2::symbolsForFeature( QgsFeature& feat )
422+
{
423+
QgsSymbolV2List lst;
424+
QgsSymbolV2* s = symbolForFeature( feat );
425+
if ( s ) lst.append( s );
426+
return lst;
427+
}

src/core/symbology-ng/qgsrendererv2.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ class CORE_EXPORT QgsFeatureRendererV2
8181
enum Capabilities
8282
{
8383
SymbolLevels = 1, // rendering with symbol levels (i.e. implements symbols(), symbolForFeature())
84-
RotationField = 1 << 1 // rotate symbols by attribute value
84+
RotationField = 1 << 1, // rotate symbols by attribute value
85+
MoreSymbolsPerFeature = 1 << 2 // may use more than one symbol to render a feature: symbolsForFeature() will return them
8586
};
8687

8788
//! returns bitwise OR-ed capabilities of the renderer
@@ -117,7 +118,17 @@ class CORE_EXPORT QgsFeatureRendererV2
117118
//! @note added in 1.9
118119
virtual void setRotationField( QString fieldName ) { Q_UNUSED( fieldName ); }
119120

121+
//! return whether the renderer will render a feature or not.
122+
//! Must be called between startRender() and stopRender() calls.
123+
//! Default implementation uses symbolForFeature().
124+
//! @note added in 1.9
125+
virtual bool willRenderFeature( QgsFeature& feat ) { return symbolForFeature( feat ) != NULL; }
120126

127+
//! return list of symbols used for rendering the feature.
128+
//! For renderers that do not support MoreSymbolsPerFeature it is more efficient
129+
//! to use symbolForFeature()
130+
//! @note added in 1.9
131+
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
121132

122133
protected:
123134
QgsFeatureRendererV2( QString type );

src/core/symbology-ng/qgsrulebasedrendererv2.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,38 @@ bool QgsRuleBasedRendererV2::Rule::renderFeature( QgsRuleBasedRendererV2::Featur
288288
return rendered;
289289
}
290290

291+
bool QgsRuleBasedRendererV2::Rule::willRenderFeature( QgsFeature& feat )
292+
{
293+
if ( !isFilterOK( feat ) )
294+
return false;
295+
if ( mSymbol )
296+
return true;
297+
298+
for ( QList<Rule*>::iterator it = mActiveChildren.begin(); it != mActiveChildren.end(); ++it )
299+
{
300+
Rule* rule = *it;
301+
if ( rule->willRenderFeature( feat ) )
302+
return true;
303+
}
304+
return false;
305+
}
306+
307+
QgsSymbolV2List QgsRuleBasedRendererV2::Rule::symbolsForFeature( QgsFeature& feat )
308+
{
309+
QgsSymbolV2List lst;
310+
if ( !isFilterOK( feat ) )
311+
return lst;
312+
if ( mSymbol )
313+
lst.append( mSymbol );
314+
315+
for ( QList<Rule*>::iterator it = mActiveChildren.begin(); it != mActiveChildren.end(); ++it )
316+
{
317+
Rule* rule = *it;
318+
lst += rule->symbolsForFeature( feat );
319+
}
320+
return lst;
321+
}
322+
291323

292324
void QgsRuleBasedRendererV2::Rule::stopRender( QgsRenderContext& context )
293325
{
@@ -581,3 +613,13 @@ QString QgsRuleBasedRendererV2::dump()
581613
msg += mRootRule->dump();
582614
return msg;
583615
}
616+
617+
bool QgsRuleBasedRendererV2::willRenderFeature( QgsFeature& feat )
618+
{
619+
return mRootRule->willRenderFeature( feat );
620+
}
621+
622+
QgsSymbolV2List QgsRuleBasedRendererV2::symbolsForFeature( QgsFeature& feat )
623+
{
624+
return mRootRule->symbolsForFeature( feat );
625+
}

src/core/symbology-ng/qgsrulebasedrendererv2.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ class CORE_EXPORT QgsRuleBasedRendererV2 : public QgsFeatureRendererV2
127127

128128
bool renderFeature( FeatureToRender& featToRender, QgsRenderContext& context, RenderQueue& renderQueue );
129129

130+
//! only tell whether a feature will be rendered without actually rendering it
131+
//! @note added in 1.9
132+
bool willRenderFeature( QgsFeature& feat );
133+
134+
//! tell which symbols will be used to render the feature
135+
//! @note added in 1.9
136+
QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
137+
130138
void stopRender( QgsRenderContext& context );
131139

132140
static Rule* create( QDomElement& ruleElem, QgsSymbolV2Map& symbolMap );
@@ -204,6 +212,21 @@ class CORE_EXPORT QgsRuleBasedRendererV2 : public QgsFeatureRendererV2
204212
//! for debugging
205213
virtual QString dump();
206214

215+
//! return whether the renderer will render a feature or not.
216+
//! Must be called between startRender() and stopRender() calls.
217+
//! @note added in 1.9
218+
virtual bool willRenderFeature( QgsFeature& feat );
219+
220+
//! return list of symbols used for rendering the feature.
221+
//! For renderers that do not support MoreSymbolsPerFeature it is more efficient
222+
//! to use symbolForFeature()
223+
//! @note added in 1.9
224+
virtual QgsSymbolV2List symbolsForFeature( QgsFeature& feat );
225+
226+
//! returns bitwise OR-ed capabilities of the renderer
227+
//! \note added in 2.0
228+
virtual int capabilities() { return MoreSymbolsPerFeature; }
229+
207230
/////
208231

209232
Rule* rootRule() { return mRootRule; }

0 commit comments

Comments
 (0)