Skip to content

Commit

Permalink
Insuring legendLayersAdded is called when adding sorted layers (fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Nov 8, 2022
1 parent 77b5c29 commit 98471b6
Showing 1 changed file with 1 addition and 47 deletions.
48 changes: 1 addition & 47 deletions src/app/layers/qgsapplayerhandling.cpp
Expand Up @@ -175,60 +175,14 @@ void QgsAppLayerHandling::addSortedLayersToLegend( QList<QgsMapLayer *> &layers
} );
}

QgsLayerTreeGroup *parent = nullptr;
QgsLayerTreeNode *currentNode = QgisApp::instance()->layerTreeView()->currentNode();
int currentIndex = 0;
if ( currentNode && currentNode->parent() )
{
if ( QgsLayerTree::isGroup( currentNode ) )
{
parent = qobject_cast<QgsLayerTreeGroup *>( currentNode );
}
else if ( QgsLayerTree::isLayer( currentNode ) )
{
const QList<QgsLayerTreeNode *> currentNodeSiblings = currentNode->parent()->children();
int nodeIdx = 0;
for ( const QgsLayerTreeNode *child : std::as_const( currentNodeSiblings ) )
{
nodeIdx++;
if ( child == currentNode )
{
currentIndex = nodeIdx - 1;
break;
}
}
parent = qobject_cast<QgsLayerTreeGroup *>( currentNode->parent() );
}
else
{
parent = qobject_cast<QgsLayerTreeGroup *>( QgsProject::instance()->layerTreeRoot() );
}
}
else
{
parent = qobject_cast<QgsLayerTreeGroup *>( QgsProject::instance()->layerTreeRoot() );
}

for ( QgsMapLayer *layer : layers )
{
if ( layer->customProperty( QStringLiteral( "_legend_added" ), false ).toBool() )
{
layer->removeCustomProperty( QStringLiteral( "_legend_added" ) );
continue;
}

switch ( QgsProject::instance()->layerTreeRegistryBridge()->layerInsertionMethod() )
{
case Qgis::LayerTreeInsertionMethod::AboveInsertionPoint:
parent->insertLayer( currentIndex, layer );
break;
case Qgis::LayerTreeInsertionMethod::TopOfTree:
QgsProject::instance()->layerTreeRoot()->insertLayer( 0, layer );
break;
case Qgis::LayerTreeInsertionMethod::OptimalInInsertionGroup:
QgsLayerTreeUtils::insertLayerAtOptimalPlacement( parent, layer );
break;
}
emit QgsProject::instance()->legendLayersAdded( QList<QgsMapLayer *>() << layer );
}
QgisApp::instance()->layerTreeView()->setCurrentLayer( layers.at( 0 ) );
}
Expand Down

0 comments on commit 98471b6

Please sign in to comment.