Skip to content
Permalink
Browse files

[layouts] When adding a new map item, auto-assign a real

unique id to the item

Previously we auto assigned an internal quasi-id to these items,
but that was rather confusing because they still had an empty
id and couldn't be referred to as "Map 1", etc when using
expressions.
  • Loading branch information
nyalldawson committed Jan 10, 2021
1 parent 60b884d commit c6f5e31d1206ee539a2b119539de7ae292de66cf
Showing with 28 additions and 0 deletions.
  1. +28 −0 src/gui/layout/qgslayoutguiutils.cpp
@@ -129,6 +129,34 @@ void QgsLayoutGuiUtils::registerGuiForKnownItemTypes( QgsMapCanvas *mapCanvas )
{
map->zoomToExtent( mapCanvas->mapSettings().visibleExtent() );
}

// auto assign a unique id to map items
QList<QgsLayoutItemMap *> mapsList;
if ( map->layout() )
map->layout()->layoutItems( mapsList );

int counter = mapsList.size() + 1;
bool existing = false;
while ( true )
{
existing = false;
for ( QgsLayoutItemMap *otherMap : qgis::as_const( mapsList ) )
{
if ( map == otherMap )
continue;

if ( otherMap->id() == QObject::tr( "Map %1" ).arg( counter ) )
{
existing = true;
break;
}
}
if ( existing )
counter++;
else
break;
}
map->setId( QObject::tr( "Map %1" ).arg( counter ) );
} );
registry->addLayoutItemGuiMetadata( mapItemMetadata.release() );

0 comments on commit c6f5e31

Please sign in to comment.
You can’t perform that action at this time.