@@ -51,7 +51,6 @@ QgsLegendLayer::QgsLegendLayer( QgsMapLayer* layer )
5151 , mLyr( layer )
5252 , mDrawingOrder( -1 )
5353 , mShowFeatureCount( false )
54- , mFeatureCount( -1 )
5554{
5655 mType = LEGEND_LAYER;
5756
@@ -558,12 +557,13 @@ QgsMapCanvasLayer& QgsLegendLayer::canvasLayer()
558557 return mLyr ;
559558}
560559
561- QString QgsLegendLayer::label () const
560+ QString QgsLegendLayer::label ()
562561{
563562 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 )
565565 {
566- name += QString ( " [%1]" ).arg ( mFeatureCount );
566+ name += QString ( " [%1]" ).arg ( vlayer-> featureCount () );
567567 }
568568 return name;
569569}
@@ -585,7 +585,7 @@ void QgsLegendLayer::afterEdit()
585585 layerNameChanged ();
586586}
587587
588- QString QgsLegendLayer::layerName () const
588+ QString QgsLegendLayer::layerName ()
589589{
590590 // The text could be edited (Rename), in that case we have to return the new name
591591 if ( text ( 0 ) != label () && text ( 0 ) != mLyr .layer ()->name () )
@@ -618,7 +618,6 @@ void QgsLegendLayer::updateAfterLayerModification( bool onlyGeomChanged )
618618
619619void QgsLegendLayer::updateItemListCountV2 ( SymbologyList& itemList, QgsVectorLayer* layer )
620620{
621- mFeatureCount = -1 ;
622621 if ( !layer )
623622 {
624623 return ;
@@ -629,70 +628,29 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
629628 {
630629 return ;
631630 }
632- QgsRenderContext dummyContext;
633- renderer->startRender ( dummyContext, layer );
634631
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 () )
659634 {
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 ;
680637 }
681- renderer->stopRender ( renderContext );
682- p.setValue ( nFeatures );
683638
684639 QMap<QString, QPixmap> itemMap;
685640 SymbologyList::const_iterator symbologyIt = itemList.constBegin ();
686641 for ( ; symbologyIt != itemList.constEnd (); ++ symbologyIt )
687642 {
688643 itemMap.insert ( symbologyIt->first , symbologyIt->second );
689644 }
645+
690646 itemList.clear ();
691647
648+ QgsLegendSymbolList symbolList = renderer->legendSymbolItems ();
649+ QgsLegendSymbolList::const_iterator symbolIt = symbolList.constBegin ();
692650 symbolIt = symbolList.constBegin ();
693651 for ( ; symbolIt != symbolList.constEnd (); ++symbolIt )
694652 {
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 ] ) );
696654 }
697655}
698656
0 commit comments