calamares/src/modules/keyboard/KeyboardPage.cpp

150 lines
3.9 KiB
C++
Raw Normal View History

/* === This file is part of Calamares - <https://calamares.io> ===
*
2020-08-22 01:19:58 +02:00
* SPDX-FileCopyrightText: 2007 Free Software Foundation, Inc.
* SPDX-FileCopyrightText: 2014-2016 Teo Mrnjavac <teo@kde.org>
* SPDX-FileCopyrightText: 2017-2018 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Portions from the Manjaro Installation Framework
* by Roland Singer <roland@manjaro.org>
* Copyright (C) 2007 Free Software Foundation, Inc.
*
* Calamares is Free Software: see the License-Identifier above.
*
*/
#include "KeyboardPage.h"
#include "Config.h"
#include "KeyboardLayoutModel.h"
2020-03-26 15:57:02 +01:00
#include "SetKeyboardLayoutJob.h"
#include "keyboardwidget/keyboardpreview.h"
#include "ui_KeyboardPage.h"
#include "GlobalStorage.h"
#include "JobQueue.h"
2014-08-06 15:37:21 +02:00
#include "utils/Logger.h"
2014-11-11 15:45:51 +01:00
#include "utils/Retranslator.h"
#include "utils/String.h"
#include <QComboBox>
#include <QProcess>
#include <QPushButton>
class LayoutItem : public QListWidgetItem
{
public:
QString data;
~LayoutItem() override;
};
2020-03-26 15:57:02 +01:00
LayoutItem::~LayoutItem() {}
KeyboardPage::KeyboardPage( Config* config, QWidget* parent )
: QWidget( parent )
, ui( new Ui::Page_Keyboard )
, m_keyboardPreview( new KeyBoardPreview( this ) )
{
ui->setupUi( this );
// Keyboard Preview
ui->KBPreviewLayout->addWidget( m_keyboardPreview );
ui->physicalModelSelector->setModel( config->keyboardModels() );
ui->layoutSelector->setModel( config->keyboardLayouts() );
2020-10-28 13:41:34 +01:00
connect( ui->buttonRestore, &QPushButton::clicked, [config=config] {
config->keyboardModels()->setCurrentIndex();
2020-10-28 13:41:34 +01:00
} );
connect( ui->physicalModelSelector,
QOverload<int>::of( &QComboBox::currentIndexChanged ),
config->keyboardModels(),
QOverload<int>::of( &KeyboardModelsModel::setCurrentIndex ) );
2014-11-11 15:45:51 +01:00
CALAMARES_RETRANSLATE( ui->retranslateUi( this ); )
}
KeyboardPage::~KeyboardPage()
{
delete ui;
}
void
2020-03-26 15:57:02 +01:00
KeyboardPage::updateVariants( const QPersistentModelIndex& currentItem, QString currentVariant )
{
// Block signals
ui->variantSelector->blockSignals( true );
2020-03-26 15:57:02 +01:00
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() )
{
2017-09-26 11:22:51 +02:00
li.next();
2017-09-26 11:22:51 +02:00
LayoutItem* item = new LayoutItem();
item->setText( li.key() );
item->data = li.value();
ui->variantSelector->addItem( item );
2017-09-26 11:22:51 +02:00
// currentVariant defaults to QString(). It is only non-empty during the
// initial setup.
if ( li.value() == currentVariant )
2020-03-26 15:57:02 +01:00
{
2017-09-26 11:22:51 +02:00
defaultItem = item;
2020-03-26 15:57:02 +01:00
}
}
// Unblock signals
ui->variantSelector->blockSignals( false );
// Set to default value
if ( defaultItem )
2020-03-26 15:57:02 +01:00
{
ui->variantSelector->setCurrentItem( defaultItem );
2020-03-26 15:57:02 +01:00
}
}
void
2020-03-26 15:57:02 +01:00
KeyboardPage::onListLayoutCurrentItemChanged( const QModelIndex& current, const QModelIndex& previous )
{
Q_UNUSED( previous )
if ( !current.isValid() )
2020-03-26 15:57:02 +01:00
{
return;
2020-03-26 15:57:02 +01:00
}
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 )
2020-03-26 15:57:02 +01:00
{
return;
2020-03-26 15:57:02 +01:00
}
QString layout = layoutIndex.data( KeyboardLayoutModel::KeyboardLayoutKeyRole ).toString();
QString variant = variantItem->data;
cDebug() << Logger::SubEntry << layout << variant;
m_keyboardPreview->setLayout( layout );
m_keyboardPreview->setVariant( variant );
}