@@ -51,6 +51,7 @@ QgsLegendLayer::QgsLegendLayer( QgsMapLayer* layer )
5151 , mLyr( layer )
5252 , mDrawingOrder( -1 )
5353 , mShowFeatureCount( false )
54+ , mFeatureCount( -1 )
5455{
5556 mType = LEGEND_LAYER;
5657
@@ -66,7 +67,7 @@ QgsLegendLayer::QgsLegendLayer( QgsMapLayer* layer )
6667
6768 setCheckState ( 0 , Qt::Checked );
6869
69- setText ( 0 , layer-> name () );
70+ layerNameChanged ( );
7071 setupFont ();
7172
7273 // Set the initial visibility flag for layers
@@ -296,6 +297,7 @@ void QgsLegendLayer::vectorLayerSymbologyV2( QgsVectorLayer* layer )
296297 }
297298
298299 changeSymbologySettings ( layer, itemList );
300+ layerNameChanged (); // update total count
299301 }
300302}
301303
@@ -556,10 +558,41 @@ QgsMapCanvasLayer& QgsLegendLayer::canvasLayer()
556558 return mLyr ;
557559}
558560
559- void QgsLegendLayer::layerNameChanged ()
561+ QString QgsLegendLayer::label () const
560562{
561563 QString name = mLyr .layer ()->name ();
562- setText ( 0 , name );
564+ if ( mShowFeatureCount && mFeatureCount >= 0 )
565+ {
566+ name += QString ( " [%1]" ).arg ( mFeatureCount );
567+ }
568+ return name;
569+ }
570+
571+ void QgsLegendLayer::layerNameChanged ()
572+ {
573+ setText ( 0 , label () );
574+ }
575+
576+ void QgsLegendLayer::beforeEdit ()
577+ {
578+ // Reset to layer name without possible feature count
579+ setText ( 0 , mLyr .layer ()->name () );
580+ }
581+
582+ void QgsLegendLayer::afterEdit ()
583+ {
584+ // Reset label with possible feature count, important if text was not changed
585+ layerNameChanged ();
586+ }
587+
588+ QString QgsLegendLayer::layerName () const
589+ {
590+ // The text could be edited (Rename), in that case we have to return the new name
591+ if ( text ( 0 ) != label () && text ( 0 ) != mLyr .layer ()->name () )
592+ {
593+ return text ( 0 );
594+ }
595+ return mLyr .layer ()->name ();
563596}
564597
565598void QgsLegendLayer::updateAfterLayerModification ()
@@ -580,10 +613,12 @@ void QgsLegendLayer::updateAfterLayerModification( bool onlyGeomChanged )
580613 widthScale = canvas->map ()->paintDevice ().logicalDpiX () / 25.4 ;
581614 }
582615 refreshSymbology ( mLyr .layer ()->id (), widthScale );
616+ layerNameChanged ();
583617}
584618
585619void QgsLegendLayer::updateItemListCountV2 ( SymbologyList& itemList, QgsVectorLayer* layer )
586620{
621+ mFeatureCount = -1 ;
587622 if ( !layer )
588623 {
589624 return ;
@@ -598,12 +633,12 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
598633 renderer->startRender ( dummyContext, layer );
599634
600635 // create map holding the symbol count
601- QMap< QgsSymbolV2*, int > mSymbolCountMap ;
636+ QMap< QgsSymbolV2*, int > symbolCountMap ;
602637 QgsLegendSymbolList symbolList = renderer->legendSymbolItems ();
603638 QgsLegendSymbolList::const_iterator symbolIt = symbolList.constBegin ();
604639 for ( ; symbolIt != symbolList.constEnd (); ++symbolIt )
605640 {
606- mSymbolCountMap .insert ( symbolIt->second , 0 );
641+ symbolCountMap .insert ( symbolIt->second , 0 );
607642 }
608643
609644 // go through all features and count the number of occurrences
@@ -625,7 +660,7 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
625660 QgsSymbolV2List symbolList = renderer->symbolsForFeature ( f );
626661 for ( QgsSymbolV2List::iterator symbolIt = symbolList.begin (); symbolIt != symbolList.end (); ++symbolIt )
627662 {
628- mSymbolCountMap [*symbolIt] += 1 ;
663+ symbolCountMap [*symbolIt] += 1 ;
629664 }
630665 ++featuresCounted;
631666 if ( featuresCounted % 50 == 0 )
@@ -637,9 +672,11 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
637672 p.setValue ( featuresCounted );
638673 if ( p.wasCanceled () )
639674 {
675+ mFeatureCount = -1 ;
640676 return ;
641677 }
642678 }
679+ mFeatureCount ++;
643680 }
644681 renderer->stopRender ( renderContext );
645682 p.setValue ( nFeatures );
@@ -655,7 +692,7 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
655692 symbolIt = symbolList.constBegin ();
656693 for ( ; symbolIt != symbolList.constEnd (); ++symbolIt )
657694 {
658- itemList.push_back ( qMakePair ( symbolIt->first + " [" + QString::number ( mSymbolCountMap [symbolIt->second ] ) + " ]" , itemMap[symbolIt->first ] ) );
695+ itemList.push_back ( qMakePair ( symbolIt->first + " [" + QString::number ( symbolCountMap [symbolIt->second ] ) + " ]" , itemMap[symbolIt->first ] ) );
659696 }
660697}
661698
0 commit comments