Skip to content

Commit

Permalink
[layout] Cache map item theme preset lookups
Browse files Browse the repository at this point in the history
Since theme style override lookups are expensive, cache them
to avoid unnecessary duplicate calls

Fixes #17027
  • Loading branch information
nyalldawson committed Jan 18, 2018
1 parent f5876ea commit 0e08e32
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/core/layout/qgslayoutitemmap.cpp
Expand Up @@ -127,6 +127,9 @@ QgsLayoutItemMap *QgsLayoutItemMap::create( QgsLayout *layout )
void QgsLayoutItemMap::refresh()
{
QgsLayoutItem::refresh();

mCachedLayerStyleOverridesPresetName.clear();

invalidateCache();

updateAtlasFeature();
Expand Down Expand Up @@ -1327,6 +1330,12 @@ void QgsLayoutItemMap::shapeChanged()
emit extentChanged();
}

void QgsLayoutItemMap::mapThemeChanged( const QString &theme )
{
if ( theme == mCachedLayerStyleOverridesPresetName )
mCachedLayerStyleOverridesPresetName.clear(); // force cache regeneration at next redraw
}

void QgsLayoutItemMap::connectUpdateSlot()
{
//connect signal from layer registry to update in case of new or deleted layers
Expand Down Expand Up @@ -1357,6 +1366,8 @@ void QgsLayoutItemMap::connectUpdateSlot()

}
connect( mLayout, &QgsLayout::refreshed, this, &QgsLayoutItemMap::invalidateCache );

connect( project->mapThemeCollection(), &QgsMapThemeCollection::mapThemeChanged, this, &QgsLayoutItemMap::mapThemeChanged );
}

void QgsLayoutItemMap::updateToolTip()
Expand Down Expand Up @@ -1436,7 +1447,16 @@ QMap<QString, QString> QgsLayoutItemMap::layerStyleOverridesToRender( const QgsE
presetName = mDataDefinedProperties.valueAsString( QgsLayoutObject::MapStylePreset, context, presetName );

if ( mLayout->project()->mapThemeCollection()->hasMapTheme( presetName ) )
return mLayout->project()->mapThemeCollection()->mapThemeStyleOverrides( presetName );
{
if ( presetName.isEmpty() || presetName != mCachedLayerStyleOverridesPresetName )
{
// have to regenerate cache of style overrides
mCachedPresetLayerStyleOverrides = mLayout->project()->mapThemeCollection()->mapThemeStyleOverrides( presetName );
mCachedLayerStyleOverridesPresetName = presetName;
}

return mCachedPresetLayerStyleOverrides;
}
else
return QMap<QString, QString>();
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/layout/qgslayoutitemmap.h
Expand Up @@ -483,6 +483,8 @@ class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem

void shapeChanged();

void mapThemeChanged( const QString &theme );

private:


Expand Down Expand Up @@ -551,6 +553,11 @@ class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem
//! Stored style names (value) to be used with particular layer IDs (key) instead of default style
QMap<QString, QString> mLayerStyleOverrides;

//! Empty if no cached style overrides stored
mutable QString mCachedLayerStyleOverridesPresetName;
//! Cached style overrides, used to avoid frequent expensive lookups of the preset style override
mutable QMap<QString, QString> mCachedPresetLayerStyleOverrides;

/**
* Whether layers and styles should be used from a preset (preset name is stored
* in mVisibilityPresetName and may be overridden by data-defined expression).
Expand Down

0 comments on commit 0e08e32

Please sign in to comment.