[libcalamaresui] Apply coding style

This commit is contained in:
Adriaan de Groot 2019-08-21 12:00:26 +02:00
parent dbe0ccf2e1
commit 85f5410735
2 changed files with 74 additions and 54 deletions

View File

@ -87,7 +87,7 @@ const QStringList Branding::s_styleEntryStrings =
// clang-format on // clang-format on
// *INDENT-ON* // *INDENT-ON*
const NamedEnumTable<Branding::WindowDimensionUnit>& const NamedEnumTable< Branding::WindowDimensionUnit >&
Branding::WindowDimension::suffixes() Branding::WindowDimension::suffixes()
{ {
using Unit = Branding::WindowDimensionUnit; using Unit = Branding::WindowDimensionUnit;
@ -110,16 +110,23 @@ Branding::WindowDimension::suffixes()
* the @p transform function, which may change strings. * the @p transform function, which may change strings.
*/ */
static void static void
loadStrings( QMap<QString, QString>& map, const YAML::Node& doc, const std::string& key, const std::function< QString(const QString&) >& transform ) loadStrings( QMap< QString, QString >& map,
const YAML::Node& doc,
const std::string& key,
const std::function< QString( const QString& ) >& transform )
{ {
if ( !doc[ key ].IsMap() ) if ( !doc[ key ].IsMap() )
throw YAML::Exception( YAML::Mark(), std::string("Branding configuration is not a map: ") + key ); {
throw YAML::Exception( YAML::Mark(), std::string( "Branding configuration is not a map: " ) + key );
}
const auto& config = CalamaresUtils::yamlMapToVariant( doc[ key ] ).toMap(); const auto& config = CalamaresUtils::yamlMapToVariant( doc[ key ] ).toMap();
map.clear(); map.clear();
for ( auto it = config.constBegin(); it != config.constEnd(); ++it ) for ( auto it = config.constBegin(); it != config.constEnd(); ++it )
{
map.insert( it.key(), transform( it.value().toString() ) ); map.insert( it.key(), transform( it.value().toString() ) );
}
} }
/** @brief Load the @p map with strings from @p config /** @brief Load the @p map with strings from @p config
@ -129,8 +136,7 @@ loadStrings( QMap<QString, QString>& map, const YAML::Node& doc, const std::stri
* documentation for details. * documentation for details.
*/ */
Branding::Branding( const QString& brandingFilePath, Branding::Branding( const QString& brandingFilePath, QObject* parent )
QObject* parent )
: QObject( parent ) : QObject( parent )
, m_descriptorPath( brandingFilePath ) , m_descriptorPath( brandingFilePath )
, m_slideshowAPI( 1 ) , m_slideshowAPI( 1 )
@ -141,7 +147,9 @@ Branding::Branding( const QString& brandingFilePath,
QDir componentDir( componentDirectory() ); QDir componentDir( componentDirectory() );
if ( !componentDir.exists() ) if ( !componentDir.exists() )
{
bail( "Bad component directory path." ); bail( "Bad component directory path." );
}
QFile file( brandingFilePath ); QFile file( brandingFilePath );
if ( file.exists() && file.open( QFile::ReadOnly | QFile::Text ) ) if ( file.exists() && file.open( QFile::ReadOnly | QFile::Text ) )
@ -153,8 +161,7 @@ Branding::Branding( const QString& brandingFilePath,
YAML::Node doc = YAML::Load( ba.constData() ); YAML::Node doc = YAML::Load( ba.constData() );
Q_ASSERT( doc.IsMap() ); Q_ASSERT( doc.IsMap() );
m_componentName = QString::fromStdString( doc[ "componentName" ] m_componentName = QString::fromStdString( doc[ "componentName" ].as< std::string >() );
.as< std::string >() );
if ( m_componentName != componentDir.dirName() ) if ( m_componentName != componentDir.dirName() )
bail( "The branding component name should match the name of the " bail( "The branding component name should match the name of the "
"component directory." ); "component directory." );
@ -165,8 +172,7 @@ Branding::Branding( const QString& brandingFilePath,
// Copy the os-release information into a QHash for use by KMacroExpander. // Copy the os-release information into a QHash for use by KMacroExpander.
KOSRelease relInfo; KOSRelease relInfo;
QHash< QString, QString > relMap{ QHash< QString, QString > relMap { std::initializer_list< std::pair< QString, QString > > {
std::initializer_list< std::pair< QString, QString > > {
{ QStringLiteral( "NAME" ), relInfo.name() }, { QStringLiteral( "NAME" ), relInfo.name() },
{ QStringLiteral( "VERSION" ), relInfo.version() }, { QStringLiteral( "VERSION" ), relInfo.version() },
{ QStringLiteral( "ID" ), relInfo.id() }, { QStringLiteral( "ID" ), relInfo.id() },
@ -182,9 +188,10 @@ Branding::Branding( const QString& brandingFilePath,
{ QStringLiteral( "BUILD_ID" ), relInfo.buildId() }, { QStringLiteral( "BUILD_ID" ), relInfo.buildId() },
{ 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
@ -192,9 +199,7 @@ Branding::Branding( const QString& brandingFilePath,
// 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", loadStrings( m_images, doc, "images", [&]( const QString& s ) -> QString {
[&]( 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 ) );
@ -203,15 +208,14 @@ Branding::Branding( const QString& brandingFilePath,
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() )
{
bail( QString( "Image file %1 does not exist." ).arg( imageFi.absoluteFilePath() ) ); bail( QString( "Image file %1 does not exist." ).arg( imageFi.absoluteFilePath() ) );
}
return imageName; // Not turned into a path return imageName; // Not turned into a path
} }
return imageFi.absoluteFilePath(); return imageFi.absoluteFilePath();
} } );
); loadStrings( m_style, doc, "style", []( const QString& s ) -> QString { return s; } );
loadStrings( m_style, doc, "style",
[]( const QString& s ) -> QString { return s; }
);
if ( doc[ "slideshow" ].IsSequence() ) if ( doc[ "slideshow" ].IsSequence() )
{ {
@ -222,8 +226,7 @@ Branding::Branding( const QString& brandingFilePath,
QString pathString = slideShowPictures[ i ]; QString pathString = slideShowPictures[ i ];
QFileInfo imageFi( componentDir.absoluteFilePath( pathString ) ); QFileInfo imageFi( componentDir.absoluteFilePath( pathString ) );
if ( !imageFi.exists() ) if ( !imageFi.exists() )
bail( QString( "Slideshow file %1 does not exist." ) bail( QString( "Slideshow file %1 does not exist." ).arg( imageFi.absoluteFilePath() ) );
.arg( imageFi.absoluteFilePath() ) );
slideShowPictures[ i ] = imageFi.absoluteFilePath(); slideShowPictures[ i ] = imageFi.absoluteFilePath();
} }
@ -232,19 +235,19 @@ Branding::Branding( const QString& brandingFilePath,
} }
else if ( doc[ "slideshow" ].IsScalar() ) else if ( doc[ "slideshow" ].IsScalar() )
{ {
QString slideshowPath = QString::fromStdString( doc[ "slideshow" ] QString slideshowPath = QString::fromStdString( doc[ "slideshow" ].as< std::string >() );
.as< std::string >() );
QFileInfo slideshowFi( componentDir.absoluteFilePath( slideshowPath ) ); QFileInfo slideshowFi( componentDir.absoluteFilePath( slideshowPath ) );
if ( !slideshowFi.exists() || if ( !slideshowFi.exists() || !slideshowFi.fileName().toLower().endsWith( ".qml" ) )
!slideshowFi.fileName().toLower().endsWith( ".qml" ) )
bail( QString( "Slideshow file %1 does not exist or is not a valid QML file." ) bail( QString( "Slideshow file %1 does not exist or is not a valid QML file." )
.arg( slideshowFi.absoluteFilePath() ) ); .arg( slideshowFi.absoluteFilePath() ) );
m_slideshowPath = slideshowFi.absoluteFilePath(); m_slideshowPath = slideshowFi.absoluteFilePath();
} }
else else
{
bail( "Syntax error in slideshow sequence." ); bail( "Syntax error in slideshow sequence." );
}
int api = doc[ "slideshowAPI" ].IsScalar() ? doc[ "slideshowAPI" ].as<int>() : -1; int api = doc[ "slideshowAPI" ].IsScalar() ? doc[ "slideshowAPI" ].as< int >() : -1;
if ( ( api < 1 ) || ( api > 2 ) ) if ( ( api < 1 ) || ( api > 2 ) )
{ {
cWarning() << "Invalid or missing *slideshowAPI* in branding file."; cWarning() << "Invalid or missing *slideshowAPI* in branding file.";
@ -260,11 +263,11 @@ Branding::Branding( const QString& brandingFilePath,
QDir translationsDir( componentDir.filePath( "lang" ) ); QDir translationsDir( componentDir.filePath( "lang" ) );
if ( !translationsDir.exists() ) if ( !translationsDir.exists() )
{
cWarning() << "the branding component" << componentDir.absolutePath() << "does not ship translations."; cWarning() << "the branding component" << componentDir.absolutePath() << "does not ship translations.";
}
m_translationsPathPrefix = translationsDir.absolutePath(); m_translationsPathPrefix = translationsDir.absolutePath();
m_translationsPathPrefix.append( QString( "%1calamares-%2" ) m_translationsPathPrefix.append( QString( "%1calamares-%2" ).arg( QDir::separator() ).arg( m_componentName ) );
.arg( QDir::separator() )
.arg( m_componentName ) );
} }
else else
{ {
@ -286,7 +289,7 @@ Branding::Branding( const QString& brandingFilePath,
QString QString
Branding::componentDirectory() const Branding::componentDirectory() const
{ {
QFileInfo fi ( m_descriptorPath ); QFileInfo fi( m_descriptorPath );
return fi.absoluteDir().absolutePath(); return fi.absoluteDir().absolutePath();
} }
@ -325,7 +328,7 @@ Branding::image( Branding::ImageEntry imageEntry, const QSize& size ) const
} }
else else
{ {
auto icon = QIcon::fromTheme(path); auto icon = QIcon::fromTheme( path );
Q_ASSERT( !icon.isNull() ); Q_ASSERT( !icon.isNull() );
return icon.pixmap( size ); return icon.pixmap( size );
@ -333,7 +336,7 @@ Branding::image( Branding::ImageEntry imageEntry, const QSize& size ) const
} }
QPixmap QPixmap
Branding::image(const QString& imageName, const QSize& size) const 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 ) );
@ -342,7 +345,9 @@ Branding::image(const QString& imageName, const QSize& size) const
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 QPixmap();
}
return icon.pixmap( size ); return icon.pixmap( size );
} }
return ImageRegistry::instance()->pixmap( imageFi.absoluteFilePath(), size ); return ImageRegistry::instance()->pixmap( imageFi.absoluteFilePath(), size );
@ -360,7 +365,9 @@ Branding::stylesheet() const
return stylesheetFile.readAll(); return stylesheetFile.readAll();
} }
else else
{
cWarning() << "The branding component" << fi.absoluteDir().absolutePath() << "does not ship stylesheet.qss."; cWarning() << "The branding component" << fi.absoluteDir().absolutePath() << "does not ship stylesheet.qss.";
}
return QString(); return QString();
} }
@ -369,7 +376,9 @@ Branding::setGlobals( GlobalStorage* globalStorage ) const
{ {
QVariantMap brandingMap; QVariantMap brandingMap;
for ( const QString& key : s_stringEntryStrings ) for ( const QString& key : s_stringEntryStrings )
{
brandingMap.insert( key, m_strings.value( key ) ); brandingMap.insert( key, m_strings.value( key ) );
}
globalStorage->insert( "branding", brandingMap ); globalStorage->insert( "branding", brandingMap );
} }
@ -384,15 +393,17 @@ Branding::WindowDimension::isValid() const
static inline QString static inline QString
getString( const YAML::Node& doc, const char* key ) getString( const YAML::Node& doc, const char* key )
{ {
if ( doc[key] ) if ( doc[ key ] )
return QString::fromStdString( doc[key].as< std::string >() ); {
return QString::fromStdString( doc[ key ].as< std::string >() );
}
return QString(); return QString();
} }
void void
Branding::initSimpleSettings( const YAML::Node& doc ) Branding::initSimpleSettings( const YAML::Node& doc )
{ {
static const NamedEnumTable< WindowExpansion > expansionNames{ static const NamedEnumTable< WindowExpansion > expansionNames {
{ QStringLiteral( "normal" ), WindowExpansion::Normal }, { QStringLiteral( "normal" ), WindowExpansion::Normal },
{ QStringLiteral( "fullscreen" ), WindowExpansion::Fullscreen }, { QStringLiteral( "fullscreen" ), WindowExpansion::Fullscreen },
{ QStringLiteral( "noexpand" ), WindowExpansion::Fixed } { QStringLiteral( "noexpand" ), WindowExpansion::Fixed }
@ -403,7 +414,10 @@ Branding::initSimpleSettings( const YAML::Node& doc )
m_welcomeExpandingLogo = doc[ "welcomeExpandingLogo" ].as< bool >( true ); m_welcomeExpandingLogo = doc[ "welcomeExpandingLogo" ].as< bool >( true );
m_windowExpansion = expansionNames.find( getString( doc, "windowExpanding" ), ok ); m_windowExpansion = expansionNames.find( getString( doc, "windowExpanding" ), ok );
if ( !ok ) if ( !ok )
cWarning() << "Branding module-setting *windowExpanding* interpreted as" << expansionNames.find( m_windowExpansion, ok ); {
cWarning() << "Branding module-setting *windowExpanding* interpreted as"
<< expansionNames.find( m_windowExpansion, ok );
}
QString windowSize = getString( doc, "windowSize" ); QString windowSize = getString( doc, "windowSize" );
if ( !windowSize.isEmpty() ) if ( !windowSize.isEmpty() )
@ -411,24 +425,26 @@ Branding::initSimpleSettings( const YAML::Node& doc )
auto l = windowSize.split( ',' ); auto l = windowSize.split( ',' );
if ( l.count() == 2 ) if ( l.count() == 2 )
{ {
m_windowWidth = WindowDimension( l[0] ); m_windowWidth = WindowDimension( l[ 0 ] );
m_windowHeight = WindowDimension( l[1] ); m_windowHeight = WindowDimension( l[ 1 ] );
} }
} }
if ( !m_windowWidth.isValid() ) if ( !m_windowWidth.isValid() )
{
m_windowWidth = WindowDimension( CalamaresUtils::windowPreferredWidth, WindowDimensionUnit::Pixies ); m_windowWidth = WindowDimension( CalamaresUtils::windowPreferredWidth, WindowDimensionUnit::Pixies );
}
if ( !m_windowHeight.isValid() ) if ( !m_windowHeight.isValid() )
{
m_windowHeight = WindowDimension( CalamaresUtils::windowPreferredHeight, WindowDimensionUnit::Pixies ); m_windowHeight = WindowDimension( CalamaresUtils::windowPreferredHeight, WindowDimensionUnit::Pixies );
}
} }
[[noreturn]] void [[noreturn]] void
Branding::bail( const QString& message ) Branding::bail( const QString& message )
{ {
cError() << "FATAL in" cError() << "FATAL in" << m_descriptorPath << "\n" + message;
<< m_descriptorPath
<< "\n" + message;
::exit( EXIT_FAILURE ); ::exit( EXIT_FAILURE );
} }
} } // namespace Calamares

View File

@ -73,10 +73,14 @@ ViewStep::setModuleInstanceKey( const QString& instanceKey )
void void
ViewStep::setConfigurationMap( const QVariantMap& configurationMap ) { Q_UNUSED( configurationMap ) } ViewStep::setConfigurationMap( const QVariantMap& configurationMap )
{
Q_UNUSED( configurationMap )
}
RequirementsList ViewStep::checkRequirements() RequirementsList
ViewStep::checkRequirements()
{ {
return RequirementsList(); return RequirementsList();
} }