@@ -51,6 +51,7 @@ QgsLegendLayer::QgsLegendLayer( QgsMapLayer* layer )
51
51
, mLyr( layer )
52
52
, mDrawingOrder( -1 )
53
53
, mShowFeatureCount( false )
54
+ , mFeatureCount( -1 )
54
55
{
55
56
mType = LEGEND_LAYER;
56
57
@@ -66,7 +67,7 @@ QgsLegendLayer::QgsLegendLayer( QgsMapLayer* layer )
66
67
67
68
setCheckState ( 0 , Qt::Checked );
68
69
69
- setText ( 0 , layer-> name () );
70
+ layerNameChanged ( );
70
71
setupFont ();
71
72
72
73
// Set the initial visibility flag for layers
@@ -296,6 +297,7 @@ void QgsLegendLayer::vectorLayerSymbologyV2( QgsVectorLayer* layer )
296
297
}
297
298
298
299
changeSymbologySettings ( layer, itemList );
300
+ layerNameChanged (); // update total count
299
301
}
300
302
}
301
303
@@ -556,10 +558,41 @@ QgsMapCanvasLayer& QgsLegendLayer::canvasLayer()
556
558
return mLyr ;
557
559
}
558
560
559
- void QgsLegendLayer::layerNameChanged ()
561
+ QString QgsLegendLayer::label () const
560
562
{
561
563
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 ();
563
596
}
564
597
565
598
void QgsLegendLayer::updateAfterLayerModification ()
@@ -580,10 +613,12 @@ void QgsLegendLayer::updateAfterLayerModification( bool onlyGeomChanged )
580
613
widthScale = canvas->map ()->paintDevice ().logicalDpiX () / 25.4 ;
581
614
}
582
615
refreshSymbology ( mLyr .layer ()->id (), widthScale );
616
+ layerNameChanged ();
583
617
}
584
618
585
619
void QgsLegendLayer::updateItemListCountV2 ( SymbologyList& itemList, QgsVectorLayer* layer )
586
620
{
621
+ mFeatureCount = -1 ;
587
622
if ( !layer )
588
623
{
589
624
return ;
@@ -598,12 +633,12 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
598
633
renderer->startRender ( dummyContext, layer );
599
634
600
635
// create map holding the symbol count
601
- QMap< QgsSymbolV2*, int > mSymbolCountMap ;
636
+ QMap< QgsSymbolV2*, int > symbolCountMap ;
602
637
QgsLegendSymbolList symbolList = renderer->legendSymbolItems ();
603
638
QgsLegendSymbolList::const_iterator symbolIt = symbolList.constBegin ();
604
639
for ( ; symbolIt != symbolList.constEnd (); ++symbolIt )
605
640
{
606
- mSymbolCountMap .insert ( symbolIt->second , 0 );
641
+ symbolCountMap .insert ( symbolIt->second , 0 );
607
642
}
608
643
609
644
// go through all features and count the number of occurrences
@@ -625,7 +660,7 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
625
660
QgsSymbolV2List symbolList = renderer->symbolsForFeature ( f );
626
661
for ( QgsSymbolV2List::iterator symbolIt = symbolList.begin (); symbolIt != symbolList.end (); ++symbolIt )
627
662
{
628
- mSymbolCountMap [*symbolIt] += 1 ;
663
+ symbolCountMap [*symbolIt] += 1 ;
629
664
}
630
665
++featuresCounted;
631
666
if ( featuresCounted % 50 == 0 )
@@ -637,9 +672,11 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
637
672
p.setValue ( featuresCounted );
638
673
if ( p.wasCanceled () )
639
674
{
675
+ mFeatureCount = -1 ;
640
676
return ;
641
677
}
642
678
}
679
+ mFeatureCount ++;
643
680
}
644
681
renderer->stopRender ( renderContext );
645
682
p.setValue ( nFeatures );
@@ -655,7 +692,7 @@ void QgsLegendLayer::updateItemListCountV2( SymbologyList& itemList, QgsVectorLa
655
692
symbolIt = symbolList.constBegin ();
656
693
for ( ; symbolIt != symbolList.constEnd (); ++symbolIt )
657
694
{
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 ] ) );
659
696
}
660
697
}
661
698
0 commit comments