Cache svg paths

Rebuilding them for each access from the settings is costly
This commit is contained in:
Matthias Kuhn 2020-12-22 11:47:58 +01:00 committed by Mathieu Pellerin
parent 54667e310d
commit af709503d5
4 changed files with 54 additions and 18 deletions

View File

@ -281,6 +281,13 @@ Returns the path to the icons image directory.
static QString srsDatabaseFilePath(); static QString srsDatabaseFilePath();
%Docstring %Docstring
Returns the path to the srs.db file. Returns the path to the srs.db file.
%End
static void setSvgPaths( const QStringList &svgPaths );
%Docstring
Sets the paths to svg directories and invalidates the svg path list cache.
.. versionadded:: 3.18
%End %End
static QStringList svgPaths(); static QStringList svgPaths();

View File

@ -1464,7 +1464,7 @@ void QgsOptions::saveOptions()
{ {
pathsList << mListSVGPaths->item( i )->text(); pathsList << mListSVGPaths->item( i )->text();
} }
mSettings->setValue( QStringLiteral( "svg/searchPathsForSVG" ), pathsList ); QgsApplication::setSvgPaths( pathsList );
pathsList.clear(); pathsList.clear();
for ( int i = 0; i < mListComposerTemplatePaths->count(); ++i ) for ( int i = 0; i < mListComposerTemplatePaths->count(); ++i )

View File

@ -70,6 +70,7 @@
#include "qgsmeshlayer.h" #include "qgsmeshlayer.h"
#include "qgsfeaturestore.h" #include "qgsfeaturestore.h"
#include "qgslocator.h" #include "qgslocator.h"
#include "qgsreadwritelocker.h"
#include "gps/qgsgpsconnectionregistry.h" #include "gps/qgsgpsconnectionregistry.h"
#include "processing/qgsprocessingregistry.h" #include "processing/qgsprocessingregistry.h"
@ -1050,27 +1051,46 @@ QString QgsApplication::srsDatabaseFilePath()
} }
} }
void QgsApplication::setSvgPaths( const QStringList &svgPaths )
{
QgsSettings().setValue( QStringLiteral( "svg/searchPathsForSVG" ), svgPaths );
members()->mSvgPathCacheValid = false;
}
QStringList QgsApplication::svgPaths() QStringList QgsApplication::svgPaths()
{ {
//local directories to search when looking for an SVG with a given basename static QReadWriteLock lock;
//defined by user in options dialog
QgsSettings settings;
const QStringList pathList = settings.value( QStringLiteral( "svg/searchPathsForSVG" ) ).toStringList();
// maintain user set order while stripping duplicates QgsReadWriteLocker locker( lock, QgsReadWriteLocker::Read );
QStringList paths;
for ( const QString &path : pathList )
{
if ( !paths.contains( path ) )
paths.append( path );
}
for ( const QString &path : qgis::as_const( *sDefaultSvgPaths() ) )
{
if ( !paths.contains( path ) )
paths.append( path );
}
return paths; if ( members()->mSvgPathCacheValid )
{
return members()->mSvgPathCache;
}
else
{
locker.changeMode( QgsReadWriteLocker::Write );
//local directories to search when looking for an SVG with a given basename
//defined by user in options dialog
QgsSettings settings;
const QStringList pathList = settings.value( QStringLiteral( "svg/searchPathsForSVG" ) ).toStringList();
// maintain user set order while stripping duplicates
QStringList paths;
for ( const QString &path : pathList )
{
if ( !paths.contains( path ) )
paths.append( path );
}
for ( const QString &path : qgis::as_const( *sDefaultSvgPaths() ) )
{
if ( !paths.contains( path ) )
paths.append( path );
}
members()->mSvgPathCache = paths;
return paths;
}
} }
QStringList QgsApplication::layoutTemplatePaths() QStringList QgsApplication::layoutTemplatePaths()

View File

@ -316,6 +316,13 @@ class CORE_EXPORT QgsApplication : public QApplication
//! Returns the path to the srs.db file. //! Returns the path to the srs.db file.
static QString srsDatabaseFilePath(); static QString srsDatabaseFilePath();
/**
* Sets the paths to svg directories and invalidates the svg path list cache.
*
* \since QGIS 3.18
*/
static void setSvgPaths( const QStringList &svgPaths );
//! Returns the paths to svg directories. //! Returns the paths to svg directories.
static QStringList svgPaths(); static QStringList svgPaths();
@ -1005,6 +1012,8 @@ class CORE_EXPORT QgsApplication : public QApplication
QgsBookmarkManager *mBookmarkManager = nullptr; QgsBookmarkManager *mBookmarkManager = nullptr;
QgsStyleModel *mStyleModel = nullptr; QgsStyleModel *mStyleModel = nullptr;
QString mNullRepresentation; QString mNullRepresentation;
QStringList mSvgPathCache;
bool mSvgPathCacheValid = false;
ApplicationMembers(); ApplicationMembers();
~ApplicationMembers(); ~ApplicationMembers();