Skip to content
Permalink
Browse files

Add option to apply parameterized colors to SVGs returned by

QgsApplication::getThemePixmap
  • Loading branch information
nyalldawson committed Oct 13, 2020
1 parent 364e2e9 commit b02d7b33bd7ce879095a15fbb2242ffacf1ac5f8
Showing with 21 additions and 13 deletions.
  1. +5 −1 python/core/auto_generated/qgsapplication.sip.in
  2. +10 −11 src/core/qgsapplication.cpp
  3. +6 −1 src/core/qgsapplication.h
@@ -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();
@@ -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 )
@@ -19,6 +19,7 @@
#include <QApplication>
#include <QEvent>
#include <QStringList>
#include <QColor>

#include "qgis_sip.h"
#include "qgsconfig.h"
@@ -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();

0 comments on commit b02d7b3

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