[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;
if ( languageName.isEmpty() )
{
languageName = QString( "* %1 (%2)" ).arg( locale, englishName );
}
bool needsCountryName = ( format == LabelFormat::AlwaysWithCountry ) ||
(locale.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 );
bool needsCountryName = ( format == LabelFormat::AlwaysWithCountry )
|| ( locale.contains( '_' ) && QLocale::countriesForLanguage( m_locale.language() ).count() > 1 );
if ( needsCountryName )
{
countryName = m_locale.nativeCountryName();
}
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" ) )
{
@ -69,8 +75,10 @@ QLocale Label::getLocale( const QString& localeName )
return QLocale( loc.language(), QLocale::Script::LatinScript, loc.country() );
}
else
{
return QLocale( localeName );
}
}
}
} // namespace
} // namespace Locale
} // namespace CalamaresUtils

View File

@ -23,7 +23,7 @@
#include <QLocale>
#include <QString>
namespace CalamaresUtils
namespace CalamaresUtils
{
namespace Locale
{
@ -39,7 +39,11 @@ class Label
{
public:
/** @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. */
Label();
@ -56,54 +60,30 @@ public:
*
* English (@see isEnglish() -- it means en_US) is sorted at the top.
*/
bool operator <( const Label& other ) const
{
return m_localeId < other.m_localeId;
}
bool operator<( const Label& other ) const { return m_localeId < other.m_localeId; }
/** @brief Is this locale English?
*
* en_US and en (American English) is defined as English. The Queen's
* English -- proper English -- is relegated to non-English status.
*/
bool isEnglish() const
{
return m_localeId == QLatin1Literal( "en_US" ) || m_localeId == QLatin1Literal( "en" );
}
bool isEnglish() const { return m_localeId == QLatin1Literal( "en_US" ) || m_localeId == QLatin1Literal( "en" ); }
/** @brief Get the human-readable name for this locale. */
QString label() const
{
return m_label;
}
QString label() const { return m_label; }
/** @brief Get the *English* human-readable name for this locale. */
QString englishLabel() const
{
return m_englishLabel;
}
QString englishLabel() const { return m_englishLabel; }
/** @brief Get the Qt locale. */
QLocale locale() const
{
return m_locale;
}
QLocale locale() const { return m_locale; }
QString name() const
{
return m_locale.name();
}
QString name() const { return m_locale.name(); }
/// @brief Convenience accessor to the language part of the locale
QLocale::Language language() const
{
return m_locale.language();
}
QLocale::Language language() const { return m_locale.language(); }
/// @brief Convenience accessor to the country part (if any) of the locale
QLocale::Country country() const
{
return m_locale.country();
}
QLocale::Country country() const { return m_locale.country(); }
/** @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_label; // the native name of the locale
QString m_englishLabel;
} ;
};
}
} // namespace
} // namespace Locale
} // namespace CalamaresUtils
#endif

View File

@ -34,12 +34,12 @@ LabelModel::LabelModel( const QStringList& locales, QObject* parent )
m_locales.reserve( locales.count() );
for ( const auto& l : locales )
{
m_locales.push_back( Label( l ) );
}
}
LabelModel::~LabelModel()
{
}
LabelModel::~LabelModel() {}
int
LabelModel::rowCount( const QModelIndex& ) const
@ -51,10 +51,14 @@ QVariant
LabelModel::data( const QModelIndex& index, int role ) const
{
if ( ( role != LabelRole ) && ( role != EnglishLabelRole ) )
{
return QVariant();
}
if ( !index.isValid() )
{
return QVariant();
}
const auto& locale = m_locales.at( index.row() );
switch ( role )
@ -75,59 +79,62 @@ LabelModel::locale( int row ) const
{
for ( const auto& l : m_locales )
if ( l.isEnglish() )
{
return l;
return m_locales[0];
}
return m_locales[ 0 ];
}
return m_locales[row];
return m_locales[ row ];
}
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 -1;
}
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 predicate( l.locale() );
} );
return find( [&]( const Label& l ) { return predicate( l.locale() ); } );
}
int
LabelModel::find( const QLocale& locale ) const
{
return find( [&]( const Label& l )
{
return locale == l.locale();
} );
return find( [&]( const Label& l ) { return locale == l.locale(); } );
}
int
LabelModel::find( const QString& countryCode ) const
{
if ( countryCode.length() != 2 )
{
return -1;
}
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 )
{
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;
}
}
} // namespace
} // namespace Locale
} // namespace CalamaresUtils

View File

@ -26,7 +26,7 @@
#include <QVector>
namespace CalamaresUtils
namespace CalamaresUtils
{
namespace Locale
{
@ -58,8 +58,8 @@ public:
*
* 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 Label& )> predicate ) const;
int find( std::function< bool( const QLocale& ) > 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
int find( const QLocale& ) const;
/// @brief Looks for an item that best matches the 2-letter country code
@ -67,7 +67,7 @@ public:
private:
QVector< Label > m_locales;
} ;
};
/** @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.
*/
DLLEXPORT LabelModel* availableTranslations();
}
} // namespace
} // namespace Locale
} // namespace CalamaresUtils
#endif

View File

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

View File

@ -24,32 +24,32 @@
#include <QLocale>
#include <QPair>
namespace CalamaresUtils
namespace CalamaresUtils
{
namespace Locale
{
/* All the functions in this file do lookups of locale data
* 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
* locale).
*/
/* All the functions in this file do lookups of locale data
* 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
* locale).
*/
/// @brief Map a 2-letter code to a Country, or AnyCountry if not found
DLLEXPORT QLocale::Country countryForCode( const QString& code );
/** @brief Map a Country to a Language, or AnyLanguage if not found
*
* This is a *likely* language for the given country, based on the
* CLDR tables. For instance, this maps Belgium to Dutch.
*/
DLLEXPORT QLocale::Language languageForCountry( QLocale::Country country );
/// @brief Map a 2-letter code to a Language, or AnyLanguage if not found
DLLEXPORT QLocale::Language languageForCountry( const QString& code );
/// @brief Map a 2-letter code to a Country, or AnyCountry if not found
DLLEXPORT QLocale::Country countryForCode( const QString& code );
/** @brief Map a Country to a Language, or AnyLanguage if not found
*
* This is a *likely* language for the given country, based on the
* CLDR tables. For instance, this maps Belgium to Dutch.
*/
DLLEXPORT QLocale::Language languageForCountry( QLocale::Country country );
/// @brief Map a 2-letter code to a Language, or AnyLanguage if not found
DLLEXPORT QLocale::Language languageForCountry( const QString& code );
/// @brief Get both Country and Language for a 2-letter code
DLLEXPORT QPair< QLocale::Country, QLocale::Language > countryData( const QString& code );
/// @brief Get a likely locale for a 2-letter country code
DLLEXPORT QLocale countryLocale( const QString& code );
}
} // namespace
/// @brief Get both Country and Language for a 2-letter code
DLLEXPORT QPair< QLocale::Country, QLocale::Language > countryData( const QString& code );
/// @brief Get a likely locale for a 2-letter country code
DLLEXPORT QLocale countryLocale( const QString& code );
} // namespace Locale
} // namespace CalamaresUtils
#endif

View File

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