Skip to content

Commit 2c85182

Browse files
committedNov 24, 2017
[layouts] Link new scalebars to selected map item, if there is one
Fixes #13851
1 parent 060270c commit 2c85182

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed
 

‎src/app/layout/qgslayoutapputils.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,27 @@ void QgsLayoutAppUtils::registerGuiForKnownItemTypes()
157157
QList<QgsLayoutItemMap *> mapItems;
158158
scalebar->layout()->layoutItems( mapItems );
159159

160-
if ( !mapItems.isEmpty() )
160+
// try to find a good map to link the scalebar with by default
161+
// start by trying to find a selected map
162+
QgsLayoutItemMap *targetMap = nullptr;
163+
for ( QgsLayoutItemMap *map : qgis::as_const( mapItems ) )
164+
{
165+
if ( map->isSelected() )
166+
{
167+
targetMap = map;
168+
break;
169+
}
170+
}
171+
// otherwise just use first map
172+
if ( !targetMap && !mapItems.isEmpty() )
173+
{
174+
targetMap = mapItems.at( 0 );
175+
}
176+
if ( targetMap )
161177
{
162-
scalebar->setMap( mapItems.at( 0 ) );
178+
scalebar->setMap( targetMap );
179+
scalebar->applyDefaultSize();
163180
}
164-
scalebar->applyDefaultSize();
165181
} );
166182

167183
registry->addLayoutItemGuiMetadata( scalebarItemMetadata.release() );

0 commit comments

Comments
 (0)
Please sign in to comment.