Skip to content

Commit b6fa94f

Browse files
committed
Merge branch 'svg_params'
2 parents eb9d610 + 4546646 commit b6fa94f

14 files changed

Lines changed: 938 additions & 61 deletions

images/svg/geometric/Square1.svg

Lines changed: 2 additions & 2 deletions
Loading

images/svg/geometric/Square2.svg

Lines changed: 3 additions & 3 deletions
Loading

images/svg/geometric/Triangle1.svg

Lines changed: 2 additions & 2 deletions
Loading

images/svg/transport/amenity=airport.svg

Lines changed: 1 addition & 1 deletion
Loading

images/svg/transport/amenity=ferry_terminal.svg

Lines changed: 2 additions & 2 deletions
Loading

images/svg/transport/amenity=taxi.svg

Lines changed: 9 additions & 9 deletions
Loading

src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ SET(QGIS_CORE_SRCS
3737
symbology-ng/qgsvectorcolorrampv2.cpp
3838
symbology-ng/qgsstylev2.cpp
3939
symbology-ng/qgssymbologyv2conversion.cpp
40+
symbology-ng/qgssvgcache.cpp
4041

4142
qgis.cpp
4243
qgsapplication.cpp

src/core/symbology-ng/qgsmarkersymbollayerv2.cpp

Lines changed: 82 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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

475528
QString QgsSvgMarkerSymbolLayerV2::layerType() const
476529
{
@@ -479,30 +532,8 @@ QString QgsSvgMarkerSymbolLayerV2::layerType() const
479532

480533
void 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

508539
void 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

553607
QgsSymbolLayerV2* 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
}

src/core/symbology-ng/qgsmarkersymbollayerv2.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,28 @@ class CORE_EXPORT QgsSvgMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
112112
QgsSymbolLayerV2* clone() const;
113113

114114
QString path() const { return mPath; }
115-
void setPath( QString path ) { mPath = path; }
115+
void setPath( QString path );
116+
117+
QColor fillColor() const { return mFillColor; }
118+
void setFillColor( const QColor& c ) { mFillColor = c; }
119+
120+
QColor outlineColor() const { return mOutlineColor; }
121+
void setOutlineColor( const QColor& c ) { mOutlineColor = c; }
122+
123+
double outlineWidth() const { return mOutlineWidth; }
124+
void setOutlineWidth( double w ) { mOutlineWidth = w; }
116125

117126
protected:
118127

119128
void loadSvg();
120129

121130
QString mPath;
122-
QPicture mPicture;
123-
QPicture mSelPicture;
131+
132+
//param(fill), param(outline), param(outline-width) are going
133+
//to be replaced in memory
134+
QColor mFillColor;
135+
QColor mOutlineColor;
136+
double mOutlineWidth;
124137
double mOrigSize;
125138
};
126139

0 commit comments

Comments
 (0)