diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index 117530a88..770c7c6b1 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014-2016, Teo Mrnjavac + * Copyright 2017, Adriaan de Groot * * Portions from the Manjaro Installation Framework * by Roland Singer @@ -221,10 +222,58 @@ KeyboardPage::createJobs( const QString& xOrgConfFileName, } +void +KeyboardPage::guessLayout( const QStringList& langParts ) +{ + const KeyboardLayoutModel* klm = dynamic_cast< KeyboardLayoutModel* >( ui->listLayout->model() ); + bool foundCountryPart = false; + for ( auto countryPart = langParts.rbegin(); !foundCountryPart && countryPart != langParts.rend(); ++countryPart) + { + cDebug() << " .. looking for locale part" << *countryPart; + for ( int i = 0; i < klm->rowCount(); ++i ) + { + QModelIndex idx = klm->index( i ); + if ( idx.isValid() && + ( idx.data( KeyboardLayoutModel::KeyboardLayoutKeyRole ).toString().compare( *countryPart, Qt::CaseInsensitive ) == 0 ) ) + { + cDebug() << " .. matched" << idx.data( KeyboardLayoutModel::KeyboardLayoutKeyRole ).toString(); + ui->listLayout->setCurrentIndex( idx ); + foundCountryPart = true; + break; + } + } + } +} + + void KeyboardPage::onActivate() { ui->listLayout->setFocus(); + + // Try to preselect a layout, depending on language and locale + Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage(); + QString lang = gs->value( "localeConf" ).toMap().value( "LANG" ).toString(); + + cDebug() << "Got locale language" << lang; + if ( !lang.isEmpty() ) + { + // Chop off .codeset and @modifier + int index = lang.indexOf('.'); + if ( index >= 0 ) + lang.truncate( index ); + index = lang.indexOf('@'); + if ( index >= 0 ) + lang.truncate( index ); + + lang.replace( '-', '_' ); // Normalize separators + const auto langParts = lang.split( '_' , QString::SkipEmptyParts ); + + QString country = QLocale::countryToString( QLocale( lang ).country() ); + cDebug() << " .. extracted country" << country << "::" << langParts; + + guessLayout( langParts ); + } } diff --git a/src/modules/keyboard/KeyboardPage.h b/src/modules/keyboard/KeyboardPage.h index c60d62b2b..6f828f446 100644 --- a/src/modules/keyboard/KeyboardPage.h +++ b/src/modules/keyboard/KeyboardPage.h @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2014-2016, Teo Mrnjavac + * Copyright 2017, Adriaan de Groot * * Portions from the Manjaro Installation Framework * by Roland Singer @@ -70,6 +71,8 @@ private: KeyboardGlobal::KeyboardInfo info; }; + /// Guess a layout based on the split-apart locale + void guessLayout( const QStringList& langParts ); void updateVariants( const QPersistentModelIndex& currentItem, QString currentVariant = QString() );