From 87c8c3e6eedd4cc66f63883fa1fa4633844faba7 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 15 Mar 2022 11:56:14 +0100 Subject: [PATCH] [libcalamaresui] Convenience for 'give-me-one-of-these-icons' --- src/libcalamaresui/Branding.cpp | 73 ++++++++++++++++++++++----------- src/libcalamaresui/Branding.h | 7 ++++ 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/src/libcalamaresui/Branding.cpp b/src/libcalamaresui/Branding.cpp index b723f5dc7..49c5dab11 100644 --- a/src/libcalamaresui/Branding.cpp +++ b/src/libcalamaresui/Branding.cpp @@ -235,31 +235,35 @@ Branding::Branding( const QString& brandingFilePath, QObject* parent ) { QStringLiteral( "VARIANT" ), relInfo.variant() }, { QStringLiteral( "VARIANT_ID" ), relInfo.variantId() }, { QStringLiteral( "LOGO" ), relInfo.logo() } } }; - auto expand = [&]( const QString& s ) -> QString { - return KMacroExpander::expandMacros( s, relMap, QLatin1Char( '@' ) ); - }; + auto expand = [ & ]( const QString& s ) -> QString + { return KMacroExpander::expandMacros( s, relMap, QLatin1Char( '@' ) ); }; #else auto expand = []( const QString& s ) -> QString { return s; }; #endif // Massage the strings, images and style sections. loadStrings( m_strings, doc, "strings", expand ); - loadStrings( m_images, doc, "images", [&]( const QString& s ) -> QString { - // See also image() - const QString imageName( expand( s ) ); - QFileInfo imageFi( componentDir.absoluteFilePath( imageName ) ); - if ( !imageFi.exists() ) - { - const auto icon = QIcon::fromTheme( imageName ); - // Not found, bail out with the filename used - if ( icon.isNull() ) - { - bail( m_descriptorPath, - QString( "Image file %1 does not exist." ).arg( imageFi.absoluteFilePath() ) ); - } - return imageName; // Not turned into a path - } - return imageFi.absoluteFilePath(); - } ); + loadStrings( m_images, + doc, + "images", + [ & ]( const QString& s ) -> QString + { + // See also image() + const QString imageName( expand( s ) ); + QFileInfo imageFi( componentDir.absoluteFilePath( imageName ) ); + if ( !imageFi.exists() ) + { + const auto icon = QIcon::fromTheme( imageName ); + // Not found, bail out with the filename used + if ( icon.isNull() ) + { + bail( + m_descriptorPath, + QString( "Image file %1 does not exist." ).arg( imageFi.absoluteFilePath() ) ); + } + return imageName; // Not turned into a path + } + return imageFi.absoluteFilePath(); + } ); loadStrings( m_style, doc, "style", []( const QString& s ) -> QString { return s; } ); m_uploadServer = uploadServerFromMap( CalamaresUtils::yamlMapToVariant( doc[ "uploadServer" ] ) ); @@ -348,19 +352,38 @@ Branding::image( const QString& imageName, const QSize& size ) const { QDir componentDir( componentDirectory() ); 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 ); // 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 _stylesheet( const QDir& dir ) { diff --git a/src/libcalamaresui/Branding.h b/src/libcalamaresui/Branding.h index 899b14c78..1654b4d56 100644 --- a/src/libcalamaresui/Branding.h +++ b/src/libcalamaresui/Branding.h @@ -198,6 +198,13 @@ public: */ 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. * * The file is loaded every time this function is called, so