@@ -592,39 +592,51 @@ void QgsLegend::legendGroupRemove()
592
592
593
593
void QgsLegend::legendLayerRemove ()
594
594
{
595
- // remove all layers of the current legendLayer
595
+ // if the current item is a legend layer: remove all layers of the current legendLayer
596
596
QgsLegendLayer* ll = dynamic_cast <QgsLegendLayer*>(currentItem ());
597
- if (!ll)
598
- {
599
- return ;
600
- }
601
-
602
- std::list<QgsMapLayer*> maplayers = ll->mapLayers ();
603
- mStateOfCheckBoxes .erase (ll);
604
-
605
- // todo: also remove the entries for the QgsLegendLayerFiles from the map
606
- std::list<QgsLegendLayerFile*> llfiles = ll->legendLayerFiles ();
607
- for (std::list<QgsLegendLayerFile*>::iterator it = llfiles.begin (); it != llfiles.end (); ++it)
608
- {
609
- mStateOfCheckBoxes .erase (*it);
610
- }
611
-
612
- for (std::list<QgsMapLayer*>::iterator it = maplayers.begin (); it!=maplayers.end (); ++it)
597
+ if (ll)
613
598
{
614
- // remove the layer
615
- if (*it)
599
+ std::list<QgsMapLayer*> maplayers = ll->mapLayers ();
600
+ mStateOfCheckBoxes .erase (ll);
601
+
602
+ // also remove the entries for the QgsLegendLayerFiles from the map
603
+ std::list<QgsLegendLayerFile*> llfiles = ll->legendLayerFiles ();
604
+ for (std::list<QgsLegendLayerFile*>::iterator it = llfiles.begin (); it != llfiles.end (); ++it)
605
+ {
606
+ mStateOfCheckBoxes .erase (*it);
607
+ }
608
+
609
+ for (std::list<QgsMapLayer*>::iterator it = maplayers.begin (); it!=maplayers.end (); ++it)
616
610
{
617
- QgsMapLayerRegistry::instance ()->removeMapLayer ((*it)->getLayerID ());
611
+ // remove the layer
612
+ if (*it)
613
+ {
614
+ QgsMapLayerRegistry::instance ()->removeMapLayer ((*it)->getLayerID ());
615
+ }
618
616
}
617
+
618
+ if (maplayers.size ()>0 )
619
+ {
620
+ mMapCanvas ->refresh ();
621
+ }
622
+ removeItem (ll);
623
+ delete ll;
624
+ adjustIconSize ();
625
+ return ;
619
626
}
620
627
621
- if (maplayers.size ()>0 )
622
- {
623
- mMapCanvas ->refresh ();
624
- }
625
- removeItem (ll);
626
- delete ll;
627
- adjustIconSize ();
628
+ // if the current item is a legend layer file
629
+ QgsLegendLayerFile* llf = dynamic_cast <QgsLegendLayerFile*>(currentItem ());
630
+ if (llf)
631
+ {
632
+ if (llf->layer ())
633
+ {
634
+ // the map layer registry emits a signal an this will remove the legend layer
635
+ // from the legend and from memory by calling QgsLegend::removeLayer(QString layer key)
636
+ QgsMapLayerRegistry::instance ()->removeMapLayer (llf->layer ()->getLayerID ());
637
+ }
638
+ }
639
+ return ;
628
640
}
629
641
630
642
void QgsLegend::legendLayerAddToOverview ()
0 commit comments