Skip to content
Permalink
Browse files

Further work on the svg cache

  • Loading branch information
Marco Hugentobler
Marco Hugentobler committed Jun 23, 2011
1 parent 651259a commit 48aa535a2918ec9bc409e75af904d99414669978
Showing with 133 additions and 11 deletions.
  1. +110 −5 src/core/symbology-ng/qgssvgcache.cpp
  2. +23 −6 src/core/symbology-ng/qgssvgcache.h
@@ -16,6 +16,41 @@
***************************************************************************/

#include "qgssvgcache.h"
#include <QDomDocument>
#include <QFile>
#include <QImage>
#include <QPicture>

QgsSvgCacheEntry::QgsSvgCacheEntry(): file( QString() ), size( 0 ), outlineWidth( 0 ), widthScaleFactor( 1.0 ), rasterScaleFactor( 1.0 ), fill( Qt::black ),
outline( Qt::black ), image( 0 ), picture( 0 )
{
}

QgsSvgCacheEntry::QgsSvgCacheEntry( const QString& f, double s, double ow, double wsf, double rsf, const QColor& fi, const QColor& ou ): file( f ), size( s ), outlineWidth( ow ),
widthScaleFactor( wsf ), rasterScaleFactor( rsf ), fill( fi ), outline( ou ), image( 0 ), picture( 0 )
{
}


QgsSvgCacheEntry::~QgsSvgCacheEntry()
{
delete image;
delete picture;
}

bool QgsSvgCacheEntry::operator==( const QgsSvgCacheEntry& other ) const
{
return ( other.file == file && other.size == size && other.outlineWidth == outlineWidth && other.widthScaleFactor == widthScaleFactor
&& other.rasterScaleFactor == rasterScaleFactor && other.fill == fill && other.outline == outline );
}

QString file;
double size;
double outlineWidth;
double widthScaleFactor;
double rasterScaleFactor;
QColor fill;
QColor outline;

QgsSvgCache* QgsSvgCache::mInstance = 0;

@@ -37,23 +72,93 @@ QgsSvgCache::~QgsSvgCache()
}


const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth ) const
const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor )
{

}

const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor )
{
//search entries in mEntryLookup
QgsSvgCacheEntry* currentEntry = 0;
QList<QgsSvgCacheEntry*> entries = mEntryLookup.values( file );

QList<QgsSvgCacheEntry*>::iterator entryIt = entries.begin();
for(; entryIt != entries.end(); ++entryIt )
{
QgsSvgCacheEntry* cacheEntry = *entryIt;
if( cacheEntry->file == file && cacheEntry->size == size && cacheEntry->fill == fill && cacheEntry->outline == outline &&
cacheEntry->outlineWidth == outlineWidth && cacheEntry->widthScaleFactor == widthScaleFactor && cacheEntry->rasterScaleFactor == rasterScaleFactor)
{
currentEntry = cacheEntry;
break;
}
}


//if not found: create new entry
//cache and replace params in svg content
if( !currentEntry )
{
currentEntry = insertSVG( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
}

//if current entry image is 0: cache image for entry
//update stats for memory usage
if( !currentEntry->picture )
{
cachePicture( currentEntry );
}

//update lastUsed with current date time

return *( currentEntry->picture );
}

const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth ) const
QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor )
{
QgsSvgCacheEntry* entry = new QgsSvgCacheEntry( file, size, outlineWidth, widthScaleFactor, rasterScaleFactor, fill, outline );
entry->lastUsed = QDateTime::currentDateTime();

replaceParamsAndCacheSvg( entry );

mEntries.insert( entry->lastUsed, entry );
mEntryLookup.insert( file, entry );
return entry;
}

void QgsSvgCache::insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth )
void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
{
if( !entry )
{
return;
}

QFile svgFile( entry->file );
if( !svgFile.open( QIODevice::ReadOnly ) )
{
return;
}

QDomDocument svgDoc;
if( !svgDoc.setContent( &svgFile ) )
{
return;
}

//todo: replace params here

entry->svgContent = svgDoc.toByteArray();
}

void QgsSvgCache::cacheImage( QgsSvgCacheEntry )
void QgsSvgCache::cacheImage( QgsSvgCacheEntry* entry )
{
}

void QgsSvgCache::cachePicture( QgsSvgCacheEntry )
void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry )
{
}

@@ -29,14 +29,25 @@ class QPicture;

struct QgsSvgCacheEntry
{
QgsSvgCacheEntry();
QgsSvgCacheEntry( const QString& file, double size, double outlineWidth, double widthScaleFactor, double rasterScaleFctor, const QColor& fill, const QColor& outline );
~QgsSvgCacheEntry();

QString file;
double size;
double outlineWidth;
double widthScaleFactor;
double rasterScaleFactor;
QColor fill;
QColor outline;
QImage* image;
QPicture* picture;
QDateTime lastUsed;
//content (with params replaced)
QByteArray svgContent;

/**Don't consider image, picture, last used timestamp for comparison*/
bool operator==( const QgsSvgCacheEntry& other ) const;
};

/**A cache for images / pictures derived from svg files. This class supports parameter replacement in svg files
@@ -49,21 +60,27 @@ class QgsSvgCache
static QgsSvgCache* instance();
~QgsSvgCache();

const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth ) const;
const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth ) const;
const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor );
const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor );

protected:
QgsSvgCache();

void insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth );
void cacheImage( QgsSvgCacheEntry );
void cachePicture( QgsSvgCacheEntry );
/**Creates new cache entry and returns pointer to it*/
QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
double widthScaleFactor, double rasterScaleFactor );

void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
void cacheImage( QgsSvgCacheEntry* entry );
void cachePicture( QgsSvgCacheEntry* entry );

private:
static QgsSvgCache* mInstance;

/**Entries sorted by last used time*/
QMap< QDateTime, QgsSvgCacheEntry > mEntries;
QMap< QDateTime, QgsSvgCacheEntry* > mEntries;
/**Entry pointers accessible by file name*/
QMultiHash< QString, QgsSvgCacheEntry* > mEntryLookup;
/**Estimated total size of all images and pictures*/

0 comments on commit 48aa535

Please sign in to comment.
You can’t perform that action at this time.