Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Seriously, I thought I told ya ta shuddup already, right?
(cherry-picked from 8f9a378)
  • Loading branch information
nyalldawson committed May 3, 2018
1 parent 1b26316 commit ca553ea
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
14 changes: 9 additions & 5 deletions python/core/symbology/qgssymbollayerutils.sip.in
Expand Up @@ -614,16 +614,20 @@ Return a list of all available svg files
Return a list of svg files at the specified directory
%End

static QString svgSymbolNameToPath( QString name, const QgsPathResolver &pathResolver );
static QString svgSymbolNameToPath( const QString &name, const QgsPathResolver &pathResolver );
%Docstring
Get SVG symbol's path from its name.
If the name is not absolute path the file is searched in SVG paths specified
Determines an SVG symbol's path from its ``name``.
If ``name`` is not an absolute path the file is scanned for in the SVG paths specified
in settings svg/searchPathsForSVG.

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

static QString svgSymbolPathToName( QString path, const QgsPathResolver &pathResolver );
static QString svgSymbolPathToName( const QString &path, const QgsPathResolver &pathResolver );
%Docstring
Get SVG symbols's name from its path
Determines an SVG symbol's name from its ``path``.

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

static QPointF polygonCentroid( const QPolygonF &points );
Expand Down
20 changes: 12 additions & 8 deletions src/core/symbology/qgssymbollayerutils.cpp
Expand Up @@ -3653,15 +3653,16 @@ QStringList QgsSymbolLayerUtils::listSvgFilesAt( const QString &directory )

}

QString QgsSymbolLayerUtils::svgSymbolNameToPath( QString name, const QgsPathResolver &pathResolver )
QString QgsSymbolLayerUtils::svgSymbolNameToPath( const QString &n, const QgsPathResolver &pathResolver )
{
if ( name.isEmpty() )
if ( n.isEmpty() )
return QString();

// we might have a full path...
if ( QFileInfo::exists( name ) )
return QFileInfo( name ).canonicalFilePath();
if ( QFileInfo::exists( n ) )
return QFileInfo( n ).canonicalFilePath();

QString name = n;
// or it might be an url...
if ( name.contains( QLatin1String( "://" ) ) )
{
Expand Down Expand Up @@ -3715,12 +3716,15 @@ QString QgsSymbolLayerUtils::svgSymbolNameToPath( QString name, const QgsPathRes
return pathResolver.readPath( name );
}

QString QgsSymbolLayerUtils::svgSymbolPathToName( QString path, const QgsPathResolver &pathResolver )
QString QgsSymbolLayerUtils::svgSymbolPathToName( const QString &p, const QgsPathResolver &pathResolver )
{
if ( !QFileInfo::exists( path ) )
return path;
if ( p.isEmpty() )
return QString();

if ( !QFileInfo::exists( p ) )
return p;

path = QFileInfo( path ).canonicalFilePath();
QString path = QFileInfo( p ).canonicalFilePath();

QStringList svgPaths = QgsApplication::svgPaths();

Expand Down
16 changes: 10 additions & 6 deletions src/core/symbology/qgssymbollayerutils.h
Expand Up @@ -563,14 +563,18 @@ class CORE_EXPORT QgsSymbolLayerUtils
static QStringList listSvgFilesAt( const QString &directory );

/**
* Get SVG symbol's path from its name.
* If the name is not absolute path the file is searched in SVG paths specified
* in settings svg/searchPathsForSVG.
* Determines an SVG symbol's path from its \a name.
* If \a name is not an absolute path the file is scanned for in the SVG paths specified
* in settings svg/searchPathsForSVG.
* \see svgSymbolPathToName()
*/
static QString svgSymbolNameToPath( QString name, const QgsPathResolver &pathResolver );
static QString svgSymbolNameToPath( const QString &name, const QgsPathResolver &pathResolver );

//! Get SVG symbols's name from its path
static QString svgSymbolPathToName( QString path, const QgsPathResolver &pathResolver );
/**
* Determines an SVG symbol's name from its \a path.
* \see svgSymbolNameToPath()
*/
static QString svgSymbolPathToName( const QString &path, const QgsPathResolver &pathResolver );

//! Calculate the centroid point of a QPolygonF
static QPointF polygonCentroid( const QPolygonF &points );
Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/testqgsproject.cpp
Expand Up @@ -212,6 +212,7 @@ void TestQgsProject::testPathResolverSvg()
QString layerPath = dataDir + "/points.shp";

QVERIFY( QgsSymbolLayerUtils::svgSymbolNameToPath( QString(), QgsPathResolver() ).isEmpty() );
QVERIFY( QgsSymbolLayerUtils::svgSymbolPathToName( QString(), QgsPathResolver() ).isEmpty() );

// build a project with 3 layers, each having a simple renderer with SVG marker
// - existing SVG file in project dir
Expand All @@ -235,6 +236,7 @@ void TestQgsProject::testPathResolverSvg()
QVERIFY( QFileInfo::exists( ourSvgPath ) ); // should exist now

QString librarySvgPath = QgsSymbolLayerUtils::svgSymbolNameToPath( QStringLiteral( "transport/transport_airport.svg" ), QgsPathResolver() );
QCOMPARE( QgsSymbolLayerUtils::svgSymbolPathToName( librarySvgPath, QgsPathResolver() ), QStringLiteral( "transport/transport_airport.svg" ) );

QgsVectorLayer *layer1 = new QgsVectorLayer( layerPath, QStringLiteral( "points 1" ), QStringLiteral( "ogr" ) );
_useRendererWithSvgSymbol( layer1, ourSvgPath );
Expand Down

0 comments on commit ca553ea

Please sign in to comment.