Skip to content

Commit e112204

Browse files
author
Marco Hugentobler
committed
Use QImage for map and QPicture for composer
1 parent aa2ad45 commit e112204

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

src/core/symbology-ng/qgsmarkersymbollayerv2.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -534,12 +534,18 @@ void QgsSvgMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Re
534534
if ( mAngle != 0 )
535535
p->rotate( mAngle );
536536

537-
const QPicture& pct = QgsSvgCache::instance()->svgAsPicture( mPath, mSize, QColor( Qt::black )/*const QColor& fill*/, QColor( Qt::black ) /*const QColor& outline*/,
538-
1.0 /*outline width*/, context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
539-
p->drawPicture( 0, 0, pct );
540-
541-
/*QPicture &pct = context.selected() ? mSelPicture : mPicture;
542-
p->drawPicture( 0, 0, pct );*/
537+
if( doubleNear( context.renderContext().rasterScaleFactor(), 1.0 ) )
538+
{
539+
const QImage& img = QgsSvgCache::instance()->svgAsImage( mPath, mSize, QColor( Qt::black )/*const QColor& fill*/, QColor( Qt::black ) /*const QColor& outline*/,
540+
1.0 /*outline width*/, context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
541+
p->drawImage( -img.width() / 2.0, -img.width() / 2.0, img );
542+
}
543+
else
544+
{
545+
const QPicture& pct = QgsSvgCache::instance()->svgAsPicture( mPath, mSize, QColor( Qt::black )/*const QColor& fill*/, QColor( Qt::black ) /*const QColor& outline*/,
546+
1.0 /*outline width*/, context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
547+
p->drawPicture( 0, 0, pct );
548+
}
543549

544550
p->restore();
545551
}

src/core/symbology-ng/qgssvgcache.cpp

+41-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgssvgcache.h"
1919
#include <QDomDocument>
20+
#include <QDomElement>
2021
#include <QFile>
2122
#include <QImage>
2223
#include <QPainter>
@@ -145,7 +146,9 @@ void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
145146
return;
146147
}
147148

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 );
149152

150153
entry->svgContent = svgDoc.toByteArray();
151154
}
@@ -164,7 +167,6 @@ void QgsSvgCache::cacheImage( QgsSvgCacheEntry* entry )
164167
QImage* image = new QImage( imageSize, imageSize, QImage::Format_ARGB32_Premultiplied );
165168
image->fill( 0 ); // transparent background
166169

167-
//rasterise byte array to image
168170
QPainter p( image );
169171
QSvgRenderer r( entry->svgContent );
170172
r.render( &p );
@@ -224,3 +226,40 @@ QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, con
224226
return currentEntry;
225227
}
226228

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+

src/core/symbology-ng/qgssvgcache.h

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <QMultiHash>
2525
#include <QString>
2626

27+
class QDomElement;
2728
class QImage;
2829
class QPicture;
2930

@@ -88,6 +89,8 @@ class QgsSvgCache
8889
QMultiHash< QString, QgsSvgCacheEntry* > mEntryLookup;
8990
/**Estimated total size of all images and pictures*/
9091
double mTotalSize;
92+
/**Replaces parameters in elements of a dom node and calls method for all child nodes*/
93+
void replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth );
9194
};
9295

9396
#endif // QGSSVGCACHE_H

0 commit comments

Comments
 (0)