Skip to content
Permalink
Browse files
QIcon linked files are lazily evaluated, so we can't use a temporary
file which is immediately deleted right after the QIcon creation
to store SVG icon content -- instead we need to place the svg in a
temporary folder which exists for the lifetime of the QGIS session
  • Loading branch information
nyalldawson committed May 17, 2021
1 parent be803e9 commit f1b436add86776a051d8d145743848f2bb103f9a
@@ -918,6 +918,8 @@
<file>themes/default/mIndicatorLowAccuracy.svg</file>
<file>themes/default/mIconFolderParams.svg</file>
<file>themes/default/mIconFolderOpenParams.svg</file>
<file>themes/default/mIconFolderLinkParams.svg</file>
<file>themes/default/mIconFolderHomeParams.svg</file>
</qresource>
<qresource prefix="/images/tips">
<file alias="symbol_levels.png">qgis_tips/symbol_levels.png</file>
@@ -0,0 +1 @@
<svg height="16" width="16" xmlns="http://www.w3.org/2000/svg"><path d="m1.5 2.501v11.249s.019.75.75.75h11.5s.749 0 .749-.75l.001-9.25h-5.3l-2-2h-5.7z" fill="param(fill) #eee" fill-opacity="param(fill-opacity) 1" stroke="param(outline) #888a85" stroke-opacity="param(outline-opacity) 1" /><g fill="none" stroke="#333" stroke-linecap="round"><path d="m4.505 9.5 3.495-3.5 3.505 3.5"/><path d="m5.505 9v3.5h5v-3.5"/></g></svg>
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><path d="M1.5 2.501V13.75s.019.75.75.75h11.5s.749 0 .749-.75L14.5 4.5H9.2l-2-2H1.5z" fill="param(fill) #eee" fill-opacity="param(fill-opacity) 1" stroke="param(outline) #888a85" stroke-opacity="param(outline-opacity) 1" /><path d="M5.507 10.495h4M8.505 8.505l1.819 1.995-1.82 1.995" fill="none" stroke="#333" stroke-linecap="round"/></svg>
@@ -719,10 +719,13 @@ a fill and stroke color for the icon.
.. versionadded:: 3.4
%End

static QIcon homeDirIcon();
static QIcon homeDirIcon( const QColor &fillColor = QColor(), const QColor &strokeColor = QColor() );
%Docstring
Shared home directory icon.

Since QGIS 3.20 the optional ``fillColor`` and ``strokeColor`` arguments can be used to specify
a fill and stroke color for the icon.

.. versionadded:: 3.4
%End

@@ -173,6 +173,8 @@ Q_GLOBAL_STATIC( QString, sUserFullName )
Q_GLOBAL_STATIC_WITH_ARGS( QString, sPlatformName, ( "desktop" ) )
Q_GLOBAL_STATIC( QString, sTranslation )

Q_GLOBAL_STATIC( QTemporaryDir, sIconCacheDir )

QgsApplication::QgsApplication( int &argc, char **argv, bool GUIenabled, const QString &profileFolder, const QString &platformName )
: QApplication( argc, argv, GUIenabled )
{
@@ -648,9 +650,9 @@ QString QgsApplication::iconPath( const QString &iconFile )

QIcon QgsApplication::getThemeIcon( const QString &name, const QColor &fillColor, const QColor &strokeColor )
{
const QString cacheKey = name
+ ( fillColor.isValid() ? QStringLiteral( ":%1" ).arg( fillColor.name( QColor::HexArgb ) ) : QString() )
+ ( strokeColor.isValid() ? QStringLiteral( ":%1" ).arg( strokeColor.name( QColor::HexArgb ) ) : QString() );
const QString cacheKey = ( name.startsWith( '/' ) ? name.mid( 1 ) : name )
+ ( fillColor.isValid() ? QStringLiteral( "_%1" ).arg( fillColor.name( QColor::HexArgb ).mid( 1 ) ) : QString() )
+ ( strokeColor.isValid() ? QStringLiteral( "_%1" ).arg( strokeColor.name( QColor::HexArgb ).mid( 1 ) ) : QString() );
QgsApplication *app = instance();
if ( app && app->mIconCache.contains( cacheKey ) )
return app->mIconCache.value( cacheKey );
@@ -662,10 +664,20 @@ QIcon QgsApplication::getThemeIcon( const QString &name, const QColor &fillColor
{
// sizes are unused here!
const QByteArray svgContent = QgsApplication::svgCache()->svgContent( path, 16, fillColor, strokeColor, 1, 1 );
QTemporaryFile f;
if ( f.open() )

const QString iconPath = sIconCacheDir()->filePath( cacheKey + QStringLiteral( ".svg" ) );
QFile f( iconPath );
if ( f.open( QFile::WriteOnly | QFile::Truncate ) )
{
f.write( svgContent );
f.close();
f.close();
}
else
{
QgsDebugMsg( QStringLiteral( "Could not create colorized icon svg at %1" ).arg( iconPath ) );
return QIcon();
}

return QIcon( f.fileName() );
};

@@ -130,9 +130,11 @@ QIcon QgsDataCollectionItem::openDirIcon( const QColor &fillColor, const QColor
: QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderOpen.svg" ) );
}

QIcon QgsDataCollectionItem::homeDirIcon()
QIcon QgsDataCollectionItem::homeDirIcon( const QColor &fillColor, const QColor &strokeColor )
{
return QgsApplication::getThemeIcon( QStringLiteral( "mIconFolderHome.svg" ) );
return fillColor.isValid() || strokeColor.isValid()
? QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderHomeParams.svg" ), fillColor, strokeColor )
: QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderHome.svg" ) );
}

QgsAbstractDatabaseProviderConnection *QgsDataCollectionItem::databaseConnection() const
@@ -1172,7 +1174,7 @@ void QgsDirectoryItem::setCustomColor( const QString &directory, const QColor &c
QIcon QgsDirectoryItem::icon()
{
if ( mDirPath == QDir::homePath() )
return homeDirIcon();
return homeDirIcon( mIconColor, mIconColor.darker() );

// still loading? show the spinner
if ( state() == Populating )
@@ -1182,7 +1184,7 @@ QIcon QgsDirectoryItem::icon()
QFileInfo fi( mDirPath );
if ( fi.isDir() && fi.isSymLink() )
{
return QgsApplication::getThemeIcon( QStringLiteral( "mIconFolderLink.svg" ) );
return QgsApplication::getThemeIcon( QStringLiteral( "mIconFolderLink.svg" ), mIconColor, mIconColor.darker() );
}

// loaded? show the open dir icon
@@ -733,9 +733,13 @@ class CORE_EXPORT QgsDataCollectionItem : public QgsDataItem

/**
* Shared home directory icon.
*
* Since QGIS 3.20 the optional \a fillColor and \a strokeColor arguments can be used to specify
* a fill and stroke color for the icon.
*
* \since QGIS 3.4
*/
static QIcon homeDirIcon();
static QIcon homeDirIcon( const QColor &fillColor = QColor(), const QColor &strokeColor = QColor() );

};

@@ -107,12 +107,11 @@ void TestQgsApplication::themeIcon()

// with colors
icon = QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderParams.svg" ), QColor( 255, 100, 100 ), QColor( 255, 0, 0 ) );
QVERIFY( !icon.isNull() );
im = QImage( icon.pixmap( 16, 16 ).toImage() );
QVERIFY( renderCheck( QStringLiteral( "theme_icon_colors_1" ), im, 0 ) );

// different colors
icon = QgsApplication::getThemeIcon( QStringLiteral( "/mIconFolderParams.svg" ), QColor( 170, 255, 170 ), QColor( 0, 255, 0 ) );
QVERIFY( !icon.isNull() );
im = QImage( icon.pixmap( 16, 16 ).toImage() );
QVERIFY( renderCheck( QStringLiteral( "theme_icon_colors_2" ), im, 0 ) );
}

0 comments on commit f1b436a

Please sign in to comment.