Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use SVG cache to handle parameterized SVG files in composer picture
selection widget (fix #14385)

(cherry-picked from 94e7720)
  • Loading branch information
nyalldawson committed Mar 20, 2016
1 parent 780c587 commit 9542653
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/app/composer/qgscomposerpicturewidget.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgscomposeritemwidget.h"
#include "qgscomposition.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgssvgcache.h"
#include <QDoubleValidator>
#include <QFileDialog>
#include <QFileInfo>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/app/composer/qgscomposerpicturewidget.h
Expand Up @@ -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 9542653

Please sign in to comment.