Skip to content
Permalink
Browse files

move svg utils functions to the symbollayerv2utils class

  • Loading branch information
brushtyler committed Dec 2, 2012
1 parent e81b044 commit dffae7962fc2dbcd540fc717daabc3acf74ba556
@@ -65,18 +65,6 @@ class QgsSvgMarkerSymbolLayerV2 : QgsMarkerSymbolLayerV2
static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() ) /Factory/;
static QgsSymbolLayerV2* createFromSld( QDomElement &element ) /Factory/;

//! Return a list of all available svg files
static QStringList listSvgFiles();

//! Return a list of svg files at the specified directory
static QStringList listSvgFilesAt( QString directory );

//! Get symbol's path from its name
static QString symbolNameToPath( QString name );

//! Get symbols's name from its path
static QString symbolPathToName( QString path );

// implemented from base classes

QString layerType() const;
@@ -181,4 +181,16 @@ class QgsSymbolLayerV2Utils
static void sortVariantList( QList<QVariant>& list, Qt::SortOrder order );
/**Returns a point on the line from startPoint to directionPoint that is a certain distance away from the starting point*/
static QPointF pointOnLineWithDistance( const QPointF& startPoint, const QPointF& directionPoint, double distance );

//! Return a list of all available svg files
static QStringList listSvgFiles();

//! Return a list of svg files at the specified directory
static QStringList listSvgFilesAt( QString directory );

//! Get symbol's path from its name
static QString symbolNameToPath( QString name );

//! Get symbols's name from its path
static QString symbolPathToName( QString path );
};
@@ -330,7 +330,7 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::create( const QgsStringMap& properties
if ( properties.contains( "svgFile" ) )
{
QString svgName = properties["svgFile"];
QString savePath = QgsSvgMarkerSymbolLayerV2::symbolNameToPath( svgName );
QString savePath = QgsSymbolLayerV2Utils::symbolNameToPath( svgName );
svgFilePath = ( savePath.isEmpty() ? svgName : savePath );
}
if ( properties.contains( "angle" ) )
@@ -418,7 +418,7 @@ QgsStringMap QgsSVGFillSymbolLayer::properties() const
QgsStringMap map;
if ( !mSvgFilePath.isEmpty() )
{
map.insert( "svgFile", QgsSvgMarkerSymbolLayerV2::symbolPathToName( mSvgFilePath ) );
map.insert( "svgFile", QgsSymbolLayerV2Utils::symbolPathToName( mSvgFilePath ) );
}
else
{
@@ -17,9 +17,7 @@
#include "qgssymbollayerv2utils.h"

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

#include <QPainter>
@@ -536,7 +534,7 @@ void QgsSimpleMarkerSymbolLayerV2::drawMarker( QPainter* p, QgsSymbolV2RenderCon

QgsSvgMarkerSymbolLayerV2::QgsSvgMarkerSymbolLayerV2( QString name, double size, double angle )
{
mPath = symbolNameToPath( name );
mPath = QgsSymbolLayerV2Utils::symbolNameToPath( name );
mSize = size;
mAngle = angle;
mOffset = QPointF( 0, 0 );
@@ -697,7 +695,7 @@ void QgsSvgMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Re
QgsStringMap QgsSvgMarkerSymbolLayerV2::properties() const
{
QgsStringMap map;
map["name"] = symbolPathToName( mPath );
map["name"] = QgsSymbolLayerV2Utils::symbolPathToName( mPath );
map["size"] = QString::number( mSize );
map["angle"] = QString::number( mAngle );
map["offset"] = QgsSymbolLayerV2Utils::encodePoint( mOffset );
@@ -784,156 +782,6 @@ QgsSymbolLayerV2* QgsSvgMarkerSymbolLayerV2::createFromSld( QDomElement &element
return m;
}


QStringList QgsSvgMarkerSymbolLayerV2::listSvgFiles()
{
// copied from QgsMarkerCatalogue - TODO: unify
QStringList list;
QStringList svgPaths = QgsApplication::svgPaths();

for ( int i = 0; i < svgPaths.size(); i++ )
{
QDir dir( svgPaths[i] );
foreach ( QString item, dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot ) )
{
svgPaths.insert( i + 1, dir.path() + "/" + item );
}

foreach ( QString item, dir.entryList( QStringList( "*.svg" ), QDir::Files ) )
{
// TODO test if it is correct SVG
list.append( dir.path() + "/" + item );
}
}
return list;
}

// Stripped down version of listSvgFiles() for specified directory
QStringList QgsSvgMarkerSymbolLayerV2::listSvgFilesAt( QString directory )
{
// TODO anything that applies for the listSvgFiles() applies this also

QStringList list;
QStringList svgPaths;
svgPaths.append( directory );

for ( int i = 0; i < svgPaths.size(); i++ )
{
QDir dir( svgPaths[i] );
foreach ( QString item, dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot ) )
{
svgPaths.insert( i + 1, dir.path() + "/" + item );
}

foreach ( QString item, dir.entryList( QStringList( "*.svg" ), QDir::Files ) )
{
list.append( dir.path() + "/" + item );
}
}
return list;

}

QString QgsSvgMarkerSymbolLayerV2::symbolNameToPath( QString name )
{
// copied from QgsSymbol::setNamedPointSymbol - TODO: unify

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

// or it might be an url...
QUrl url( name );
if ( url.isValid() && !url.scheme().isEmpty() )
{
if ( url.scheme().compare( "file", Qt::CaseInsensitive ) == 0 )
{
// it's a url to a local file
name = url.toLocalFile();
if ( QFile( name ).exists() )
{
return QFileInfo( name ).canonicalFilePath();
}
}
else
{
// it's a url pointing to a online resource
return name;
}
}

// SVG symbol not found - probably a relative path was used

QStringList svgPaths = QgsApplication::svgPaths();
for ( int i = 0; i < svgPaths.size(); i++ )
{
QgsDebugMsg( "SvgPath: " + svgPaths[i] );
QFileInfo myInfo( name );
QString myFileName = myInfo.fileName(); // foo.svg
QString myLowestDir = myInfo.dir().dirName();
QString myLocalPath = svgPaths[i] + "/" + myLowestDir + "/" + myFileName;

QgsDebugMsg( "Alternative svg path: " + myLocalPath );
if ( QFile( myLocalPath ).exists() )
{
QgsDebugMsg( "Svg found in alternative path" );
return QFileInfo( myLocalPath ).canonicalFilePath();
}
else if ( myInfo.isRelative() )
{
QFileInfo pfi( QgsProject::instance()->fileName() );
QString alternatePath = pfi.canonicalPath() + QDir::separator() + name;
if ( pfi.exists() && QFile( alternatePath ).exists() )
{
QgsDebugMsg( "Svg found in alternative path" );
return QFileInfo( alternatePath ).canonicalFilePath();
}
else
{
QgsDebugMsg( "Svg not found in project path" );
}
}
else
{
//couldnt find the file, no happy ending :-(
QgsDebugMsg( "Computed alternate path but no svg there either" );
}
}
return QString();
}

QString QgsSvgMarkerSymbolLayerV2::symbolPathToName( QString path )
{
// copied from QgsSymbol::writeXML

QFileInfo fi( path );
if ( !fi.exists() )
return path;

path = fi.canonicalFilePath();

QStringList svgPaths = QgsApplication::svgPaths();

bool isInSvgPathes = false;
for ( int i = 0; i < svgPaths.size(); i++ )
{
QString dir = QFileInfo( svgPaths[i] ).canonicalFilePath();

if ( !dir.isEmpty() && path.startsWith( dir ) )
{
path = path.mid( dir.size() );
isInSvgPathes = true;
break;
}
}

if ( isInSvgPathes )
return path;

return QgsProject::instance()->writePath( path );
}


//////////

QgsFontMarkerSymbolLayerV2::QgsFontMarkerSymbolLayerV2( QString fontFamily, QChar chr, double pointSize, QColor color, double angle )
@@ -108,18 +108,6 @@ class CORE_EXPORT QgsSvgMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() );
static QgsSymbolLayerV2* createFromSld( QDomElement &element );

//! Return a list of all available svg files
static QStringList listSvgFiles();

//! Return a list of svg files at the specified directory
static QStringList listSvgFilesAt( QString directory );

//! Get symbol's path from its name
static QString symbolNameToPath( QString name );

//! Get symbols's name from its path
static QString symbolPathToName( QString path );

// implemented from base classes

QString layerType() const;

0 comments on commit dffae79

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