17
17
18
18
#include " qgssvgcache.h"
19
19
#include < QDomDocument>
20
+ #include < QDomElement>
20
21
#include < QFile>
21
22
#include < QImage>
22
23
#include < QPainter>
@@ -145,7 +146,9 @@ void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
145
146
return ;
146
147
}
147
148
148
- // todo: replace params here
149
+ // replace fill color, outline color, outline with in all nodes
150
+ QDomElement docElem = svgDoc.documentElement ();
151
+ replaceElemParams ( docElem, entry->fill , entry->outline , entry->outlineWidth );
149
152
150
153
entry->svgContent = svgDoc.toByteArray ();
151
154
}
@@ -164,7 +167,6 @@ void QgsSvgCache::cacheImage( QgsSvgCacheEntry* entry )
164
167
QImage* image = new QImage ( imageSize, imageSize, QImage::Format_ARGB32_Premultiplied );
165
168
image->fill ( 0 ); // transparent background
166
169
167
- // rasterise byte array to image
168
170
QPainter p ( image );
169
171
QSvgRenderer r ( entry->svgContent );
170
172
r.render ( &p );
@@ -224,3 +226,40 @@ QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, con
224
226
return currentEntry;
225
227
}
226
228
229
+ void QgsSvgCache::replaceElemParams ( QDomElement& elem, const QColor& fill, const QColor& outline , double outlineWidth )
230
+ {
231
+ if ( elem.isNull () )
232
+ {
233
+ return ;
234
+ }
235
+
236
+ // go through attributes
237
+ QDomNamedNodeMap attributes = elem.attributes ();
238
+ int nAttributes = attributes.count ();
239
+ for ( int i = 0 ; i < nAttributes; ++i )
240
+ {
241
+ QDomAttr attribute = attributes.item ( i ).toAttr ();
242
+ QString value = attribute.value ();
243
+ if ( value.startsWith (" params(fill)" ) )
244
+ {
245
+ elem.setAttribute ( attribute.name (), fill.name () );
246
+ }
247
+ else if ( value.startsWith (" params(outline)" ) )
248
+ {
249
+ elem.setAttribute ( attribute.name (), outline .name () );
250
+ }
251
+ else if ( value.startsWith (" params(outline-width)" ) )
252
+ {
253
+ elem.setAttribute ( attribute.name (), QString::number ( outlineWidth ) );
254
+ }
255
+ }
256
+
257
+ QDomNodeList childList = elem.childNodes ();
258
+ int nChildren = childList.count ();
259
+ for ( int i = 0 ; i < nChildren; ++i )
260
+ {
261
+ QDomElement childElem = childList.at ( i ).toElement ();
262
+ replaceElemParams ( childElem, fill, outline , outlineWidth );
263
+ }
264
+ }
265
+
0 commit comments