Skip to content

Commit

Permalink
Store svg fill pathes relativ to default svg directory. Fixes bug #3154
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14463 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Oct 31, 2010
1 parent afd377c commit 5442c19
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 2 deletions.
110 changes: 110 additions & 0 deletions src/core/qgsapplication.cpp
Expand Up @@ -482,3 +482,113 @@ void QgsApplication::registerOgrDrivers()
OGRRegisterAll();
}
}

QString QgsApplication::absolutePathToRelativePath( const QString& apath, const QString& targetPath )
{
#if defined( Q_OS_WIN )
const Qt::CaseSensitivity cs = Qt::CaseInsensitive;

aPath.replace( "\\", "/" );
if ( aPath.startsWith( "//" ) )
{
// keep UNC prefix
aPath = "\\\\" + aPath.mid( 2 );
}

targetPath.replace( "\\", "/" );
if ( targetPath.startsWith( "//" ) )
{
// keep UNC prefix
targetPath = "\\\\" + targetPath.mid( 2 );
}
#else
const Qt::CaseSensitivity cs = Qt::CaseSensitive;
#endif

QStringList targetElems = targetPath.split( "/", QString::SkipEmptyParts );
QStringList aPathElems = apath.split( "/", QString::SkipEmptyParts );

targetElems.removeAll( "." );
aPathElems.removeAll( "." );

// remove common part
int n = 0;
while ( aPathElems.size() > 0 &&
targetElems.size() > 0 &&
aPathElems[0].compare( targetElems[0], cs ) == 0 )
{
aPathElems.removeFirst();
targetElems.removeFirst();
n++;
}

if ( n == 0 )
{
// no common parts; might not even by a file
return apath;
}

if ( targetElems.size() > 0 )
{
// go up to the common directory
for ( int i = 0; i < targetElems.size(); i++ )
{
aPathElems.insert( 0, ".." );
}
}
else
{
// let it start with . nevertheless,
// so relative path always start with either ./ or ../
aPathElems.insert( 0, "." );
}

return aPathElems.join( "/" );
}

QString QgsApplication::relativePathToAbsolutePath( const QString& rpath, const QString& targetPath )
{
// relative path should always start with ./ or ../
if ( !rpath.startsWith( "./" ) && !rpath.startsWith( "../" ) )
{
return rpath;
}

#if defined(Q_OS_WIN)
rPath.replace( "\\", "/" );
targetPath.replace( "\\", "/" );

bool uncPath = targetPath.startsWith( "//" );
#endif

QStringList srcElems = rpath.split( "/", QString::SkipEmptyParts );
QStringList targetElems = targetPath.split( "/", QString::SkipEmptyParts );

#if defined(Q_OS_WIN)
if ( uncPath )
{
targetElems.insert( 0, "" );
targetElems.insert( 0, "" );
}
#endif

// append source path elements
targetElems << srcElems;
targetElems.removeAll( "." );

// resolve ..
int pos;
while (( pos = targetElems.indexOf( ".." ) ) > 0 )
{
// remove preceding element and ..
targetElems.removeAt( pos - 1 );
targetElems.removeAt( pos - 1 );
}

#if !defined(Q_OS_WIN)
// make path absolute
targetElems.prepend( "" );
#endif

return targetElems.join( "/" );
}
7 changes: 7 additions & 0 deletions src/core/qgsapplication.h
Expand Up @@ -192,6 +192,13 @@ class CORE_EXPORT QgsApplication: public QApplication
*/
static void registerOgrDrivers();

/**Converts absolute path to path relative to target
@note: this method was added in version 1.6*/
static QString absolutePathToRelativePath( const QString& apath, const QString& targetPath );
/**Converts path relative to target to an absolute path
@note: this method was added in version 1.6*/
static QString relativePathToAbsolutePath( const QString& rpath, const QString& targetPath );

private:
static QString mPrefixPath;
static QString mPluginPath;
Expand Down
5 changes: 3 additions & 2 deletions src/core/symbology-ng/qgsfillsymbollayerv2.cpp
@@ -1,6 +1,7 @@
#include "qgsfillsymbollayerv2.h"
#include "qgssymbollayerv2utils.h"

#include "qgsapplication.h"
#include "qgsrendercontext.h"
#include "qgsproject.h"

Expand Down Expand Up @@ -158,7 +159,7 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::create( const QgsStringMap& properties
}
if ( properties.contains( "svgFile" ) )
{
svgFilePath = QgsProject::instance()->readPath( properties["svgFile"] );
svgFilePath = QgsApplication::relativePathToAbsolutePath( properties["svgFile"], QgsApplication::svgPath() );
}

if ( !svgFilePath.isEmpty() )
Expand Down Expand Up @@ -264,7 +265,7 @@ QgsStringMap QgsSVGFillSymbolLayer::properties() const
QgsStringMap map;
if ( !mSvgFilePath.isEmpty() )
{
map.insert( "svgFile", QgsProject::instance()->writePath( mSvgFilePath ) );
map.insert( "svgFile", QgsApplication::absolutePathToRelativePath( mSvgFilePath, QgsApplication::svgPath() ) );
}
else
{
Expand Down

0 comments on commit 5442c19

Please sign in to comment.