[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,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 )
{

View File

@ -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