Skip to content

Commit 48aa535

Browse files
author
Marco Hugentobler
committed
Further work on the svg cache
1 parent 651259a commit 48aa535

File tree

2 files changed

+133
-11
lines changed

2 files changed

+133
-11
lines changed

src/core/symbology-ng/qgssvgcache.cpp

+110-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,41 @@
1616
***************************************************************************/
1717

1818
#include "qgssvgcache.h"
19+
#include <QDomDocument>
20+
#include <QFile>
21+
#include <QImage>
22+
#include <QPicture>
23+
24+
QgsSvgCacheEntry::QgsSvgCacheEntry(): file( QString() ), size( 0 ), outlineWidth( 0 ), widthScaleFactor( 1.0 ), rasterScaleFactor( 1.0 ), fill( Qt::black ),
25+
outline( Qt::black ), image( 0 ), picture( 0 )
26+
{
27+
}
28+
29+
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 ),
30+
widthScaleFactor( wsf ), rasterScaleFactor( rsf ), fill( fi ), outline( ou ), image( 0 ), picture( 0 )
31+
{
32+
}
33+
34+
35+
QgsSvgCacheEntry::~QgsSvgCacheEntry()
36+
{
37+
delete image;
38+
delete picture;
39+
}
40+
41+
bool QgsSvgCacheEntry::operator==( const QgsSvgCacheEntry& other ) const
42+
{
43+
return ( other.file == file && other.size == size && other.outlineWidth == outlineWidth && other.widthScaleFactor == widthScaleFactor
44+
&& other.rasterScaleFactor == rasterScaleFactor && other.fill == fill && other.outline == outline );
45+
}
46+
47+
QString file;
48+
double size;
49+
double outlineWidth;
50+
double widthScaleFactor;
51+
double rasterScaleFactor;
52+
QColor fill;
53+
QColor outline;
1954

2055
QgsSvgCache* QgsSvgCache::mInstance = 0;
2156

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

3974

40-
const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth ) const
75+
const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
76+
double widthScaleFactor, double rasterScaleFactor )
77+
{
78+
79+
}
80+
81+
const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
82+
double widthScaleFactor, double rasterScaleFactor )
4183
{
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+
}
107+
108+
//if current entry image is 0: cache image for entry
109+
//update stats for memory usage
110+
if( !currentEntry->picture )
111+
{
112+
cachePicture( currentEntry );
113+
}
114+
115+
//update lastUsed with current date time
116+
117+
return *( currentEntry->picture );
42118
}
43119

44-
const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth ) const
120+
QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
121+
double widthScaleFactor, double rasterScaleFactor )
45122
{
123+
QgsSvgCacheEntry* entry = new QgsSvgCacheEntry( file, size, outlineWidth, widthScaleFactor, rasterScaleFactor, fill, outline );
124+
entry->lastUsed = QDateTime::currentDateTime();
125+
126+
replaceParamsAndCacheSvg( entry );
127+
128+
mEntries.insert( entry->lastUsed, entry );
129+
mEntryLookup.insert( file, entry );
130+
return entry;
46131
}
47132

48-
void QgsSvgCache::insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth )
133+
void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
49134
{
135+
if( !entry )
136+
{
137+
return;
138+
}
139+
140+
QFile svgFile( entry->file );
141+
if( !svgFile.open( QIODevice::ReadOnly ) )
142+
{
143+
return;
144+
}
145+
146+
QDomDocument svgDoc;
147+
if( !svgDoc.setContent( &svgFile ) )
148+
{
149+
return;
150+
}
151+
152+
//todo: replace params here
153+
154+
entry->svgContent = svgDoc.toByteArray();
50155
}
51156

52-
void QgsSvgCache::cacheImage( QgsSvgCacheEntry )
157+
void QgsSvgCache::cacheImage( QgsSvgCacheEntry* entry )
53158
{
54159
}
55160

56-
void QgsSvgCache::cachePicture( QgsSvgCacheEntry )
161+
void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry )
57162
{
58163
}
59164

src/core/symbology-ng/qgssvgcache.h

+23-6
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,25 @@ class QPicture;
2929

3030
struct QgsSvgCacheEntry
3131
{
32+
QgsSvgCacheEntry();
33+
QgsSvgCacheEntry( const QString& file, double size, double outlineWidth, double widthScaleFactor, double rasterScaleFctor, const QColor& fill, const QColor& outline );
34+
~QgsSvgCacheEntry();
35+
3236
QString file;
3337
double size;
3438
double outlineWidth;
39+
double widthScaleFactor;
40+
double rasterScaleFactor;
3541
QColor fill;
3642
QColor outline;
3743
QImage* image;
3844
QPicture* picture;
3945
QDateTime lastUsed;
46+
//content (with params replaced)
47+
QByteArray svgContent;
48+
49+
/**Don't consider image, picture, last used timestamp for comparison*/
50+
bool operator==( const QgsSvgCacheEntry& other ) const;
4051
};
4152

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

52-
const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth ) const;
53-
const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth ) const;
63+
const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
64+
double widthScaleFactor, double rasterScaleFactor );
65+
const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
66+
double widthScaleFactor, double rasterScaleFactor );
5467

5568
protected:
5669
QgsSvgCache();
5770

58-
void insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth );
59-
void cacheImage( QgsSvgCacheEntry );
60-
void cachePicture( QgsSvgCacheEntry );
71+
/**Creates new cache entry and returns pointer to it*/
72+
QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
73+
double widthScaleFactor, double rasterScaleFactor );
74+
75+
void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
76+
void cacheImage( QgsSvgCacheEntry* entry );
77+
void cachePicture( QgsSvgCacheEntry* entry );
6178

6279
private:
6380
static QgsSvgCache* mInstance;
6481

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

0 commit comments

Comments
 (0)