Skip to content

Commit b2c7bae

Browse files
committedJul 4, 2011
Fix some problems with svg cache
1 parent a946a20 commit b2c7bae

File tree

4 files changed

+28
-56
lines changed

4 files changed

+28
-56
lines changed
 

‎src/core/symbology-ng/qgsmarkersymbollayerv2.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -555,24 +555,20 @@ void QgsSvgMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Re
555555
outputOffset = _rotatedOffset( outputOffset, mAngle );
556556
p->translate( point + outputOffset );
557557

558-
if ( context.renderHints() & QgsSymbolV2::DataDefinedSizeScale )
559-
{
560-
double s = mSize / mOrigSize;
561-
p->scale( s, s );
562-
}
558+
int size = (int)( context.outputLineWidth( mSize ) );
563559

564560
if ( mAngle != 0 )
565561
p->rotate( mAngle );
566562

567563
if ( doubleNear( context.renderContext().rasterScaleFactor(), 1.0, 0.1 ) )
568564
{
569-
const QImage& img = QgsSvgCache::instance()->svgAsImage( mPath, mSize, mFillColor, mOutlineColor, mOutlineWidth,
565+
const QImage& img = QgsSvgCache::instance()->svgAsImage( mPath, size, mFillColor, mOutlineColor, mOutlineWidth,
570566
context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
571567
p->drawImage( -img.width() / 2.0, -img.width() / 2.0, img );
572568
}
573569
else
574570
{
575-
const QPicture& pct = QgsSvgCache::instance()->svgAsPicture( mPath, mSize, mFillColor, mOutlineColor, mOutlineWidth,
571+
const QPicture& pct = QgsSvgCache::instance()->svgAsPicture( mPath, size, mFillColor, mOutlineColor, mOutlineWidth,
576572
context.renderContext().scaleFactor(), context.renderContext().rasterScaleFactor() );
577573
p->drawPicture( 0, 0, pct );
578574
}

‎src/core/symbology-ng/qgssvgcache.cpp

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ QgsSvgCache::~QgsSvgCache()
9595
}
9696

9797

98-
const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
98+
const QImage& QgsSvgCache::svgAsImage( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
9999
double widthScaleFactor, double rasterScaleFactor )
100100
{
101101
QgsSvgCacheEntry* currentEntry = cacheEntry( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
@@ -111,7 +111,7 @@ const QImage& QgsSvgCache::svgAsImage( const QString& file, double size, const Q
111111
return *( currentEntry->image );
112112
}
113113

114-
const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
114+
const QPicture& QgsSvgCache::svgAsPicture( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
115115
double widthScaleFactor, double rasterScaleFactor )
116116
{
117117
QgsSvgCacheEntry* currentEntry = cacheEntry( file, size, fill, outline, outlineWidth, widthScaleFactor, rasterScaleFactor );
@@ -127,7 +127,7 @@ const QPicture& QgsSvgCache::svgAsPicture( const QString& file, double size, con
127127
return *( currentEntry->picture );
128128
}
129129

130-
QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
130+
QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
131131
double widthScaleFactor, double rasterScaleFactor )
132132
{
133133
QgsSvgCacheEntry* entry = new QgsSvgCacheEntry( file, size, outlineWidth, widthScaleFactor, rasterScaleFactor, fill, outline );
@@ -159,37 +159,6 @@ QgsSvgCacheEntry* QgsSvgCache::insertSVG( const QString& file, double size, cons
159159
void QgsSvgCache::containsParams( const QString& path, bool& hasFillParam, QColor& defaultFillColor, bool& hasOutlineParam, QColor& defaultOutlineColor,
160160
bool& hasOutlineWidthParam, double& defaultOutlineWidth ) const
161161
{
162-
/*hasFillParam = false;
163-
hasOutlineParam = false;
164-
hasOutlineWidthParam = false;
165-
166-
QFile svgFile( path );
167-
if ( !svgFile.open( QIODevice::ReadOnly ) )
168-
{
169-
return;
170-
}
171-
172-
QDomDocument svgDoc;
173-
if ( !svgDoc.setContent( &svgFile ) )
174-
{
175-
return;
176-
}
177-
178-
//there are surely faster ways to get this information
179-
QString content = svgDoc.toString();
180-
if ( content.contains( "param(fill" ) )
181-
{
182-
hasFillParam = true;
183-
}
184-
if ( content.contains( "param(outline" ) )
185-
{
186-
hasOutlineParam = true;
187-
}
188-
if ( content.contains( "param(outline-width)" ) )
189-
{
190-
hasOutlineWidthParam = true;
191-
}*/
192-
193162
defaultFillColor = QColor( Qt::black );
194163
defaultOutlineColor = QColor( Qt::black );
195164
defaultOutlineWidth = 1.0;
@@ -247,7 +216,7 @@ void QgsSvgCache::cacheImage( QgsSvgCacheEntry* entry )
247216
delete entry->image;
248217
entry->image = 0;
249218

250-
double imageSize = entry->size * entry->widthScaleFactor * entry->rasterScaleFactor;
219+
int imageSize = entry->size;
251220
QImage* image = new QImage( imageSize, imageSize, QImage::Format_ARGB32_Premultiplied );
252221
image->fill( 0 ); // transparent background
253222

@@ -271,8 +240,7 @@ void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry )
271240

272241
//correct QPictures dpi correction
273242
QPicture* picture = new QPicture();
274-
double dpi = entry->widthScaleFactor * 25.4 * entry->rasterScaleFactor;
275-
double pictureSize = entry->size * entry->widthScaleFactor / dpi * picture->logicalDpiX();
243+
double pictureSize = entry->size / 25.4 / entry->rasterScaleFactor * picture->logicalDpiX();
276244
QRectF rect( QPointF( -pictureSize / 2.0, -pictureSize / 2.0 ), QSizeF( pictureSize, pictureSize ) );
277245

278246

@@ -283,7 +251,7 @@ void QgsSvgCache::cachePicture( QgsSvgCacheEntry *entry )
283251
mTotalSize += entry->picture->size();
284252
}
285253

286-
QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
254+
QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
287255
double widthScaleFactor, double rasterScaleFactor )
288256
{
289257
//search entries in mEntryLookup
@@ -302,7 +270,6 @@ QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, con
302270
}
303271
}
304272

305-
306273
//if not found: create new entry
307274
//cache and replace params in svg content
308275
if ( !currentEntry )
@@ -312,10 +279,18 @@ QgsSvgCacheEntry* QgsSvgCache::cacheEntry( const QString& file, double size, con
312279
else
313280
{
314281
takeEntryFromList( currentEntry );
315-
mMostRecentEntry->nextEntry = currentEntry;
316-
currentEntry->previousEntry = mMostRecentEntry;
317-
currentEntry->nextEntry = 0;
318-
mMostRecentEntry = currentEntry;
282+
if( !mMostRecentEntry ) //list is empty
283+
{
284+
mMostRecentEntry = currentEntry;
285+
mLeastRecentEntry = currentEntry;
286+
}
287+
else
288+
{
289+
mMostRecentEntry->nextEntry = currentEntry;
290+
currentEntry->previousEntry = mMostRecentEntry;
291+
currentEntry->nextEntry = 0;
292+
mMostRecentEntry = currentEntry;
293+
}
319294
}
320295

321296
//debugging
@@ -537,6 +512,7 @@ void QgsSvgCache::trimToMaximumSize()
537512
entry = entry->nextEntry;
538513

539514
takeEntryFromList( bkEntry );
515+
mEntryLookup.remove( bkEntry->file, bkEntry );
540516
mTotalSize -= bkEntry->dataSize();
541517
delete bkEntry;
542518
}

‎src/core/symbology-ng/qgssvgcache.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct CORE_EXPORT QgsSvgCacheEntry
3434
~QgsSvgCacheEntry();
3535

3636
QString file;
37-
double size;
37+
int size; //size in pixel
3838
double outlineWidth;
3939
double widthScaleFactor;
4040
double rasterScaleFactor;
@@ -65,9 +65,9 @@ class CORE_EXPORT QgsSvgCache
6565
static QgsSvgCache* instance();
6666
~QgsSvgCache();
6767

68-
const QImage& svgAsImage( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
68+
const QImage& svgAsImage( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
6969
double widthScaleFactor, double rasterScaleFactor );
70-
const QPicture& svgAsPicture( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
70+
const QPicture& svgAsPicture( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
7171
double widthScaleFactor, double rasterScaleFactor );
7272

7373
/**Tests if an svg file contains parameters for fill, outline color, outline width. If yes, possible default values are returned. If there are several
@@ -79,14 +79,14 @@ class CORE_EXPORT QgsSvgCache
7979
QgsSvgCache();
8080

8181
/**Creates new cache entry and returns pointer to it*/
82-
QgsSvgCacheEntry* insertSVG( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
82+
QgsSvgCacheEntry* insertSVG( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
8383
double widthScaleFactor, double rasterScaleFactor );
8484

8585
void replaceParamsAndCacheSvg( QgsSvgCacheEntry* entry );
8686
void cacheImage( QgsSvgCacheEntry* entry );
8787
void cachePicture( QgsSvgCacheEntry* entry );
8888
/**Returns entry from cache or creates a new entry if it does not exist already*/
89-
QgsSvgCacheEntry* cacheEntry( const QString& file, double size, const QColor& fill, const QColor& outline, double outlineWidth,
89+
QgsSvgCacheEntry* cacheEntry( const QString& file, int size, const QColor& fill, const QColor& outline, double outlineWidth,
9090
double widthScaleFactor, double rasterScaleFactor );
9191

9292
/**Removes the least used items until the maximum size is under the limit*/

‎src/gui/symbology-ng/qgssymbollayerv2widget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ class QgsSvgListModel : public QAbstractListModel
547547
bool fillParam, outlineParam, outlineWidthParam;
548548
QgsSvgCache::instance()->containsParams( entry, fillParam, fill, outlineParam, outline, outlineWidthParam, outlineWidth );
549549

550-
const QImage& img = QgsSvgCache::instance()->svgAsImage( entry, 8, fill, outline, outlineWidth, 3.5 /*appr. 88 dpi*/, 1.0 );
550+
const QImage& img = QgsSvgCache::instance()->svgAsImage( entry, 30, fill, outline, outlineWidth, 3.5 /*appr. 88 dpi*/, 1.0 );
551551
pixmap = QPixmap::fromImage( img );
552552
QPixmapCache::insert( entry, pixmap );
553553
}

0 commit comments

Comments
 (0)
Please sign in to comment.