From 5afe54132bf6bcaaef73f960d25813770f06bf18 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 28 Oct 2020 15:34:47 +0100 Subject: [PATCH] [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 | 136 ++++++++++++-------------- src/modules/keyboard/KeyboardPage.h | 10 +- src/modules/keyboard/KeyboardPage.ui | 2 +- 3 files changed, 69 insertions(+), 79 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 ); ) } - -KeyboardPage::~KeyboardPage() +void +KeyboardPage::layoutChangedByUser( const QModelIndex& current, const QModelIndex& previous ) { - delete ui; + cDebug() << "index ->" << current.row(); + m_config->keyboardLayouts()->setCurrentIndex( current.row() ); + cDebug() << Logger::SubEntry << "variants now" << m_config->keyboardVariants()->rowCount(); } void -KeyboardPage::updateVariants( const QPersistentModelIndex& currentItem, QString currentVariant ) +KeyboardPage::layoutChangedByConfig( int index ) { - // 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 ); - } + cDebug() << "index ->" << index; + ui->layoutSelector->setCurrentIndex( m_config->keyboardLayouts()->index( index ) ); + cDebug() << Logger::SubEntry << "variants now" << m_config->keyboardVariants()->rowCount(); } - void -KeyboardPage::onListLayoutCurrentItemChanged( const QModelIndex& current, const QModelIndex& previous ) +KeyboardPage::variantChangedByUser( const QModelIndex& current, const QModelIndex& previous ) { - Q_UNUSED( previous ) - if ( !current.isValid() ) - { - return; - } - - updateVariants( QPersistentModelIndex( current ) ); + cDebug() << "index ->" << current.row(); } void -KeyboardPage::onListVariantCurrentItemChanged( QListWidgetItem* current, QListWidgetItem* previous ) +KeyboardPage::variantChangedByConfig( int index ) { - 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; + cDebug() << "index ->" << index; +} - m_keyboardPreview->setLayout( layout ); - m_keyboardPreview->setVariant( variant ); +KeyboardPage::~KeyboardPage() +{ + delete ui; } 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 ); -private: - void updateVariants( const QPersistentModelIndex& currentItem, QString currentVariant = QString() ); + void variantChangedByUser( const QModelIndex& current, const QModelIndex& previous ); + void variantChangedByConfig( int index ); +private: 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 - +