[libcalamares] Apply current coding style to libcalamares/locale/

This commit is contained in:
Adriaan de Groot 2019-08-04 22:17:12 +02:00
parent 43ba59361b
commit 7fcb7be1e4
7 changed files with 122 additions and 118 deletions

View File

@ -50,18 +50,24 @@ Label::setLabels( const QString& locale, LabelFormat format )
QString countryName; QString countryName;
if ( languageName.isEmpty() ) if ( languageName.isEmpty() )
{
languageName = QString( "* %1 (%2)" ).arg( locale, englishName ); languageName = QString( "* %1 (%2)" ).arg( locale, englishName );
}
bool needsCountryName = ( format == LabelFormat::AlwaysWithCountry ) || bool needsCountryName = ( format == LabelFormat::AlwaysWithCountry )
(locale.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 ); || ( locale.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 );
if ( needsCountryName ) if ( needsCountryName )
{
countryName = m_locale.nativeCountryName(); countryName = m_locale.nativeCountryName();
}
m_label = needsCountryName ? longFormat.arg( languageName, countryName ) : languageName; m_label = needsCountryName ? longFormat.arg( languageName, countryName ) : languageName;
m_englishLabel = needsCountryName ? longFormat.arg( englishName, QLocale::countryToString( m_locale.country() ) ) : englishName; m_englishLabel = needsCountryName ? longFormat.arg( englishName, QLocale::countryToString( m_locale.country() ) )
: englishName;
} }
QLocale Label::getLocale( const QString& localeName ) QLocale
Label::getLocale( const QString& localeName )
{ {
if ( localeName.contains( "@latin" ) ) if ( localeName.contains( "@latin" ) )
{ {
@ -69,8 +75,10 @@ QLocale Label::getLocale( const QString& localeName )
return QLocale( loc.language(), QLocale::Script::LatinScript, loc.country() ); return QLocale( loc.language(), QLocale::Script::LatinScript, loc.country() );
} }
else else
{
return QLocale( localeName ); return QLocale( localeName );
}
} }
} } // namespace Locale
} // namespace } // namespace CalamaresUtils

View File

@ -39,7 +39,11 @@ class Label
{ {
public: public:
/** @brief Formatting option for label -- add (country) to label. */ /** @brief Formatting option for label -- add (country) to label. */
enum class LabelFormat { AlwaysWithCountry, IfNeededWithCountry } ; enum class LabelFormat
{
AlwaysWithCountry,
IfNeededWithCountry
};
/** @brief Empty locale. This uses the system-default locale. */ /** @brief Empty locale. This uses the system-default locale. */
Label(); Label();
@ -56,54 +60,30 @@ public:
* *
* English (@see isEnglish() -- it means en_US) is sorted at the top. * English (@see isEnglish() -- it means en_US) is sorted at the top.
*/ */
bool operator <( const Label& other ) const bool operator<( const Label& other ) const { return m_localeId < other.m_localeId; }
{
return m_localeId < other.m_localeId;
}
/** @brief Is this locale English? /** @brief Is this locale English?
* *
* en_US and en (American English) is defined as English. The Queen's * en_US and en (American English) is defined as English. The Queen's
* English -- proper English -- is relegated to non-English status. * English -- proper English -- is relegated to non-English status.
*/ */
bool isEnglish() const bool isEnglish() const { return m_localeId == QLatin1Literal( "en_US" ) || m_localeId == QLatin1Literal( "en" ); }
{
return m_localeId == QLatin1Literal( "en_US" ) || m_localeId == QLatin1Literal( "en" );
}
/** @brief Get the human-readable name for this locale. */ /** @brief Get the human-readable name for this locale. */
QString label() const QString label() const { return m_label; }
{
return m_label;
}
/** @brief Get the *English* human-readable name for this locale. */ /** @brief Get the *English* human-readable name for this locale. */
QString englishLabel() const QString englishLabel() const { return m_englishLabel; }
{
return m_englishLabel;
}
/** @brief Get the Qt locale. */ /** @brief Get the Qt locale. */
QLocale locale() const QLocale locale() const { return m_locale; }
{
return m_locale;
}
QString name() const QString name() const { return m_locale.name(); }
{
return m_locale.name();
}
/// @brief Convenience accessor to the language part of the locale /// @brief Convenience accessor to the language part of the locale
QLocale::Language language() const QLocale::Language language() const { return m_locale.language(); }
{
return m_locale.language();
}
/// @brief Convenience accessor to the country part (if any) of the locale /// @brief Convenience accessor to the country part (if any) of the locale
QLocale::Country country() const QLocale::Country country() const { return m_locale.country(); }
{
return m_locale.country();
}
/** @brief Get a Qt locale for the given @p localeName /** @brief Get a Qt locale for the given @p localeName
* *
@ -119,9 +99,9 @@ protected:
QString m_localeId; // the locale identifier, e.g. "en_GB" QString m_localeId; // the locale identifier, e.g. "en_GB"
QString m_label; // the native name of the locale QString m_label; // the native name of the locale
QString m_englishLabel; QString m_englishLabel;
} ; };
} } // namespace Locale
} // namespace } // namespace CalamaresUtils
#endif #endif

View File

@ -34,12 +34,12 @@ LabelModel::LabelModel( const QStringList& locales, QObject* parent )
m_locales.reserve( locales.count() ); m_locales.reserve( locales.count() );
for ( const auto& l : locales ) for ( const auto& l : locales )
{
m_locales.push_back( Label( l ) ); m_locales.push_back( Label( l ) );
}
} }
LabelModel::~LabelModel() LabelModel::~LabelModel() {}
{
}
int int
LabelModel::rowCount( const QModelIndex& ) const LabelModel::rowCount( const QModelIndex& ) const
@ -51,10 +51,14 @@ QVariant
LabelModel::data( const QModelIndex& index, int role ) const LabelModel::data( const QModelIndex& index, int role ) const
{ {
if ( ( role != LabelRole ) && ( role != EnglishLabelRole ) ) if ( ( role != LabelRole ) && ( role != EnglishLabelRole ) )
{
return QVariant(); return QVariant();
}
if ( !index.isValid() ) if ( !index.isValid() )
{
return QVariant(); return QVariant();
}
const auto& locale = m_locales.at( index.row() ); const auto& locale = m_locales.at( index.row() );
switch ( role ) switch ( role )
@ -75,59 +79,62 @@ LabelModel::locale( int row ) const
{ {
for ( const auto& l : m_locales ) for ( const auto& l : m_locales )
if ( l.isEnglish() ) if ( l.isEnglish() )
{
return l; return l;
return m_locales[0]; }
return m_locales[ 0 ];
} }
return m_locales[row]; return m_locales[ row ];
} }
int int
LabelModel::find( std::function<bool ( const Label& )> predicate ) const LabelModel::find( std::function< bool( const Label& ) > predicate ) const
{ {
for ( int row = 0; row < m_locales.count() ; ++row ) for ( int row = 0; row < m_locales.count(); ++row )
{ {
if ( predicate( m_locales[row] ) ) if ( predicate( m_locales[ row ] ) )
{
return row; return row;
}
} }
return -1; return -1;
} }
int int
LabelModel::find( std::function<bool ( const QLocale& )> predicate ) const LabelModel::find( std::function< bool( const QLocale& ) > predicate ) const
{ {
return find( [&]( const Label& l ) return find( [&]( const Label& l ) { return predicate( l.locale() ); } );
{
return predicate( l.locale() );
} );
} }
int int
LabelModel::find( const QLocale& locale ) const LabelModel::find( const QLocale& locale ) const
{ {
return find( [&]( const Label& l ) return find( [&]( const Label& l ) { return locale == l.locale(); } );
{
return locale == l.locale();
} );
} }
int int
LabelModel::find( const QString& countryCode ) const LabelModel::find( const QString& countryCode ) const
{ {
if ( countryCode.length() != 2 ) if ( countryCode.length() != 2 )
{
return -1; return -1;
}
auto c_l = countryData( countryCode ); auto c_l = countryData( countryCode );
int r = find( [&]( const Label& l ){ return ( l.language() == c_l.second ) && ( l.country() == c_l.first ); } ); int r = find( [&]( const Label& l ) { return ( l.language() == c_l.second ) && ( l.country() == c_l.first ); } );
if ( r >= 0 ) if ( r >= 0 )
{
return r; return r;
return find( [&]( const Label& l ){ return l.language() == c_l.second; } ); }
return find( [&]( const Label& l ) { return l.language() == c_l.second; } );
} }
LabelModel* availableTranslations() LabelModel*
availableTranslations()
{ {
static LabelModel* model = new LabelModel( QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';') ); static LabelModel* model = new LabelModel( QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' ) );
return model; return model;
} }
} } // namespace Locale
} // namespace } // namespace CalamaresUtils

View File

@ -58,8 +58,8 @@ public:
* *
* Returns the row number of the first match, or -1 if there isn't one. * Returns the row number of the first match, or -1 if there isn't one.
*/ */
int find( std::function<bool( const QLocale& )> predicate ) const; int find( std::function< bool( const QLocale& ) > predicate ) const;
int find( std::function<bool( const Label& )> predicate ) const; int find( std::function< bool( const Label& ) > predicate ) const;
/// @brief Looks for an item using the same locale, -1 if there isn't one /// @brief Looks for an item using the same locale, -1 if there isn't one
int find( const QLocale& ) const; int find( const QLocale& ) const;
/// @brief Looks for an item that best matches the 2-letter country code /// @brief Looks for an item that best matches the 2-letter country code
@ -67,7 +67,7 @@ public:
private: private:
QVector< Label > m_locales; QVector< Label > m_locales;
} ; };
/** @brief Returns a model with all available translations. /** @brief Returns a model with all available translations.
* *
@ -80,6 +80,6 @@ private:
* NOTE: While the model is not typed const, it should be. Do not modify. * NOTE: While the model is not typed const, it should be. Do not modify.
*/ */
DLLEXPORT LabelModel* availableTranslations(); DLLEXPORT LabelModel* availableTranslations();
} } // namespace Locale
} // namespace } // namespace CalamaresUtils
#endif #endif

View File

@ -28,13 +28,13 @@ namespace Locale
struct TwoChar struct TwoChar
{ {
TwoChar( const QString& code ) TwoChar( const QString& code )
: cc1(0) : cc1( 0 )
, cc2(0) , cc2( 0 )
{ {
if ( code.length() == 2 ) if ( code.length() == 2 )
{ {
cc1 = code[0].toLatin1(); cc1 = code[ 0 ].toLatin1();
cc2 = code[1].toLatin1(); cc2 = code[ 1 ].toLatin1();
} }
} }
@ -42,52 +42,65 @@ struct TwoChar
char cc2; char cc2;
}; };
static const CountryData* lookup( TwoChar c ) static const CountryData*
lookup( TwoChar c )
{ {
if ( !c.cc1 ) if ( !c.cc1 )
{
return nullptr; return nullptr;
}
const CountryData* p = std::find_if(country_data_table, country_data_table + country_data_size, const CountryData* p
[c=c]( const CountryData& d ){ return (d.cc1 == c.cc1) && (d.cc2 == c.cc2); } = std::find_if( country_data_table, country_data_table + country_data_size, [c = c]( const CountryData& d ) {
); return ( d.cc1 == c.cc1 ) && ( d.cc2 == c.cc2 );
} );
if ( p == country_data_table + country_data_size ) if ( p == country_data_table + country_data_size )
{
return nullptr; return nullptr;
}
return p; return p;
} }
QLocale::Country countryForCode(const QString& code) QLocale::Country
countryForCode( const QString& code )
{ {
const CountryData* p = lookup( TwoChar( code ) ); const CountryData* p = lookup( TwoChar( code ) );
return p ? p->c : QLocale::Country::AnyCountry; return p ? p->c : QLocale::Country::AnyCountry;
} }
QLocale::Language languageForCountry(const QString& code) QLocale::Language
languageForCountry( const QString& code )
{ {
const CountryData* p = lookup( TwoChar( code ) ); const CountryData* p = lookup( TwoChar( code ) );
return p ? p->l : QLocale::Language::AnyLanguage; return p ? p->l : QLocale::Language::AnyLanguage;
} }
QPair<QLocale::Country, QLocale::Language> countryData(const QString& code) QPair< QLocale::Country, QLocale::Language >
countryData( const QString& code )
{ {
const CountryData* p = lookup( TwoChar( code ) ); const CountryData* p = lookup( TwoChar( code ) );
return p ? qMakePair( p->c, p->l ) : qMakePair( QLocale::Country::AnyCountry, QLocale::Language::AnyLanguage ); return p ? qMakePair( p->c, p->l ) : qMakePair( QLocale::Country::AnyCountry, QLocale::Language::AnyLanguage );
} }
QLocale countryLocale(const QString& code) QLocale
countryLocale( const QString& code )
{ {
auto p = countryData( code ); auto p = countryData( code );
return QLocale( p.second, p.first ); return QLocale( p.second, p.first );
} }
QLocale::Language languageForCountry(QLocale::Country country) QLocale::Language
languageForCountry( QLocale::Country country )
{ {
const CountryData* p = std::find_if(country_data_table, country_data_table + country_data_size, const CountryData* p = std::find_if( country_data_table,
[c=country]( const CountryData& d ){ return d.c == c; } country_data_table + country_data_size,
); [c = country]( const CountryData& d ) { return d.c == c; } );
if ( p == country_data_table + country_data_size ) if ( p == country_data_table + country_data_size )
{
return QLocale::Language::AnyLanguage; return QLocale::Language::AnyLanguage;
}
return p->l; return p->l;
} }
} } // namespace Locale
} // namespace } // namespace CalamaresUtils

View File

@ -28,28 +28,28 @@ namespace CalamaresUtils
{ {
namespace Locale namespace Locale
{ {
/* All the functions in this file do lookups of locale data /* All the functions in this file do lookups of locale data
* based on CLDR tables; these are lookups that you can't (easily) * based on CLDR tables; these are lookups that you can't (easily)
* do with just QLocale (e.g. from 2-letter country code to a likely * do with just QLocale (e.g. from 2-letter country code to a likely
* locale). * locale).
*/ */
/// @brief Map a 2-letter code to a Country, or AnyCountry if not found /// @brief Map a 2-letter code to a Country, or AnyCountry if not found
DLLEXPORT QLocale::Country countryForCode( const QString& code ); DLLEXPORT QLocale::Country countryForCode( const QString& code );
/** @brief Map a Country to a Language, or AnyLanguage if not found /** @brief Map a Country to a Language, or AnyLanguage if not found
* *
* This is a *likely* language for the given country, based on the * This is a *likely* language for the given country, based on the
* CLDR tables. For instance, this maps Belgium to Dutch. * CLDR tables. For instance, this maps Belgium to Dutch.
*/ */
DLLEXPORT QLocale::Language languageForCountry( QLocale::Country country ); DLLEXPORT QLocale::Language languageForCountry( QLocale::Country country );
/// @brief Map a 2-letter code to a Language, or AnyLanguage if not found /// @brief Map a 2-letter code to a Language, or AnyLanguage if not found
DLLEXPORT QLocale::Language languageForCountry( const QString& code ); DLLEXPORT QLocale::Language languageForCountry( const QString& code );
/// @brief Get both Country and Language for a 2-letter code /// @brief Get both Country and Language for a 2-letter code
DLLEXPORT QPair< QLocale::Country, QLocale::Language > countryData( const QString& code ); DLLEXPORT QPair< QLocale::Country, QLocale::Language > countryData( const QString& code );
/// @brief Get a likely locale for a 2-letter country code /// @brief Get a likely locale for a 2-letter country code
DLLEXPORT QLocale countryLocale( const QString& code ); DLLEXPORT QLocale countryLocale( const QString& code );
} } // namespace Locale
} // namespace } // namespace CalamaresUtils
#endif #endif

View File

@ -25,13 +25,9 @@
QTEST_GUILESS_MAIN( LocaleTests ) QTEST_GUILESS_MAIN( LocaleTests )
LocaleTests::LocaleTests() LocaleTests::LocaleTests() {}
{
}
LocaleTests::~LocaleTests() LocaleTests::~LocaleTests() {}
{
}
void void
LocaleTests::initTestCase() LocaleTests::initTestCase()