Skip to content

Commit af71f5f

Browse files
committed
Consider svg parameters for svg fill
1 parent dd60292 commit af71f5f

File tree

5 files changed

+190
-27
lines changed

5 files changed

+190
-27
lines changed

src/core/symbology-ng/qgsfillsymbollayerv2.cpp

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ QgsSVGFillSymbolLayer::QgsSVGFillSymbolLayer( const QString& svgFilePath, double
206206
setSvgFilePath( svgFilePath );
207207
mOutlineWidth = 0.3;
208208
mAngle = angle;
209+
setDefaultSvgParams();
209210
}
210211

211212
QgsSVGFillSymbolLayer::QgsSVGFillSymbolLayer( const QByteArray& svgData, double width, double angle ): QgsImageFillSymbolLayer(), mPatternWidth( width ),
@@ -215,6 +216,7 @@ QgsSVGFillSymbolLayer::QgsSVGFillSymbolLayer( const QByteArray& svgData, double
215216
mOutlineWidth = 0.3;
216217
mAngle = angle;
217218
setSubSymbol( new QgsLineSymbolV2() );
219+
setDefaultSvgParams();
218220
}
219221

220222
QgsSVGFillSymbolLayer::~QgsSVGFillSymbolLayer()
@@ -228,9 +230,11 @@ void QgsSVGFillSymbolLayer::setSvgFilePath( const QString& svgPath )
228230
if ( svgFile.open( QFile::ReadOnly ) )
229231
{
230232
mSvgData = svgFile.readAll();
233+
231234
storeViewBox();
232235
}
233236
mSvgFilePath = svgPath;
237+
setDefaultSvgParams();
234238
}
235239

236240
QgsSymbolLayerV2* QgsSVGFillSymbolLayer::create( const QgsStringMap& properties )
@@ -240,7 +244,6 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::create( const QgsStringMap& properties
240244
QString svgFilePath;
241245
double angle = 0.0;
242246

243-
244247
if ( properties.contains( "width" ) )
245248
{
246249
width = properties["width"].toDouble();
@@ -256,19 +259,36 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::create( const QgsStringMap& properties
256259
angle = properties["angle"].toDouble();
257260
}
258261

262+
QgsSVGFillSymbolLayer* symbolLayer = 0;
259263
if ( !svgFilePath.isEmpty() )
260264
{
261-
return new QgsSVGFillSymbolLayer( svgFilePath, width, angle );
265+
symbolLayer = new QgsSVGFillSymbolLayer( svgFilePath, width, angle );
262266
}
263267
else
264268
{
265269
if ( properties.contains( "data" ) )
266270
{
267271
data = QByteArray::fromHex( properties["data"].toLocal8Bit() );
268272
}
273+
symbolLayer = new QgsSVGFillSymbolLayer( data, width, angle );
274+
}
269275

270-
return new QgsSVGFillSymbolLayer( data, width, angle );
276+
//svg parameters
277+
if ( properties.contains( "svgFillColor" ) )
278+
{
279+
symbolLayer->setSvgFillColor( QColor( properties["svgFillColor"] ) );
280+
}
281+
if ( properties.contains( "svgOutlineColor" ) )
282+
{
283+
symbolLayer->setSvgOutlineColor( QColor( properties["svgOutlineColor"] ) );
284+
}
285+
if ( properties.contains( "svgOutlineWidth" ) )
286+
{
287+
symbolLayer->setSvgOutlineWidth( properties["svgOutlineWidth"].toDouble() );
271288
}
289+
290+
291+
return symbolLayer;
272292
}
273293

274294
QString QgsSVGFillSymbolLayer::layerType() const
@@ -283,11 +303,8 @@ void QgsSVGFillSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
283303
return;
284304
}
285305

286-
QColor fillColor( Qt::black );
287-
QColor outlineColor( Qt::black );
288-
double outlineWidth = 1;
289306
int size = context.outputPixelSize( mPatternWidth );
290-
const QImage& patternImage = QgsSvgCache::instance()->svgAsImage( mSvgFilePath, size, fillColor, outlineColor, outlineWidth,
307+
const QImage& patternImage = QgsSvgCache::instance()->svgAsImage( mSvgFilePath, size, mSvgFillColor, mSvgOutlineColor, mSvgOutlineWidth,
291308
context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
292309
QTransform brushTransform;
293310
brushTransform.scale( 1.0 / context.renderContext().rasterScaleFactor(), 1.0 / context.renderContext().rasterScaleFactor() );
@@ -331,6 +348,12 @@ QgsStringMap QgsSVGFillSymbolLayer::properties() const
331348

332349
map.insert( "width", QString::number( mPatternWidth ) );
333350
map.insert( "angle", QString::number( mAngle ) );
351+
352+
//svg parameters
353+
map.insert( "svgFillColor", mSvgFillColor.name() );
354+
map.insert( "svgOutlineColor", mSvgOutlineColor.name() );
355+
map.insert( "svgOutlineWidth", QString::number( mSvgOutlineWidth ) );
356+
334357
return map;
335358
}
336359

@@ -340,6 +363,10 @@ QgsSymbolLayerV2* QgsSVGFillSymbolLayer::clone() const
340363
if ( !mSvgFilePath.isEmpty() )
341364
{
342365
clonedLayer = new QgsSVGFillSymbolLayer( mSvgFilePath, mPatternWidth, mAngle );
366+
QgsSVGFillSymbolLayer* sl = static_cast<QgsSVGFillSymbolLayer*>( clonedLayer );
367+
sl->setSvgFillColor( mSvgFillColor );
368+
sl->setSvgOutlineColor( mSvgOutlineColor );
369+
sl->setSvgOutlineWidth( mSvgOutlineWidth );
343370
}
344371
else
345372
{
@@ -369,6 +396,38 @@ void QgsSVGFillSymbolLayer::storeViewBox()
369396
return;
370397
}
371398

399+
void QgsSVGFillSymbolLayer::setDefaultSvgParams()
400+
{
401+
//default values
402+
mSvgFillColor = QColor( 0, 0, 0 );
403+
mSvgOutlineColor = QColor( 0, 0, 0 );
404+
mSvgOutlineWidth = 0.3;
405+
406+
if ( mSvgFilePath.isEmpty() )
407+
{
408+
return;
409+
}
410+
411+
bool hasFillParam, hasOutlineParam, hasOutlineWidthParam;
412+
QColor defaultFillColor, defaultOutlineColor;
413+
double defaultOutlineWidth;
414+
QgsSvgCache::instance()->containsParams( mSvgFilePath, hasFillParam, defaultFillColor, hasOutlineParam, defaultOutlineColor, hasOutlineWidthParam,
415+
defaultOutlineWidth );
416+
417+
if ( hasFillParam )
418+
{
419+
mSvgFillColor = defaultFillColor;
420+
}
421+
if ( hasOutlineParam )
422+
{
423+
mSvgOutlineColor = defaultOutlineColor;
424+
}
425+
if ( hasOutlineWidthParam )
426+
{
427+
mSvgOutlineWidth = defaultOutlineWidth;
428+
}
429+
}
430+
372431
QgsLinePatternFillSymbolLayer::QgsLinePatternFillSymbolLayer(): QgsImageFillSymbolLayer()
373432
{
374433
}

src/core/symbology-ng/qgsfillsymbollayerv2.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ class CORE_EXPORT QgsSVGFillSymbolLayer: public QgsImageFillSymbolLayer
115115
void setPatternWidth( double width ) { mPatternWidth = width;}
116116
double patternWidth() const { return mPatternWidth; }
117117

118+
void setSvgFillColor( const QColor& c ) { mSvgFillColor = c; }
119+
QColor svgFillColor() const { return mSvgFillColor; }
120+
void setSvgOutlineColor( const QColor& c ) { mSvgOutlineColor = c; }
121+
QColor svgOutlineColor() const { return mSvgOutlineColor; }
122+
void setSvgOutlineWidth( double w ) { mSvgOutlineWidth = w; }
123+
double svgOutlineWidth() const { return mSvgOutlineWidth; }
124+
118125
protected:
119126
/**Width of the pattern (in QgsSymbolV2 output units)*/
120127
double mPatternWidth;
@@ -125,9 +132,16 @@ class CORE_EXPORT QgsSVGFillSymbolLayer: public QgsImageFillSymbolLayer
125132
/**SVG view box (to keep the aspect ratio */
126133
QRectF mSvgViewBox;
127134

135+
//param(fill), param(outline), param(outline-width) are going
136+
//to be replaced in memory
137+
QColor mSvgFillColor;
138+
QColor mSvgOutlineColor;
139+
double mSvgOutlineWidth;
140+
128141
private:
129142
/**Helper function that gets the view box from the byte array*/
130143
void storeViewBox();
144+
void setDefaultSvgParams(); //fills mSvgFillColor, mSvgOutlineColor, mSvgOutlineWidth with default values for mSvgFilePath
131145
};
132146

133147
class CORE_EXPORT QgsLinePatternFillSymbolLayer: public QgsImageFillSymbolLayer

src/gui/symbology-ng/qgssymbollayerv2widget.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@ void QgsSVGFillSymbolLayerWidget::setSymbolLayer( QgsSymbolLayerV2* layer )
828828
mRotationSpinBox->setValue( mLayer->angle() );
829829
}
830830
updateOutlineIcon();
831+
updateParamGui();
831832
}
832833

833834
QgsSymbolLayerV2* QgsSVGFillSymbolLayerWidget::symbolLayer()
@@ -868,11 +869,13 @@ void QgsSVGFillSymbolLayerWidget::on_mSVGLineEdit_textChanged( const QString & t
868869
}
869870
mLayer->setSvgFilePath( text );
870871
emit changed();
872+
updateParamGui();
871873
}
872874

873875
void QgsSVGFillSymbolLayerWidget::setFile( const QModelIndex& item )
874876
{
875877
mSVGLineEdit->setText( item.data( Qt::UserRole ).toString() );
878+
updateParamGui();
876879
}
877880

878881
void QgsSVGFillSymbolLayerWidget::insertIcons()
@@ -911,6 +914,55 @@ void QgsSVGFillSymbolLayerWidget::updateOutlineIcon()
911914
}
912915
}
913916

917+
void QgsSVGFillSymbolLayerWidget::updateParamGui()
918+
{
919+
//activate gui for svg parameters only if supported by the svg file
920+
bool hasFillParam, hasOutlineParam, hasOutlineWidthParam;
921+
QColor defaultFill, defaultOutline;
922+
double defaultOutlineWidth;
923+
QgsSvgCache::instance()->containsParams( mSVGLineEdit->text(), hasFillParam, defaultFill, hasOutlineParam, defaultOutline, hasOutlineWidthParam, defaultOutlineWidth );
924+
mChangeColorButton->setEnabled( hasFillParam );
925+
mChangeBorderColorButton->setEnabled( hasOutlineParam );
926+
mBorderWidthSpinBox->setEnabled( hasOutlineWidthParam );
927+
}
928+
929+
void QgsSVGFillSymbolLayerWidget::on_mChangeColorButton_clicked()
930+
{
931+
if ( !mLayer )
932+
{
933+
return;
934+
}
935+
QColor c = QColorDialog::getColor( mLayer->svgFillColor() );
936+
if ( c.isValid() )
937+
{
938+
mLayer->setSvgFillColor( c );
939+
emit changed();
940+
}
941+
}
942+
943+
void QgsSVGFillSymbolLayerWidget::on_mChangeBorderColorButton_clicked()
944+
{
945+
if ( !mLayer )
946+
{
947+
return;
948+
}
949+
QColor c = QColorDialog::getColor( mLayer->svgOutlineColor() );
950+
if ( c.isValid() )
951+
{
952+
mLayer->setSvgOutlineColor( c );
953+
emit changed();
954+
}
955+
}
956+
957+
void QgsSVGFillSymbolLayerWidget::on_mBorderWidthSpinBox_valueChanged( double d )
958+
{
959+
if ( mLayer )
960+
{
961+
mLayer->setSvgOutlineWidth( d );
962+
emit changed();
963+
}
964+
}
965+
914966
/////////////
915967

916968
QgsLinePatternFillSymbolLayerWidget::QgsLinePatternFillSymbolLayerWidget( const QgsVectorLayer* vl, QWidget* parent ):

src/gui/symbology-ng/qgssymbollayerv2widget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ class GUI_EXPORT QgsSVGFillSymbolLayerWidget : public QgsSymbolLayerV2Widget, pr
251251
void setOutputUnit();
252252
void insertIcons();
253253
void updateOutlineIcon();
254+
void updateParamGui();
254255

255256
private slots:
256257
void on_mBrowseToolButton_clicked();
@@ -259,6 +260,9 @@ class GUI_EXPORT QgsSVGFillSymbolLayerWidget : public QgsSymbolLayerV2Widget, pr
259260
void setFile( const QModelIndex& item );
260261
void on_mChangeOutlinePushButton_clicked();
261262
void on_mRotationSpinBox_valueChanged( double d );
263+
void on_mChangeColorButton_clicked();
264+
void on_mChangeBorderColorButton_clicked();
265+
void on_mBorderWidthSpinBox_valueChanged( double d );
262266
};
263267

264268
//////////

0 commit comments

Comments
 (0)