Delete parent legend layer item if a map layer has been removed from registry and

it was the only (last) legend layer file.


git-svn-id: http://svn.osgeo.org/qgis/trunk@9682 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
wonder 2008-11-21 22:26:14 +00:00
parent 623e7e824a
commit c6ce755bdc

View File

@ -156,12 +156,18 @@ void QgsLegend::removeLayer( QString layer_key )
QTreeWidgetItem* theItem = firstItem(); QTreeWidgetItem* theItem = firstItem();
QgsDebugMsg( "called." ); QgsDebugMsg( "called." );
QgsLegendLayer* lastLL = NULL;
while ( theItem ) while ( theItem )
{ {
QgsLegendItem *li = dynamic_cast<QgsLegendItem*>( theItem ); QgsLegendItem *li = dynamic_cast<QgsLegendItem*>( theItem );
if ( li ) if ( li )
{ {
// save legend layer (parent of a legend layer file we're going to delete)
QgsLegendLayer* ll = dynamic_cast<QgsLegendLayer*>( li );
if ( ll ) lastLL = ll;
QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>( li ); QgsLegendLayerFile* llf = dynamic_cast<QgsLegendLayerFile*>( li );
if ( llf ) if ( llf )
{ {
@ -171,6 +177,15 @@ void QgsLegend::removeLayer( QString layer_key )
mStateOfCheckBoxes.erase( llf ); mStateOfCheckBoxes.erase( llf );
removeItem( llf ); removeItem( llf );
delete llf; delete llf;
// delete also parent legend layer if now it's empty
if (lastLL->mapLayers().size() == 0)
{
mStateOfCheckBoxes.erase( lastLL );
removeItem( lastLL );
delete lastLL;
}
break; break;
} }
} }
@ -609,28 +624,28 @@ void QgsLegend::legendLayerRemove()
if ( ll ) if ( ll )
{ {
std::list<QgsMapLayer*> maplayers = ll->mapLayers(); std::list<QgsMapLayer*> maplayers = ll->mapLayers();
mStateOfCheckBoxes.erase( ll ); int layerCount = maplayers.size();
//also remove the entries for the QgsLegendLayerFiles from the map
std::list<QgsLegendLayerFile*> llfiles = ll->legendLayerFiles();
for ( std::list<QgsLegendLayerFile*>::iterator it = llfiles.begin(); it != llfiles.end(); ++it )
{
mStateOfCheckBoxes.erase( *it );
}
for ( std::list<QgsMapLayer*>::iterator it = maplayers.begin(); it != maplayers.end(); ++it ) for ( std::list<QgsMapLayer*>::iterator it = maplayers.begin(); it != maplayers.end(); ++it )
{ {
//remove the layer //remove the layer
if ( *it ) if ( *it )
{ {
//the map layer registry emits a signal an this will remove the legend layer //the map layer registry emits a signal and this will remove the legend layer file
//from the legend and from memory by calling QgsLegend::removeLayer(QString layer key) //from the legend and from memory by calling QgsLegend::removeLayer(QString layer key)
QgsMapLayerRegistry::instance()->removeMapLayer(( *it )->getLayerID() ); QgsMapLayerRegistry::instance()->removeMapLayer(( *it )->getLayerID() );
} }
} }
removeItem( ll ); if (layerCount == 0)
delete ll; {
// delete the item only when it didn't have any child legend layer files
// (otherwise it is deleted in QgsLegend::removeLayer when deleting last legend layer file)
mStateOfCheckBoxes.erase( ll );
removeItem( ll );
delete ll;
}
adjustIconSize(); adjustIconSize();
return; return;
} }