Skip to content

Commit

Permalink
Setup API to get theme linked to map pushed to linked legends
Browse files Browse the repository at this point in the history
(cherry picked from commit 406867d)
  • Loading branch information
nyalldawson committed Apr 6, 2020
1 parent 8ad01a2 commit 77927b4
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
9 changes: 9 additions & 0 deletions python/core/auto_generated/layout/qgslayoutitemlegend.sip.in
Expand Up @@ -502,6 +502,15 @@ Sets the ``map`` to associate with the legend.
Returns the associated map.

.. seealso:: :py:func:`setLinkedMap`
%End

QString themeName() const;
%Docstring
Returns the name of the theme currently linked to the legend.

This usually equates to the theme rendered in the linkedMap().

.. versionadded:: 3.14
%End

void updateLegend();
Expand Down
15 changes: 14 additions & 1 deletion src/core/layout/qgslayoutitemlegend.cpp
Expand Up @@ -678,13 +678,15 @@ void QgsLayoutItemLegend::setupMapConnections( QgsLayoutItemMap *map, bool conne
disconnect( map, &QgsLayoutObject::changed, this, &QgsLayoutItemLegend::updateFilterByMapAndRedraw );
disconnect( map, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemLegend::updateFilterByMapAndRedraw );
disconnect( map, &QgsLayoutItemMap::layerStyleOverridesChanged, this, &QgsLayoutItemLegend::mapLayerStyleOverridesChanged );
disconnect( map, &QgsLayoutItemMap::themeChanged, this, &QgsLayoutItemLegend::mapThemeChanged );
}
else
{
connect( map, &QObject::destroyed, this, &QgsLayoutItemLegend::invalidateCurrentMap );
connect( map, &QgsLayoutObject::changed, this, &QgsLayoutItemLegend::updateFilterByMapAndRedraw );
connect( map, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemLegend::updateFilterByMapAndRedraw );
connect( map, &QgsLayoutItemMap::layerStyleOverridesChanged, this, &QgsLayoutItemLegend::mapLayerStyleOverridesChanged );
connect( map, &QgsLayoutItemMap::themeChanged, this, &QgsLayoutItemLegend::mapThemeChanged );
}
}

Expand All @@ -700,10 +702,10 @@ void QgsLayoutItemLegend::setLinkedMap( QgsLayoutItemMap *map )
if ( mMap )
{
setupMapConnections( mMap, true );
mThemeName = mMap->themeToRender( mMap->createExpressionContext() );
}

updateFilterByMap();

}

void QgsLayoutItemLegend::invalidateCurrentMap()
Expand Down Expand Up @@ -773,9 +775,15 @@ void QgsLayoutItemLegend::mapLayerStyleOverridesChanged()
}

adjustBoxSize();

updateFilterByMap( false );
}

void QgsLayoutItemLegend::mapThemeChanged( const QString &theme )
{
mThemeName = theme;
}

void QgsLayoutItemLegend::updateFilterByMap( bool redraw )
{
// ask for update
Expand Down Expand Up @@ -821,6 +829,11 @@ void QgsLayoutItemLegend::doUpdateFilterByMap()
mForceResize = true;
}

QString QgsLayoutItemLegend::themeName() const
{
return mThemeName;
}

void QgsLayoutItemLegend::setLegendFilterOutAtlas( bool doFilter )
{
mFilterOutAtlas = doFilter;
Expand Down
14 changes: 14 additions & 0 deletions src/core/layout/qgslayoutitemlegend.h
Expand Up @@ -476,6 +476,15 @@ class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
*/
QgsLayoutItemMap *linkedMap() const { return mMap; }

/**
* Returns the name of the theme currently linked to the legend.
*
* This usually equates to the theme rendered in the linkedMap().
*
* \since QGIS 3.14
*/
QString themeName() const;

/**
* Updates the model and all legend entries.
*/
Expand Down Expand Up @@ -518,6 +527,8 @@ class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem

//! update legend in case style of associated map has changed
void mapLayerStyleOverridesChanged();
//! update legend in case theme of associated map has changed
void mapThemeChanged( const QString &theme );

//! react to atlas
void onAtlasEnded();
Expand Down Expand Up @@ -567,6 +578,9 @@ class CORE_EXPORT QgsLayoutItemLegend : public QgsLayoutItem
//! Will be TRUE if the legend should be resized automatically to fit contents
bool mSizeToContents = true;

//! Name of theme for legend -- usually the theme associated with the linked map.
QString mThemeName;

friend class QgsCompositionConverter;

};
Expand Down
34 changes: 34 additions & 0 deletions tests/src/python/test_qgslayoutlegend.py
Expand Up @@ -490,6 +490,40 @@ def testSymbolExpressionRender(self):

QgsProject.instance().removeMapLayers([point_layer.id()])

def testThemes(self):
layout = QgsPrintLayout(QgsProject.instance())
layout.setName('LAYOUT')

map = QgsLayoutItemMap(layout)
layout.addLayoutItem(map)
legend = QgsLayoutItemLegend(layout)

self.assertFalse(legend.themeName())
legend.setLinkedMap(map)
self.assertFalse(legend.themeName())

map.setFollowVisibilityPresetName('theme1')
map.setFollowVisibilityPreset(True)
self.assertEqual(legend.themeName(), 'theme1')
map.setFollowVisibilityPresetName('theme2')
self.assertEqual(legend.themeName(), 'theme2')
map.setFollowVisibilityPreset(False)
self.assertFalse(legend.themeName())

# with theme set before linking map
map2 = QgsLayoutItemMap(layout)
map2.setFollowVisibilityPresetName('theme3')
map2.setFollowVisibilityPreset(True)
legend.setLinkedMap(map2)
self.assertEqual(legend.themeName(), 'theme3')
map2.setFollowVisibilityPresetName('theme2')
self.assertEqual(legend.themeName(), 'theme2')

# replace with map with no theme
map3 = QgsLayoutItemMap(layout)
legend.setLinkedMap(map3)
self.assertFalse(legend.themeName())


if __name__ == '__main__':
unittest.main()

0 comments on commit 77927b4

Please sign in to comment.