155 changes: 155 additions & 0 deletions src/core/symbology-ng/qgssymbollayerv2utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
#include "qgssymbolv2.h"
#include "qgsvectorcolorrampv2.h"
#include "qgsexpression.h"
#include "qgsapplication.h"
#include "qgsproject.h"


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

Expand Down Expand Up @@ -2584,3 +2589,153 @@ QPointF QgsSymbolLayerV2Utils::pointOnLineWithDistance( const QPointF& startPoin
double scaleFactor = distance / length;
return QPointF( startPoint.x() + dx * scaleFactor, startPoint.y() + dy * scaleFactor );
}


QStringList QgsSymbolLayerV2Utils::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 QgsSymbolLayerV2Utils::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 QgsSymbolLayerV2Utils::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 QgsSymbolLayerV2Utils::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 );
}

12 changes: 12 additions & 0 deletions src/core/symbology-ng/qgssymbollayerv2utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ class CORE_EXPORT 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 );
};

class QPolygonF;
Expand Down
5 changes: 3 additions & 2 deletions src/gui/symbology-ng/qgssymbollayerv2widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qgsdashspacedialog.h"
#include "qgssymbolv2selectordialog.h"
#include "qgssvgcache.h"
#include "qgssymbollayerv2utils.h"

#include "qgsstylev2.h" //for symbol selector dialog

Expand Down Expand Up @@ -527,13 +528,13 @@ class QgsSvgListModel : public QAbstractListModel
public:
QgsSvgListModel( QObject* parent ) : QAbstractListModel( parent )
{
mSvgFiles = QgsSvgMarkerSymbolLayerV2::listSvgFiles();
mSvgFiles = QgsSymbolLayerV2Utils::listSvgFiles();
}

// Constructor to create model for icons in a specific path
QgsSvgListModel( QObject* parent, QString path ) : QAbstractListModel( parent )
{
mSvgFiles = QgsSvgMarkerSymbolLayerV2::listSvgFilesAt( path );
mSvgFiles = QgsSymbolLayerV2Utils::listSvgFilesAt( path );
}

int rowCount( const QModelIndex & parent = QModelIndex() ) const
Expand Down