[layouts] Link new scalebars to selected map item, if there is one

Fixes #13851
This commit is contained in:
Nyall Dawson 2017-11-21 11:33:36 +10:00
parent 060270c0be
commit 2c85182c3f

View File

@ -157,11 +157,27 @@ void QgsLayoutAppUtils::registerGuiForKnownItemTypes()
QList<QgsLayoutItemMap *> mapItems;
scalebar->layout()->layoutItems( mapItems );
if ( !mapItems.isEmpty() )
// try to find a good map to link the scalebar with by default
// start by trying to find a selected map
QgsLayoutItemMap *targetMap = nullptr;
for ( QgsLayoutItemMap *map : qgis::as_const( mapItems ) )
{
scalebar->setMap( mapItems.at( 0 ) );
if ( map->isSelected() )
{
targetMap = map;
break;
}
}
// otherwise just use first map
if ( !targetMap && !mapItems.isEmpty() )
{
targetMap = mapItems.at( 0 );
}
if ( targetMap )
{
scalebar->setMap( targetMap );
scalebar->applyDefaultSize();
}
scalebar->applyDefaultSize();
} );
registry->addLayoutItemGuiMetadata( scalebarItemMetadata.release() );