Skip to content

Commit

Permalink
Use QImage for map and QPicture for composer
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Hugentobler committed Jun 25, 2011
1 parent aa2ad45 commit e112204
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
18 changes: 12 additions & 6 deletions src/core/symbology-ng/qgsmarkersymbollayerv2.cpp
Expand Up @@ -534,12 +534,18 @@ void QgsSvgMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Re
if ( mAngle != 0 )
p->rotate( mAngle );

const QPicture& pct = QgsSvgCache::instance()->svgAsPicture( mPath, mSize, QColor( Qt::black )/*const QColor& fill*/, QColor( Qt::black ) /*const QColor& outline*/,
1.0 /*outline width*/, context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
p->drawPicture( 0, 0, pct );

/*QPicture &pct = context.selected() ? mSelPicture : mPicture;
p->drawPicture( 0, 0, pct );*/
if( doubleNear( context.renderContext().rasterScaleFactor(), 1.0 ) )
{
const QImage& img = QgsSvgCache::instance()->svgAsImage( mPath, mSize, QColor( Qt::black )/*const QColor& fill*/, QColor( Qt::black ) /*const QColor& outline*/,
1.0 /*outline width*/, context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
p->drawImage( -img.width() / 2.0, -img.width() / 2.0, img );
}
else
{
const QPicture& pct = QgsSvgCache::instance()->svgAsPicture( mPath, mSize, QColor( Qt::black )/*const QColor& fill*/, QColor( Qt::black ) /*const QColor& outline*/,
1.0 /*outline width*/, context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
p->drawPicture( 0, 0, pct );
}

p->restore();
}
Expand Down
43 changes: 41 additions & 2 deletions src/core/symbology-ng/qgssvgcache.cpp
Expand Up @@ -17,6 +17,7 @@

#include "qgssvgcache.h"
#include <QDomDocument>
#include <QDomElement>
#include <QFile>
#include <QImage>
#include <QPainter>
Expand Down Expand Up @@ -145,7 +146,9 @@ void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
return;
}

//todo: replace params here
//replace fill color, outline color, outline with in all nodes
QDomElement docElem = svgDoc.documentElement();
replaceElemParams( docElem, entry->fill, entry->outline, entry->outlineWidth );

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

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

void QgsSvgCache::replaceElemParams( QDomElement& elem, const QColor& fill, const QColor& outline, double outlineWidth )
{
if( elem.isNull() )
{
return;
}

//go through attributes
QDomNamedNodeMap attributes = elem.attributes();
int nAttributes = attributes.count();
for( int i = 0; i < nAttributes; ++i )
{
QDomAttr attribute = attributes.item( i ).toAttr();
QString value = attribute.value();
if( value.startsWith("params(fill)") )
{
elem.setAttribute( attribute.name(), fill.name() );
}
else if( value.startsWith("params(outline)") )
{
elem.setAttribute( attribute.name(), outline.name() );
}
else if( value.startsWith("params(outline-width)") )
{
elem.setAttribute( attribute.name(), QString::number( outlineWidth ) );
}
}

QDomNodeList childList = elem.childNodes();
int nChildren = childList.count();
for( int i = 0; i < nChildren; ++i )
{
QDomElement childElem = childList.at( i ).toElement();
replaceElemParams( childElem, fill, outline, outlineWidth );
}
}

3 changes: 3 additions & 0 deletions src/core/symbology-ng/qgssvgcache.h
Expand Up @@ -24,6 +24,7 @@
#include <QMultiHash>
#include <QString>

class QDomElement;
class QImage;
class QPicture;

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

#endif // QGSSVGCACHE_H

0 comments on commit e112204

Please sign in to comment.