Skip to content

Commit

Permalink
[layouts] When adding a new map item, auto-assign a real
Browse files Browse the repository at this point in the history
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.

(cherry picked from commit c6f5e31)
  • Loading branch information
nyalldawson committed Jan 15, 2021
1 parent be97dc0 commit 9a9ea89
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/gui/layout/qgslayoutguiutils.cpp
Expand Up @@ -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() );

Expand Down

0 comments on commit 9a9ea89

Please sign in to comment.