5
5
#include " qgsrendercontext.h"
6
6
#include " qgsapplication.h"
7
7
#include " qgslogger.h"
8
+ #include " qgsproject.h"
8
9
9
10
#include < QPainter>
10
11
#include < QSvgRenderer>
12
+ #include < QFileInfo>
13
+ #include < QDir>
11
14
12
15
#include < cmath>
13
16
@@ -105,11 +108,11 @@ void QgsSimpleMarkerSymbolLayerV2::startRender( QgsRenderContext& context )
105
108
<< QPointF ( -half, -sixth )
106
109
<< QPointF ( -sixth, 0 )
107
110
<< QPointF ( -half, half )
108
- << QPointF ( 0 , +sixth )
111
+ << QPointF ( 0 , + sixth )
109
112
<< QPointF ( half, half )
110
- << QPointF ( +sixth, 0 )
113
+ << QPointF ( + sixth, 0 )
111
114
<< QPointF ( half, -sixth )
112
- << QPointF ( +sixth, -sixth );
115
+ << QPointF ( + sixth, -sixth );
113
116
}
114
117
else if ( mName == " regular_star" )
115
118
{
@@ -248,7 +251,7 @@ void QgsSimpleMarkerSymbolLayerV2::drawMarker( QPainter* p )
248
251
249
252
QgsSvgMarkerSymbolLayerV2::QgsSvgMarkerSymbolLayerV2 ( QString name, double size, double angle )
250
253
{
251
- mName = name;
254
+ mPath = symbolNameToPath ( name ) ;
252
255
mSize = size;
253
256
mAngle = angle;
254
257
mOffset = QPointF ( 0 , 0 );
@@ -282,11 +285,8 @@ QString QgsSvgMarkerSymbolLayerV2::layerType() const
282
285
283
286
void QgsSvgMarkerSymbolLayerV2::startRender ( QgsRenderContext& context )
284
287
{
285
- QString svgPath = QgsApplication::svgPath ();
286
- QString file = svgPath + " /" + mName ;
287
-
288
288
QRectF rect ( QPointF ( -mSize / 2.0 , -mSize / 2.0 ), QSizeF ( mSize , mSize ) );
289
- QSvgRenderer renderer ( file );
289
+ QSvgRenderer renderer ( mPath );
290
290
QPainter painter ( &mPicture );
291
291
renderer.render ( &painter, rect );
292
292
}
@@ -317,7 +317,7 @@ void QgsSvgMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsRenderCont
317
317
QgsStringMap QgsSvgMarkerSymbolLayerV2::properties () const
318
318
{
319
319
QgsStringMap map;
320
- map[" name" ] = mName ;
320
+ map[" name" ] = symbolPathToName ( mPath ) ;
321
321
map[" size" ] = QString::number ( mSize );
322
322
map[" angle" ] = QString::number ( mAngle );
323
323
map[" offset" ] = QgsSymbolLayerV2Utils::encodePoint ( mOffset );
@@ -326,7 +326,105 @@ QgsStringMap QgsSvgMarkerSymbolLayerV2::properties() const
326
326
327
327
QgsSymbolLayerV2* QgsSvgMarkerSymbolLayerV2::clone () const
328
328
{
329
- QgsSvgMarkerSymbolLayerV2* m = new QgsSvgMarkerSymbolLayerV2 ( mName , mSize , mAngle );
329
+ QgsSvgMarkerSymbolLayerV2* m = new QgsSvgMarkerSymbolLayerV2 ( mPath , mSize , mAngle );
330
330
m->setOffset ( mOffset );
331
331
return m;
332
332
}
333
+
334
+
335
+ QStringList QgsSvgMarkerSymbolLayerV2::listSvgFiles ()
336
+ {
337
+ // copied from QgsMarkerCatalogue - TODO: unify
338
+ QStringList list;
339
+ QStringList svgPaths = QgsApplication::svgPaths ();
340
+
341
+ for ( int i = 0 ; i < svgPaths.size (); i++ )
342
+ {
343
+ QDir dir ( svgPaths[i] );
344
+ foreach ( QString item, dir.entryList ( QDir::Dirs | QDir::NoDotAndDotDot ) )
345
+ {
346
+ svgPaths.insert ( i + 1 , dir.path () + " /" + item );
347
+ }
348
+
349
+ foreach ( QString item, dir.entryList ( QStringList ( " *.svg" ), QDir::Files ) )
350
+ {
351
+ // TODO test if it is correct SVG
352
+ list.append ( dir.path () + " /" + item );
353
+ }
354
+ }
355
+ return list;
356
+ }
357
+
358
+ QString QgsSvgMarkerSymbolLayerV2::symbolNameToPath ( QString name )
359
+ {
360
+ // copied from QgsSymbol::setNamedPointSymbol - TODO: unify
361
+
362
+ // we might have a full path...
363
+ if ( QFile ( name ).exists () )
364
+ return QFileInfo ( name ).canonicalFilePath ();
365
+
366
+ // SVG symbol not found - probably a relative path was used
367
+
368
+ QStringList svgPaths = QgsApplication::svgPaths ();
369
+ for ( int i = 0 ; i < svgPaths.size (); i++ )
370
+ {
371
+ QgsDebugMsg ( " SvgPath: " + svgPaths[i] );
372
+ QFileInfo myInfo ( name );
373
+ QString myFileName = myInfo.fileName (); // foo.svg
374
+ QString myLowestDir = myInfo.dir ().dirName ();
375
+ QString myLocalPath = svgPaths[i] + " /" + myLowestDir + " /" + myFileName;
376
+
377
+ QgsDebugMsg ( " Alternative svg path: " + myLocalPath );
378
+ if ( QFile ( myLocalPath ).exists () )
379
+ {
380
+ QgsDebugMsg ( " Svg found in alternative path" );
381
+ return QFileInfo ( myLocalPath ).canonicalFilePath ();
382
+ }
383
+ else if ( myInfo.isRelative () )
384
+ {
385
+ QFileInfo pfi ( QgsProject::instance ()->fileName () );
386
+ QString alternatePath = pfi.canonicalPath () + QDir::separator () + name;
387
+ if ( pfi.exists () && QFile ( alternatePath ).exists () )
388
+ {
389
+ QgsDebugMsg ( " Svg found in alternative path" );
390
+ return QFileInfo ( alternatePath ).canonicalFilePath ();
391
+ }
392
+ else
393
+ {
394
+ QgsDebugMsg ( " Svg not found in project path" );
395
+ }
396
+ }
397
+ else
398
+ {
399
+ // couldnt find the file, no happy ending :-(
400
+ QgsDebugMsg ( " Computed alternate path but no svg there either" );
401
+ }
402
+ }
403
+ return QString ();
404
+ }
405
+
406
+ QString QgsSvgMarkerSymbolLayerV2::symbolPathToName ( QString path )
407
+ {
408
+ // copied from QgsSymbol::writeXML
409
+
410
+ QFileInfo fi ( path );
411
+ if ( !fi.exists () )
412
+ return path;
413
+
414
+ path = fi.canonicalFilePath ();
415
+
416
+ QStringList svgPaths = QgsApplication::svgPaths ();
417
+
418
+ for ( int i = 0 ; i < svgPaths.size (); i++ )
419
+ {
420
+ QString dir = QFileInfo ( svgPaths[i] ).canonicalFilePath ();
421
+
422
+ if ( !dir.isEmpty () && path.startsWith ( dir ) )
423
+ {
424
+ path = path.mid ( dir.size () );
425
+ break ;
426
+ }
427
+ }
428
+
429
+ return path;
430
+ }
0 commit comments