@@ -51,7 +51,6 @@ QgsLegendLayer::QgsLegendLayer( QgsMapLayer* layer )
51
51
, mLyr( layer )
52
52
, mDrawingOrder( -1 )
53
53
, mShowFeatureCount( false )
54
- , mFeatureCount( -1 )
55
54
{
56
55
mType = LEGEND_LAYER;
57
56
@@ -558,12 +557,13 @@ QgsMapCanvasLayer& QgsLegendLayer::canvasLayer()
558
557
return mLyr ;
559
558
}
560
559
561
- QString QgsLegendLayer::label () const
560
+ QString QgsLegendLayer::label ()
562
561
{
563
562
QString name = mLyr .layer ()->name ();
564
- if ( mShowFeatureCount && mFeatureCount >= 0 )
563
+ QgsVectorLayer *vlayer = dynamic_cast <QgsVectorLayer *>( mLyr .layer () );
564
+ if ( mShowFeatureCount && vlayer && vlayer->featureCount () >= 0 )
565
565
{
566
- name += QString ( " [%1]" ).arg ( mFeatureCount );
566
+ name += QString ( " [%1]" ).arg ( vlayer-> featureCount () );
567
567
}
568
568
return name;
569
569
}
@@ -585,7 +585,7 @@ void QgsLegendLayer::afterEdit()
585
585
layerNameChanged ();
586
586
}
587
587
588
- QString QgsLegendLayer::layerName () const
588
+ QString QgsLegendLayer::layerName ()
589
589
{
590
590
// The text could be edited (Rename), in that case we have to return the new name
591
591
if ( text ( 0 ) != label () && text ( 0 ) != mLyr .layer ()->name () )
@@ -618,7 +618,6 @@ void QgsLegendLayer::updateAfterLayerModification( bool onlyGeomChanged )
618
618
619
619
void QgsLegendLayer::updateItemListCountV2 ( SymbologyList& itemList, QgsVectorLayer* layer )
620
620
{
621
- mFeatureCount = -1 ;
622
621
if ( !layer )
623
622
{
624
623
return ;
@@ -629,70 +628,29 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
629
628
{
630
629
return ;
631
630
}
632
- QgsRenderContext dummyContext;
633
- renderer->startRender ( dummyContext, layer );
634
631
635
- // create map holding the symbol count
636
- QMap< QgsSymbolV2*, int > symbolCountMap;
637
- QgsLegendSymbolList symbolList = renderer->legendSymbolItems ();
638
- QgsLegendSymbolList::const_iterator symbolIt = symbolList.constBegin ();
639
- for ( ; symbolIt != symbolList.constEnd (); ++symbolIt )
640
- {
641
- symbolCountMap.insert ( symbolIt->second , 0 );
642
- }
643
-
644
- // go through all features and count the number of occurrences
645
- int nFeatures = layer->pendingFeatureCount ();
646
- QProgressDialog p ( tr ( " Updating feature count for layer %1" ).arg ( layer->name () ), tr ( " Abort" ), 0 , nFeatures );
647
- p.setWindowModality ( Qt::WindowModal );
648
- int featuresCounted = 0 ;
649
-
650
- layer->select ( layer->pendingAllAttributesList (), QgsRectangle (), false , false );
651
- QgsFeature f;
652
-
653
- // Renderer (rule based) may depend on context scale, with scale is ignored if 0
654
- QgsRenderContext renderContext;
655
- renderContext.setRendererScale ( 0 );
656
- renderer->startRender ( renderContext, layer );
657
-
658
- while ( layer->nextFeature ( f ) )
632
+ // Count features
633
+ if ( !layer->countSymbolFeatures () )
659
634
{
660
- QgsSymbolV2List symbolList = renderer->symbolsForFeature ( f );
661
- for ( QgsSymbolV2List::iterator symbolIt = symbolList.begin (); symbolIt != symbolList.end (); ++symbolIt )
662
- {
663
- symbolCountMap[*symbolIt] += 1 ;
664
- }
665
- ++featuresCounted;
666
- if ( featuresCounted % 50 == 0 )
667
- {
668
- if ( featuresCounted > nFeatures ) // sometimes the feature count is not correct
669
- {
670
- p.setMaximum ( 0 );
671
- }
672
- p.setValue ( featuresCounted );
673
- if ( p.wasCanceled () )
674
- {
675
- mFeatureCount = -1 ;
676
- return ;
677
- }
678
- }
679
- mFeatureCount ++;
635
+ QgsDebugMsg ( " Cannot get feature counts" );
636
+ return ;
680
637
}
681
- renderer->stopRender ( renderContext );
682
- p.setValue ( nFeatures );
683
638
684
639
QMap<QString, QPixmap> itemMap;
685
640
SymbologyList::const_iterator symbologyIt = itemList.constBegin ();
686
641
for ( ; symbologyIt != itemList.constEnd (); ++ symbologyIt )
687
642
{
688
643
itemMap.insert ( symbologyIt->first , symbologyIt->second );
689
644
}
645
+
690
646
itemList.clear ();
691
647
648
+ QgsLegendSymbolList symbolList = renderer->legendSymbolItems ();
649
+ QgsLegendSymbolList::const_iterator symbolIt = symbolList.constBegin ();
692
650
symbolIt = symbolList.constBegin ();
693
651
for ( ; symbolIt != symbolList.constEnd (); ++symbolIt )
694
652
{
695
- itemList.push_back ( qMakePair ( symbolIt->first + " [" + QString::number ( symbolCountMap[ symbolIt->second ] ) + " ]" , itemMap[symbolIt->first ] ) );
653
+ itemList.push_back ( qMakePair ( symbolIt->first + " [" + QString::number ( layer-> featureCount ( symbolIt->second ) ) + " ]" , itemMap[symbolIt->first ] ) );
696
654
}
697
655
}
698
656
0 commit comments