[libcalamaresui] Give LocaleLabel a default constructor

- Needed for use in containers
 - While here refactor building the english label
This commit is contained in:
Adriaan de Groot 2019-04-19 09:18:26 +02:00
parent 94765d40cd
commit bd0af4bb77
2 changed files with 20 additions and 1 deletions

View File

@ -262,9 +262,23 @@ clearLayout( QLayout* layout )
} }
} }
LocaleLabel::LocaleLabel()
: m_locale( QLocale() )
{
m_localeId = m_locale.name();
setLabels( QString(), LabelFormat::IfNeededWithCountry );
}
LocaleLabel::LocaleLabel( const QString& locale, LabelFormat format ) LocaleLabel::LocaleLabel( const QString& locale, LabelFormat format )
: m_locale( LocaleLabel::getLocale( locale ) ) : m_locale( LocaleLabel::getLocale( locale ) )
, m_localeId( locale ) , m_localeId( locale )
{
setLabels( locale, format );
}
void
LocaleLabel::setLabels( const QString& locale, LabelFormat format )
{ {
//: language[name] (country[name]) //: language[name] (country[name])
QString longFormat = QObject::tr( "%1 (%2)" ); QString longFormat = QObject::tr( "%1 (%2)" );
@ -274,7 +288,7 @@ LocaleLabel::LocaleLabel( const QString& locale, LabelFormat format )
QString countryName; QString countryName;
if ( languageName.isEmpty() ) if ( languageName.isEmpty() )
languageName = QString( QLatin1Literal( "* %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 );

View File

@ -140,6 +140,9 @@ 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. */
LocaleLabel();
/** @brief Construct from a locale name. /** @brief Construct from a locale name.
* *
* The @p localeName should be one that Qt recognizes, e.g. en_US or ar_EY. * The @p localeName should be one that Qt recognizes, e.g. en_US or ar_EY.
@ -192,6 +195,8 @@ public:
static QLocale getLocale( const QString& localeName ); static QLocale getLocale( const QString& localeName );
protected: protected:
void setLabels( const QString& name, LabelFormat format );
QLocale m_locale; QLocale m_locale;
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