Use LocaleConfiguration in LocalePage. Add relevant widgets.

Also fix error reporting.
Move guesswork to LocaleConfiguration + a total rewrite.
Locale is now a LocaleConfiguration, that converts to QMap.
This commit is contained in:
Teo Mrnjavac 2016-08-10 11:45:22 +02:00
parent f60f1c6220
commit 8f44f76443
2 changed files with 118 additions and 70 deletions

View File

@ -81,6 +81,17 @@ LocalePage::LocalePage( QWidget* parent )
localeLayout->addWidget( m_localeChangeButton ); localeLayout->addWidget( m_localeChangeButton );
mainLayout->addLayout( localeLayout ); mainLayout->addLayout( localeLayout );
QBoxLayout* formatsLayout = new QHBoxLayout;
m_formatsLabel = new QLabel( this );
m_formatsLabel->setWordWrap( true );
m_formatsLabel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
formatsLayout->addWidget( m_formatsLabel );
m_formatsChangeButton = new QPushButton( this );
m_formatsChangeButton->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred );
formatsLayout->addWidget( m_formatsChangeButton );
mainLayout->addLayout( formatsLayout );
setLayout( mainLayout ); setLayout( mainLayout );
connect( m_regionCombo, connect( m_regionCombo,
@ -146,31 +157,68 @@ LocalePage::LocalePage( QWidget* parent )
} ); } );
connect( m_localeChangeButton, &QPushButton::clicked, connect( m_localeChangeButton, &QPushButton::clicked,
[this]() [this]
{ {
LCLocaleDialog* dlg = new LCLocaleDialog( lcLocale(), LCLocaleDialog* dlg =
m_localeGenLines, new LCLocaleDialog( m_selectedLocaleConfiguration.isEmpty() ?
this ); guessLocaleConfiguration().lang :
m_selectedLocaleConfiguration.lang,
m_localeGenLines,
this );
dlg->exec(); dlg->exec();
if ( dlg->result() == QDialog::Accepted && if ( dlg->result() == QDialog::Accepted &&
!dlg->selectedLCLocale().isEmpty() ) !dlg->selectedLCLocale().isEmpty() )
{ {
m_selectedLocale = dlg->selectedLCLocale(); m_selectedLocaleConfiguration.lang = dlg->selectedLCLocale();
m_localeLabel->setText( tr( "The system locale is set to %1." ) m_localeLabel->setText( tr( "The system language will be set to %1." )
.arg( prettyLCLocale( m_selectedLocale ) ) ); .arg( prettyLCLocale(
m_selectedLocaleConfiguration.lang ) ) );
} }
dlg->deleteLater(); dlg->deleteLater();
} ); } );
connect( m_formatsChangeButton, &QPushButton::clicked,
[this]
{
LCLocaleDialog* dlg =
new LCLocaleDialog( m_selectedLocaleConfiguration.isEmpty() ?
guessLocaleConfiguration().lc_numeric :
m_selectedLocaleConfiguration.lc_numeric,
m_localeGenLines,
this );
dlg->exec();
if ( dlg->result() == QDialog::Accepted &&
!dlg->selectedLCLocale().isEmpty() )
{
// TODO: improve the granularity of this setting.
m_selectedLocaleConfiguration.lc_numeric = dlg->selectedLCLocale();
m_selectedLocaleConfiguration.lc_time = dlg->selectedLCLocale();
m_selectedLocaleConfiguration.lc_monetary = dlg->selectedLCLocale();
m_selectedLocaleConfiguration.lc_paper = dlg->selectedLCLocale();
m_selectedLocaleConfiguration.lc_name = dlg->selectedLCLocale();
m_selectedLocaleConfiguration.lc_address = dlg->selectedLCLocale();
m_selectedLocaleConfiguration.lc_telephone = dlg->selectedLCLocale();
m_selectedLocaleConfiguration.lc_measurement = dlg->selectedLCLocale();
m_selectedLocaleConfiguration.lc_identification = dlg->selectedLCLocale();
m_formatsLabel->setText( tr( "The numbers and dates locale will be set to %1." )
.arg( prettyLCLocale(
m_selectedLocaleConfiguration.lc_numeric ) ) );
}
dlg->deleteLater();
} );
CALAMARES_RETRANSLATE( CALAMARES_RETRANSLATE(
m_regionLabel->setText( tr( "Region:" ) ); m_regionLabel->setText( tr( "Region:" ) );
m_zoneLabel->setText( tr( "Zone:" ) ); m_zoneLabel->setText( tr( "Zone:" ) );
m_localeLabel->setText( tr( "The system locale is set to %1." ) updateLocaleLabels();
.arg( prettyLCLocale( lcLocale() ) ) );
m_localeChangeButton->setText( tr( "&Change..." ) ); m_localeChangeButton->setText( tr( "&Change..." ) );
m_formatsChangeButton->setText( tr( "&Change..." ) );
) )
} }
@ -179,6 +227,20 @@ LocalePage::~LocalePage()
{} {}
void
LocalePage::updateLocaleLabels()
{
LocaleConfiguration lc = m_selectedLocaleConfiguration.isEmpty() ?
guessLocaleConfiguration() :
m_selectedLocaleConfiguration;
m_localeLabel->setText( tr( "The system language will be set to %1." )
.arg( prettyLCLocale( lc.lang ) ) );
m_formatsLabel->setText( tr( "The numbers and dates locale will be set to %1." )
.arg( prettyLCLocale( lc.lc_numeric ) ) );
}
void void
LocalePage::init( const QString& initialRegion, LocalePage::init( const QString& initialRegion,
const QString& initialZone, const QString& initialZone,
@ -284,7 +346,15 @@ LocalePage::init( const QString& initialRegion,
if ( m_localeGenLines.isEmpty() ) if ( m_localeGenLines.isEmpty() )
{ {
cDebug() << "WARNING: cannot get list of supported locales from anywhere."; cDebug() << "WARNING: cannot acquire a list of available locales."
<< "The locale and localecfg modules will be broken as long as this "
"system does not provide"
<< " * a /usr/share/i18n/SUPPORTED file"
<< "\tOR"
<< " * a well-formed /etc/locale.gen"
<< "\tOR"
<< " * a complete pre-compiled locale-gen database which allows complete locale -a output.";
return; // something went wrong and there's nothing we can do about it. return; // something went wrong and there's nothing we can do about it.
} }
@ -297,6 +367,8 @@ LocalePage::init( const QString& initialRegion,
else else
++it; ++it;
} }
} }
@ -325,10 +397,12 @@ LocalePage::createJobs()
} }
QString QMap< QString, QString >
LocalePage::lcLocale() LocalePage::localesMap()
{ {
return m_selectedLocale.isEmpty() ? guessLCLocale() : m_selectedLocale; return m_selectedLocaleConfiguration.isEmpty() ?
guessLocaleConfiguration().toMap() :
m_selectedLocaleConfiguration.toMap();
} }
@ -339,66 +413,32 @@ LocalePage::onActivate()
} }
QString LocaleConfiguration
LocalePage::guessLCLocale() LocalePage::guessLocaleConfiguration()
{ {
QLocale myLocale = QLocale(); QLocale myLocale = QLocale(); // User-selected language
// If we cannot say anything about available locales
if ( m_localeGenLines.isEmpty() ) if ( m_localeGenLines.isEmpty() )
return "en_US.UTF-8 UTF-8";
QString myLanguage = myLocale.name().split( '_' ).first();
QStringList linesForLanguage;
foreach ( QString line, m_localeGenLines )
{ {
if ( line.startsWith( myLanguage ) ) cDebug() << "WARNING: cannot acquire a list of available locales."
linesForLanguage.append( line ); << "The locale and localecfg modules will be broken as long as this "
"system does not provide"
<< " * a /usr/share/i18n/SUPPORTED file"
<< "\tOR"
<< " * a well-formed /etc/locale.gen"
<< "\tOR"
<< " * a complete pre-compiled locale-gen database which allows complete locale -a output.";
return LocaleConfiguration::createDefault();
} }
if ( linesForLanguage.length() == 0 ) QString myLanguageLocale = myLocale.name();
return "en_US.UTF-8 UTF-8"; if ( myLanguageLocale.isEmpty() )
else if ( linesForLanguage.length() == 1 ) return LocaleConfiguration::createDefault();
return linesForLanguage.first();
else
{
QStringList linesForLanguageUtf;
foreach ( QString line, linesForLanguage )
{
if ( line.contains( "UTF-8" ) )
linesForLanguageUtf.append( line );
}
if ( linesForLanguageUtf.length() == 1 ) return LocaleConfiguration::fromLanguageAndLocation( myLanguageLocale,
return linesForLanguageUtf.first(); m_localeGenLines,
} m_tzWidget->getCurrentLocation().country );
// FIXME: use reverse geocoding to guess the country
QString prefix = myLocale.name();
QStringList linesForLanguageAndCountry;
foreach ( QString line, linesForLanguage )
{
if ( line.startsWith( prefix ) )
linesForLanguageAndCountry.append( line );
}
if ( linesForLanguageAndCountry.length() == 0 )
return "en_US.UTF-8 UTF-8";
else if ( linesForLanguageAndCountry.length() == 1 )
return linesForLanguageAndCountry.first();
else
{
QStringList linesForLanguageAndCountryUtf;
foreach ( QString line, linesForLanguageAndCountry )
{
if ( line.contains( "UTF-8" ) )
linesForLanguageAndCountryUtf.append( line );
}
if ( linesForLanguageAndCountryUtf.length() == 1 )
return linesForLanguageAndCountryUtf.first();
}
return "en_US.UTF-8 UTF-8";
} }
@ -419,4 +459,7 @@ LocalePage::updateGlobalStorage()
->insert( "locationRegion", location.region ); ->insert( "locationRegion", location.region );
Calamares::JobQueue::instance()->globalStorage() Calamares::JobQueue::instance()->globalStorage()
->insert( "locationZone", location.zone ); ->insert( "locationZone", location.zone );
m_selectedLocaleConfiguration = guessLocaleConfiguration();
updateLocaleLabels();
} }

View File

@ -21,6 +21,8 @@
#include "Typedefs.h" #include "Typedefs.h"
#include "LocaleConfiguration.h"
#include <QWidget> #include <QWidget>
class QComboBox; class QComboBox;
@ -43,14 +45,15 @@ public:
QList< Calamares::job_ptr > createJobs(); QList< Calamares::job_ptr > createJobs();
QString lcLocale(); QMap< QString, QString > localesMap();
void onActivate(); void onActivate();
private: private:
QString guessLCLocale(); LocaleConfiguration guessLocaleConfiguration();
QString prettyLCLocale( const QString& lcLocale ); QString prettyLCLocale( const QString& localesMap );
void updateGlobalStorage(); void updateGlobalStorage();
void updateLocaleLabels();
TimeZoneWidget* m_tzWidget; TimeZoneWidget* m_tzWidget;
QComboBox* m_regionCombo; QComboBox* m_regionCombo;
@ -60,8 +63,10 @@ private:
QLabel* m_zoneLabel; QLabel* m_zoneLabel;
QLabel* m_localeLabel; QLabel* m_localeLabel;
QPushButton* m_localeChangeButton; QPushButton* m_localeChangeButton;
QLabel* m_formatsLabel;
QPushButton* m_formatsChangeButton;
QString m_selectedLocale; LocaleConfiguration m_selectedLocaleConfiguration;
QStringList m_localeGenLines; QStringList m_localeGenLines;