[libcalamaresui] Convenience for 'give-me-one-of-these-icons'

This commit is contained in:
Adriaan de Groot 2022-03-15 11:56:14 +01:00
parent 1a6fb1c3d2
commit 87c8c3e6ee
2 changed files with 55 additions and 25 deletions

View File

@ -235,15 +235,18 @@ Branding::Branding( const QString& brandingFilePath, QObject* parent )
{ QStringLiteral( "VARIANT" ), relInfo.variant() }, { QStringLiteral( "VARIANT" ), relInfo.variant() },
{ QStringLiteral( "VARIANT_ID" ), relInfo.variantId() }, { QStringLiteral( "VARIANT_ID" ), relInfo.variantId() },
{ QStringLiteral( "LOGO" ), relInfo.logo() } } }; { QStringLiteral( "LOGO" ), relInfo.logo() } } };
auto expand = [&]( const QString& s ) -> QString { auto expand = [ & ]( const QString& s ) -> QString
return KMacroExpander::expandMacros( s, relMap, QLatin1Char( '@' ) ); { return KMacroExpander::expandMacros( s, relMap, QLatin1Char( '@' ) ); };
};
#else #else
auto expand = []( const QString& s ) -> QString { return s; }; auto expand = []( const QString& s ) -> QString { return s; };
#endif #endif
// Massage the strings, images and style sections. // Massage the strings, images and style sections.
loadStrings( m_strings, doc, "strings", expand ); loadStrings( m_strings, doc, "strings", expand );
loadStrings( m_images, doc, "images", [&]( const QString& s ) -> QString { loadStrings( m_images,
doc,
"images",
[ & ]( const QString& s ) -> QString
{
// See also image() // See also image()
const QString imageName( expand( s ) ); const QString imageName( expand( s ) );
QFileInfo imageFi( componentDir.absoluteFilePath( imageName ) ); QFileInfo imageFi( componentDir.absoluteFilePath( imageName ) );
@ -253,7 +256,8 @@ Branding::Branding( const QString& brandingFilePath, QObject* parent )
// Not found, bail out with the filename used // Not found, bail out with the filename used
if ( icon.isNull() ) if ( icon.isNull() )
{ {
bail( m_descriptorPath, bail(
m_descriptorPath,
QString( "Image file %1 does not exist." ).arg( imageFi.absoluteFilePath() ) ); QString( "Image file %1 does not exist." ).arg( imageFi.absoluteFilePath() ) );
} }
return imageName; // Not turned into a path return imageName; // Not turned into a path
@ -348,18 +352,37 @@ Branding::image( const QString& imageName, const QSize& size ) const
{ {
QDir componentDir( componentDirectory() ); QDir componentDir( componentDirectory() );
QFileInfo imageFi( componentDir.absoluteFilePath( imageName ) ); QFileInfo imageFi( componentDir.absoluteFilePath( imageName ) );
if ( !imageFi.exists() ) if ( imageFi.exists() )
{
return ImageRegistry::instance()->pixmap( imageFi.absoluteFilePath(), size );
}
else
{ {
const auto icon = QIcon::fromTheme( imageName ); const auto icon = QIcon::fromTheme( imageName );
// Not found, bail out with the filename used // Not found, bail out with the filename used
if ( icon.isNull() ) if ( !icon.isNull() )
{ {
return QPixmap();
}
return icon.pixmap( size ); return icon.pixmap( size );
} }
return ImageRegistry::instance()->pixmap( imageFi.absoluteFilePath(), size );
} }
return QPixmap();
}
QPixmap
Branding::image( const QStringList& list, const QSize& size ) const
{
QDir componentDir( componentDirectory() );
for ( const QString& imageName : list )
{
auto p = image( imageName, size );
if ( !p.isNull() )
{
return p;
}
}
return QPixmap();
}
static QString static QString
_stylesheet( const QDir& dir ) _stylesheet( const QDir& dir )

View File

@ -198,6 +198,13 @@ public:
*/ */
QPixmap image( const QString& name, const QSize& size ) const; QPixmap image( const QString& name, const QSize& size ) const;
/** @brief Look up image with alternate names
*
* Calls image() for each name in the @p list and returns the first
* one that is non-null. May return a null pixmap if nothing is found.
*/
QPixmap image( const QStringList& list, const QSize& size ) const;
/** @brief Stylesheet to apply for this branding. May be empty. /** @brief Stylesheet to apply for this branding. May be empty.
* *
* The file is loaded every time this function is called, so * The file is loaded every time this function is called, so