Skip to content
Permalink
Browse files

Cache svg paths

Rebuilding them for each access from the settings is costly
  • Loading branch information
m-kuhn authored and nirvn committed Dec 24, 2020
1 parent 54667e3 commit af709503d56a01cb74e51910a3852ea812c9d651
@@ -281,6 +281,13 @@ Returns the path to the icons image directory.
static QString srsDatabaseFilePath();
%Docstring
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

static QStringList svgPaths();
@@ -1464,7 +1464,7 @@ void QgsOptions::saveOptions()
{
pathsList << mListSVGPaths->item( i )->text();
}
mSettings->setValue( QStringLiteral( "svg/searchPathsForSVG" ), pathsList );
QgsApplication::setSvgPaths( pathsList );

pathsList.clear();
for ( int i = 0; i < mListComposerTemplatePaths->count(); ++i )
@@ -70,6 +70,7 @@
#include "qgsmeshlayer.h"
#include "qgsfeaturestore.h"
#include "qgslocator.h"
#include "qgsreadwritelocker.h"

#include "gps/qgsgpsconnectionregistry.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()
{
//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();
static QReadWriteLock lock;

QgsReadWriteLocker locker( lock, QgsReadWriteLocker::Read );

// maintain user set order while stripping duplicates
QStringList paths;
for ( const QString &path : pathList )
if ( members()->mSvgPathCacheValid )
{
if ( !paths.contains( path ) )
paths.append( path );
return members()->mSvgPathCache;
}
for ( const QString &path : qgis::as_const( *sDefaultSvgPaths() ) )
else
{
if ( !paths.contains( path ) )
paths.append( path );
}
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();

return paths;
// 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()
@@ -316,6 +316,13 @@ class CORE_EXPORT QgsApplication : public QApplication
//! Returns the path to the srs.db file.
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.
static QStringList svgPaths();

@@ -1005,6 +1012,8 @@ class CORE_EXPORT QgsApplication : public QApplication
QgsBookmarkManager *mBookmarkManager = nullptr;
QgsStyleModel *mStyleModel = nullptr;
QString mNullRepresentation;
QStringList mSvgPathCache;
bool mSvgPathCacheValid = false;

ApplicationMembers();
~ApplicationMembers();

0 comments on commit af70950

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