Skip to content

Commit

Permalink
Delete parent legend layer item if a map layer has been removed from …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
wonder committed Nov 21, 2008
1 parent 623e7e8 commit c6ce755
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/app/legend/qgslegend.cpp
Expand Up @@ -156,12 +156,18 @@ void QgsLegend::removeLayer( QString layer_key )

QTreeWidgetItem* theItem = firstItem();
QgsDebugMsg( "called." );

QgsLegendLayer* lastLL = NULL;

while ( theItem )
{
QgsLegendItem *li = dynamic_cast<QgsLegendItem*>( theItem );
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 );
if ( llf )
{
Expand All @@ -171,6 +177,15 @@ void QgsLegend::removeLayer( QString layer_key )
mStateOfCheckBoxes.erase( llf );
removeItem( 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;
}
}
Expand Down Expand Up @@ -609,28 +624,28 @@ void QgsLegend::legendLayerRemove()
if ( ll )
{
std::list<QgsMapLayer*> maplayers = ll->mapLayers();
mStateOfCheckBoxes.erase( ll );

//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 );
}
int layerCount = maplayers.size();

for ( std::list<QgsMapLayer*>::iterator it = maplayers.begin(); it != maplayers.end(); ++it )
{
//remove the layer
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)
QgsMapLayerRegistry::instance()->removeMapLayer(( *it )->getLayerID() );
}
}

removeItem( ll );
delete ll;
if (layerCount == 0)
{
// 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();
return;
}
Expand Down

0 comments on commit c6ce755

Please sign in to comment.