Skip to content

Commit

Permalink
[layouts] Handle duplicated composer names when upgrading 2.x
Browse files Browse the repository at this point in the history
projects

Somehow 2.x projects could end up with compositions with duplicate
names. This is strictly forbidden in 3.x, so we autogenerate a
new unique name if we encounter any duplicate composer names when
upgrading a 2.x project.

Fixes #17924
  • Loading branch information
nyalldawson committed Jan 24, 2018
1 parent c1f0657 commit b8880d4
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/core/layout/qgslayoutmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,31 @@ bool QgsLayoutManager::readXml( const QDomElement &element, const QDomDocument &
{
if ( l->name().isEmpty() )
l->setName( legacyTitle );

// some 2.x projects could end in a state where they had duplicated layout names. This is strictly forbidden in 3.x
// so check for duplicate name in layouts already added
int id = 2;
bool isDuplicateName = false;
QString originalName = l->name();
do
{
isDuplicateName = false;
for ( QgsMasterLayoutInterface *layout : qgis::as_const( mLayouts ) )
{
if ( l->name() == layout->name() )
{
isDuplicateName = true;
break;
}
}
if ( isDuplicateName )
{
l->setName( QStringLiteral( "%1 %2" ).arg( originalName ).arg( id ) );
id++;
}
}
while ( isDuplicateName );

bool added = addLayout( l.release() );
result = added && result;
}
Expand Down

0 comments on commit b8880d4

Please sign in to comment.