From dffae7962fc2dbcd540fc717daabc3acf74ba556 Mon Sep 17 00:00:00 2001 From: Giuseppe Sucameli Date: Fri, 30 Nov 2012 02:17:55 +0100 Subject: [PATCH] move svg utils functions to the symbollayerv2utils class --- .../symbology-ng/qgsmarkersymbollayerv2.sip | 12 -- .../symbology-ng/qgssymbollayerv2utils.sip | 12 ++ .../symbology-ng/qgsfillsymbollayerv2.cpp | 4 +- .../symbology-ng/qgsmarkersymbollayerv2.cpp | 156 +----------------- .../symbology-ng/qgsmarkersymbollayerv2.h | 12 -- .../symbology-ng/qgssymbollayerv2utils.cpp | 155 +++++++++++++++++ src/core/symbology-ng/qgssymbollayerv2utils.h | 12 ++ .../symbology-ng/qgssymbollayerv2widget.cpp | 5 +- 8 files changed, 186 insertions(+), 182 deletions(-) diff --git a/python/core/symbology-ng/qgsmarkersymbollayerv2.sip b/python/core/symbology-ng/qgsmarkersymbollayerv2.sip index b1db3dc30a9..bbe774d904b 100644 --- a/python/core/symbology-ng/qgsmarkersymbollayerv2.sip +++ b/python/core/symbology-ng/qgsmarkersymbollayerv2.sip @@ -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; diff --git a/python/core/symbology-ng/qgssymbollayerv2utils.sip b/python/core/symbology-ng/qgssymbollayerv2utils.sip index 2de2852d91a..44e41feabd4 100644 --- a/python/core/symbology-ng/qgssymbollayerv2utils.sip +++ b/python/core/symbology-ng/qgssymbollayerv2utils.sip @@ -181,4 +181,16 @@ class QgsSymbolLayerV2Utils static void sortVariantList( QList& 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 ); }; diff --git a/src/core/symbology-ng/qgsfillsymbollayerv2.cpp b/src/core/symbology-ng/qgsfillsymbollayerv2.cpp index 98d8ae69988..ddbcebe3249 100644 --- a/src/core/symbology-ng/qgsfillsymbollayerv2.cpp +++ b/src/core/symbology-ng/qgsfillsymbollayerv2.cpp @@ -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 { diff --git a/src/core/symbology-ng/qgsmarkersymbollayerv2.cpp b/src/core/symbology-ng/qgsmarkersymbollayerv2.cpp index 47b1dad8ca4..b431443f096 100644 --- a/src/core/symbology-ng/qgsmarkersymbollayerv2.cpp +++ b/src/core/symbology-ng/qgsmarkersymbollayerv2.cpp @@ -17,9 +17,7 @@ #include "qgssymbollayerv2utils.h" #include "qgsrendercontext.h" -#include "qgsapplication.h" #include "qgslogger.h" -#include "qgsproject.h" #include "qgssvgcache.h" #include @@ -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 ) diff --git a/src/core/symbology-ng/qgsmarkersymbollayerv2.h b/src/core/symbology-ng/qgsmarkersymbollayerv2.h index 0174a18f485..f5f87542495 100644 --- a/src/core/symbology-ng/qgsmarkersymbollayerv2.h +++ b/src/core/symbology-ng/qgsmarkersymbollayerv2.h @@ -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; diff --git a/src/core/symbology-ng/qgssymbollayerv2utils.cpp b/src/core/symbology-ng/qgssymbollayerv2utils.cpp index 18c3b30b0ff..59fb5969609 100644 --- a/src/core/symbology-ng/qgssymbollayerv2utils.cpp +++ b/src/core/symbology-ng/qgssymbollayerv2utils.cpp @@ -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 ); +} + diff --git a/src/core/symbology-ng/qgssymbollayerv2utils.h b/src/core/symbology-ng/qgssymbollayerv2utils.h index 1b80f8e1784..ade884985f1 100644 --- a/src/core/symbology-ng/qgssymbollayerv2utils.h +++ b/src/core/symbology-ng/qgssymbollayerv2utils.h @@ -216,6 +216,18 @@ class CORE_EXPORT QgsSymbolLayerV2Utils static void sortVariantList( QList& 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; diff --git a/src/gui/symbology-ng/qgssymbollayerv2widget.cpp b/src/gui/symbology-ng/qgssymbollayerv2widget.cpp index 00502f1bfb6..e91caed3ddf 100644 --- a/src/gui/symbology-ng/qgssymbollayerv2widget.cpp +++ b/src/gui/symbology-ng/qgssymbollayerv2widget.cpp @@ -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