Skip to content

Commit af71f5f

Browse files
committedFeb 6, 2012
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
//////////

‎src/ui/symbollayer/widget_svgfill.ui

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>185</width>
10-
<height>221</height>
9+
<width>236</width>
10+
<height>310</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
1414
<string>Form</string>
1515
</property>
16-
<layout class="QGridLayout" name="gridLayout">
16+
<layout class="QGridLayout" name="gridLayout_2">
1717
<item row="0" column="0">
18-
<layout class="QHBoxLayout" name="horizontalLayout_2">
19-
<item>
18+
<layout class="QGridLayout" name="gridLayout">
19+
<item row="0" column="0">
2020
<widget class="QLabel" name="mTextureWidthLabel">
2121
<property name="text">
2222
<string>Texture width</string>
2323
</property>
2424
</widget>
2525
</item>
26-
<item>
26+
<item row="0" column="1">
2727
<widget class="QDoubleSpinBox" name="mTextureWidthSpinBox">
2828
<property name="decimals">
2929
<number>5</number>
@@ -33,36 +33,70 @@
3333
</property>
3434
</widget>
3535
</item>
36-
</layout>
37-
</item>
38-
<item row="1" column="0">
39-
<layout class="QHBoxLayout" name="horizontalLayout_3">
40-
<item>
36+
<item row="1" column="0">
4137
<widget class="QLabel" name="mRotationLabel">
4238
<property name="text">
4339
<string>Rotation</string>
4440
</property>
4541
</widget>
4642
</item>
47-
<item>
43+
<item row="1" column="1">
4844
<widget class="QDoubleSpinBox" name="mRotationSpinBox">
4945
<property name="maximum">
5046
<double>360.000000000000000</double>
5147
</property>
5248
</widget>
5349
</item>
54-
</layout>
55-
</item>
56-
<item row="2" column="0">
57-
<layout class="QHBoxLayout" name="horizontalLayout_4">
58-
<item>
50+
<item row="2" column="0">
51+
<widget class="QLabel" name="mColorLabel">
52+
<property name="text">
53+
<string>Color</string>
54+
</property>
55+
</widget>
56+
</item>
57+
<item row="2" column="1">
58+
<widget class="QPushButton" name="mChangeColorButton">
59+
<property name="text">
60+
<string>Change</string>
61+
</property>
62+
</widget>
63+
</item>
64+
<item row="3" column="0">
65+
<widget class="QLabel" name="mBorderColorLabel">
66+
<property name="text">
67+
<string>Border color</string>
68+
</property>
69+
</widget>
70+
</item>
71+
<item row="3" column="1">
72+
<widget class="QPushButton" name="mChangeBorderColorButton">
73+
<property name="text">
74+
<string>Change</string>
75+
</property>
76+
</widget>
77+
</item>
78+
<item row="4" column="0">
79+
<widget class="QLabel" name="mBorderWidthLabel">
80+
<property name="text">
81+
<string>Border width</string>
82+
</property>
83+
</widget>
84+
</item>
85+
<item row="4" column="1">
86+
<widget class="QDoubleSpinBox" name="mBorderWidthSpinBox">
87+
<property name="decimals">
88+
<number>5</number>
89+
</property>
90+
</widget>
91+
</item>
92+
<item row="5" column="0">
5993
<widget class="QLabel" name="mOutlineLabel">
6094
<property name="text">
6195
<string>Outline</string>
6296
</property>
6397
</widget>
6498
</item>
65-
<item>
99+
<item row="5" column="1">
66100
<widget class="QPushButton" name="mChangeOutlinePushButton">
67101
<property name="text">
68102
<string>Change</string>
@@ -71,7 +105,7 @@
71105
</item>
72106
</layout>
73107
</item>
74-
<item row="3" column="0">
108+
<item row="1" column="0">
75109
<widget class="QListView" name="mSvgListView">
76110
<property name="flow">
77111
<enum>QListView::LeftToRight</enum>
@@ -87,7 +121,7 @@
87121
</property>
88122
</widget>
89123
</item>
90-
<item row="4" column="0">
124+
<item row="2" column="0">
91125
<layout class="QHBoxLayout" name="horizontalLayout">
92126
<item>
93127
<widget class="QLineEdit" name="mSVGLineEdit"/>

0 commit comments

Comments
 (0)
Please sign in to comment.