Skip to content

Commit

Permalink
Add option to apply parameterized colors to SVGs returned by
Browse files Browse the repository at this point in the history
QgsApplication::getThemePixmap
  • Loading branch information
nyalldawson committed Oct 13, 2020
1 parent 364e2e9 commit b02d7b3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
6 changes: 5 additions & 1 deletion python/core/auto_generated/qgsapplication.sip.in
Expand Up @@ -356,10 +356,14 @@ Cursors are automatically scaled to look like a 16px cursor on 96dpi
screens.
%End

static QPixmap getThemePixmap( const QString &name );
static QPixmap getThemePixmap( const QString &name, const QColor &foreColor = QColor(), const QColor &backColor = QColor(), int size = 16 );
%Docstring
Helper to get a theme icon as a pixmap. It will fall back to the
default theme if the active theme does not have the required icon.

If ``foreColor`` or ``backColor`` are specified, then these colors will
be used for parameterized colors in SVG files wherever available. If
colors are specified then the ``size`` argument also must be set.
%End

static QString userStylePath();
Expand Down
21 changes: 10 additions & 11 deletions src/core/qgsapplication.cpp
Expand Up @@ -715,20 +715,19 @@ QCursor QgsApplication::getThemeCursor( Cursor cursor )
}

// TODO: add some caching mechanism ?
QPixmap QgsApplication::getThemePixmap( const QString &name )
QPixmap QgsApplication::getThemePixmap( const QString &name, const QColor &foreColor, const QColor &backColor, const int size )
{
QString myPreferredPath = activeThemePath() + QDir::separator() + name;
QString myDefaultPath = defaultThemePath() + QDir::separator() + name;
if ( QFile::exists( myPreferredPath ) )
const QString preferredPath = activeThemePath() + QDir::separator() + name;
const QString defaultPath = defaultThemePath() + QDir::separator() + name;
const QString path = QFile::exists( preferredPath ) ? preferredPath : defaultPath;
if ( foreColor.isValid() || backColor.isValid() )
{
return QPixmap( myPreferredPath );
}
else
{
//could still return an empty icon if it
//doesn't exist in the default theme either!
return QPixmap( myDefaultPath );
bool fitsInCache = false;
const QImage image = svgCache()->svgAsImage( path, size, backColor, foreColor, 1, 1, fitsInCache );
return QPixmap::fromImage( image );
}

return QPixmap( path );
}

void QgsApplication::setThemeName( const QString &themeName )
Expand Down
7 changes: 6 additions & 1 deletion src/core/qgsapplication.h
Expand Up @@ -19,6 +19,7 @@
#include <QApplication>
#include <QEvent>
#include <QStringList>
#include <QColor>

#include "qgis_sip.h"
#include "qgsconfig.h"
Expand Down Expand Up @@ -379,8 +380,12 @@ class CORE_EXPORT QgsApplication : public QApplication
/**
* Helper to get a theme icon as a pixmap. It will fall back to the
* default theme if the active theme does not have the required icon.
*
* If \a foreColor or \a backColor are specified, then these colors will
* be used for parameterized colors in SVG files wherever available. If
* colors are specified then the \a size argument also must be set.
*/
static QPixmap getThemePixmap( const QString &name );
static QPixmap getThemePixmap( const QString &name, const QColor &foreColor = QColor(), const QColor &backColor = QColor(), int size = 16 );

//! Returns the path to user's style.
static QString userStylePath();
Expand Down

0 comments on commit b02d7b3

Please sign in to comment.