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
2055QgsSvgCache* 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
0 commit comments