calamares/src/modules/keyboard/KeyboardPage.cpp

183 lines
4.8 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 "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( QWidget* parent )
: QWidget( parent )
, ui( new Ui::Page_Keyboard )
, m_keyboardPreview( new KeyBoardPreview( this ) )
2015-06-13 21:33:00 +02:00
, m_defaultIndex( 0 )
{
ui->setupUi( this );
// Keyboard Preview
ui->KBPreviewLayout->addWidget( m_keyboardPreview );
m_setxkbmapTimer.setSingleShot( true );
// Connect signals and slots
2020-03-26 15:57:02 +01:00
connect( ui->listVariant, &QListWidget::currentItemChanged, this, &KeyboardPage::onListVariantCurrentItemChanged );
2020-03-26 15:57:02 +01:00
connect(
ui->buttonRestore, &QPushButton::clicked, [this] { ui->comboBoxModel->setCurrentIndex( m_defaultIndex ); } );
2020-10-12 14:27:01 +02:00
connect( ui->comboBoxModel, &QComboBox::currentTextChanged, [this]( const QString& text ) {
QString model = m_models.value( text, "pc105" );
// Set Xorg keyboard model
QProcess::execute( "setxkbmap", QStringList { "-model", model } );
} );
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->listVariant->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->listVariant->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->listVariant->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->listVariant->blockSignals( false );
// Set to default value
if ( defaultItem )
2020-03-26 15:57:02 +01:00
{
2017-09-26 11:22:51 +02:00
ui->listVariant->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 ) );
}
2017-06-19 16:46:30 +02:00
/* Returns stringlist with suitable setxkbmap command-line arguments
* to set the given @p layout and @p variant.
*/
2020-03-26 15:57:02 +01:00
static inline QStringList
xkbmap_args( const QString& layout, const QString& variant )
{
2020-03-26 15:57:02 +01:00
QStringList r { "-layout", layout };
if ( !variant.isEmpty() )
2020-03-26 15:57:02 +01:00
{
r << "-variant" << variant;
2020-03-26 15:57:02 +01:00
}
return r;
}
void
KeyboardPage::onListVariantCurrentItemChanged( QListWidgetItem* current, QListWidgetItem* previous )
{
Q_UNUSED( previous )
QPersistentModelIndex layoutIndex = ui->listLayout->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;
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 );
}
2020-03-26 15:57:02 +01:00
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;
}