@@ -48,6 +48,20 @@ bool QgsSvgCacheEntry::operator==( const QgsSvgCacheEntry& other ) const
48
48
&& other.rasterScaleFactor == rasterScaleFactor && other.fill == fill && other.outline == outline );
49
49
}
50
50
51
+ int QgsSvgCacheEntry::dataSize () const
52
+ {
53
+ int size = svgContent.size ();
54
+ if ( picture )
55
+ {
56
+ size += picture->size ();
57
+ }
58
+ if ( image )
59
+ {
60
+ size += ( image->width () * image->height () * 32 );
61
+ }
62
+ return size;
63
+ }
64
+
51
65
QString file;
52
66
double size;
53
67
double outlineWidth;
@@ -67,7 +81,7 @@ QgsSvgCache* QgsSvgCache::instance()
67
81
return mInstance ;
68
82
}
69
83
70
- QgsSvgCache::QgsSvgCache (): mTotalSize( 0 )
84
+ QgsSvgCache::QgsSvgCache (): mTotalSize( 0 ), mLeastRecentEntry( 0 ), mMostRecentEntry( 0 )
71
85
{
72
86
}
73
87
@@ -91,14 +105,9 @@ const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const Q
91
105
if ( !currentEntry->image )
92
106
{
93
107
cacheImage ( currentEntry );
108
+ trimToMaximumSize ();
94
109
}
95
110
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 ();
101
-
102
111
return *( currentEntry->image );
103
112
}
104
113
@@ -112,26 +121,38 @@ const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, con
112
121
if ( !currentEntry->picture )
113
122
{
114
123
cachePicture ( currentEntry );
124
+ trimToMaximumSize ();
115
125
}
116
126
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 ();
122
-
123
127
return *( currentEntry->picture );
124
128
}
125
129
126
130
QgsSvgCacheEntry* QgsSvgCache::insertSVG ( const QString& file, double size, const QColor& fill, const QColor& outline , double outlineWidth,
127
131
double widthScaleFactor, double rasterScaleFactor )
128
132
{
129
133
QgsSvgCacheEntry* entry = new QgsSvgCacheEntry ( file, size, outlineWidth, widthScaleFactor, rasterScaleFactor, fill, outline );
130
- entry->lastUsed = QDateTime::currentDateTime ();
131
134
132
135
replaceParamsAndCacheSvg ( entry );
133
136
134
137
mEntryLookup .insert ( file, entry );
138
+
139
+ // insert to most recent place in entry list
140
+ if ( !mMostRecentEntry ) // inserting first entry
141
+ {
142
+ mLeastRecentEntry = entry;
143
+ mMostRecentEntry = entry;
144
+ entry->previousEntry = 0 ;
145
+ entry->nextEntry = 0 ;
146
+ }
147
+ else
148
+ {
149
+ entry->previousEntry = mMostRecentEntry ;
150
+ entry->nextEntry = 0 ;
151
+ mMostRecentEntry ->nextEntry = entry;
152
+ mMostRecentEntry = entry;
153
+ }
154
+
155
+ trimToMaximumSize ();
135
156
return entry;
136
157
}
137
158
@@ -268,6 +289,18 @@ QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, con
268
289
{
269
290
currentEntry = insertSVG ( file, size, fill, outline , outlineWidth, widthScaleFactor, rasterScaleFactor );
270
291
}
292
+ else
293
+ {
294
+ takeEntryFromList ( currentEntry );
295
+ mMostRecentEntry ->nextEntry = currentEntry;
296
+ currentEntry->previousEntry = mMostRecentEntry ;
297
+ currentEntry->nextEntry = 0 ;
298
+ mMostRecentEntry = currentEntry;
299
+ }
300
+
301
+ // debugging
302
+ // printEntryList();
303
+
271
304
return currentEntry;
272
305
}
273
306
@@ -350,3 +383,58 @@ void QgsSvgCache::removeCacheEntry( QString s, QgsSvgCacheEntry* entry )
350
383
mEntryLookup .remove ( s , entry );
351
384
}
352
385
386
+ void QgsSvgCache::printEntryList ()
387
+ {
388
+ QgsDebugMsg ( " ****************svg cache entry list*************************" );
389
+ QgsDebugMsg ( " Cache size: " + QString::number ( mTotalSize ) );
390
+ QgsSvgCacheEntry* entry = mLeastRecentEntry ;
391
+ while ( entry )
392
+ {
393
+ QgsDebugMsg ( " ***Entry:" );
394
+ QgsDebugMsg ( " File:" + entry->file );
395
+ QgsDebugMsg ( " Size:" + QString::number ( entry->size ) );
396
+ QgsDebugMsg ( " Width scale factor" + QString::number ( entry->widthScaleFactor ) );
397
+ QgsDebugMsg ( " Raster scale factor" + QString::number ( entry->rasterScaleFactor ) );
398
+ entry = entry->nextEntry ;
399
+ }
400
+ }
401
+
402
+ void QgsSvgCache::trimToMaximumSize ()
403
+ {
404
+ QgsSvgCacheEntry* entry = mLeastRecentEntry ;
405
+ while ( entry && ( mTotalSize > mMaximumSize ) )
406
+ {
407
+ QgsSvgCacheEntry* bkEntry = entry;
408
+ entry = entry->nextEntry ;
409
+
410
+ takeEntryFromList ( bkEntry );
411
+ mTotalSize -= bkEntry->dataSize ();
412
+ delete bkEntry;
413
+ }
414
+ }
415
+
416
+ void QgsSvgCache::takeEntryFromList ( QgsSvgCacheEntry* entry )
417
+ {
418
+ if ( !entry )
419
+ {
420
+ return ;
421
+ }
422
+
423
+ if ( entry->previousEntry )
424
+ {
425
+ entry->previousEntry ->nextEntry = entry->nextEntry ;
426
+ }
427
+ else
428
+ {
429
+ mLeastRecentEntry = entry->nextEntry ;
430
+ }
431
+ if ( entry->nextEntry )
432
+ {
433
+ entry->nextEntry ->previousEntry = entry->previousEntry ;
434
+ }
435
+ else
436
+ {
437
+ mMostRecentEntry = entry->previousEntry ;
438
+ }
439
+ }
440
+
0 commit comments