Skip to content
Permalink
Browse files

Use SVG cache to handle parameterized SVG files in composer picture

selection widget (fix #14385)
  • Loading branch information
nyalldawson committed Mar 7, 2016
1 parent b1b7b65 commit 94e7720fe365794be5ea09564f0bc65577a5e068
Showing with 35 additions and 1 deletion.
  1. +32 −1 src/app/composer/qgscomposerpicturewidget.cpp
  2. +3 −0 src/app/composer/qgscomposerpicturewidget.h
@@ -22,6 +22,7 @@
#include "qgscomposeritemwidget.h"
#include "qgscomposition.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgssvgcache.h"
#include <QDoubleValidator>
#include <QFileDialog>
#include <QFileInfo>
@@ -435,6 +436,35 @@ void QgsComposerPictureWidget::setGuiElementValues()
}
}

QIcon QgsComposerPictureWidget::svgToIcon( const QString& filePath ) const
{
QColor fill, outline;
double outlineWidth, fillOpacity, outlineOpacity;
bool fillParam, fillOpacityParam, outlineParam, outlineWidthParam, outlineOpacityParam;
bool hasDefaultFillColor = false, hasDefaultFillOpacity = false, hasDefaultOutlineColor = false,
hasDefaultOutlineWidth = false, hasDefaultOutlineOpacity = false;
QgsSvgCache::instance()->containsParams( filePath, fillParam, hasDefaultFillColor, fill,
fillOpacityParam, hasDefaultFillOpacity, fillOpacity,
outlineParam, hasDefaultOutlineColor, outline,
outlineWidthParam, hasDefaultOutlineWidth, outlineWidth,
outlineOpacityParam, hasDefaultOutlineOpacity, outlineOpacity );

//if defaults not set in symbol, use these values
if ( !hasDefaultFillColor )
fill = QColor( 200, 200, 200 );
fill.setAlphaF( hasDefaultFillOpacity ? fillOpacity : 1.0 );
if ( !hasDefaultOutlineColor )
outline = Qt::black;
outline.setAlphaF( hasDefaultOutlineOpacity ? outlineOpacity : 1.0 );
if ( !hasDefaultOutlineWidth )
outlineWidth = 0.6;

bool fitsInCache; // should always fit in cache at these sizes (i.e. under 559 px ^ 2, or half cache size)
const QImage& img = QgsSvgCache::instance()->svgAsImage( filePath, 30.0, fill, outline, outlineWidth, 3.5 /*appr. 88 dpi*/, 1.0, fitsInCache );

return QIcon( QPixmap::fromImage( img ) );
}

int QgsComposerPictureWidget::addDirectoryToPreview( const QString& path )
{
//go through all files of a directory
@@ -484,7 +514,8 @@ int QgsComposerPictureWidget::addDirectoryToPreview( const QString& path )

if ( fileIsSvg )
{
QIcon icon( filePath );
// render SVG file
QIcon icon = svgToIcon( filePath );
listItem->setIcon( icon );
}
else //for pixel formats: create icon from scaled pixmap
@@ -84,6 +84,9 @@ class QgsComposerPictureWidget: public QgsComposerItemBaseWidget, private Ui::Qg
bool testImageFile( const QString& filename ) const;
/** Updates the map combo box with the current composer map ids*/
void refreshMapComboBox();

//! Renders an svg file to a QIcon, correctly handling any SVG parameters present in the file
QIcon svgToIcon( const QString& filePath ) const;
};

#endif

0 comments on commit 94e7720

Please sign in to comment.
You can’t perform that action at this time.