Skip to content

Commit 94e7720

Browse files
committedMar 7, 2016
Use SVG cache to handle parameterized SVG files in composer picture
selection widget (fix #14385)
1 parent b1b7b65 commit 94e7720

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed
 

‎src/app/composer/qgscomposerpicturewidget.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "qgscomposeritemwidget.h"
2323
#include "qgscomposition.h"
2424
#include "qgsexpressionbuilderdialog.h"
25+
#include "qgssvgcache.h"
2526
#include <QDoubleValidator>
2627
#include <QFileDialog>
2728
#include <QFileInfo>
@@ -435,6 +436,35 @@ void QgsComposerPictureWidget::setGuiElementValues()
435436
}
436437
}
437438

439+
QIcon QgsComposerPictureWidget::svgToIcon( const QString& filePath ) const
440+
{
441+
QColor fill, outline;
442+
double outlineWidth, fillOpacity, outlineOpacity;
443+
bool fillParam, fillOpacityParam, outlineParam, outlineWidthParam, outlineOpacityParam;
444+
bool hasDefaultFillColor = false, hasDefaultFillOpacity = false, hasDefaultOutlineColor = false,
445+
hasDefaultOutlineWidth = false, hasDefaultOutlineOpacity = false;
446+
QgsSvgCache::instance()->containsParams( filePath, fillParam, hasDefaultFillColor, fill,
447+
fillOpacityParam, hasDefaultFillOpacity, fillOpacity,
448+
outlineParam, hasDefaultOutlineColor, outline,
449+
outlineWidthParam, hasDefaultOutlineWidth, outlineWidth,
450+
outlineOpacityParam, hasDefaultOutlineOpacity, outlineOpacity );
451+
452+
//if defaults not set in symbol, use these values
453+
if ( !hasDefaultFillColor )
454+
fill = QColor( 200, 200, 200 );
455+
fill.setAlphaF( hasDefaultFillOpacity ? fillOpacity : 1.0 );
456+
if ( !hasDefaultOutlineColor )
457+
outline = Qt::black;
458+
outline.setAlphaF( hasDefaultOutlineOpacity ? outlineOpacity : 1.0 );
459+
if ( !hasDefaultOutlineWidth )
460+
outlineWidth = 0.6;
461+
462+
bool fitsInCache; // should always fit in cache at these sizes (i.e. under 559 px ^ 2, or half cache size)
463+
const QImage& img = QgsSvgCache::instance()->svgAsImage( filePath, 30.0, fill, outline, outlineWidth, 3.5 /*appr. 88 dpi*/, 1.0, fitsInCache );
464+
465+
return QIcon( QPixmap::fromImage( img ) );
466+
}
467+
438468
int QgsComposerPictureWidget::addDirectoryToPreview( const QString& path )
439469
{
440470
//go through all files of a directory
@@ -484,7 +514,8 @@ int QgsComposerPictureWidget::addDirectoryToPreview( const QString& path )
484514

485515
if ( fileIsSvg )
486516
{
487-
QIcon icon( filePath );
517+
// render SVG file
518+
QIcon icon = svgToIcon( filePath );
488519
listItem->setIcon( icon );
489520
}
490521
else //for pixel formats: create icon from scaled pixmap

‎src/app/composer/qgscomposerpicturewidget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ class QgsComposerPictureWidget: public QgsComposerItemBaseWidget, private Ui::Qg
8484
bool testImageFile( const QString& filename ) const;
8585
/** Updates the map combo box with the current composer map ids*/
8686
void refreshMapComboBox();
87+
88+
//! Renders an svg file to a QIcon, correctly handling any SVG parameters present in the file
89+
QIcon svgToIcon( const QString& filePath ) const;
8790
};
8891

8992
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.