move svg utils functions to the symbollayerv2utils class

This commit is contained in:
Giuseppe Sucameli 2012-11-30 02:17:55 +01:00
parent e81b044889
commit dffae7962f
8 changed files with 186 additions and 182 deletions

View File

@ -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;

View File

@ -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 );
};

View File

@ -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
{

View File

@ -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 )

View File

@ -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;

View File

@ -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"
@ -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 );
}

View File

@ -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;

View File

@ -24,6 +24,7 @@
#include "qgsdashspacedialog.h"
#include "qgssymbolv2selectordialog.h"
#include "qgssvgcache.h"
#include "qgssymbollayerv2utils.h"
#include "qgsstylev2.h" //for symbol selector dialog
@ -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