From 5a126816f4ccfd671f997a67574e1dcbf266f534 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 20 May 2019 15:49:13 +0200 Subject: [PATCH] [libcalamaresui] WIP: refactor copying strings from config to Branding - this is mostly to make the constructor easier to read by moving parts of the story to easily-understood methods. - doesn't actually compile. --- src/libcalamaresui/Branding.cpp | 80 +++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 29 deletions(-) diff --git a/src/libcalamaresui/Branding.cpp b/src/libcalamaresui/Branding.cpp index b7bf4c7aa..f9e5751f3 100644 --- a/src/libcalamaresui/Branding.cpp +++ b/src/libcalamaresui/Branding.cpp @@ -87,6 +87,52 @@ Branding::WindowDimension::suffixes() return names; } +/** @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 + * + * Paths are translated relative to componentDir, and must exist. + * All paths are transformed to absolute paths before putting + * them in the map. + */ +static void +loadImages(QMap& map, const QVariantMap& config) +{ + map.clear(); + for ( auto it = config.constBegin(); it != config.constEnd(); ++it ) + { + QString pathString = it.value().toString(); + QFileInfo imageFi( componentDir.absoluteFilePath( pathString ) ); + if ( !imageFi.exists() ) + bail( QString( "Image file %1 does not exist." ) + .arg( imageFi.absoluteFilePath() ) ); + + map.insert( it.key(), imageFi.absoluteFilePath() ); + } +} + +/** @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 ) @@ -120,29 +166,15 @@ Branding::Branding( const QString& brandingFilePath, if ( !doc[ "strings" ].IsMap() ) bail( "Syntax error in strings map." ); - - QVariantMap strings = - CalamaresUtils::yamlMapToVariant( doc[ "strings" ] ).toMap(); - m_strings.clear(); - for ( auto it = strings.constBegin(); it != strings.constEnd(); ++it ) - m_strings.insert( it.key(), it.value().toString() ); + loadStrings( m_strings, CalamaresUtils::yamlMapToVariant( doc[ "strings" ] ).toMap() ); if ( !doc[ "images" ].IsMap() ) bail( "Syntax error in images map." ); + loadImages( m_images, CalamaresUtils::yamlMapToVariant( doc[ "images" ] ).toMap() ); - QVariantMap images = - CalamaresUtils::yamlMapToVariant( doc[ "images" ] ).toMap(); - m_images.clear(); - for ( auto it = images.constBegin(); it != images.constEnd(); ++it ) - { - QString pathString = it.value().toString(); - QFileInfo imageFi( componentDir.absoluteFilePath( pathString ) ); - if ( !imageFi.exists() ) - bail( QString( "Image file %1 does not exist." ) - .arg( imageFi.absoluteFilePath() ) ); - - m_images.insert( it.key(), imageFi.absoluteFilePath() ); - } + if ( !doc[ "style" ].IsMap() ) + bail( "Syntax error in style map." ); + loadStyles( m_style, CalamaresUtils::yamlMapToVariant( doc[ "style" ] ).toMap() ); if ( doc[ "slideshow" ].IsSequence() ) { @@ -174,16 +206,6 @@ Branding::Branding( const QString& brandingFilePath, } else bail( "Syntax error in slideshow sequence." ); - - if ( !doc[ "style" ].IsMap() ) - bail( "Syntax error in style map." ); - - QVariantMap style = - CalamaresUtils::yamlMapToVariant( doc[ "style" ] ).toMap(); - m_style.clear(); - for ( auto it = style.constBegin(); it != style.constEnd(); ++it ) - m_style.insert( it.key(), it.value().toString() ); - } catch ( YAML::Exception& e ) {