Skip to content

Commit aa2ad45

Browse files
committedJun 23, 2011
Add caching, experimental test with QPicture for svg marker
1 parent 48aa535 commit aa2ad45

File tree

3 files changed

+94
-24
lines changed

3 files changed

+94
-24
lines changed
 

‎src/core/symbology-ng/qgsmarkersymbollayerv2.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "qgsapplication.h"
77
#include "qgslogger.h"
88
#include "qgsproject.h"
9+
#include "qgssvgcache.h"
910

1011
#include <QPainter>
1112
#include <QSvgRenderer>
@@ -533,9 +534,13 @@ void QgsSvgMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Re
533534
if ( mAngle != 0 )
534535
p->rotate( mAngle );
535536

536-
QPicture &pct = context.selected() ? mSelPicture : mPicture;
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() );
537539
p->drawPicture( 0, 0, pct );
538540

541+
/*QPicture &pct = context.selected() ? mSelPicture : mPicture;
542+
p->drawPicture( 0, 0, pct );*/
543+
539544
p->restore();
540545
}
541546

‎src/core/symbology-ng/qgssvgcache.cpp

Lines changed: 85 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
#include <QDomDocument>
2020
#include <QFile>
2121
#include <QImage>
22+
#include <QPainter>
2223
#include <QPicture>
24+
#include <QSvgRenderer>
2325

2426
QgsSvgCacheEntry::QgsSvgCacheEntry(): file( QString() ), size( 0 ), outlineWidth( 0 ), widthScaleFactor( 1.0 ), rasterScaleFactor( 1.0 ), fill( Qt::black ),
2527
outline( Qt::black ), image( 0 ), picture( 0 )
@@ -69,41 +71,35 @@ QgsSvgCache::QgsSvgCache()
6971

7072
QgsSvgCache::~QgsSvgCache()
7173
{
74+
QMap< QDateTime, QgsSvgCacheEntry* >::iterator it = mEntries.begin();
75+
for(; it != mEntries.end(); ++it )
76+
{
77+
delete it.value();
78+
}
7279
}
7380

7481

7582
const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
7683
double widthScaleFactor, double rasterScaleFactor )
7784
{
85+
QgsSvgCacheEntry* currentEntry = this->cacheEntry( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
86+
87+
//if current entry image is 0: cache image for entry
88+
//update stats for memory usage
89+
if( !currentEntry->image )
90+
{
91+
cacheImage( currentEntry );
92+
}
7893

94+
//update lastUsed with current date time
95+
96+
return *( currentEntry->image );
7997
}
8098

8199
const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
82100
double widthScaleFactor, double rasterScaleFactor )
83101
{
84-
//search entries in mEntryLookup
85-
QgsSvgCacheEntry* currentEntry = 0;
86-
QList<QgsSvgCacheEntry*> entries = mEntryLookup.values( file );
87-
88-
QList<QgsSvgCacheEntry*>::iterator entryIt = entries.begin();
89-
for(; entryIt != entries.end(); ++entryIt )
90-
{
91-
QgsSvgCacheEntry* cacheEntry = *entryIt;
92-
if( cacheEntry->file == file && cacheEntry->size == size && cacheEntry->fill == fill && cacheEntry->outline == outline &&
93-
cacheEntry->outlineWidth == outlineWidth && cacheEntry->widthScaleFactor == widthScaleFactor && cacheEntry->rasterScaleFactor == rasterScaleFactor)
94-
{
95-
currentEntry = cacheEntry;
96-
break;
97-
}
98-
}
99-
100-
101-
//if not found: create new entry
102-
//cache and replace params in svg content
103-
if( !currentEntry )
104-
{
105-
currentEntry = insertSVG( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
106-
}
102+
QgsSvgCacheEntry* currentEntry = this->cacheEntry( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
107103

108104
//if current entry image is 0: cache image for entry
109105
//update stats for memory usage
@@ -156,9 +152,75 @@ void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
156152

157153
void QgsSvgCache::cacheImage( QgsSvgCacheEntry* entry )
158154
{
155+
if( !entry )
156+
{
157+
return;
158+
}
159+
160+
delete entry->image;
161+
entry->image = 0;
162+
163+
double imageSize = entry->size * entry->widthScaleFactor * entry->rasterScaleFactor;
164+
QImage* image = new QImage( imageSize, imageSize, QImage::Format_ARGB32_Premultiplied );
165+
image->fill( 0 ); // transparent background
166+
167+
//rasterise byte array to image
168+
QPainter p( image );
169+
QSvgRenderer r( entry->svgContent );
170+
r.render( &p );
171+
172+
entry->image = image;
159173
}
160174

161175
void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry )
162176
{
177+
if( !entry )
178+
{
179+
return;
180+
}
181+
182+
delete entry->picture;
183+
entry->picture = 0;
184+
185+
//correct QPictures dpi correction
186+
QPicture* picture = new QPicture();
187+
double dpi = entry->widthScaleFactor * 25.4 * entry->rasterScaleFactor;
188+
double pictureSize = entry->size * entry->widthScaleFactor / dpi * picture->logicalDpiX();
189+
QRectF rect( QPointF( -pictureSize / 2.0, -pictureSize / 2.0 ), QSizeF( pictureSize, pictureSize ) );
190+
191+
192+
QSvgRenderer renderer( entry->svgContent );
193+
QPainter painter( picture );
194+
renderer.render( &painter, rect );
195+
entry->picture = picture;
196+
}
197+
198+
QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
199+
double widthScaleFactor, double rasterScaleFactor )
200+
{
201+
//search entries in mEntryLookup
202+
QgsSvgCacheEntry* currentEntry = 0;
203+
QList<QgsSvgCacheEntry*> entries = mEntryLookup.values( file );
204+
205+
QList<QgsSvgCacheEntry*>::iterator entryIt = entries.begin();
206+
for(; entryIt != entries.end(); ++entryIt )
207+
{
208+
QgsSvgCacheEntry* cacheEntry = *entryIt;
209+
if( cacheEntry->file == file && cacheEntry->size == size && cacheEntry->fill == fill && cacheEntry->outline == outline &&
210+
cacheEntry->outlineWidth == outlineWidth && cacheEntry->widthScaleFactor == widthScaleFactor && cacheEntry->rasterScaleFactor == rasterScaleFactor)
211+
{
212+
currentEntry = cacheEntry;
213+
break;
214+
}
215+
}
216+
217+
218+
//if not found: create new entry
219+
//cache and replace params in svg content
220+
if( !currentEntry )
221+
{
222+
currentEntry = insertSVG( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
223+
}
224+
return currentEntry;
163225
}
164226

‎src/core/symbology-ng/qgssvgcache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ class QgsSvgCache
7575
void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
7676
void cacheImage( QgsSvgCacheEntry* entry );
7777
void cachePicture( QgsSvgCacheEntry* entry );
78+
/**Returns entry from cache or creates a new entry if it does not exist already*/
79+
QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
80+
double widthScaleFactor, double rasterScaleFactor );
7881

7982
private:
8083
static QgsSvgCache* mInstance;

0 commit comments

Comments
 (0)
Please sign in to comment.