Skip to content

Commit 63f14bd

Browse files
committed
Also link new legends to selected map item
1 parent 2c85182 commit 63f14bd

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/app/layout/qgslayoutapputils.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,25 @@ void QgsLayoutAppUtils::registerGuiForKnownItemTypes()
132132
QList<QgsLayoutItemMap *> mapItems;
133133
legend->layout()->layoutItems( mapItems );
134134

135-
if ( !mapItems.isEmpty() )
135+
// try to find a good map to link the legend with by default
136+
// start by trying to find a selected map
137+
QgsLayoutItemMap *targetMap = nullptr;
138+
for ( QgsLayoutItemMap *map : qgis::as_const( mapItems ) )
139+
{
140+
if ( map->isSelected() )
141+
{
142+
targetMap = map;
143+
break;
144+
}
145+
}
146+
// otherwise just use first map
147+
if ( !targetMap && !mapItems.isEmpty() )
148+
{
149+
targetMap = mapItems.at( 0 );
150+
}
151+
if ( targetMap )
136152
{
137-
legend->setMap( mapItems.at( 0 ) );
153+
legend->setMap( targetMap );
138154
}
139155

140156
legend->updateLegend();

0 commit comments

Comments
 (0)