From 544d905b2f31f6b556d500bf96651437bb6c504b Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 20 May 2019 16:20:31 +0200 Subject: [PATCH] [libcalamaresui] WIP: refactor again - the string-manipulation can be done through a lambda. That makes handling *strings* and *styles* simple. - still doesn't compile. --- src/libcalamaresui/Branding.cpp | 39 +++++++++++++++++---------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/libcalamaresui/Branding.cpp b/src/libcalamaresui/Branding.cpp index f9e5751f3..a618a901d 100644 --- a/src/libcalamaresui/Branding.cpp +++ b/src/libcalamaresui/Branding.cpp @@ -32,6 +32,8 @@ #include #include +#include + namespace Calamares { @@ -87,19 +89,26 @@ Branding::WindowDimension::suffixes() return names; } +/** @brief Load the @p map with strings from @p config + * + * Each key-value pair from @p config is inserted into the @p map, + * but the value is first transformed by the @p transform function, + * which may change strings. + */ +static void +loadStrings( QMap& map, const QVariantMap& config, const std::function< QString(const QString&) >& transform ) +{ + map.clear(); + for ( auto it = config.constBegin(); it != config.constEnd(); ++it ) + map.insert( it.key(), transform( it.value().toString() ) ); +} + /** @brief Load the @p map with strings from @p config * * If os-release is supported (with KF5 CoreAddons >= 5.58) then * special substitutions can be done as well. See the branding * documentation for details. */ -static void -loadStrings(QMap& map, const QVariantMap& config) -{ - map.clear(); - for ( auto it = config.constBegin(); it != config.constEnd(); ++it ) - map.insert( it.key(), it.value().toString() ); -} /** @brief Load the @p map of image-filepaths from @p config * @@ -123,16 +132,6 @@ loadImages(QMap& map, const QVariantMap& config) } } -/** @brief Load the @p map with stylesheet-strings from @p config. - */ -static void -loadStyles(QMap& map, const QVariantMap& config) -{ - map.clear(); - for ( auto it = config.constBegin(); it != config.constEnd(); ++it ) - map.insert( it.key(), it.value().toString() ); -} - Branding::Branding( const QString& brandingFilePath, QObject* parent ) : QObject( parent ) @@ -166,7 +165,8 @@ Branding::Branding( const QString& brandingFilePath, if ( !doc[ "strings" ].IsMap() ) bail( "Syntax error in strings map." ); - loadStrings( m_strings, CalamaresUtils::yamlMapToVariant( doc[ "strings" ] ).toMap() ); + loadStrings( m_strings, CalamaresUtils::yamlMapToVariant( doc[ "strings" ] ).toMap(), + []( const QString& s ) -> QString { return s; } ); if ( !doc[ "images" ].IsMap() ) bail( "Syntax error in images map." ); @@ -174,7 +174,8 @@ Branding::Branding( const QString& brandingFilePath, if ( !doc[ "style" ].IsMap() ) bail( "Syntax error in style map." ); - loadStyles( m_style, CalamaresUtils::yamlMapToVariant( doc[ "style" ] ).toMap() ); + loadStrings( m_style, CalamaresUtils::yamlMapToVariant( doc[ "style" ] ).toMap(), + []( const QString& s ) -> QString { return s; } ); if ( doc[ "slideshow" ].IsSequence() ) {