16
16
***************************************************************************/
17
17
18
18
#include " qgssvgcache.h"
19
+ #include " qgslogger.h"
19
20
#include < QDomDocument>
20
21
#include < QDomElement>
21
22
#include < QFile>
@@ -66,14 +67,14 @@ QgsSvgCache* QgsSvgCache::instance()
66
67
return mInstance ;
67
68
}
68
69
69
- QgsSvgCache::QgsSvgCache ()
70
+ QgsSvgCache::QgsSvgCache (): mTotalSize( 0 )
70
71
{
71
72
}
72
73
73
74
QgsSvgCache::~QgsSvgCache ()
74
75
{
75
- QMap< QDateTime , QgsSvgCacheEntry* >::iterator it = mEntries .begin ();
76
- for ( ; it != mEntries .end (); ++it )
76
+ QMultiHash< QString , QgsSvgCacheEntry* >::iterator it = mEntryLookup .begin ();
77
+ for ( ; it != mEntryLookup .end (); ++it )
77
78
{
78
79
delete it.value ();
79
80
}
@@ -92,7 +93,11 @@ const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const Q
92
93
cacheImage ( currentEntry );
93
94
}
94
95
95
- // update lastUsed with current date time
96
+ // debug: display current cache usage
97
+ QgsDebugMsg (" cache size: " + QString::number ( mTotalSize ) );
98
+
99
+ // set entry timestamp to current time
100
+ currentEntry->lastUsed = QDateTime::currentDateTime ();
96
101
97
102
return *( currentEntry->image );
98
103
}
@@ -109,7 +114,11 @@ const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, con
109
114
cachePicture ( currentEntry );
110
115
}
111
116
112
- // update lastUsed with current date time
117
+ // debug: display current cache usage
118
+ QgsDebugMsg (" cache size: " + QString::number ( mTotalSize ) );
119
+
120
+ // set entry timestamp to current time
121
+ currentEntry->lastUsed = QDateTime::currentDateTime ();
113
122
114
123
return *( currentEntry->picture );
115
124
}
@@ -122,11 +131,44 @@ QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, cons
122
131
123
132
replaceParamsAndCacheSvg ( entry );
124
133
125
- mEntries .insert ( entry->lastUsed , entry );
126
134
mEntryLookup .insert ( file, entry );
127
135
return entry;
128
136
}
129
137
138
+ void QgsSvgCache::containsParams ( const QString& path, bool & hasFillParam, bool & hasOutlineParam, bool & hasOutlineWidthParam ) const
139
+ {
140
+ hasFillParam = false ;
141
+ hasOutlineParam = false ;
142
+ hasOutlineWidthParam = false ;
143
+
144
+ QFile svgFile ( path );
145
+ if ( !svgFile.open ( QIODevice::ReadOnly ) )
146
+ {
147
+ return ;
148
+ }
149
+
150
+ QDomDocument svgDoc;
151
+ if ( !svgDoc.setContent ( &svgFile ) )
152
+ {
153
+ return ;
154
+ }
155
+
156
+ // there are surely faster ways to get this information
157
+ QString content = svgDoc.toString ();
158
+ if ( content.contains (" param(fill" ) )
159
+ {
160
+ hasFillParam = true ;
161
+ }
162
+ if ( content.contains (" param(outline" ) )
163
+ {
164
+ hasOutlineParam = true ;
165
+ }
166
+ if ( content.contains (" param(outline-width)" ) )
167
+ {
168
+ hasOutlineWidthParam = true ;
169
+ }
170
+ }
171
+
130
172
void QgsSvgCache::replaceParamsAndCacheSvg ( QgsSvgCacheEntry* entry )
131
173
{
132
174
if ( !entry )
@@ -151,6 +193,7 @@ void QgsSvgCache::replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry )
151
193
replaceElemParams ( docElem, entry->fill , entry->outline , entry->outlineWidth );
152
194
153
195
entry->svgContent = svgDoc.toByteArray ();
196
+ mTotalSize += entry->svgContent .size ();
154
197
}
155
198
156
199
void QgsSvgCache::cacheImage ( QgsSvgCacheEntry* entry )
@@ -172,6 +215,7 @@ void QgsSvgCache::cacheImage( QgsSvgCacheEntry* entry )
172
215
r.render ( &p );
173
216
174
217
entry->image = image;
218
+ mTotalSize += (image->width () * image->height () * 32 );
175
219
}
176
220
177
221
void QgsSvgCache::cachePicture ( QgsSvgCacheEntry *entry )
@@ -195,6 +239,7 @@ void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry )
195
239
QPainter painter ( picture );
196
240
renderer.render ( &painter, rect );
197
241
entry->picture = picture;
242
+ mTotalSize += entry->picture ->size ();
198
243
}
199
244
200
245
QgsSvgCacheEntry* QgsSvgCache::cacheEntry ( const QString& file, double size, const QColor& fill, const QColor& outline , double outlineWidth,
@@ -263,3 +308,9 @@ void QgsSvgCache::replaceElemParams( QDomElement& elem, const QColor& fill, cons
263
308
}
264
309
}
265
310
311
+ void QgsSvgCache::removeCacheEntry ( QString s, QgsSvgCacheEntry* entry )
312
+ {
313
+ delete entry;
314
+ mEntryLookup .remove ( s , entry );
315
+ }
316
+
0 commit comments