Skip to content
Permalink
Browse files

[layout] Cache map item theme preset lookups

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 0e08e329f4a7628ea7a0b9ef0c0cffb910aad637
Showing with 28 additions and 1 deletion.
  1. +21 −1 src/core/layout/qgslayoutitemmap.cpp
  2. +7 −0 src/core/layout/qgslayoutitemmap.h
@@ -127,6 +127,9 @@ QgsLayoutItemMap *QgsLayoutItemMap::create( QgsLayout *layout )
void QgsLayoutItemMap::refresh()
{
QgsLayoutItem::refresh();

mCachedLayerStyleOverridesPresetName.clear();

invalidateCache();

updateAtlasFeature();
@@ -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
@@ -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()
@@ -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>();
}
@@ -483,6 +483,8 @@ class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem

void shapeChanged();

void mapThemeChanged( const QString &theme );

private:


@@ -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).

0 comments on commit 0e08e32

Please sign in to comment.
You can’t perform that action at this time.