[keyboard] match keyboard layouts with selected sys-lang

This commit is contained in:
Philip 2017-10-27 17:48:49 +02:00
parent 4b4bd83a39
commit 243d7c5e29
4 changed files with 97 additions and 77 deletions

View File

@ -64,9 +64,7 @@ KeyboardLayoutModel::init()
KeyboardGlobal::getKeyboardLayouts(); KeyboardGlobal::getKeyboardLayouts();
for ( KeyboardGlobal::LayoutsMap::const_iterator it = layouts.constBegin(); for ( KeyboardGlobal::LayoutsMap::const_iterator it = layouts.constBegin();
it != layouts.constEnd(); ++it ) it != layouts.constEnd(); ++it )
{
m_layouts.append( qMakePair( it.key(), it.value() ) ); m_layouts.append( qMakePair( it.key(), it.value() ) );
}
std::stable_sort( m_layouts.begin(), m_layouts.end(), []( const QPair< QString, KeyboardGlobal::KeyboardInfo >& a, std::stable_sort( m_layouts.begin(), m_layouts.end(), []( const QPair< QString, KeyboardGlobal::KeyboardInfo >& a,
const QPair< QString, KeyboardGlobal::KeyboardInfo >& b ) const QPair< QString, KeyboardGlobal::KeyboardInfo >& b )

View File

@ -86,7 +86,7 @@ KeyboardPage::KeyboardPage( QWidget* parent )
[this] [this]
{ {
ui->comboBoxModel->setCurrentIndex( m_defaultIndex ); ui->comboBoxModel->setCurrentIndex( m_defaultIndex );
}); } );
connect( ui->comboBoxModel, connect( ui->comboBoxModel,
static_cast< void ( QComboBox::* )( const QString& ) >( &QComboBox::currentIndexChanged ), static_cast< void ( QComboBox::* )( const QString& ) >( &QComboBox::currentIndexChanged ),
@ -97,7 +97,7 @@ KeyboardPage::KeyboardPage( QWidget* parent )
// Set Xorg keyboard model // Set Xorg keyboard model
QProcess::execute( QLatin1Literal( "setxkbmap" ), QProcess::execute( QLatin1Literal( "setxkbmap" ),
QStringList() << "-model" << model ); QStringList() << "-model" << model );
}); } );
CALAMARES_RETRANSLATE( ui->retranslateUi( this ); ) CALAMARES_RETRANSLATE( ui->retranslateUi( this ); )
} }
@ -253,21 +253,38 @@ KeyboardPage::guessLayout( const QStringList& langParts )
{ {
const KeyboardLayoutModel* klm = dynamic_cast< KeyboardLayoutModel* >( ui->listLayout->model() ); const KeyboardLayoutModel* klm = dynamic_cast< KeyboardLayoutModel* >( ui->listLayout->model() );
bool foundCountryPart = false; bool foundCountryPart = false;
for ( auto countryPart = langParts.rbegin(); !foundCountryPart && countryPart != langParts.rend(); ++countryPart) for ( auto countryPart = langParts.rbegin(); !foundCountryPart && countryPart != langParts.rend(); ++countryPart )
{ {
cDebug() << " .. looking for locale part" << *countryPart; cDebug() << " .. looking for locale part" << *countryPart;
for ( int i = 0; i < klm->rowCount(); ++i ) for ( int i = 0; i < klm->rowCount(); ++i )
{ {
QModelIndex idx = klm->index( i ); QModelIndex idx = klm->index( i );
if ( idx.isValid() && QString name = idx.isValid() ? idx.data( KeyboardLayoutModel::KeyboardLayoutKeyRole ).toString() : QString();
( idx.data( KeyboardLayoutModel::KeyboardLayoutKeyRole ).toString().compare( *countryPart, Qt::CaseInsensitive ) == 0 ) ) if ( idx.isValid() && ( name.compare( *countryPart, Qt::CaseInsensitive ) == 0 ) )
{ {
cDebug() << " .. matched" << idx.data( KeyboardLayoutModel::KeyboardLayoutKeyRole ).toString(); cDebug() << " .. matched" << name;
ui->listLayout->setCurrentIndex( idx ); ui->listLayout->setCurrentIndex( idx );
foundCountryPart = true; foundCountryPart = true;
break; break;
} }
} }
if ( foundCountryPart )
{
++countryPart;
if ( countryPart != langParts.rend() )
{
cDebug() << "Next level:" << *countryPart;
for (int variantnumber = 0; variantnumber < ui->listVariant->count(); ++variantnumber)
{
LayoutItem *variantdata = dynamic_cast< LayoutItem* >( ui->listVariant->item( variantnumber ) );
if ( variantdata && (variantdata->data.compare( *countryPart, Qt::CaseInsensitive ) == 0) )
{
ui->listVariant->setCurrentItem( variantdata );
cDebug() << " .. matched variant" << variantdata->data << ' ' << variantdata->text();
}
}
}
}
} }
} }
@ -275,6 +292,12 @@ KeyboardPage::guessLayout( const QStringList& langParts )
void void
KeyboardPage::onActivate() KeyboardPage::onActivate()
{ {
static auto specialCaseMap = QMap<std::string, std::string>( {
{ "ar_EG", "ara" },
{ "ca_ES", "cat_ES" },
{ "as_ES", "ast_ES" },
} );
ui->listLayout->setFocus(); ui->listLayout->setFocus();
// Try to preselect a layout, depending on language and locale // Try to preselect a layout, depending on language and locale
@ -285,15 +308,24 @@ KeyboardPage::onActivate()
if ( !lang.isEmpty() ) if ( !lang.isEmpty() )
{ {
// Chop off .codeset and @modifier // Chop off .codeset and @modifier
int index = lang.indexOf('.'); int index = lang.indexOf( '.' );
if ( index >= 0 ) if ( index >= 0 )
lang.truncate( index ); lang.truncate( index );
index = lang.indexOf('@'); index = lang.indexOf( '@' );
if ( index >= 0 ) if ( index >= 0 )
lang.truncate( index ); lang.truncate( index );
lang.replace( '-', '_' ); // Normalize separators lang.replace( '-', '_' ); // Normalize separators
const auto langParts = lang.split( '_' , QString::SkipEmptyParts ); }
if ( !lang.isEmpty() && specialCaseMap.contains( lang.toStdString() ) )
{
QLatin1String newLang( specialCaseMap.value( lang.toStdString() ).c_str() );
cDebug() << " .. special case language" << lang << '>' << newLang;
lang = newLang;
}
if ( !lang.isEmpty() )
{
const auto langParts = lang.split( '_', QString::SkipEmptyParts );
QString country = QLocale::countryToString( QLocale( lang ).country() ); QString country = QLocale::countryToString( QLocale( lang ).country() );
cDebug() << " .. extracted country" << country << "::" << langParts; cDebug() << " .. extracted country" << country << "::" << langParts;
@ -370,7 +402,7 @@ KeyboardPage::onListLayoutCurrentItemChanged( const QModelIndex& current,
/* Returns stringlist with suitable setxkbmap command-line arguments /* Returns stringlist with suitable setxkbmap command-line arguments
* to set the given @p layout and @p variant. * to set the given @p layout and @p variant.
*/ */
static inline QStringList xkbmap_args( QStringList&& r, const QString& layout, const QString& variant) static inline QStringList xkbmap_args( QStringList&& r, const QString& layout, const QString& variant )
{ {
r << "-layout" << layout; r << "-layout" << layout;
if ( !variant.isEmpty() ) if ( !variant.isEmpty() )

View File

@ -142,9 +142,7 @@ KeyboardViewStep::setConfigurationMap( const QVariantMap& configurationMap )
.toString(); .toString();
} }
else else
{
m_xOrgConfFileName = "00-keyboard.conf"; m_xOrgConfFileName = "00-keyboard.conf";
}
if ( configurationMap.contains( "convertedKeymapPath" ) && if ( configurationMap.contains( "convertedKeymapPath" ) &&
configurationMap.value( "convertedKeymapPath" ).type() == QVariant::String && configurationMap.value( "convertedKeymapPath" ).type() == QVariant::String &&
@ -154,17 +152,11 @@ KeyboardViewStep::setConfigurationMap( const QVariantMap& configurationMap )
.toString(); .toString();
} }
else else
{
m_convertedKeymapPath = QString(); m_convertedKeymapPath = QString();
}
if ( configurationMap.contains( "writeEtcDefaultKeyboard" ) && if ( configurationMap.contains( "writeEtcDefaultKeyboard" ) &&
configurationMap.value( "writeEtcDefaultKeyboard" ).type() == QVariant::Bool ) configurationMap.value( "writeEtcDefaultKeyboard" ).type() == QVariant::Bool )
{
m_writeEtcDefaultKeyboard = configurationMap.value( "writeEtcDefaultKeyboard" ).toBool(); m_writeEtcDefaultKeyboard = configurationMap.value( "writeEtcDefaultKeyboard" ).toBool();
}
else else
{
m_writeEtcDefaultKeyboard = true; m_writeEtcDefaultKeyboard = true;
}
} }

View File

@ -41,7 +41,7 @@ SetKeyboardLayoutJob::SetKeyboardLayoutJob( const QString& model,
const QString& variant, const QString& variant,
const QString& xOrgConfFileName, const QString& xOrgConfFileName,
const QString& convertedKeymapPath, const QString& convertedKeymapPath,
bool writeEtcDefaultKeyboard) bool writeEtcDefaultKeyboard )
: Calamares::Job() : Calamares::Job()
, m_model( model ) , m_model( model )
, m_layout( layout ) , m_layout( layout )
@ -303,9 +303,7 @@ SetKeyboardLayoutJob::exec()
QString defaultKeyboardPath; QString defaultKeyboardPath;
if ( QDir( destDir.absoluteFilePath( "etc/default" ) ).exists() ) if ( QDir( destDir.absoluteFilePath( "etc/default" ) ).exists() )
{
defaultKeyboardPath = destDir.absoluteFilePath( "etc/default/keyboard" ); defaultKeyboardPath = destDir.absoluteFilePath( "etc/default/keyboard" );
}
// Get the path to the destination's path to the converted key mappings // Get the path to the destination's path to the converted key mappings
QString convertedKeymapPath = m_convertedKeymapPath; QString convertedKeymapPath = m_convertedKeymapPath;