Skip to content

Commit 3a789e5

Browse files
authored
Merge pull request #3825 from nyalldawson/legend_count
QgsVectorLayer::featureCount( QgsSymbol* symbol ) to featureCount( const QString& legendKey )
2 parents eb85e3f + 7de1cc2 commit 3a789e5

File tree

8 files changed

+28
-27
lines changed

8 files changed

+28
-27
lines changed

doc/api_break.dox

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,11 @@ QgsLayerTreeModel {#qgis_api_break_3_0_QgsLayerTreeMode}
10161016
- setAutoCollapseSymbologyNodes() was removed. Use setAutoCollapseLegendNodes() instead.
10171017
- autoCollapseSymbologyNodes() was removed. Use autoCollapseLegendNodes() instead.
10181018

1019+
QgsLayerTreeModelLegendNode {#qgis_api_break_3_0_QgsLayerTreeModelLegendNode}
1020+
---------------------------
1021+
1022+
- The SymbolLegacyRuleKeyRole role was removed. Use RuleKeyRole instead.
1023+
10191024

10201025
QgsLayerTreeNode {#qgis_api_break_3_0_QgsLayerTreeNode}
10211026
----------------
@@ -1572,6 +1577,7 @@ displayExpression instead. For the map tip use mapTipTemplate() instead.
15721577
- getStyleFromDatabase(): msgError argument is correctly declared as output argument
15731578
- loadNamedStyle(): theResultFlag argument is correctly declared as output argument
15741579
- The duplicate selectionChanged() signal was removed. Use selectionChanged( const QgsFeatureIds&, const QgsFeatureIds&, const bool ) instead.
1580+
- featureCount() now requires a legend key string instead of a QgsSymbol pointer argument.
15751581

15761582
QgsVectorLayerEditBuffer {#qgis_api_break_3_0_QgsVectorLayerEditBuffer}
15771583
------------------------

python/core/layertree/qgslayertreemodellegendnode.sip

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class QgsLayerTreeModelLegendNode : QObject
2020
enum LegendNodeRoles
2121
{
2222
RuleKeyRole, //!< rule key of the node (QString)
23-
SymbolLegacyRuleKeyRole, //!< for QgsSymbolLegendNode only - legacy rule key (void ptr, to be cast to QgsSymbol ptr)
2423
ParentRuleKeyRole //!< rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2.8
2524
};
2625

python/core/qgsvectorlayer.sip

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -732,12 +732,11 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator
732732
bool readSld( const QDomNode& node, QString& errorMessage );
733733

734734
/**
735-
* Number of features rendered with specified symbol. Features must be first
735+
* Number of features rendered with specified legend key. Features must be first
736736
* calculated by countSymbolFeatures()
737-
* @param symbol the symbol
738737
* @return number of features rendered by symbol or -1 if failed or counts are not available
739738
*/
740-
long featureCount( QgsSymbol* symbol ) const;
739+
long featureCount( const QString& legendKey ) const;
741740

742741
/**
743742
* Update the data source of the layer. The layer's renderer and legend will be preserved only
@@ -752,7 +751,7 @@ class QgsVectorLayer : QgsMapLayer, QgsExpressionContextGenerator
752751
void setDataSource( const QString& dataSource, const QString& baseName, const QString& provider, bool loadDefaultStyleFlag = false );
753752

754753
/**
755-
* Count features for symbols. Feature counts may be get by featureCount( QgsSymbol*).
754+
* Count features for symbols. Feature counts may be get by featureCount().
756755
* @param showProgress show progress dialog
757756
* @return true if calculated, false if failed or was canceled by user
758757
*/

src/core/layertree/qgslayertreemodel.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,16 +1130,19 @@ QList<QgsLayerTreeModelLegendNode*> QgsLayerTreeModel::filterLegendNodes( const
11301130
{
11311131
Q_FOREACH ( QgsLayerTreeModelLegendNode* node, nodes )
11321132
{
1133-
QgsSymbol* ruleKey = reinterpret_cast< QgsSymbol* >( node->data( QgsSymbolLegendNode::SymbolLegacyRuleKeyRole ).value<void*>() );
1133+
QString ruleKey = node->data( QgsSymbolLegendNode::RuleKeyRole ).toString();
11341134
bool checked = mLegendFilterUsesExtent || node->data( Qt::CheckStateRole ).toInt() == Qt::Checked;
1135-
if ( ruleKey && checked )
1135+
if ( checked )
11361136
{
1137-
QString ruleKey = node->data( QgsSymbolLegendNode::RuleKeyRole ).toString();
11381137
if ( QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( node->layerNode()->layer() ) )
11391138
{
11401139
if ( mLegendFilterHitTest->legendKeyVisible( ruleKey, vl ) )
11411140
filtered << node;
11421141
}
1142+
else
1143+
{
1144+
filtered << node;
1145+
}
11431146
}
11441147
else // unknown node type or unchecked
11451148
filtered << node;

src/core/layertree/qgslayertreemodellegendnode.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,6 @@ QVariant QgsSymbolLegendNode::data( int role ) const
311311
{
312312
return mItem.ruleKey();
313313
}
314-
else if ( role == SymbolLegacyRuleKeyRole )
315-
{
316-
return QVariant::fromValue<void*>( mItem.legacyRuleKey() );
317-
}
318314
else if ( role == ParentRuleKeyRole )
319315
{
320316
return mItem.parentRuleKey();
@@ -468,8 +464,8 @@ void QgsSymbolLegendNode::updateLabel()
468464
else
469465
{
470466
mLabel = mUserLabel.isEmpty() ? mItem.label() : mUserLabel;
471-
if ( showFeatureCount && vl && mItem.legacyRuleKey() )
472-
mLabel += QStringLiteral( " [%1]" ).arg( vl->featureCount( mItem.legacyRuleKey() ) );
467+
if ( showFeatureCount && vl )
468+
mLabel += QStringLiteral( " [%1]" ).arg( vl->featureCount( mItem.ruleKey() ) );
473469
}
474470
}
475471

src/core/layertree/qgslayertreemodellegendnode.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ class CORE_EXPORT QgsLayerTreeModelLegendNode : public QObject
4949
enum LegendNodeRoles
5050
{
5151
RuleKeyRole = Qt::UserRole, //!< Rule key of the node (QString)
52-
SymbolLegacyRuleKeyRole, //!< For QgsSymbolLegendNode only - legacy rule key (void ptr, to be cast to QgsSymbol ptr)
5352
ParentRuleKeyRole //!< Rule key of the parent legend node - for legends with tree hierarchy (QString). Added in 2.8
5453
};
5554

src/core/qgsvectorlayer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -622,12 +622,12 @@ bool QgsVectorLayer::diagramsEnabled() const
622622
return false;
623623
}
624624

625-
long QgsVectorLayer::featureCount( QgsSymbol* symbol ) const
625+
long QgsVectorLayer::featureCount( const QString& legendKey ) const
626626
{
627627
if ( !mSymbolFeatureCounted )
628628
return -1;
629629

630-
return mSymbolFeatureCountMap.value( symbol );
630+
return mSymbolFeatureCountMap.value( legendKey );
631631
}
632632

633633
/** \ingroup core
@@ -697,7 +697,7 @@ bool QgsVectorLayer::countSymbolFeatures( bool showProgress )
697697

698698
for ( ; symbolIt != symbolList.constEnd(); ++symbolIt )
699699
{
700-
mSymbolFeatureCountMap.insert( symbolIt->second, 0 );
700+
mSymbolFeatureCountMap.insert( symbolIt->first, 0 );
701701
}
702702

703703
long nFeatures = featureCount();
@@ -749,10 +749,10 @@ bool QgsVectorLayer::countSymbolFeatures( bool showProgress )
749749
while ( fit.nextFeature( f ) )
750750
{
751751
renderContext.expressionContext().setFeature( f );
752-
QgsSymbolList featureSymbolList = mRenderer->originalSymbolsForFeature( f, renderContext );
753-
for ( QgsSymbolList::iterator symbolIt = featureSymbolList.begin(); symbolIt != featureSymbolList.end(); ++symbolIt )
752+
QSet<QString> featureKeyList = mRenderer->legendKeysForFeature( f, renderContext );
753+
Q_FOREACH ( const QString& key, featureKeyList )
754754
{
755-
mSymbolFeatureCountMap[*symbolIt] += 1;
755+
mSymbolFeatureCountMap[key] += 1;
756756
}
757757
++featuresCounted;
758758

src/core/qgsvectorlayer.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -837,12 +837,11 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
837837
bool readSld( const QDomNode& node, QString& errorMessage ) override;
838838

839839
/**
840-
* Number of features rendered with specified symbol. Features must be first
840+
* Number of features rendered with specified legend key. Features must be first
841841
* calculated by countSymbolFeatures()
842-
* @param symbol the symbol
843842
* @return number of features rendered by symbol or -1 if failed or counts are not available
844843
*/
845-
long featureCount( QgsSymbol* symbol ) const;
844+
long featureCount( const QString& legendKey ) const;
846845

847846
/**
848847
* Update the data source of the layer. The layer's renderer and legend will be preserved only
@@ -857,7 +856,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
857856
void setDataSource( const QString& dataSource, const QString& baseName, const QString& provider, bool loadDefaultStyleFlag = false );
858857

859858
/**
860-
* Count features for symbols. Feature counts may be get by featureCount( QgsSymbol*).
859+
* Count features for symbols. Feature counts may be get by featureCount().
861860
* @param showProgress show progress dialog
862861
* @return true if calculated, false if failed or was canceled by user
863862
*/
@@ -2080,8 +2079,8 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer, public QgsExpressionConte
20802079
// Features in renderer classes counted
20812080
bool mSymbolFeatureCounted;
20822081

2083-
// Feature counts for each renderer symbol
2084-
QMap<QgsSymbol*, long> mSymbolFeatureCountMap;
2082+
// Feature counts for each renderer legend key
2083+
QHash<QString, long> mSymbolFeatureCountMap;
20852084

20862085
//! True while an undo command is active
20872086
bool mEditCommandActive;

0 commit comments

Comments
 (0)