From f6e6774f92b95caa3c25d5fd6f94fedeab468436 Mon Sep 17 00:00:00 2001 From: demmm Date: Sun, 25 Oct 2020 18:35:03 +0100 Subject: [PATCH 01/28] [keyboardq] fix build --- src/modules/keyboardq/KeyboardQmlViewStep.cpp | 41 ++----------------- src/modules/keyboardq/KeyboardQmlViewStep.h | 6 +-- 2 files changed, 5 insertions(+), 42 deletions(-) diff --git a/src/modules/keyboardq/KeyboardQmlViewStep.cpp b/src/modules/keyboardq/KeyboardQmlViewStep.cpp index 08b72b3f4..8e4663ee8 100644 --- a/src/modules/keyboardq/KeyboardQmlViewStep.cpp +++ b/src/modules/keyboardq/KeyboardQmlViewStep.cpp @@ -2,6 +2,7 @@ * * SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac * SPDX-FileCopyrightText: 2020 Camilo Higuita + * SPDX-FileCopyrightText: 2020 Anke Boersma * SPDX-License-Identifier: GPL-3.0-or-later * * Calamares is Free Software: see the License-Identifier above. @@ -10,9 +11,10 @@ #include "KeyboardQmlViewStep.h" +#include "Config.h" + #include "GlobalStorage.h" #include "JobQueue.h" -#include "utils/Variant.h" CALAMARES_PLUGIN_FACTORY_DEFINITION( KeyboardQmlViewStepFactory, registerPlugin< KeyboardQmlViewStep >(); ) @@ -20,7 +22,6 @@ KeyboardQmlViewStep::KeyboardQmlViewStep( QObject* parent ) : Calamares::QmlViewStep( parent ) , m_config( new Config( this ) ) , m_nextEnabled( false ) - , m_writeEtcDefaultKeyboard( true ) { m_config->init(); m_nextEnabled = true; @@ -66,7 +67,7 @@ KeyboardQmlViewStep::isAtEnd() const Calamares::JobList KeyboardQmlViewStep::jobs() const { - return m_jobs; + return m_config->createJobs(); } void @@ -79,7 +80,6 @@ void KeyboardQmlViewStep::onLeave() { m_config->finalize(); - // m_jobs = m_config->createJobs( m_xOrgConfFileName, m_convertedKeymapPath, m_writeEtcDefaultKeyboard ); m_prettyStatus = m_config->prettyStatus(); } @@ -92,39 +92,6 @@ KeyboardQmlViewStep::getConfig() void KeyboardQmlViewStep::setConfigurationMap( const QVariantMap& configurationMap ) { - using namespace CalamaresUtils; - - if ( configurationMap.contains( "xOrgConfFileName" ) - && configurationMap.value( "xOrgConfFileName" ).type() == QVariant::String - && !getString( configurationMap, "xOrgConfFileName" ).isEmpty() ) - { - m_xOrgConfFileName = getString( configurationMap, "xOrgConfFileName" ); - } - else - { - m_xOrgConfFileName = "00-keyboard.conf"; - } - - if ( configurationMap.contains( "convertedKeymapPath" ) - && configurationMap.value( "convertedKeymapPath" ).type() == QVariant::String - && !getString( configurationMap, "convertedKeymapPath" ).isEmpty() ) - { - m_convertedKeymapPath = getString( configurationMap, "convertedKeymapPath" ); - } - else - { - m_convertedKeymapPath = QString(); - } - - if ( configurationMap.contains( "writeEtcDefaultKeyboard" ) - && configurationMap.value( "writeEtcDefaultKeyboard" ).type() == QVariant::Bool ) - { - m_writeEtcDefaultKeyboard = getBool( configurationMap, "writeEtcDefaultKeyboard", true ); - } - else - { - m_writeEtcDefaultKeyboard = true; - } Calamares::QmlViewStep::setConfigurationMap( configurationMap ); } diff --git a/src/modules/keyboardq/KeyboardQmlViewStep.h b/src/modules/keyboardq/KeyboardQmlViewStep.h index 4571a9a60..09390d8d9 100644 --- a/src/modules/keyboardq/KeyboardQmlViewStep.h +++ b/src/modules/keyboardq/KeyboardQmlViewStep.h @@ -19,6 +19,7 @@ #include +class Config; class KeyboardPage; class PLUGINDLLEXPORT KeyboardQmlViewStep : public Calamares::QmlViewStep @@ -50,11 +51,6 @@ private: bool m_nextEnabled; QString m_prettyStatus; - QString m_xOrgConfFileName; - QString m_convertedKeymapPath; - bool m_writeEtcDefaultKeyboard; - - Calamares::JobList m_jobs; }; CALAMARES_PLUGIN_FACTORY_DECLARATION( KeyboardQmlViewStepFactory ) From 8142d6f86cccc5119b25e0f737d23e5fff5188f6 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 27 Oct 2020 15:42:27 +0100 Subject: [PATCH 02/28] [keyboardq] Drop unnecessary variable -- just use the Config status --- src/modules/keyboardq/KeyboardQmlViewStep.cpp | 4 +--- src/modules/keyboardq/KeyboardQmlViewStep.h | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/modules/keyboardq/KeyboardQmlViewStep.cpp b/src/modules/keyboardq/KeyboardQmlViewStep.cpp index 8e4663ee8..3e37b58ec 100644 --- a/src/modules/keyboardq/KeyboardQmlViewStep.cpp +++ b/src/modules/keyboardq/KeyboardQmlViewStep.cpp @@ -37,7 +37,7 @@ KeyboardQmlViewStep::prettyName() const QString KeyboardQmlViewStep::prettyStatus() const { - return m_prettyStatus; + return m_config->prettyStatus(); } bool @@ -80,7 +80,6 @@ void KeyboardQmlViewStep::onLeave() { m_config->finalize(); - m_prettyStatus = m_config->prettyStatus(); } QObject* @@ -92,6 +91,5 @@ KeyboardQmlViewStep::getConfig() void KeyboardQmlViewStep::setConfigurationMap( const QVariantMap& configurationMap ) { - Calamares::QmlViewStep::setConfigurationMap( configurationMap ); } diff --git a/src/modules/keyboardq/KeyboardQmlViewStep.h b/src/modules/keyboardq/KeyboardQmlViewStep.h index 09390d8d9..c90c4a326 100644 --- a/src/modules/keyboardq/KeyboardQmlViewStep.h +++ b/src/modules/keyboardq/KeyboardQmlViewStep.h @@ -49,8 +49,6 @@ public: private: Config* m_config; bool m_nextEnabled; - QString m_prettyStatus; - }; CALAMARES_PLUGIN_FACTORY_DECLARATION( KeyboardQmlViewStepFactory ) From a940be2bb4233d7d2b99de99d8914dd4ffd3f2b3 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 27 Oct 2020 15:46:18 +0100 Subject: [PATCH 03/28] [keyboardq] Load configuration into Config object --- src/modules/keyboardq/KeyboardQmlViewStep.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/modules/keyboardq/KeyboardQmlViewStep.cpp b/src/modules/keyboardq/KeyboardQmlViewStep.cpp index 3e37b58ec..2eb9de0e7 100644 --- a/src/modules/keyboardq/KeyboardQmlViewStep.cpp +++ b/src/modules/keyboardq/KeyboardQmlViewStep.cpp @@ -91,5 +91,6 @@ KeyboardQmlViewStep::getConfig() void KeyboardQmlViewStep::setConfigurationMap( const QVariantMap& configurationMap ) { + m_config->setConfigurationMap( configurationMap ); Calamares::QmlViewStep::setConfigurationMap( configurationMap ); } From 679f61395529f79fc6ee23bd039eb002d9b68afa Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 27 Oct 2020 15:48:51 +0100 Subject: [PATCH 04/28] [keyboard] Rename mysterious Config::init() to descriptive name --- src/modules/keyboard/Config.cpp | 2 +- src/modules/keyboard/Config.h | 2 +- src/modules/keyboardq/KeyboardQmlViewStep.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/keyboard/Config.cpp b/src/modules/keyboard/Config.cpp index 7f7eb7b27..efb345edf 100644 --- a/src/modules/keyboard/Config.cpp +++ b/src/modules/keyboard/Config.cpp @@ -127,7 +127,7 @@ findLayout( const KeyboardLayoutModel* klm, const QString& currentLayout ) } void -Config::init() +Config::detectCurrentKeyboardLayout() { //### Detect current keyboard layout and variant QString currentLayout; diff --git a/src/modules/keyboard/Config.h b/src/modules/keyboard/Config.h index 9a3a3a013..7c18577f6 100644 --- a/src/modules/keyboard/Config.h +++ b/src/modules/keyboard/Config.h @@ -31,7 +31,7 @@ class Config : public QObject public: Config( QObject* parent = nullptr ); - void init(); + void detectCurrentKeyboardLayout(); Calamares::JobList createJobs(); QString prettyStatus() const; diff --git a/src/modules/keyboardq/KeyboardQmlViewStep.cpp b/src/modules/keyboardq/KeyboardQmlViewStep.cpp index 2eb9de0e7..62063b187 100644 --- a/src/modules/keyboardq/KeyboardQmlViewStep.cpp +++ b/src/modules/keyboardq/KeyboardQmlViewStep.cpp @@ -23,7 +23,7 @@ KeyboardQmlViewStep::KeyboardQmlViewStep( QObject* parent ) , m_config( new Config( this ) ) , m_nextEnabled( false ) { - m_config->init(); + m_config->detectCurrentKeyboardLayout(); m_nextEnabled = true; emit nextStatusChanged( m_nextEnabled ); } From acb51902174a435bbc58ea45abb407e3533f3711 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 27 Oct 2020 15:51:48 +0100 Subject: [PATCH 05/28] [keyboard] Use Config methods rather than own copy - this continues the port of the keyboard module to use the Config object, which was horribly botched earlier. --- src/modules/keyboard/KeyboardPage.cpp | 102 ---------------------- src/modules/keyboard/KeyboardPage.h | 2 - src/modules/keyboard/KeyboardViewStep.cpp | 2 +- 3 files changed, 1 insertion(+), 105 deletions(-) diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index 07c5cf763..f97e3ac52 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -93,108 +93,6 @@ KeyboardPage::~KeyboardPage() } -void -KeyboardPage::init() -{ - //### Detect current keyboard layout and variant - QString currentLayout; - QString currentVariant; - QProcess process; - process.start( "setxkbmap", QStringList() << "-print" ); - - if ( process.waitForFinished() ) - { - const QStringList list = QString( process.readAll() ).split( "\n", SplitSkipEmptyParts ); - - for ( QString line : list ) - { - line = line.trimmed(); - if ( !line.startsWith( "xkb_symbols" ) ) - { - continue; - } - - line = line.remove( "}" ).remove( "{" ).remove( ";" ); - line = line.mid( line.indexOf( "\"" ) + 1 ); - - QStringList split = line.split( "+", SplitSkipEmptyParts ); - if ( split.size() >= 2 ) - { - currentLayout = split.at( 1 ); - - if ( currentLayout.contains( "(" ) ) - { - int parenthesisIndex = currentLayout.indexOf( "(" ); - currentVariant = currentLayout.mid( parenthesisIndex + 1 ).trimmed(); - currentVariant.chop( 1 ); - currentLayout = currentLayout.mid( 0, parenthesisIndex ).trimmed(); - } - - break; - } - } - } - - //### Models - m_models = KeyboardGlobal::getKeyboardModels(); - QMapIterator< QString, QString > mi( m_models ); - - ui->comboBoxModel->blockSignals( true ); - - while ( mi.hasNext() ) - { - mi.next(); - - if ( mi.value() == "pc105" ) - { - m_defaultIndex = ui->comboBoxModel->count(); - } - - ui->comboBoxModel->addItem( mi.key() ); - } - - ui->comboBoxModel->blockSignals( false ); - - // Set to default value pc105 - ui->comboBoxModel->setCurrentIndex( m_defaultIndex ); - - - //### Layouts and Variants - - KeyboardLayoutModel* klm = new KeyboardLayoutModel( this ); - ui->listLayout->setModel( klm ); - connect( ui->listLayout->selectionModel(), - &QItemSelectionModel::currentChanged, - this, - &KeyboardPage::onListLayoutCurrentItemChanged ); - - // Block signals - ui->listLayout->blockSignals( true ); - - QPersistentModelIndex currentLayoutItem = findLayout( klm, currentLayout ); - if ( !currentLayoutItem.isValid() && ( ( currentLayout == "latin" ) || ( currentLayout == "pc" ) ) ) - { - currentLayout = "us"; - currentLayoutItem = findLayout( klm, currentLayout ); - } - - // Set current layout and variant - if ( currentLayoutItem.isValid() ) - { - ui->listLayout->setCurrentIndex( currentLayoutItem ); - updateVariants( currentLayoutItem, currentVariant ); - } - - // Unblock signals - ui->listLayout->blockSignals( false ); - - // Default to the first available layout if none was set - // Do this after unblocking signals so we get the default variant handling. - if ( !currentLayoutItem.isValid() && klm->rowCount() > 0 ) - { - ui->listLayout->setCurrentIndex( klm->index( 0 ) ); - } -} QString diff --git a/src/modules/keyboard/KeyboardPage.h b/src/modules/keyboard/KeyboardPage.h index 4faeebd57..4e85ae8b7 100644 --- a/src/modules/keyboard/KeyboardPage.h +++ b/src/modules/keyboard/KeyboardPage.h @@ -36,8 +36,6 @@ public: explicit KeyboardPage( QWidget* parent = nullptr ); ~KeyboardPage() override; - void init(); - QString prettyStatus() const; Calamares::JobList diff --git a/src/modules/keyboard/KeyboardViewStep.cpp b/src/modules/keyboard/KeyboardViewStep.cpp index 03159268e..0ba20dd87 100644 --- a/src/modules/keyboard/KeyboardViewStep.cpp +++ b/src/modules/keyboard/KeyboardViewStep.cpp @@ -23,7 +23,7 @@ KeyboardViewStep::KeyboardViewStep( QObject* parent ) , m_widget( new KeyboardPage() ) , m_nextEnabled( false ) { - m_widget->init(); + m_config->detectCurrentKeyboardLayout(); m_nextEnabled = true; emit nextStatusChanged( m_nextEnabled ); } From 14c079d1d6d8eab45a6476c43f740bf5aac72d59 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 27 Oct 2020 15:53:59 +0100 Subject: [PATCH 06/28] [keyboard] Replace own copy of status by the one from Config --- src/modules/keyboard/KeyboardPage.cpp | 17 ----------------- src/modules/keyboard/KeyboardPage.h | 2 -- src/modules/keyboard/KeyboardViewStep.cpp | 3 +-- src/modules/keyboard/KeyboardViewStep.h | 1 - 4 files changed, 1 insertion(+), 22 deletions(-) diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index f97e3ac52..25cd7a366 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -92,23 +92,6 @@ KeyboardPage::~KeyboardPage() delete ui; } - - - -QString -KeyboardPage::prettyStatus() const -{ - QString status; - status += tr( "Set keyboard model to %1.
" ).arg( ui->comboBoxModel->currentText() ); - - QString layout = ui->listLayout->currentIndex().data().toString(); - QString variant = ui->listVariant->currentItem() ? ui->listVariant->currentItem()->text() : QString( "" ); - status += tr( "Set keyboard layout to %1/%2." ).arg( layout, variant ); - - return status; -} - - QList< Calamares::job_ptr > KeyboardPage::createJobs( const QString& xOrgConfFileName, const QString& convertedKeymapPath, diff --git a/src/modules/keyboard/KeyboardPage.h b/src/modules/keyboard/KeyboardPage.h index 4e85ae8b7..6c2f99b68 100644 --- a/src/modules/keyboard/KeyboardPage.h +++ b/src/modules/keyboard/KeyboardPage.h @@ -36,8 +36,6 @@ public: explicit KeyboardPage( QWidget* parent = nullptr ); ~KeyboardPage() override; - QString prettyStatus() const; - Calamares::JobList createJobs( const QString& xOrgConfFileName, const QString& convertedKeymapPath, bool writeEtcDefaultKeyboard ); diff --git a/src/modules/keyboard/KeyboardViewStep.cpp b/src/modules/keyboard/KeyboardViewStep.cpp index 0ba20dd87..5cb168dc5 100644 --- a/src/modules/keyboard/KeyboardViewStep.cpp +++ b/src/modules/keyboard/KeyboardViewStep.cpp @@ -48,7 +48,7 @@ KeyboardViewStep::prettyName() const QString KeyboardViewStep::prettyStatus() const { - return m_prettyStatus; + return m_config->prettyStatus(); } @@ -105,7 +105,6 @@ void KeyboardViewStep::onLeave() { m_widget->finalize(); - m_prettyStatus = m_widget->prettyStatus(); } diff --git a/src/modules/keyboard/KeyboardViewStep.h b/src/modules/keyboard/KeyboardViewStep.h index 5cf2c6fd9..31769d192 100644 --- a/src/modules/keyboard/KeyboardViewStep.h +++ b/src/modules/keyboard/KeyboardViewStep.h @@ -50,7 +50,6 @@ private: Config* m_config; KeyboardPage* m_widget; bool m_nextEnabled; - QString m_prettyStatus; }; CALAMARES_PLUGIN_FACTORY_DECLARATION( KeyboardViewStepFactory ) From 35a2bd3f0fd1ba1a46f0c08ec21a1f23cd577d79 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 27 Oct 2020 15:55:29 +0100 Subject: [PATCH 07/28] [keyboard] Use Config's own finalize() --- src/modules/keyboard/KeyboardPage.cpp | 14 -------------- src/modules/keyboard/KeyboardPage.h | 1 - src/modules/keyboard/KeyboardViewStep.cpp | 2 +- 3 files changed, 1 insertion(+), 16 deletions(-) diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index 25cd7a366..7ca60be10 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -250,20 +250,6 @@ KeyboardPage::onActivate() } -void -KeyboardPage::finalize() -{ - Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage(); - if ( !m_selectedLayout.isEmpty() ) - { - gs->insert( "keyboardLayout", m_selectedLayout ); - gs->insert( "keyboardVariant", m_selectedVariant ); //empty means default variant - } - - //FIXME: also store keyboard model for something? -} - - void KeyboardPage::updateVariants( const QPersistentModelIndex& currentItem, QString currentVariant ) { diff --git a/src/modules/keyboard/KeyboardPage.h b/src/modules/keyboard/KeyboardPage.h index 6c2f99b68..41b4b7903 100644 --- a/src/modules/keyboard/KeyboardPage.h +++ b/src/modules/keyboard/KeyboardPage.h @@ -40,7 +40,6 @@ public: createJobs( const QString& xOrgConfFileName, const QString& convertedKeymapPath, bool writeEtcDefaultKeyboard ); void onActivate(); - void finalize(); protected slots: void onListLayoutCurrentItemChanged( const QModelIndex& current, const QModelIndex& previous ); diff --git a/src/modules/keyboard/KeyboardViewStep.cpp b/src/modules/keyboard/KeyboardViewStep.cpp index 5cb168dc5..a068b5a5d 100644 --- a/src/modules/keyboard/KeyboardViewStep.cpp +++ b/src/modules/keyboard/KeyboardViewStep.cpp @@ -104,7 +104,7 @@ KeyboardViewStep::onActivate() void KeyboardViewStep::onLeave() { - m_widget->finalize(); + m_config->finalize(); } From 0ffa500432a2dd5330e05d33c583336595c92ef6 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 27 Oct 2020 15:56:50 +0100 Subject: [PATCH 08/28] [keyboard] Remove unused code - this has already migrated to Config but had not been removed locally --- src/modules/keyboard/KeyboardPage.cpp | 37 --------------------------- src/modules/keyboard/KeyboardPage.h | 3 --- 2 files changed, 40 deletions(-) diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index 7ca60be10..decf7714a 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -40,23 +40,6 @@ public: LayoutItem::~LayoutItem() {} -static QPersistentModelIndex -findLayout( const KeyboardLayoutModel* klm, const QString& currentLayout ) -{ - QPersistentModelIndex currentLayoutItem; - - for ( int i = 0; i < klm->rowCount(); ++i ) - { - QModelIndex idx = klm->index( i ); - if ( idx.isValid() && idx.data( KeyboardLayoutModel::KeyboardLayoutKeyRole ).toString() == currentLayout ) - { - currentLayoutItem = idx; - } - } - - return currentLayoutItem; -} - KeyboardPage::KeyboardPage( QWidget* parent ) : QWidget( parent ) , ui( new Ui::Page_Keyboard ) @@ -92,26 +75,6 @@ KeyboardPage::~KeyboardPage() delete ui; } -QList< Calamares::job_ptr > -KeyboardPage::createJobs( const QString& xOrgConfFileName, - const QString& convertedKeymapPath, - bool writeEtcDefaultKeyboard ) -{ - QList< Calamares::job_ptr > list; - QString selectedModel = m_models.value( ui->comboBoxModel->currentText(), "pc105" ); - - Calamares::Job* j = new SetKeyboardLayoutJob( selectedModel, - m_selectedLayout, - m_selectedVariant, - xOrgConfFileName, - convertedKeymapPath, - writeEtcDefaultKeyboard ); - list.append( Calamares::job_ptr( j ) ); - - return list; -} - - void KeyboardPage::guessLayout( const QStringList& langParts ) { diff --git a/src/modules/keyboard/KeyboardPage.h b/src/modules/keyboard/KeyboardPage.h index 41b4b7903..554832b5f 100644 --- a/src/modules/keyboard/KeyboardPage.h +++ b/src/modules/keyboard/KeyboardPage.h @@ -36,9 +36,6 @@ public: explicit KeyboardPage( QWidget* parent = nullptr ); ~KeyboardPage() override; - Calamares::JobList - createJobs( const QString& xOrgConfFileName, const QString& convertedKeymapPath, bool writeEtcDefaultKeyboard ); - void onActivate(); protected slots: From 0947cd835486d238fbe8bb990c83b675f7621d50 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 27 Oct 2020 16:12:36 +0100 Subject: [PATCH 09/28] [keyboard] Migrate to Config::onActivate() which is badly-named --- src/modules/keyboard/KeyboardPage.cpp | 138 ---------------------- src/modules/keyboard/KeyboardPage.h | 4 - src/modules/keyboard/KeyboardViewStep.cpp | 2 +- 3 files changed, 1 insertion(+), 143 deletions(-) diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index decf7714a..bfa0b4b42 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -75,144 +75,6 @@ KeyboardPage::~KeyboardPage() delete ui; } -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() << Logger::SubEntry << "looking for locale part" << *countryPart; - for ( int i = 0; i < klm->rowCount(); ++i ) - { - QModelIndex idx = klm->index( i ); - QString name - = idx.isValid() ? idx.data( KeyboardLayoutModel::KeyboardLayoutKeyRole ).toString() : QString(); - if ( idx.isValid() && ( name.compare( *countryPart, Qt::CaseInsensitive ) == 0 ) ) - { - cDebug() << Logger::SubEntry << "matched" << name; - ui->listLayout->setCurrentIndex( idx ); - foundCountryPart = true; - 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() << Logger::SubEntry << "matched variant" << variantdata->data << ' ' - << variantdata->text(); - } - } - } - } - } -} - - -void -KeyboardPage::onActivate() -{ - /* Guessing a keyboard layout based on the locale means - * mapping between language identifiers in _ - * format to keyboard mappings, which are _ - * format; in addition, some countries have multiple languages, - * so fr_BE and nl_BE want different layouts (both Belgian) - * and sometimes the language-country name doesn't match the - * keyboard-country name at all (e.g. Ellas vs. Greek). - * - * This is a table of language-to-keyboard mappings. The - * language identifier is the key, while the value is - * a string that is used instead of the real language - * identifier in guessing -- so it should be something - * like _. - */ - static constexpr char arabic[] = "ara"; - static const auto specialCaseMap = QMap< std::string, std::string >( { - /* Most Arab countries map to Arabic keyboard (Default) */ - { "ar_AE", arabic }, - { "ar_BH", arabic }, - { "ar_DZ", arabic }, - { "ar_EG", arabic }, - { "ar_IN", arabic }, - { "ar_IQ", arabic }, - { "ar_JO", arabic }, - { "ar_KW", arabic }, - { "ar_LB", arabic }, - { "ar_LY", arabic }, - /* Not Morocco: use layout ma */ - { "ar_OM", arabic }, - { "ar_QA", arabic }, - { "ar_SA", arabic }, - { "ar_SD", arabic }, - { "ar_SS", arabic }, - /* Not Syria: use layout sy */ - { "ar_TN", arabic }, - { "ar_YE", arabic }, - { "ca_ES", "cat_ES" }, /* Catalan */ - { "as_ES", "ast_ES" }, /* Asturian */ - { "en_CA", "us" }, /* Canadian English */ - { "el_CY", "gr" }, /* Greek in Cyprus */ - { "el_GR", "gr" }, /* Greek in Greeze */ - { "ig_NG", "igbo_NG" }, /* Igbo in Nigeria */ - { "ha_NG", "hausa_NG" } /* Hausa */ - } ); - - 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 - } - if ( !lang.isEmpty() ) - { - std::string lang_s = lang.toStdString(); - if ( specialCaseMap.contains( lang_s ) ) - { - QString newLang = QString::fromStdString( specialCaseMap.value( lang_s ) ); - cDebug() << Logger::SubEntry << "special case language" << lang << "becomes" << newLang; - lang = newLang; - } - } - if ( !lang.isEmpty() ) - { - const auto langParts = lang.split( '_', SplitSkipEmptyParts ); - - // Note that this his string is not fit for display purposes! - // It doesn't come from QLocale::nativeCountryName. - QString country = QLocale::countryToString( QLocale( lang ).country() ); - cDebug() << Logger::SubEntry << "extracted country" << country << "::" << langParts; - - guessLayout( langParts ); - } -} - - void KeyboardPage::updateVariants( const QPersistentModelIndex& currentItem, QString currentVariant ) { diff --git a/src/modules/keyboard/KeyboardPage.h b/src/modules/keyboard/KeyboardPage.h index 554832b5f..bb2ec17a9 100644 --- a/src/modules/keyboard/KeyboardPage.h +++ b/src/modules/keyboard/KeyboardPage.h @@ -36,15 +36,11 @@ public: explicit KeyboardPage( QWidget* parent = nullptr ); ~KeyboardPage() override; - void onActivate(); - protected slots: void onListLayoutCurrentItemChanged( const QModelIndex& current, const QModelIndex& previous ); void onListVariantCurrentItemChanged( QListWidgetItem* current, QListWidgetItem* previous ); private: - /// Guess a layout based on the split-apart locale - void guessLayout( const QStringList& langParts ); void updateVariants( const QPersistentModelIndex& currentItem, QString currentVariant = QString() ); Ui::Page_Keyboard* ui; diff --git a/src/modules/keyboard/KeyboardViewStep.cpp b/src/modules/keyboard/KeyboardViewStep.cpp index a068b5a5d..844c3c719 100644 --- a/src/modules/keyboard/KeyboardViewStep.cpp +++ b/src/modules/keyboard/KeyboardViewStep.cpp @@ -97,7 +97,7 @@ KeyboardViewStep::jobs() const void KeyboardViewStep::onActivate() { - m_widget->onActivate(); + m_config->onActivate(); } From 734dbece8afe98645f5d916a686406f236860308 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 27 Oct 2020 16:47:47 +0100 Subject: [PATCH 10/28] [keyboardq] Fix include style, remove unnecessary declarations --- src/modules/keyboardq/KeyboardQmlViewStep.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/modules/keyboardq/KeyboardQmlViewStep.h b/src/modules/keyboardq/KeyboardQmlViewStep.h index c90c4a326..2b9f98e27 100644 --- a/src/modules/keyboardq/KeyboardQmlViewStep.h +++ b/src/modules/keyboardq/KeyboardQmlViewStep.h @@ -13,15 +13,12 @@ #include "Config.h" -#include -#include -#include +#include "DllMacro.h" +#include "utils/PluginFactory.h" +#include "viewpages/QmlViewStep.h" #include -class Config; -class KeyboardPage; - class PLUGINDLLEXPORT KeyboardQmlViewStep : public Calamares::QmlViewStep { Q_OBJECT From 8cf3bd23b9f5ff84c1b664ecf7e41e3df507d7da Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 27 Oct 2020 16:55:02 +0100 Subject: [PATCH 11/28] [keyboardq] Remove superfluous variable (set to true, never changed) --- src/modules/keyboardq/KeyboardQmlViewStep.cpp | 6 ++---- src/modules/keyboardq/KeyboardQmlViewStep.h | 1 - 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/modules/keyboardq/KeyboardQmlViewStep.cpp b/src/modules/keyboardq/KeyboardQmlViewStep.cpp index 62063b187..e8ae630e7 100644 --- a/src/modules/keyboardq/KeyboardQmlViewStep.cpp +++ b/src/modules/keyboardq/KeyboardQmlViewStep.cpp @@ -21,11 +21,9 @@ CALAMARES_PLUGIN_FACTORY_DEFINITION( KeyboardQmlViewStepFactory, registerPlugin< KeyboardQmlViewStep::KeyboardQmlViewStep( QObject* parent ) : Calamares::QmlViewStep( parent ) , m_config( new Config( this ) ) - , m_nextEnabled( false ) { m_config->detectCurrentKeyboardLayout(); - m_nextEnabled = true; - emit nextStatusChanged( m_nextEnabled ); + emit nextStatusChanged( true ); } QString @@ -43,7 +41,7 @@ KeyboardQmlViewStep::prettyStatus() const bool KeyboardQmlViewStep::isNextEnabled() const { - return m_nextEnabled; + return true; } bool diff --git a/src/modules/keyboardq/KeyboardQmlViewStep.h b/src/modules/keyboardq/KeyboardQmlViewStep.h index 2b9f98e27..eb31c3d59 100644 --- a/src/modules/keyboardq/KeyboardQmlViewStep.h +++ b/src/modules/keyboardq/KeyboardQmlViewStep.h @@ -45,7 +45,6 @@ public: private: Config* m_config; - bool m_nextEnabled; }; CALAMARES_PLUGIN_FACTORY_DECLARATION( KeyboardQmlViewStepFactory ) From 4a3273d6341e8f1fcc81eea937d0b45d9df5cc67 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 27 Oct 2020 16:59:22 +0100 Subject: [PATCH 12/28] [keyboard] Remove superfluous variable (set to true, never changed) --- src/modules/keyboard/KeyboardViewStep.cpp | 6 ++---- src/modules/keyboard/KeyboardViewStep.h | 1 - 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/modules/keyboard/KeyboardViewStep.cpp b/src/modules/keyboard/KeyboardViewStep.cpp index 844c3c719..80945f848 100644 --- a/src/modules/keyboard/KeyboardViewStep.cpp +++ b/src/modules/keyboard/KeyboardViewStep.cpp @@ -21,11 +21,9 @@ KeyboardViewStep::KeyboardViewStep( QObject* parent ) : Calamares::ViewStep( parent ) , m_config( new Config(this) ) , m_widget( new KeyboardPage() ) - , m_nextEnabled( false ) { m_config->detectCurrentKeyboardLayout(); - m_nextEnabled = true; - emit nextStatusChanged( m_nextEnabled ); + emit nextStatusChanged( true ); } @@ -62,7 +60,7 @@ KeyboardViewStep::widget() bool KeyboardViewStep::isNextEnabled() const { - return m_nextEnabled; + return true; } diff --git a/src/modules/keyboard/KeyboardViewStep.h b/src/modules/keyboard/KeyboardViewStep.h index 31769d192..902b888fd 100644 --- a/src/modules/keyboard/KeyboardViewStep.h +++ b/src/modules/keyboard/KeyboardViewStep.h @@ -49,7 +49,6 @@ public: private: Config* m_config; KeyboardPage* m_widget; - bool m_nextEnabled; }; CALAMARES_PLUGIN_FACTORY_DECLARATION( KeyboardViewStepFactory ) From fe5757c7d1cdbfb0d38ffc7c954b745c1cf43614 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 01:07:49 +0100 Subject: [PATCH 13/28] [keyboard] Matching of layouts is very suspect --- src/modules/keyboard/Config.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/keyboard/Config.cpp b/src/modules/keyboard/Config.cpp index 2df2e8935..9f93d9197 100644 --- a/src/modules/keyboard/Config.cpp +++ b/src/modules/keyboard/Config.cpp @@ -381,11 +381,11 @@ Config::guessLayout( const QStringList& langParts ) for ( int variantnumber = 0; variantnumber < m_keyboardVariantsModel->rowCount(); ++variantnumber ) { if ( m_keyboardVariantsModel->item( variantnumber )[ "key" ].compare( *countryPart, - Qt::CaseInsensitive ) ) + Qt::CaseInsensitive ) == 0 ) { m_keyboardVariantsModel->setCurrentIndex( variantnumber ); cDebug() << Logger::SubEntry << "matched variant" - << m_keyboardVariantsModel->item( variantnumber )[ "key" ] << ' ' + << *countryPart << ' ' << m_keyboardVariantsModel->item( variantnumber )[ "key" ]; } } From d464e98e892c2a396621590ecfbb04844441b4b6 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 01:10:05 +0100 Subject: [PATCH 14/28] [keyboard] Move remaining variables to Config - Config already *has* everythind, but drop the useless copies and duplicated code from the Page - Plug the models model into the Page - While here, document the model / layout / variant distinctions The code doesn't fill the UI properly, and the drop-down for the models combobox is not right, but at least the data is shared. --- src/modules/keyboard/Config.h | 27 +++++++----- src/modules/keyboard/KeyboardPage.cpp | 54 ++++++----------------- src/modules/keyboard/KeyboardPage.h | 9 +--- src/modules/keyboard/KeyboardViewStep.cpp | 2 +- 4 files changed, 34 insertions(+), 58 deletions(-) diff --git a/src/modules/keyboard/Config.h b/src/modules/keyboard/Config.h index 0446da525..fa8728975 100644 --- a/src/modules/keyboard/Config.h +++ b/src/modules/keyboard/Config.h @@ -44,6 +44,23 @@ public: static AdditionalLayoutInfo getAdditionalLayoutInfo( const QString& layout ); + /* A model is a physical configuration of a keyboard, e.g. 105-key PC + * or TKL 88-key physical size. + */ + KeyboardModelsModel* keyboardModels() const; + /* A layout describes the basic keycaps / language assigned to the + * keys of the physical keyboard, e.g. English (US) or Russian. + */ + KeyboardLayoutModel* keyboardLayouts() const; + /* A variant describes a variant of the basic keycaps; this can + * concern options (dead keys), or different placements of the keycaps + * (dvorak). + */ + KeyboardVariantsModel* keyboardVariants() const; + +signals: + void prettyStatusChanged(); + private: void guessLayout( const QStringList& langParts ); void updateVariants( const QPersistentModelIndex& currentItem, QString currentVariant = QString() ); @@ -65,16 +82,6 @@ private: QString m_xOrgConfFileName; QString m_convertedKeymapPath; bool m_writeEtcDefaultKeyboard = true; - - -protected: - KeyboardModelsModel* keyboardModels() const; - KeyboardLayoutModel* keyboardLayouts() const; - KeyboardVariantsModel* keyboardVariants() const; - - -signals: - void prettyStatusChanged(); }; diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index bfa0b4b42..f26c3d122 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -15,6 +15,7 @@ #include "KeyboardPage.h" +#include "Config.h" #include "KeyboardLayoutModel.h" #include "SetKeyboardLayoutJob.h" #include "keyboardwidget/keyboardpreview.h" @@ -40,30 +41,32 @@ public: LayoutItem::~LayoutItem() {} -KeyboardPage::KeyboardPage( QWidget* parent ) +KeyboardPage::KeyboardPage( Config* config, QWidget* parent ) : QWidget( parent ) , ui( new Ui::Page_Keyboard ) , m_keyboardPreview( new KeyBoardPreview( this ) ) - , m_defaultIndex( 0 ) { ui->setupUi( this ); // Keyboard Preview ui->KBPreviewLayout->addWidget( m_keyboardPreview ); - m_setxkbmapTimer.setSingleShot( true ); - + ui->comboBoxModel->setModel( config->keyboardModels() ); // Connect signals and slots connect( ui->listVariant, &QListWidget::currentItemChanged, this, &KeyboardPage::onListVariantCurrentItemChanged ); connect( - ui->buttonRestore, &QPushButton::clicked, [this] { ui->comboBoxModel->setCurrentIndex( m_defaultIndex ); } ); + ui->buttonRestore, &QPushButton::clicked, [this] { + cDebug() << "Restore clicked"; + // ui->comboBoxModel->setCurrentIndex( m_defaultIndex ); + } ); connect( ui->comboBoxModel, &QComboBox::currentTextChanged, [this]( const QString& text ) { - QString model = m_models.value( text, "pc105" ); + cDebug() << "ComboBox changed to" << text; + // QString model = m_models.value( text, "pc105" ); // Set Xorg keyboard model - QProcess::execute( "setxkbmap", QStringList { "-model", model } ); + // QProcess::execute( "setxkbmap", QStringList { "-model", model } ); } ); CALAMARES_RETRANSLATE( ui->retranslateUi( this ); ) @@ -128,25 +131,13 @@ KeyboardPage::onListLayoutCurrentItemChanged( const QModelIndex& current, const updateVariants( QPersistentModelIndex( current ) ); } -/* Returns stringlist with suitable setxkbmap command-line arguments - * to set the given @p layout and @p variant. - */ -static inline QStringList -xkbmap_args( const QString& layout, const QString& variant ) -{ - QStringList r { "-layout", layout }; - if ( !variant.isEmpty() ) - { - r << "-variant" << variant; - } - return r; -} - void KeyboardPage::onListVariantCurrentItemChanged( QListWidgetItem* current, QListWidgetItem* previous ) { Q_UNUSED( previous ) + cDebug() << "item" << Logger::Pointer( current ); + QPersistentModelIndex layoutIndex = ui->listLayout->currentIndex(); LayoutItem* variantItem = dynamic_cast< LayoutItem* >( current ); @@ -158,25 +149,8 @@ KeyboardPage::onListVariantCurrentItemChanged( QListWidgetItem* current, QListWi QString layout = layoutIndex.data( KeyboardLayoutModel::KeyboardLayoutKeyRole ).toString(); QString variant = variantItem->data; + cDebug() << Logger::SubEntry << layout << variant; + m_keyboardPreview->setLayout( layout ); m_keyboardPreview->setVariant( variant ); - - //emit checkReady(); - - // Set Xorg keyboard layout - if ( m_setxkbmapTimer.isActive() ) - { - m_setxkbmapTimer.stop(); - m_setxkbmapTimer.disconnect( this ); - } - - connect( &m_setxkbmapTimer, &QTimer::timeout, this, [=] { - QProcess::execute( "setxkbmap", xkbmap_args( layout, variant ) ); - cDebug() << "xkbmap selection changed to: " << layout << '-' << variant; - m_setxkbmapTimer.disconnect( this ); - } ); - m_setxkbmapTimer.start( QApplication::keyboardInputInterval() ); - - m_selectedLayout = layout; - m_selectedVariant = variant; } diff --git a/src/modules/keyboard/KeyboardPage.h b/src/modules/keyboard/KeyboardPage.h index bb2ec17a9..231099d0b 100644 --- a/src/modules/keyboard/KeyboardPage.h +++ b/src/modules/keyboard/KeyboardPage.h @@ -27,13 +27,14 @@ namespace Ui class Page_Keyboard; } +class Config; class KeyBoardPreview; class KeyboardPage : public QWidget { Q_OBJECT public: - explicit KeyboardPage( QWidget* parent = nullptr ); + explicit KeyboardPage( Config* config, QWidget* parent = nullptr ); ~KeyboardPage() override; protected slots: @@ -45,12 +46,6 @@ private: Ui::Page_Keyboard* ui; KeyBoardPreview* m_keyboardPreview; - int m_defaultIndex; - QMap< QString, QString > m_models; - - QString m_selectedLayout; - QString m_selectedVariant; - QTimer m_setxkbmapTimer; }; #endif // KEYBOARDPAGE_H diff --git a/src/modules/keyboard/KeyboardViewStep.cpp b/src/modules/keyboard/KeyboardViewStep.cpp index 80945f848..773ed1401 100644 --- a/src/modules/keyboard/KeyboardViewStep.cpp +++ b/src/modules/keyboard/KeyboardViewStep.cpp @@ -20,7 +20,7 @@ CALAMARES_PLUGIN_FACTORY_DEFINITION( KeyboardViewStepFactory, registerPlugin< Ke KeyboardViewStep::KeyboardViewStep( QObject* parent ) : Calamares::ViewStep( parent ) , m_config( new Config(this) ) - , m_widget( new KeyboardPage() ) + , m_widget( new KeyboardPage( m_config ) ) { m_config->detectCurrentKeyboardLayout(); emit nextStatusChanged( true ); From 5f1d7b2e8dc6449298dbec13c26edcf0fcd7a0a0 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 10:38:51 +0100 Subject: [PATCH 15/28] [keyboard] Rename UI widgets to make code clearer --- src/modules/keyboard/KeyboardPage.cpp | 20 ++++++++++---------- src/modules/keyboard/KeyboardPage.ui | 12 ++++++------ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index f26c3d122..9ce947ef6 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -51,17 +51,17 @@ KeyboardPage::KeyboardPage( Config* config, QWidget* parent ) // Keyboard Preview ui->KBPreviewLayout->addWidget( m_keyboardPreview ); - ui->comboBoxModel->setModel( config->keyboardModels() ); + ui->physicalModelSelector->setModel( config->keyboardModels() ); // Connect signals and slots - connect( ui->listVariant, &QListWidget::currentItemChanged, this, &KeyboardPage::onListVariantCurrentItemChanged ); + connect( ui->variantSelector, &QListWidget::currentItemChanged, this, &KeyboardPage::onListVariantCurrentItemChanged ); connect( ui->buttonRestore, &QPushButton::clicked, [this] { cDebug() << "Restore clicked"; - // ui->comboBoxModel->setCurrentIndex( m_defaultIndex ); + // ui->physicalModelSelector->setCurrentIndex( m_defaultIndex ); } ); - connect( ui->comboBoxModel, &QComboBox::currentTextChanged, [this]( const QString& text ) { + connect( ui->physicalModelSelector, &QComboBox::currentTextChanged, [this]( const QString& text ) { cDebug() << "ComboBox changed to" << text; // QString model = m_models.value( text, "pc105" ); @@ -82,14 +82,14 @@ void KeyboardPage::updateVariants( const QPersistentModelIndex& currentItem, QString currentVariant ) { // Block signals - ui->listVariant->blockSignals( true ); + ui->variantSelector->blockSignals( true ); QMap< QString, QString > variants = currentItem.data( KeyboardLayoutModel::KeyboardVariantsRole ).value< QMap< QString, QString > >(); QMapIterator< QString, QString > li( variants ); LayoutItem* defaultItem = nullptr; - ui->listVariant->clear(); + ui->variantSelector->clear(); while ( li.hasNext() ) { @@ -98,7 +98,7 @@ KeyboardPage::updateVariants( const QPersistentModelIndex& currentItem, QString LayoutItem* item = new LayoutItem(); item->setText( li.key() ); item->data = li.value(); - ui->listVariant->addItem( item ); + ui->variantSelector->addItem( item ); // currentVariant defaults to QString(). It is only non-empty during the // initial setup. @@ -109,12 +109,12 @@ KeyboardPage::updateVariants( const QPersistentModelIndex& currentItem, QString } // Unblock signals - ui->listVariant->blockSignals( false ); + ui->variantSelector->blockSignals( false ); // Set to default value if ( defaultItem ) { - ui->listVariant->setCurrentItem( defaultItem ); + ui->variantSelector->setCurrentItem( defaultItem ); } } @@ -138,7 +138,7 @@ KeyboardPage::onListVariantCurrentItemChanged( QListWidgetItem* current, QListWi cDebug() << "item" << Logger::Pointer( current ); - QPersistentModelIndex layoutIndex = ui->listLayout->currentIndex(); + QPersistentModelIndex layoutIndex = ui->layoutSelector->currentIndex(); LayoutItem* variantItem = dynamic_cast< LayoutItem* >( current ); if ( !layoutIndex.isValid() || !variantItem ) diff --git a/src/modules/keyboard/KeyboardPage.ui b/src/modules/keyboard/KeyboardPage.ui index f7e430a04..e8de90dec 100644 --- a/src/modules/keyboard/KeyboardPage.ui +++ b/src/modules/keyboard/KeyboardPage.ui @@ -76,7 +76,7 @@ SPDX-License-Identifier: GPL-3.0-or-later - + 0 @@ -110,10 +110,10 @@ SPDX-License-Identifier: GPL-3.0-or-later 9 - + - + @@ -139,9 +139,9 @@ SPDX-License-Identifier: GPL-3.0-or-later - comboBoxModel - listLayout - listVariant + physicalModelSelector + layoutSelector + variantSelector LE_TestKeyboard buttonRestore From a1c70b46a1e490682326085ba811dfdbed356ae5 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 13:06:43 +0100 Subject: [PATCH 16/28] [keyboard] Typo in comment --- src/modules/keyboard/keyboardwidget/keyboardglobal.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/keyboard/keyboardwidget/keyboardglobal.cpp b/src/modules/keyboard/keyboardwidget/keyboardglobal.cpp index 329913d79..8099cb231 100644 --- a/src/modules/keyboard/keyboardwidget/keyboardglobal.cpp +++ b/src/modules/keyboard/keyboardwidget/keyboardglobal.cpp @@ -75,7 +75,7 @@ parseKeyboardModels( const char* filepath ) break; } - // here we are in the model section, otherwhise we would continue or break + // here we are in the model section, otherwise we would continue or break QRegExp rx; rx.setPattern( "^\\s+(\\S+)\\s+(\\w.*)\n$" ); From 365a2ad6fd5fea34d705974418216575cff5d26f Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 13:41:21 +0100 Subject: [PATCH 17/28] [keyboard] Re-do the keyboard physical models model from scratch --- src/modules/keyboard/Config.cpp | 78 ++++----- src/modules/keyboard/KeyboardLayoutModel.cpp | 172 ++++++++++--------- src/modules/keyboard/KeyboardLayoutModel.h | 83 +++++---- 3 files changed, 181 insertions(+), 152 deletions(-) diff --git a/src/modules/keyboard/Config.cpp b/src/modules/keyboard/Config.cpp index 9f93d9197..d821146b3 100644 --- a/src/modules/keyboard/Config.cpp +++ b/src/modules/keyboard/Config.cpp @@ -50,22 +50,25 @@ xkbmap_layout_args( const QString& layout, const QString& variant ) } static inline QStringList -xkbmap_layout_args( const QStringList& layouts, const QStringList& variants, const QString& switchOption = "grp:alt_shift_toggle") +xkbmap_layout_args( const QStringList& layouts, + const QStringList& variants, + const QString& switchOption = "grp:alt_shift_toggle" ) { if ( layouts.size() != variants.size() ) { - cError() << "Number of layouts and variants must be equal (empty string should be used if there is no corresponding variant)"; + cError() << "Number of layouts and variants must be equal (empty string should be used if there is no " + "corresponding variant)"; return QStringList(); } - QStringList r{ "-layout", layouts.join( "," ) }; + QStringList r { "-layout", layouts.join( "," ) }; if ( !variants.isEmpty() ) { r << "-variant" << variants.join( "," ); } - if ( !switchOption.isEmpty()) + if ( !switchOption.isEmpty() ) { r << "-option" << switchOption; } @@ -88,17 +91,16 @@ xkbmap_query_grp_option() do { outputLine = setxkbmapQuery.readLine(); - } - while( setxkbmapQuery.canReadLine() && !outputLine.startsWith("options:") ); + } while ( setxkbmapQuery.canReadLine() && !outputLine.startsWith( "options:" ) ); - if( !outputLine.startsWith( "options:" ) ) + if ( !outputLine.startsWith( "options:" ) ) { return QString(); } int index = outputLine.indexOf( "grp:" ); - if( index == -1 ) + if ( index == -1 ) { return QString(); } @@ -106,14 +108,16 @@ xkbmap_query_grp_option() //it's either in the end of line or before the other option so \s or , int lastIndex = outputLine.indexOf( QRegExp( "[\\s,]" ), index ); - return outputLine.mid( index, lastIndex-1 ); + return outputLine.mid( index, lastIndex - 1 ); } -AdditionalLayoutInfo Config::getAdditionalLayoutInfo( const QString &layout ) +AdditionalLayoutInfo +Config::getAdditionalLayoutInfo( const QString& layout ) { QFile layoutTable( ":/non-ascii-layouts" ); - if( !layoutTable.open( QIODevice::ReadOnly | QIODevice::Text ) ) { + if ( !layoutTable.open( QIODevice::ReadOnly | QIODevice::Text ) ) + { cError() << "Non-ASCII layout table could not be opened"; return AdditionalLayoutInfo(); } @@ -123,10 +127,9 @@ AdditionalLayoutInfo Config::getAdditionalLayoutInfo( const QString &layout ) do { tableLine = layoutTable.readLine(); - } - while( layoutTable.canReadLine() && !tableLine.startsWith( layout ) ); + } while ( layoutTable.canReadLine() && !tableLine.startsWith( layout ) ); - if( !tableLine.startsWith( layout ) ) + if ( !tableLine.startsWith( layout ) ) { return AdditionalLayoutInfo(); } @@ -135,10 +138,10 @@ AdditionalLayoutInfo Config::getAdditionalLayoutInfo( const QString &layout ) AdditionalLayoutInfo r; - r.additionalLayout = tableEntries[1]; - r.additionalVariant = tableEntries[2] == "-" ? "" : tableEntries[2]; + r.additionalLayout = tableEntries[ 1 ]; + r.additionalVariant = tableEntries[ 2 ] == "-" ? "" : tableEntries[ 2 ]; - r.vconsoleKeymap = tableEntries[3]; + r.vconsoleKeymap = tableEntries[ 3 ]; return r; } @@ -153,7 +156,7 @@ Config::Config( QObject* parent ) // Connect signals and slots connect( m_keyboardModelsModel, &KeyboardModelsModel::currentIndexChanged, [&]( int index ) { - m_selectedModel = m_keyboardModelsModel->item( index ).value( "key", "pc105" ); + m_selectedModel = m_keyboardModelsModel->modelKey( index ); // Set Xorg keyboard model QProcess::execute( "setxkbmap", xkbmap_model_args( m_selectedModel ) ); emit prettyStatusChanged(); @@ -182,23 +185,20 @@ Config::Config( QObject* parent ) { m_additionalLayoutInfo.groupSwitcher = xkbmap_query_grp_option(); - if( m_additionalLayoutInfo.groupSwitcher.isEmpty() ) + if ( m_additionalLayoutInfo.groupSwitcher.isEmpty() ) { m_additionalLayoutInfo.groupSwitcher = "grp:alt_shift_toggle"; } - QProcess::execute( "setxkbmap", xkbmap_layout_args( - { m_additionalLayoutInfo.additionalLayout, m_selectedLayout }, - { m_additionalLayoutInfo.additionalVariant, m_selectedVariant }, - m_additionalLayoutInfo.groupSwitcher ) - ); + QProcess::execute( "setxkbmap", + xkbmap_layout_args( { m_additionalLayoutInfo.additionalLayout, m_selectedLayout }, + { m_additionalLayoutInfo.additionalVariant, m_selectedVariant }, + m_additionalLayoutInfo.groupSwitcher ) ); - - cDebug() << "xkbmap selection changed to: " << m_selectedLayout << '-' << m_selectedVariant - << "(added " << m_additionalLayoutInfo.additionalLayout << "-" - << m_additionalLayoutInfo.additionalVariant << " since current layout is not ASCII-capable)"; - + cDebug() << "xkbmap selection changed to: " << m_selectedLayout << '-' << m_selectedVariant << "(added " + << m_additionalLayoutInfo.additionalLayout << "-" << m_additionalLayoutInfo.additionalVariant + << " since current layout is not ASCII-capable)"; } else { @@ -269,15 +269,15 @@ Config::detectCurrentKeyboardLayout() continue; } - int firstQuote = line.indexOf('"'); - int lastQuote = line.lastIndexOf('"'); + int firstQuote = line.indexOf( '"' ); + int lastQuote = line.lastIndexOf( '"' ); - if (firstQuote < 0 || lastQuote < 0 || lastQuote <= firstQuote) + if ( firstQuote < 0 || lastQuote < 0 || lastQuote <= firstQuote ) { continue; } - QStringList split = line.mid(firstQuote+1, lastQuote-firstQuote).split( "+", SplitSkipEmptyParts ); + QStringList split = line.mid( firstQuote + 1, lastQuote - firstQuote ).split( "+", SplitSkipEmptyParts ); cDebug() << split; if ( split.size() >= 2 ) { @@ -324,7 +324,7 @@ Config::prettyStatus() const { QString status; status += tr( "Set keyboard model to %1.
" ) - .arg( m_keyboardModelsModel->item( m_keyboardModelsModel->currentIndex() )[ "label" ] ); + .arg( m_keyboardModelsModel->modelLabel( m_keyboardModelsModel->currentIndex() ) ); QString layout = m_keyboardLayoutsModel->item( m_keyboardLayoutsModel->currentIndex() ).second.description; QString variant = m_keyboardVariantsModel->currentIndex() >= 0 @@ -381,11 +381,11 @@ Config::guessLayout( const QStringList& langParts ) for ( int variantnumber = 0; variantnumber < m_keyboardVariantsModel->rowCount(); ++variantnumber ) { if ( m_keyboardVariantsModel->item( variantnumber )[ "key" ].compare( *countryPart, - Qt::CaseInsensitive ) == 0 ) + Qt::CaseInsensitive ) + == 0 ) { m_keyboardVariantsModel->setCurrentIndex( variantnumber ); - cDebug() << Logger::SubEntry << "matched variant" - << *countryPart << ' ' + cDebug() << Logger::SubEntry << "matched variant" << *countryPart << ' ' << m_keyboardVariantsModel->item( variantnumber )[ "key" ]; } } @@ -497,8 +497,8 @@ Config::finalize() if ( !m_additionalLayoutInfo.additionalLayout.isEmpty() ) { - gs->insert( "keyboardAdditionalLayout", m_additionalLayoutInfo.additionalLayout); - gs->insert( "keyboardAdditionalLayout", m_additionalLayoutInfo.additionalVariant); + gs->insert( "keyboardAdditionalLayout", m_additionalLayoutInfo.additionalLayout ); + gs->insert( "keyboardAdditionalLayout", m_additionalLayoutInfo.additionalVariant ); gs->insert( "keyboardVConsoleKeymap", m_additionalLayoutInfo.vconsoleKeymap ); } } diff --git a/src/modules/keyboard/KeyboardLayoutModel.cpp b/src/modules/keyboard/KeyboardLayoutModel.cpp index 2a4ffa4d6..f0393ba6c 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.cpp +++ b/src/modules/keyboard/KeyboardLayoutModel.cpp @@ -10,8 +10,96 @@ #include "KeyboardLayoutModel.h" +#include "utils/Logger.h" + #include +KeyboardModelsModel::KeyboardModelsModel( QObject* parent ) + : QAbstractListModel( parent ) +{ + // The models map is from human-readable names (!) to xkb identifier + const auto models = KeyboardGlobal::getKeyboardModels(); + for ( const auto& key : models.keys() ) + { + // So here *key* is the key in the map, which is the human-readable thing, + // while the struct fields are xkb-id, and human-readable + m_list << ModelInfo { models[ key ], key }; + } + + cDebug() << "Loaded" << m_list.count() << "keyboard models"; +} + +int +KeyboardModelsModel::rowCount( const QModelIndex& ) const +{ + return m_list.count(); +} + +QVariant +KeyboardModelsModel::data( const QModelIndex& index, int role ) const +{ + if ( !index.isValid() ) + { + return QVariant(); + } + if ( index.row() < 0 || index.row() >= m_list.count() ) + { + return QVariant(); + } + + const auto item = m_list.at( index.row() ); + switch ( role ) + { + case LabelRole: + return item.label; + case KeyRole: + return item.key; + default: + return QVariant(); + } + __builtin_unreachable(); +} + +QString +KeyboardModelsModel::modelKey( int index ) const +{ + if ( index < 0 || index >= m_list.count() ) + { + return QString(); + } + return m_list[ index ].key; +} + +QString +KeyboardModelsModel::modelLabel( int index ) const +{ + if ( index < 0 || index >= m_list.count() ) + { + return QString(); + } + return m_list[ index ].label; +} + +QHash< int, QByteArray > +KeyboardModelsModel::roleNames() const +{ + return { { Qt::DisplayRole, "label" }, { Qt::UserRole, "key" } }; +} + +void +KeyboardModelsModel::setCurrentIndex( int index ) +{ + if ( index >= m_list.count() || index < 0 ) + { + return; + } + if ( m_currentIndex != index ) + { + m_currentIndex = index; + emit currentIndexChanged( m_currentIndex ); + } +} + KeyboardLayoutModel::KeyboardLayoutModel( QObject* parent ) : QAbstractListModel( parent ) @@ -100,77 +188,6 @@ KeyboardLayoutModel::currentIndex() const return m_currentIndex; } -KeyboardModelsModel::KeyboardModelsModel( QObject* parent ) - : QAbstractListModel( parent ) -{ - detectModels(); -} - -void -KeyboardModelsModel::detectModels() -{ - beginResetModel(); - const auto models = KeyboardGlobal::getKeyboardModels(); - auto index = -1; - for ( const auto& key : models.keys() ) - { - index++; - m_list << QMap< QString, QString > { { "label", key }, { "key", models[ key ] } }; - if ( models[ key ] == "pc105" ) - { - this->setCurrentIndex( index ); - } - } - endResetModel(); -} - -void -KeyboardModelsModel::refresh() -{ - m_list.clear(); - setCurrentIndex( -1 ); - detectModels(); -} - -QVariant -KeyboardModelsModel::data( const QModelIndex& index, int role ) const -{ - if ( !index.isValid() ) - { - return QVariant(); - } - const auto item = m_list.at( index.row() ); - return role == Qt::DisplayRole ? item[ "label" ] : item[ "key" ]; -} - -int -KeyboardModelsModel::rowCount( const QModelIndex& ) const -{ - return m_list.count(); -} - -QHash< int, QByteArray > -KeyboardModelsModel::roleNames() const -{ - return { { Qt::DisplayRole, "label" }, { Qt::UserRole, "key" } }; -} - -int -KeyboardModelsModel::currentIndex() const -{ - return m_currentIndex; -} - -const QMap< QString, QString > -KeyboardModelsModel::item( const int& index ) const -{ - if ( index >= m_list.count() || index < 0 ) - { - return QMap< QString, QString >(); - } - - return m_list.at( index ); -} const QMap< QString, QString > KeyboardVariantsModel::item( const int& index ) const @@ -183,18 +200,6 @@ KeyboardVariantsModel::item( const int& index ) const return m_list.at( index ); } -void -KeyboardModelsModel::setCurrentIndex( const int& index ) -{ - if ( index >= m_list.count() || index < 0 ) - { - return; - } - - m_currentIndex = index; - emit currentIndexChanged( m_currentIndex ); -} - KeyboardVariantsModel::KeyboardVariantsModel( QObject* parent ) : QAbstractListModel( parent ) { @@ -253,4 +258,3 @@ KeyboardVariantsModel::setVariants( QMap< QString, QString > variants ) } endResetModel(); } - diff --git a/src/modules/keyboard/KeyboardLayoutModel.h b/src/modules/keyboard/KeyboardLayoutModel.h index 0fd662263..a7075b798 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.h +++ b/src/modules/keyboard/KeyboardLayoutModel.h @@ -17,6 +17,60 @@ #include #include +/** @brief A list model of the physical keyboard formats ("models" in xkb) + * + * This model acts like it has a single selection, as well. + */ +class KeyboardModelsModel : public QAbstractListModel +{ + Q_OBJECT + Q_PROPERTY( int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged ) + +public: + enum + { + LabelRole = Qt::DisplayRole, ///< Human-readable + KeyRole = Qt::UserRole ///< xkb identifier + }; + + explicit KeyboardModelsModel( QObject* parent = nullptr ); + + int rowCount( const QModelIndex& ) const override; + QVariant data( const QModelIndex& index, int role ) const override; + /** @brief xkb key for a given index (row) + * + * This is like calling data( QModelIndex( index ), KeyRole ).toString(), + * but shorter and faster. Can return an empty string if index is invalid. + */ + QString modelKey( int index ) const; + + /** @brief human-readable label for a given index (row) + * + * This is like calling data( QModelIndex( index ), LabelRole ).toString(), + * but shorter and faster. Can return an empty string if index is invalid. + */ + QString modelLabel( int index ) const; + + QHash< int, QByteArray > roleNames() const override; + + void setCurrentIndex( int index ); + int currentIndex() const { return m_currentIndex; } + +signals: + void currentIndexChanged( int index ); + +private: + struct ModelInfo + { + /// XKB identifier + QString key; + /// Human-readable + QString label; + }; + QVector< ModelInfo > m_list; + int m_currentIndex; +}; + class KeyboardLayoutModel : public QAbstractListModel { Q_OBJECT @@ -51,35 +105,6 @@ signals: void currentIndexChanged( int index ); }; -class KeyboardModelsModel : public QAbstractListModel -{ - Q_OBJECT - Q_PROPERTY( int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged ) - -public: - explicit KeyboardModelsModel( QObject* parent = nullptr ); - int rowCount( const QModelIndex& = QModelIndex() ) const override; - QVariant data( const QModelIndex& index, int role ) const override; - - void setCurrentIndex( const int& index ); - int currentIndex() const; - const QMap< QString, QString > item( const int& index ) const; - -public slots: - void refresh(); - -protected: - QHash< int, QByteArray > roleNames() const override; - -private: - int m_currentIndex = -1; - QVector< QMap< QString, QString > > m_list; - void detectModels(); - -signals: - void currentIndexChanged( int index ); -}; - class KeyboardVariantsModel : public QAbstractListModel { Q_OBJECT From 6aedf4401f8a549d2b51be2b3341a16ebd46942f Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 13:41:34 +0100 Subject: [PATCH 18/28] [keyboard] Apply coding style --- src/modules/keyboard/AdditionalLayoutInfo.h | 3 ++- src/modules/keyboard/Config.h | 2 +- src/modules/keyboard/KeyboardPage.cpp | 12 ++++++------ src/modules/keyboard/KeyboardViewStep.cpp | 4 ++-- src/modules/keyboard/SetKeyboardLayoutJob.cpp | 8 +++++--- src/modules/keyboard/SetKeyboardLayoutJob.h | 3 +-- 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/modules/keyboard/AdditionalLayoutInfo.h b/src/modules/keyboard/AdditionalLayoutInfo.h index 660449952..61e854d3b 100644 --- a/src/modules/keyboard/AdditionalLayoutInfo.h +++ b/src/modules/keyboard/AdditionalLayoutInfo.h @@ -12,7 +12,8 @@ #include -struct AdditionalLayoutInfo { +struct AdditionalLayoutInfo +{ QString additionalLayout; QString additionalVariant; diff --git a/src/modules/keyboard/Config.h b/src/modules/keyboard/Config.h index fa8728975..e35193484 100644 --- a/src/modules/keyboard/Config.h +++ b/src/modules/keyboard/Config.h @@ -11,9 +11,9 @@ #ifndef KEYBOARD_CONFIG_H #define KEYBOARD_CONFIG_H +#include "AdditionalLayoutInfo.h" #include "Job.h" #include "KeyboardLayoutModel.h" -#include "AdditionalLayoutInfo.h" #include #include diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index 9ce947ef6..a6b1d3a16 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -53,13 +53,13 @@ KeyboardPage::KeyboardPage( Config* config, QWidget* parent ) ui->physicalModelSelector->setModel( config->keyboardModels() ); // Connect signals and slots - connect( ui->variantSelector, &QListWidget::currentItemChanged, this, &KeyboardPage::onListVariantCurrentItemChanged ); - connect( - ui->buttonRestore, &QPushButton::clicked, [this] { - cDebug() << "Restore clicked"; - // ui->physicalModelSelector->setCurrentIndex( m_defaultIndex ); - } ); + ui->variantSelector, &QListWidget::currentItemChanged, this, &KeyboardPage::onListVariantCurrentItemChanged ); + + connect( ui->buttonRestore, &QPushButton::clicked, [this] { + cDebug() << "Restore clicked"; + // ui->physicalModelSelector->setCurrentIndex( m_defaultIndex ); + } ); connect( ui->physicalModelSelector, &QComboBox::currentTextChanged, [this]( const QString& text ) { cDebug() << "ComboBox changed to" << text; diff --git a/src/modules/keyboard/KeyboardViewStep.cpp b/src/modules/keyboard/KeyboardViewStep.cpp index 773ed1401..d1eb3eb68 100644 --- a/src/modules/keyboard/KeyboardViewStep.cpp +++ b/src/modules/keyboard/KeyboardViewStep.cpp @@ -19,7 +19,7 @@ CALAMARES_PLUGIN_FACTORY_DEFINITION( KeyboardViewStepFactory, registerPlugin< Ke KeyboardViewStep::KeyboardViewStep( QObject* parent ) : Calamares::ViewStep( parent ) - , m_config( new Config(this) ) + , m_config( new Config( this ) ) , m_widget( new KeyboardPage( m_config ) ) { m_config->detectCurrentKeyboardLayout(); @@ -109,5 +109,5 @@ KeyboardViewStep::onLeave() void KeyboardViewStep::setConfigurationMap( const QVariantMap& configurationMap ) { - m_config->setConfigurationMap(configurationMap); + m_config->setConfigurationMap( configurationMap ); } diff --git a/src/modules/keyboard/SetKeyboardLayoutJob.cpp b/src/modules/keyboard/SetKeyboardLayoutJob.cpp index 766044179..419d6f3fc 100644 --- a/src/modules/keyboard/SetKeyboardLayoutJob.cpp +++ b/src/modules/keyboard/SetKeyboardLayoutJob.cpp @@ -30,7 +30,7 @@ #include -SetKeyboardLayoutJob::SetKeyboardLayoutJob(const QString& model, +SetKeyboardLayoutJob::SetKeyboardLayoutJob( const QString& model, const QString& layout, const QString& variant, const AdditionalLayoutInfo& additionalLayoutInfo, @@ -269,12 +269,14 @@ SetKeyboardLayoutJob::writeX11Data( const QString& keyboardConfPath ) const { if ( !m_layout.isEmpty() ) { - stream << " Option \"XkbLayout\" \"" << m_additionalLayoutInfo.additionalLayout << "," << m_layout << "\"\n"; + stream << " Option \"XkbLayout\" \"" << m_additionalLayoutInfo.additionalLayout << "," << m_layout + << "\"\n"; } if ( !m_variant.isEmpty() ) { - stream << " Option \"XkbVariant\" \"" << m_additionalLayoutInfo.additionalVariant << "," << m_variant << "\"\n"; + stream << " Option \"XkbVariant\" \"" << m_additionalLayoutInfo.additionalVariant << "," << m_variant + << "\"\n"; } stream << " Option \"XkbOptions\" \"" << m_additionalLayoutInfo.groupSwitcher << "\"\n"; diff --git a/src/modules/keyboard/SetKeyboardLayoutJob.h b/src/modules/keyboard/SetKeyboardLayoutJob.h index b1ce0bb15..15fadfb52 100644 --- a/src/modules/keyboard/SetKeyboardLayoutJob.h +++ b/src/modules/keyboard/SetKeyboardLayoutJob.h @@ -11,9 +11,8 @@ #ifndef SETKEYBOARDLAYOUTJOB_H #define SETKEYBOARDLAYOUTJOB_H -#include "Job.h" #include "AdditionalLayoutInfo.h" - +#include "Job.h" class SetKeyboardLayoutJob : public Calamares::Job From 193efe171007cad73c3baa2c9e360a7a530a77c6 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 13:52:30 +0100 Subject: [PATCH 19/28] [keyboard] Restore the notion of PC105 default index --- src/modules/keyboard/KeyboardLayoutModel.cpp | 6 ++++++ src/modules/keyboard/KeyboardLayoutModel.h | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/modules/keyboard/KeyboardLayoutModel.cpp b/src/modules/keyboard/KeyboardLayoutModel.cpp index f0393ba6c..ab7dbd37f 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.cpp +++ b/src/modules/keyboard/KeyboardLayoutModel.cpp @@ -19,11 +19,17 @@ KeyboardModelsModel::KeyboardModelsModel( QObject* parent ) { // The models map is from human-readable names (!) to xkb identifier const auto models = KeyboardGlobal::getKeyboardModels(); + int index = 0; for ( const auto& key : models.keys() ) { // So here *key* is the key in the map, which is the human-readable thing, // while the struct fields are xkb-id, and human-readable m_list << ModelInfo { models[ key ], key }; + if ( models[ key ] == "pc105" ) + { + m_defaultPC105 = index; + } + index++; } cDebug() << "Loaded" << m_list.count() << "keyboard models"; diff --git a/src/modules/keyboard/KeyboardLayoutModel.h b/src/modules/keyboard/KeyboardLayoutModel.h index a7075b798..d0edac2d1 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.h +++ b/src/modules/keyboard/KeyboardLayoutModel.h @@ -54,6 +54,8 @@ public: QHash< int, QByteArray > roleNames() const override; void setCurrentIndex( int index ); + /// @brief Set the index back to PC105 (the default physical model) + void setCurrentIndex() { setCurrentIndex( m_defaultPC105 ); } int currentIndex() const { return m_currentIndex; } signals: @@ -68,7 +70,8 @@ private: QString label; }; QVector< ModelInfo > m_list; - int m_currentIndex; + int m_currentIndex = -1; + int m_defaultPC105 = -1; ///< The index of pc105, if there is one }; class KeyboardLayoutModel : public QAbstractListModel From 87aafb2442c3208a256c7c01b92d1b9aa405b1df Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 13:55:21 +0100 Subject: [PATCH 20/28] [keyboard] Tear up connections between widgets and model --- src/modules/keyboard/Config.cpp | 7 +++++-- src/modules/keyboard/KeyboardPage.cpp | 16 +++------------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/modules/keyboard/Config.cpp b/src/modules/keyboard/Config.cpp index d821146b3..13954eb20 100644 --- a/src/modules/keyboard/Config.cpp +++ b/src/modules/keyboard/Config.cpp @@ -156,22 +156,25 @@ Config::Config( QObject* parent ) // Connect signals and slots connect( m_keyboardModelsModel, &KeyboardModelsModel::currentIndexChanged, [&]( int index ) { + // Set Xorg keyboard model m_selectedModel = m_keyboardModelsModel->modelKey( index ); - // Set Xorg keyboard model + cDebug() << "Setting model" << index << m_selectedModel; QProcess::execute( "setxkbmap", xkbmap_model_args( m_selectedModel ) ); emit prettyStatusChanged(); } ); connect( m_keyboardLayoutsModel, &KeyboardLayoutModel::currentIndexChanged, [&]( int index ) { m_selectedLayout = m_keyboardLayoutsModel->item( index ).first; + cDebug() << "Setting layout" << index << m_selectedLayout; updateVariants( QPersistentModelIndex( m_keyboardLayoutsModel->index( index ) ) ); emit prettyStatusChanged(); } ); connect( m_keyboardVariantsModel, &KeyboardVariantsModel::currentIndexChanged, [&]( int index ) { + // Set Xorg keyboard layout + variant m_selectedVariant = m_keyboardVariantsModel->item( index )[ "key" ]; + cDebug() << "Setting variant" << index << m_selectedVariant; - // Set Xorg keyboard layout if ( m_setxkbmapTimer.isActive() ) { m_setxkbmapTimer.stop(); diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index a6b1d3a16..9974f7bf7 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -52,22 +52,12 @@ KeyboardPage::KeyboardPage( Config* config, QWidget* parent ) ui->KBPreviewLayout->addWidget( m_keyboardPreview ); ui->physicalModelSelector->setModel( config->keyboardModels() ); - // Connect signals and slots - connect( - ui->variantSelector, &QListWidget::currentItemChanged, this, &KeyboardPage::onListVariantCurrentItemChanged ); + ui->layoutSelector->setModel( config->keyboardLayouts() ); - connect( ui->buttonRestore, &QPushButton::clicked, [this] { - cDebug() << "Restore clicked"; - // ui->physicalModelSelector->setCurrentIndex( m_defaultIndex ); + connect( ui->buttonRestore, &QPushButton::clicked, [config=config] { + config->keyboardModels()->setCurrentIndex(); } ); - connect( ui->physicalModelSelector, &QComboBox::currentTextChanged, [this]( const QString& text ) { - cDebug() << "ComboBox changed to" << text; - // QString model = m_models.value( text, "pc105" ); - - // Set Xorg keyboard model - // QProcess::execute( "setxkbmap", QStringList { "-model", model } ); - } ); CALAMARES_RETRANSLATE( ui->retranslateUi( this ); ) } From 168be02c964c8971925b60250c39cc019c03a4a3 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 14:03:46 +0100 Subject: [PATCH 21/28] [keyboard] Hook up the model-selection again --- src/modules/keyboard/KeyboardPage.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index 9974f7bf7..135aa1bc5 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -57,7 +57,10 @@ KeyboardPage::KeyboardPage( Config* config, QWidget* parent ) connect( ui->buttonRestore, &QPushButton::clicked, [config=config] { config->keyboardModels()->setCurrentIndex(); } ); - + connect( ui->physicalModelSelector, + QOverload::of( &QComboBox::currentIndexChanged ), + config->keyboardModels(), + QOverload::of( &KeyboardModelsModel::setCurrentIndex ) ); CALAMARES_RETRANSLATE( ui->retranslateUi( this ); ) } From 5afe54132bf6bcaaef73f960d25813770f06bf18 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 15:34:47 +0100 Subject: [PATCH 22/28] [keyboard] Use the models from Config - Remove code that is duplicated in Config. - Hook up UI for physical keyboard model, and back. - For now, introduce some named slots with debugging output. This makes debugging a lot easier since we have function names to work with rather than anonymous lambdas --- src/modules/keyboard/KeyboardPage.cpp | 156 ++++++++++++-------------- src/modules/keyboard/KeyboardPage.h | 10 +- src/modules/keyboard/KeyboardPage.ui | 2 +- 3 files changed, 79 insertions(+), 89 deletions(-) diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index 135aa1bc5..90dacef95 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -45,105 +45,93 @@ KeyboardPage::KeyboardPage( Config* config, QWidget* parent ) : QWidget( parent ) , ui( new Ui::Page_Keyboard ) , m_keyboardPreview( new KeyBoardPreview( this ) ) + , m_config( config ) { ui->setupUi( this ); // Keyboard Preview ui->KBPreviewLayout->addWidget( m_keyboardPreview ); - ui->physicalModelSelector->setModel( config->keyboardModels() ); - ui->layoutSelector->setModel( config->keyboardLayouts() ); + { + auto* model = config->keyboardModels(); + model->setCurrentIndex(); // To default PC105 + ui->physicalModelSelector->setModel( model ); + ui->physicalModelSelector->setCurrentIndex( model->currentIndex() ); + } + { + auto* model = config->keyboardLayouts(); + ui->layoutSelector->setModel( model ); + ui->layoutSelector->setCurrentIndex( model->index( model->currentIndex() ) ); + } + { + auto* model = config->keyboardVariants(); + ui->variantSelector->setModel( model ); + ui->variantSelector->setCurrentIndex( model->index( model->currentIndex() ) ); + cDebug() << "Variants now" << model->rowCount() << model->currentIndex(); + } + + connect( + ui->buttonRestore, &QPushButton::clicked, [config = config] { config->keyboardModels()->setCurrentIndex(); } ); - connect( ui->buttonRestore, &QPushButton::clicked, [config=config] { - config->keyboardModels()->setCurrentIndex(); - } ); connect( ui->physicalModelSelector, - QOverload::of( &QComboBox::currentIndexChanged ), + QOverload< int >::of( &QComboBox::currentIndexChanged ), config->keyboardModels(), - QOverload::of( &KeyboardModelsModel::setCurrentIndex ) ); + QOverload< int >::of( &KeyboardModelsModel::setCurrentIndex ) ); + connect( config->keyboardModels(), + &KeyboardModelsModel::currentIndexChanged, + ui->physicalModelSelector, + &QComboBox::setCurrentIndex ); + + connect( ui->layoutSelector->selectionModel(), + &QItemSelectionModel::currentChanged, + this, + &KeyboardPage::layoutChangedByUser ); + connect( config->keyboardLayouts(), + &KeyboardLayoutModel::currentIndexChanged, + this, + &KeyboardPage::layoutChangedByConfig ); + + connect( ui->variantSelector->selectionModel(), + &QItemSelectionModel::currentChanged, + this, + &KeyboardPage::variantChangedByUser ); + connect( config->keyboardVariants(), + &KeyboardVariantsModel::currentIndexChanged, + this, + &KeyboardPage::variantChangedByConfig ); CALAMARES_RETRANSLATE( ui->retranslateUi( this ); ) } +void +KeyboardPage::layoutChangedByUser( const QModelIndex& current, const QModelIndex& previous ) +{ + cDebug() << "index ->" << current.row(); + m_config->keyboardLayouts()->setCurrentIndex( current.row() ); + cDebug() << Logger::SubEntry << "variants now" << m_config->keyboardVariants()->rowCount(); +} + +void +KeyboardPage::layoutChangedByConfig( int index ) +{ + cDebug() << "index ->" << index; + ui->layoutSelector->setCurrentIndex( m_config->keyboardLayouts()->index( index ) ); + cDebug() << Logger::SubEntry << "variants now" << m_config->keyboardVariants()->rowCount(); +} + +void +KeyboardPage::variantChangedByUser( const QModelIndex& current, const QModelIndex& previous ) +{ + cDebug() << "index ->" << current.row(); +} + +void +KeyboardPage::variantChangedByConfig( int index ) +{ + cDebug() << "index ->" << index; +} KeyboardPage::~KeyboardPage() { delete ui; } - -void -KeyboardPage::updateVariants( const QPersistentModelIndex& currentItem, QString currentVariant ) -{ - // Block signals - ui->variantSelector->blockSignals( true ); - - QMap< QString, QString > variants - = currentItem.data( KeyboardLayoutModel::KeyboardVariantsRole ).value< QMap< QString, QString > >(); - QMapIterator< QString, QString > li( variants ); - LayoutItem* defaultItem = nullptr; - - ui->variantSelector->clear(); - - while ( li.hasNext() ) - { - li.next(); - - LayoutItem* item = new LayoutItem(); - item->setText( li.key() ); - item->data = li.value(); - ui->variantSelector->addItem( item ); - - // currentVariant defaults to QString(). It is only non-empty during the - // initial setup. - if ( li.value() == currentVariant ) - { - defaultItem = item; - } - } - - // Unblock signals - ui->variantSelector->blockSignals( false ); - - // Set to default value - if ( defaultItem ) - { - ui->variantSelector->setCurrentItem( defaultItem ); - } -} - - -void -KeyboardPage::onListLayoutCurrentItemChanged( const QModelIndex& current, const QModelIndex& previous ) -{ - Q_UNUSED( previous ) - if ( !current.isValid() ) - { - return; - } - - updateVariants( QPersistentModelIndex( current ) ); -} - -void -KeyboardPage::onListVariantCurrentItemChanged( QListWidgetItem* current, QListWidgetItem* previous ) -{ - Q_UNUSED( previous ) - - cDebug() << "item" << Logger::Pointer( current ); - - QPersistentModelIndex layoutIndex = ui->layoutSelector->currentIndex(); - LayoutItem* variantItem = dynamic_cast< LayoutItem* >( current ); - - if ( !layoutIndex.isValid() || !variantItem ) - { - return; - } - - QString layout = layoutIndex.data( KeyboardLayoutModel::KeyboardLayoutKeyRole ).toString(); - QString variant = variantItem->data; - - cDebug() << Logger::SubEntry << layout << variant; - - m_keyboardPreview->setLayout( layout ); - m_keyboardPreview->setVariant( variant ); -} diff --git a/src/modules/keyboard/KeyboardPage.h b/src/modules/keyboard/KeyboardPage.h index 231099d0b..582b614e3 100644 --- a/src/modules/keyboard/KeyboardPage.h +++ b/src/modules/keyboard/KeyboardPage.h @@ -38,14 +38,16 @@ public: ~KeyboardPage() override; protected slots: - void onListLayoutCurrentItemChanged( const QModelIndex& current, const QModelIndex& previous ); - void onListVariantCurrentItemChanged( QListWidgetItem* current, QListWidgetItem* previous ); + void layoutChangedByUser( const QModelIndex& current, const QModelIndex& previous ); + void layoutChangedByConfig( int index ); + + void variantChangedByUser( const QModelIndex& current, const QModelIndex& previous ); + void variantChangedByConfig( int index ); private: - void updateVariants( const QPersistentModelIndex& currentItem, QString currentVariant = QString() ); - Ui::Page_Keyboard* ui; KeyBoardPreview* m_keyboardPreview; + Config* m_config; }; #endif // KEYBOARDPAGE_H diff --git a/src/modules/keyboard/KeyboardPage.ui b/src/modules/keyboard/KeyboardPage.ui index e8de90dec..f7592fc6a 100644 --- a/src/modules/keyboard/KeyboardPage.ui +++ b/src/modules/keyboard/KeyboardPage.ui @@ -113,7 +113,7 @@ SPDX-License-Identifier: GPL-3.0-or-later - + From d536173d66a03deade6c578156cc32a81bbe8d72 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 16:20:02 +0100 Subject: [PATCH 23/28] [keyboard] Factor out a 2-column k-v list --- src/modules/keyboard/KeyboardLayoutModel.cpp | 52 +++++++++++--------- src/modules/keyboard/KeyboardLayoutModel.h | 29 ++++++++--- src/modules/keyboard/KeyboardPage.cpp | 2 +- 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/src/modules/keyboard/KeyboardLayoutModel.cpp b/src/modules/keyboard/KeyboardLayoutModel.cpp index ab7dbd37f..4026bfc64 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.cpp +++ b/src/modules/keyboard/KeyboardLayoutModel.cpp @@ -14,35 +14,19 @@ #include -KeyboardModelsModel::KeyboardModelsModel( QObject* parent ) +XKBListModel::XKBListModel( QObject* parent ) : QAbstractListModel( parent ) { - // The models map is from human-readable names (!) to xkb identifier - const auto models = KeyboardGlobal::getKeyboardModels(); - int index = 0; - for ( const auto& key : models.keys() ) - { - // So here *key* is the key in the map, which is the human-readable thing, - // while the struct fields are xkb-id, and human-readable - m_list << ModelInfo { models[ key ], key }; - if ( models[ key ] == "pc105" ) - { - m_defaultPC105 = index; - } - index++; - } - - cDebug() << "Loaded" << m_list.count() << "keyboard models"; } int -KeyboardModelsModel::rowCount( const QModelIndex& ) const +XKBListModel::rowCount( const QModelIndex& ) const { return m_list.count(); } QVariant -KeyboardModelsModel::data( const QModelIndex& index, int role ) const +XKBListModel::data( const QModelIndex& index, int role ) const { if ( !index.isValid() ) { @@ -67,7 +51,7 @@ KeyboardModelsModel::data( const QModelIndex& index, int role ) const } QString -KeyboardModelsModel::modelKey( int index ) const +XKBListModel::modelKey( int index ) const { if ( index < 0 || index >= m_list.count() ) { @@ -77,7 +61,7 @@ KeyboardModelsModel::modelKey( int index ) const } QString -KeyboardModelsModel::modelLabel( int index ) const +XKBListModel::modelLabel( int index ) const { if ( index < 0 || index >= m_list.count() ) { @@ -87,13 +71,13 @@ KeyboardModelsModel::modelLabel( int index ) const } QHash< int, QByteArray > -KeyboardModelsModel::roleNames() const +XKBListModel::roleNames() const { return { { Qt::DisplayRole, "label" }, { Qt::UserRole, "key" } }; } void -KeyboardModelsModel::setCurrentIndex( int index ) +XKBListModel::setCurrentIndex( int index ) { if ( index >= m_list.count() || index < 0 ) { @@ -106,6 +90,28 @@ KeyboardModelsModel::setCurrentIndex( int index ) } } +KeyboardModelsModel::KeyboardModelsModel( QObject* parent ) + : XKBListModel( parent ) +{ + // The models map is from human-readable names (!) to xkb identifier + const auto models = KeyboardGlobal::getKeyboardModels(); + m_list.reserve( models.count() ); + int index = 0; + for ( const auto& key : models.keys() ) + { + // So here *key* is the key in the map, which is the human-readable thing, + // while the struct fields are xkb-id, and human-readable + m_list << ModelInfo { models[ key ], key }; + if ( models[ key ] == "pc105" ) + { + m_defaultPC105 = index; + } + index++; + } + + cDebug() << "Loaded" << m_list.count() << "keyboard models"; +} + KeyboardLayoutModel::KeyboardLayoutModel( QObject* parent ) : QAbstractListModel( parent ) diff --git a/src/modules/keyboard/KeyboardLayoutModel.h b/src/modules/keyboard/KeyboardLayoutModel.h index d0edac2d1..76b921fd4 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.h +++ b/src/modules/keyboard/KeyboardLayoutModel.h @@ -17,11 +17,12 @@ #include #include -/** @brief A list model of the physical keyboard formats ("models" in xkb) +/** @brief A list model with an xkb key and a human-readable string * * This model acts like it has a single selection, as well. */ -class KeyboardModelsModel : public QAbstractListModel + +class XKBListModel : public QAbstractListModel { Q_OBJECT Q_PROPERTY( int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged ) @@ -33,7 +34,7 @@ public: KeyRole = Qt::UserRole ///< xkb identifier }; - explicit KeyboardModelsModel( QObject* parent = nullptr ); + explicit XKBListModel( QObject* parent = nullptr ); int rowCount( const QModelIndex& ) const override; QVariant data( const QModelIndex& index, int role ) const override; @@ -54,14 +55,12 @@ public: QHash< int, QByteArray > roleNames() const override; void setCurrentIndex( int index ); - /// @brief Set the index back to PC105 (the default physical model) - void setCurrentIndex() { setCurrentIndex( m_defaultPC105 ); } int currentIndex() const { return m_currentIndex; } signals: void currentIndexChanged( int index ); -private: +protected: struct ModelInfo { /// XKB identifier @@ -71,6 +70,24 @@ private: }; QVector< ModelInfo > m_list; int m_currentIndex = -1; +}; + + +/** @brief A list model of the physical keyboard formats ("models" in xkb) + * + * This model acts like it has a single selection, as well. + */ +class KeyboardModelsModel : public XKBListModel +{ + Q_OBJECT + +public: + explicit KeyboardModelsModel( QObject* parent = nullptr ); + + /// @brief Set the index back to PC105 (the default physical model) + void setCurrentIndex() { XKBListModel::setCurrentIndex( m_defaultPC105 ); } + +private: int m_defaultPC105 = -1; ///< The index of pc105, if there is one }; diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index 90dacef95..28ed3d25a 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -76,7 +76,7 @@ KeyboardPage::KeyboardPage( Config* config, QWidget* parent ) connect( ui->physicalModelSelector, QOverload< int >::of( &QComboBox::currentIndexChanged ), config->keyboardModels(), - QOverload< int >::of( &KeyboardModelsModel::setCurrentIndex ) ); + QOverload< int >::of( &XKBListModel::setCurrentIndex ) ); connect( config->keyboardModels(), &KeyboardModelsModel::currentIndexChanged, ui->physicalModelSelector, From 14a76a386a1a32030712bfa54a1cdc86ff3e4927 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 16:28:00 +0100 Subject: [PATCH 24/28] [keyboard] Replace broken-ish variants model with k-v list - Use the just-refactored XKBListModel to store the xkb key-value pairs for variants, drop most of the complicated implementation, store just a single list of values. --- src/modules/keyboard/Config.cpp | 13 ++-- src/modules/keyboard/KeyboardLayoutModel.cpp | 64 ++------------------ src/modules/keyboard/KeyboardLayoutModel.h | 28 ++------- 3 files changed, 17 insertions(+), 88 deletions(-) diff --git a/src/modules/keyboard/Config.cpp b/src/modules/keyboard/Config.cpp index 13954eb20..8cd97f08b 100644 --- a/src/modules/keyboard/Config.cpp +++ b/src/modules/keyboard/Config.cpp @@ -157,7 +157,7 @@ Config::Config( QObject* parent ) // Connect signals and slots connect( m_keyboardModelsModel, &KeyboardModelsModel::currentIndexChanged, [&]( int index ) { // Set Xorg keyboard model - m_selectedModel = m_keyboardModelsModel->modelKey( index ); + m_selectedModel = m_keyboardModelsModel->key( index ); cDebug() << "Setting model" << index << m_selectedModel; QProcess::execute( "setxkbmap", xkbmap_model_args( m_selectedModel ) ); emit prettyStatusChanged(); @@ -172,7 +172,7 @@ Config::Config( QObject* parent ) connect( m_keyboardVariantsModel, &KeyboardVariantsModel::currentIndexChanged, [&]( int index ) { // Set Xorg keyboard layout + variant - m_selectedVariant = m_keyboardVariantsModel->item( index )[ "key" ]; + m_selectedVariant = m_keyboardVariantsModel->key( index ); cDebug() << "Setting variant" << index << m_selectedVariant; if ( m_setxkbmapTimer.isActive() ) @@ -327,11 +327,11 @@ Config::prettyStatus() const { QString status; status += tr( "Set keyboard model to %1.
" ) - .arg( m_keyboardModelsModel->modelLabel( m_keyboardModelsModel->currentIndex() ) ); + .arg( m_keyboardModelsModel->label( m_keyboardModelsModel->currentIndex() ) ); QString layout = m_keyboardLayoutsModel->item( m_keyboardLayoutsModel->currentIndex() ).second.description; QString variant = m_keyboardVariantsModel->currentIndex() >= 0 - ? m_keyboardVariantsModel->item( m_keyboardVariantsModel->currentIndex() )[ "label" ] + ? m_keyboardVariantsModel->label( m_keyboardVariantsModel->currentIndex() ) : QString( "" ); status += tr( "Set keyboard layout to %1/%2." ).arg( layout, variant ); @@ -383,13 +383,12 @@ Config::guessLayout( const QStringList& langParts ) cDebug() << "Next level:" << *countryPart; for ( int variantnumber = 0; variantnumber < m_keyboardVariantsModel->rowCount(); ++variantnumber ) { - if ( m_keyboardVariantsModel->item( variantnumber )[ "key" ].compare( *countryPart, - Qt::CaseInsensitive ) + if ( m_keyboardVariantsModel->key( variantnumber ).compare( *countryPart, Qt::CaseInsensitive ) == 0 ) { m_keyboardVariantsModel->setCurrentIndex( variantnumber ); cDebug() << Logger::SubEntry << "matched variant" << *countryPart << ' ' - << m_keyboardVariantsModel->item( variantnumber )[ "key" ]; + << m_keyboardVariantsModel->key( variantnumber ); } } } diff --git a/src/modules/keyboard/KeyboardLayoutModel.cpp b/src/modules/keyboard/KeyboardLayoutModel.cpp index 4026bfc64..5ecc4ccb1 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.cpp +++ b/src/modules/keyboard/KeyboardLayoutModel.cpp @@ -51,7 +51,7 @@ XKBListModel::data( const QModelIndex& index, int role ) const } QString -XKBListModel::modelKey( int index ) const +XKBListModel::key( int index ) const { if ( index < 0 || index >= m_list.count() ) { @@ -61,7 +61,7 @@ XKBListModel::modelKey( int index ) const } QString -XKBListModel::modelLabel( int index ) const +XKBListModel::label( int index ) const { if ( index < 0 || index >= m_list.count() ) { @@ -201,72 +201,20 @@ KeyboardLayoutModel::currentIndex() const } -const QMap< QString, QString > -KeyboardVariantsModel::item( const int& index ) const -{ - if ( index >= m_list.count() || index < 0 ) - { - return QMap< QString, QString >(); - } - - return m_list.at( index ); -} - KeyboardVariantsModel::KeyboardVariantsModel( QObject* parent ) - : QAbstractListModel( parent ) + : XKBListModel( parent ) { } -int -KeyboardVariantsModel::currentIndex() const -{ - return m_currentIndex; -} - -void -KeyboardVariantsModel::setCurrentIndex( const int& index ) -{ - if ( index >= m_list.count() || index < 0 ) - { - return; - } - - m_currentIndex = index; - emit currentIndexChanged( m_currentIndex ); -} - -QVariant -KeyboardVariantsModel::data( const QModelIndex& index, int role ) const -{ - if ( !index.isValid() ) - { - return QVariant(); - } - const auto item = m_list.at( index.row() ); - return role == Qt::DisplayRole ? item[ "label" ] : item[ "key" ]; -} - -int -KeyboardVariantsModel::rowCount( const QModelIndex& ) const -{ - return m_list.count(); -} - -QHash< int, QByteArray > -KeyboardVariantsModel::roleNames() const -{ - return { { Qt::DisplayRole, "label" }, { Qt::UserRole, "key" } }; -} - void KeyboardVariantsModel::setVariants( QMap< QString, QString > variants ) { - m_list.clear(); beginResetModel(); + m_list.clear(); + m_list.reserve( variants.count() ); for ( const auto& key : variants.keys() ) { - const auto item = QMap< QString, QString > { { "label", key }, { "key", variants[ key ] } }; - m_list << item; + m_list << ModelInfo { variants[ key ], key }; } endResetModel(); } diff --git a/src/modules/keyboard/KeyboardLayoutModel.h b/src/modules/keyboard/KeyboardLayoutModel.h index 76b921fd4..9cb2da851 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.h +++ b/src/modules/keyboard/KeyboardLayoutModel.h @@ -36,21 +36,21 @@ public: explicit XKBListModel( QObject* parent = nullptr ); - int rowCount( const QModelIndex& ) const override; + int rowCount( const QModelIndex& = QModelIndex() ) const override; QVariant data( const QModelIndex& index, int role ) const override; /** @brief xkb key for a given index (row) * * This is like calling data( QModelIndex( index ), KeyRole ).toString(), * but shorter and faster. Can return an empty string if index is invalid. */ - QString modelKey( int index ) const; + QString key( int index ) const; /** @brief human-readable label for a given index (row) * * This is like calling data( QModelIndex( index ), LabelRole ).toString(), * but shorter and faster. Can return an empty string if index is invalid. */ - QString modelLabel( int index ) const; + QString label( int index ) const; QHash< int, QByteArray > roleNames() const override; @@ -125,32 +125,14 @@ signals: void currentIndexChanged( int index ); }; -class KeyboardVariantsModel : public QAbstractListModel +class KeyboardVariantsModel : public XKBListModel { Q_OBJECT - Q_PROPERTY( int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged ) public: explicit KeyboardVariantsModel( QObject* parent = nullptr ); + void setVariants( QMap< QString, QString > variants ); - - int rowCount( const QModelIndex& = QModelIndex() ) const override; - QVariant data( const QModelIndex& index, int role ) const override; - - void setCurrentIndex( const int& index ); - int currentIndex() const; - - const QMap< QString, QString > item( const int& index ) const; - -protected: - QHash< int, QByteArray > roleNames() const override; - -private: - int m_currentIndex = -1; - QVector< QMap< QString, QString > > m_list; - -signals: - void currentIndexChanged( int index ); }; #endif // KEYBOARDLAYOUTMODEL_H From 9e141636c54002758ee57071ada2af387097df7c Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 16:40:43 +0100 Subject: [PATCH 25/28] [keyboard] Tidy up the debugging output, add docs --- src/modules/keyboard/KeyboardLayoutModel.h | 11 +++++++++++ src/modules/keyboard/KeyboardPage.cpp | 8 ++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/modules/keyboard/KeyboardLayoutModel.h b/src/modules/keyboard/KeyboardLayoutModel.h index 9cb2da851..3eef595c3 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.h +++ b/src/modules/keyboard/KeyboardLayoutModel.h @@ -91,6 +91,11 @@ private: int m_defaultPC105 = -1; ///< The index of pc105, if there is one }; +/** @brief A list of keyboard layouts (arrangements of keycaps) + * + * Layouts can have a list of associated Variants, so this + * is slightly more complicated than the "regular" XKBListModel. + */ class KeyboardLayoutModel : public QAbstractListModel { Q_OBJECT @@ -125,6 +130,12 @@ signals: void currentIndexChanged( int index ); }; +/** @brief A list of variants (xkb id and human-readable) + * + * The variants that are available depend on the Layout that is used, + * so the `setVariants()` function can be used to update the variants + * when the two models are related. + */ class KeyboardVariantsModel : public XKBListModel { Q_OBJECT diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index 28ed3d25a..cb8aada5b 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -106,29 +106,25 @@ KeyboardPage::KeyboardPage( Config* config, QWidget* parent ) void KeyboardPage::layoutChangedByUser( const QModelIndex& current, const QModelIndex& previous ) { - cDebug() << "index ->" << current.row(); m_config->keyboardLayouts()->setCurrentIndex( current.row() ); - cDebug() << Logger::SubEntry << "variants now" << m_config->keyboardVariants()->rowCount(); } void KeyboardPage::layoutChangedByConfig( int index ) { - cDebug() << "index ->" << index; ui->layoutSelector->setCurrentIndex( m_config->keyboardLayouts()->index( index ) ); - cDebug() << Logger::SubEntry << "variants now" << m_config->keyboardVariants()->rowCount(); } void KeyboardPage::variantChangedByUser( const QModelIndex& current, const QModelIndex& previous ) { - cDebug() << "index ->" << current.row(); + m_config->keyboardVariants()->setCurrentIndex( current.row() ); } void KeyboardPage::variantChangedByConfig( int index ) { - cDebug() << "index ->" << index; + ui->variantSelector->setCurrentIndex( m_config->keyboardVariants()->index( index ) ); } KeyboardPage::~KeyboardPage() From 0bf28b0b94aa030e5ccf421cdbc709b30abf4216 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 16:47:52 +0100 Subject: [PATCH 26/28] [keyboard] Sanity in setCurrentIndex() parameters --- src/modules/keyboard/KeyboardLayoutModel.cpp | 9 ++++++--- src/modules/keyboard/KeyboardLayoutModel.h | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/modules/keyboard/KeyboardLayoutModel.cpp b/src/modules/keyboard/KeyboardLayoutModel.cpp index 5ecc4ccb1..b8cb892f4 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.cpp +++ b/src/modules/keyboard/KeyboardLayoutModel.cpp @@ -183,15 +183,18 @@ KeyboardLayoutModel::roleNames() const } void -KeyboardLayoutModel::setCurrentIndex( const int& index ) +KeyboardLayoutModel::setCurrentIndex( int index ) { if ( index >= m_layouts.count() || index < 0 ) { return; } - m_currentIndex = index; - emit currentIndexChanged( m_currentIndex ); + if ( m_currentIndex != index ) + { + m_currentIndex = index; + emit currentIndexChanged( m_currentIndex ); + } } int diff --git a/src/modules/keyboard/KeyboardLayoutModel.h b/src/modules/keyboard/KeyboardLayoutModel.h index 3eef595c3..60747da55 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.h +++ b/src/modules/keyboard/KeyboardLayoutModel.h @@ -114,7 +114,7 @@ public: QVariant data( const QModelIndex& index, int role ) const override; - void setCurrentIndex( const int& index ); + void setCurrentIndex( int index ); int currentIndex() const; const QPair< QString, KeyboardGlobal::KeyboardInfo > item( const int& index ) const; From 2e5301c5c91e211c73069d72d516dd6218ca63d8 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 16:51:34 +0100 Subject: [PATCH 27/28] [keyboard] Simplify back down to lambdas - With debugging and untangling done, the lambdas are simple and short, so return to using them. One point of improvement might be to give the models suitable slots themselves, to avoid even this indirection. --- src/modules/keyboard/KeyboardPage.cpp | 45 +++++---------------------- src/modules/keyboard/KeyboardPage.h | 7 ----- 2 files changed, 8 insertions(+), 44 deletions(-) diff --git a/src/modules/keyboard/KeyboardPage.cpp b/src/modules/keyboard/KeyboardPage.cpp index cb8aada5b..2be897e58 100644 --- a/src/modules/keyboard/KeyboardPage.cpp +++ b/src/modules/keyboard/KeyboardPage.cpp @@ -84,49 +84,20 @@ KeyboardPage::KeyboardPage( Config* config, QWidget* parent ) connect( ui->layoutSelector->selectionModel(), &QItemSelectionModel::currentChanged, - this, - &KeyboardPage::layoutChangedByUser ); - connect( config->keyboardLayouts(), - &KeyboardLayoutModel::currentIndexChanged, - this, - &KeyboardPage::layoutChangedByConfig ); + [this]( const QModelIndex& current ) { m_config->keyboardLayouts()->setCurrentIndex( current.row() ); } ); + connect( config->keyboardLayouts(), &KeyboardLayoutModel::currentIndexChanged, [this]( int index ) { + ui->layoutSelector->setCurrentIndex( m_config->keyboardLayouts()->index( index ) ); + } ); connect( ui->variantSelector->selectionModel(), &QItemSelectionModel::currentChanged, - this, - &KeyboardPage::variantChangedByUser ); - connect( config->keyboardVariants(), - &KeyboardVariantsModel::currentIndexChanged, - this, - &KeyboardPage::variantChangedByConfig ); - + [this]( const QModelIndex& current ) { m_config->keyboardVariants()->setCurrentIndex( current.row() ); } ); + connect( config->keyboardVariants(), &KeyboardVariantsModel::currentIndexChanged, [this]( int index ) { + ui->variantSelector->setCurrentIndex( m_config->keyboardVariants()->index( index ) ); + } ); CALAMARES_RETRANSLATE( ui->retranslateUi( this ); ) } -void -KeyboardPage::layoutChangedByUser( const QModelIndex& current, const QModelIndex& previous ) -{ - m_config->keyboardLayouts()->setCurrentIndex( current.row() ); -} - -void -KeyboardPage::layoutChangedByConfig( int index ) -{ - ui->layoutSelector->setCurrentIndex( m_config->keyboardLayouts()->index( index ) ); -} - -void -KeyboardPage::variantChangedByUser( const QModelIndex& current, const QModelIndex& previous ) -{ - m_config->keyboardVariants()->setCurrentIndex( current.row() ); -} - -void -KeyboardPage::variantChangedByConfig( int index ) -{ - ui->variantSelector->setCurrentIndex( m_config->keyboardVariants()->index( index ) ); -} - KeyboardPage::~KeyboardPage() { delete ui; diff --git a/src/modules/keyboard/KeyboardPage.h b/src/modules/keyboard/KeyboardPage.h index 582b614e3..98925fcad 100644 --- a/src/modules/keyboard/KeyboardPage.h +++ b/src/modules/keyboard/KeyboardPage.h @@ -37,13 +37,6 @@ public: explicit KeyboardPage( Config* config, QWidget* parent = nullptr ); ~KeyboardPage() override; -protected slots: - void layoutChangedByUser( const QModelIndex& current, const QModelIndex& previous ); - void layoutChangedByConfig( int index ); - - void variantChangedByUser( const QModelIndex& current, const QModelIndex& previous ); - void variantChangedByConfig( int index ); - private: Ui::Page_Keyboard* ui; KeyBoardPreview* m_keyboardPreview; From cd9c0ea78172e7940f9264bd056123ce59f62492 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 17:00:12 +0100 Subject: [PATCH 28/28] [keyboard] Reduce debugging output --- src/modules/keyboard/Config.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/modules/keyboard/Config.cpp b/src/modules/keyboard/Config.cpp index 8cd97f08b..6475d9bc8 100644 --- a/src/modules/keyboard/Config.cpp +++ b/src/modules/keyboard/Config.cpp @@ -158,14 +158,12 @@ Config::Config( QObject* parent ) connect( m_keyboardModelsModel, &KeyboardModelsModel::currentIndexChanged, [&]( int index ) { // Set Xorg keyboard model m_selectedModel = m_keyboardModelsModel->key( index ); - cDebug() << "Setting model" << index << m_selectedModel; QProcess::execute( "setxkbmap", xkbmap_model_args( m_selectedModel ) ); emit prettyStatusChanged(); } ); connect( m_keyboardLayoutsModel, &KeyboardLayoutModel::currentIndexChanged, [&]( int index ) { m_selectedLayout = m_keyboardLayoutsModel->item( index ).first; - cDebug() << "Setting layout" << index << m_selectedLayout; updateVariants( QPersistentModelIndex( m_keyboardLayoutsModel->index( index ) ) ); emit prettyStatusChanged(); } ); @@ -173,7 +171,6 @@ Config::Config( QObject* parent ) connect( m_keyboardVariantsModel, &KeyboardVariantsModel::currentIndexChanged, [&]( int index ) { // Set Xorg keyboard layout + variant m_selectedVariant = m_keyboardVariantsModel->key( index ); - cDebug() << "Setting variant" << index << m_selectedVariant; if ( m_setxkbmapTimer.isActive() ) {