66#include " qgsapplication.h"
77#include " qgslogger.h"
88#include " qgsproject.h"
9+ #include " qgssvgcache.h"
910
1011#include < QPainter>
1112#include < QSvgRenderer>
@@ -449,6 +450,9 @@ QgsSvgMarkerSymbolLayerV2::QgsSvgMarkerSymbolLayerV2( QString name, double size,
449450 mSize = size;
450451 mAngle = angle;
451452 mOffset = QPointF ( 0 , 0 );
453+ mOutlineWidth = 1.0 ;
454+ mFillColor = QColor ( Qt::black );
455+ mOutlineColor = QColor ( Qt::black );
452456}
453457
454458
@@ -466,11 +470,60 @@ QgsSymbolLayerV2* QgsSvgMarkerSymbolLayerV2::create( const QgsStringMap& props )
466470 angle = props[" angle" ].toDouble ();
467471
468472 QgsSvgMarkerSymbolLayerV2* m = new QgsSvgMarkerSymbolLayerV2 ( name, size, angle );
473+
474+ // we only check the svg default parameters if necessary, since it could be expensive
475+ if ( !props.contains (" fill" ) && !props.contains (" outline" ) && !props.contains (" outline-width" ) )
476+ {
477+ QColor fillColor, outlineColor;
478+ double outlineWidth;
479+ bool hasFillParam, hasOutlineParam, hasOutlineWidthParam;
480+ QgsSvgCache::instance ()->containsParams ( name, hasFillParam, fillColor, hasOutlineParam, outlineColor, hasOutlineWidthParam, outlineWidth );
481+ if ( hasFillParam )
482+ {
483+ m->setFillColor ( fillColor );
484+ }
485+ if ( hasOutlineParam )
486+ {
487+ m->setOutlineColor ( outlineColor );
488+ }
489+ if ( hasOutlineWidthParam )
490+ {
491+ m->setOutlineWidth ( outlineWidth );
492+ }
493+ }
494+
469495 if ( props.contains ( " offset" ) )
470496 m->setOffset ( QgsSymbolLayerV2Utils::decodePoint ( props[" offset" ] ) );
497+ if ( props.contains ( " fill" ) )
498+ m->setFillColor ( QColor ( props[" fill" ] ) );
499+ if ( props.contains ( " outline" ) )
500+ m->setOutlineColor ( QColor ( props[" outline" ] ) );
501+ if ( props.contains ( " outline-width" ) )
502+ m->setOutlineWidth ( props[" outline-width" ].toDouble () );
471503 return m;
472504}
473505
506+ void QgsSvgMarkerSymbolLayerV2::setPath ( QString path )
507+ {
508+ mPath = path;
509+ QColor fillColor, outlineColor;
510+ double outlineWidth;
511+ bool hasFillParam, hasOutlineParam, hasOutlineWidthParam;
512+ QgsSvgCache::instance ()->containsParams ( path, hasFillParam, fillColor, hasOutlineParam, outlineColor, hasOutlineWidthParam, outlineWidth );
513+ if ( hasFillParam )
514+ {
515+ setFillColor ( fillColor );
516+ }
517+ if ( hasOutlineParam )
518+ {
519+ setOutlineColor ( outlineColor );
520+ }
521+ if ( hasOutlineWidthParam )
522+ {
523+ setOutlineWidth ( outlineWidth );
524+ }
525+ }
526+
474527
475528QString QgsSvgMarkerSymbolLayerV2::layerType () const
476529{
@@ -479,30 +532,8 @@ QString QgsSvgMarkerSymbolLayerV2::layerType() const
479532
480533void QgsSvgMarkerSymbolLayerV2::startRender ( QgsSymbolV2RenderContext& context )
481534{
482- double pictureSize = 0 ;
483- QgsRenderContext& rc = context.renderContext ();
484-
485- if ( rc.painter () && rc.painter ()->device () )
486- {
487- // correct QPictures DPI correction
488- pictureSize = context.outputLineWidth ( mSize ) / rc.painter ()->device ()->logicalDpiX () * mPicture .logicalDpiX ();
489- }
490- else
491- {
492- pictureSize = context.outputLineWidth ( mSize );
493- }
494- QRectF rect ( QPointF ( -pictureSize / 2.0 , -pictureSize / 2.0 ), QSizeF ( pictureSize, pictureSize ) );
495- QSvgRenderer renderer ( mPath );
496- QPainter painter ( &mPicture );
497- renderer.render ( &painter, rect );
498- QPainter selPainter ( &mSelPicture );
499- selPainter.setRenderHint ( QPainter::Antialiasing );
500- selPainter.setBrush ( QBrush ( context.selectionColor () ) );
501- selPainter.setPen ( Qt::NoPen );
502- selPainter.drawEllipse ( QPointF ( 0 , 0 ), pictureSize*0.6 , pictureSize*0.6 );
503- renderer.render ( &selPainter, rect );
504-
505535 mOrigSize = mSize ; // save in case the size would be data defined
536+ Q_UNUSED ( context );
506537}
507538
508539void QgsSvgMarkerSymbolLayerV2::stopRender ( QgsSymbolV2RenderContext& context )
@@ -533,8 +564,28 @@ void QgsSvgMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Re
533564 if ( mAngle != 0 )
534565 p->rotate ( mAngle );
535566
536- QPicture &pct = context.selected () ? mSelPicture : mPicture ;
537- p->drawPicture ( 0 , 0 , pct );
567+ if ( doubleNear ( context.renderContext ().rasterScaleFactor (), 1.0 , 0.1 ) )
568+ {
569+ const QImage& img = QgsSvgCache::instance ()->svgAsImage ( mPath , mSize , mFillColor , mOutlineColor , mOutlineWidth ,
570+ context.renderContext ().scaleFactor (), context.renderContext ().rasterScaleFactor () );
571+ p->drawImage ( -img.width () / 2.0 , -img.width () / 2.0 , img );
572+ }
573+ else
574+ {
575+ const QPicture& pct = QgsSvgCache::instance ()->svgAsPicture ( mPath , mSize , mFillColor , mOutlineColor , mOutlineWidth ,
576+ context.renderContext ().scaleFactor (), context.renderContext ().rasterScaleFactor () );
577+ p->drawPicture ( 0 , 0 , pct );
578+ }
579+
580+ if ( context.selected () )
581+ {
582+ QPen pen ( context.selectionColor () );
583+ pen.setWidth ( context.outputLineWidth ( 1.0 ) );
584+ p->setPen ( pen );
585+ p->setBrush ( Qt::NoBrush );
586+ double sizePixel = context.outputLineWidth ( mSize );
587+ p->drawRect ( QRectF ( -sizePixel / 2.0 , -sizePixel / 2.0 , sizePixel, sizePixel ) );
588+ }
538589
539590 p->restore ();
540591}
@@ -547,12 +598,18 @@ QgsStringMap QgsSvgMarkerSymbolLayerV2::properties() const
547598 map[" size" ] = QString::number ( mSize );
548599 map[" angle" ] = QString::number ( mAngle );
549600 map[" offset" ] = QgsSymbolLayerV2Utils::encodePoint ( mOffset );
601+ map[" fill" ] = mFillColor .name ();
602+ map[" outline" ] = mOutlineColor .name ();
603+ map[" outline-width" ] = QString::number ( mOutlineWidth );
550604 return map;
551605}
552606
553607QgsSymbolLayerV2* QgsSvgMarkerSymbolLayerV2::clone () const
554608{
555609 QgsSvgMarkerSymbolLayerV2* m = new QgsSvgMarkerSymbolLayerV2 ( mPath , mSize , mAngle );
610+ m->setFillColor ( mFillColor );
611+ m->setOutlineColor ( mOutlineColor );
612+ m->setOutlineWidth ( mOutlineWidth );
556613 m->setOffset ( mOffset );
557614 return m;
558615}
0 commit comments