Merge branch 'fix-keyboardq' into calamares

This lands Artem's work on supporting non-ASCII layouts better,
which currently only applies to Russian layouts -- it works with
a table, so it is easy to extend for other layouts.

The QML and Widget steps now completely share the Config backend,
which is why there's so many commits here: the Widget page needed
to have most of its code ripped out, and the models for
keyboard data were broken in various ways and needed fixing.

FIXES #1211
This commit is contained in:
Adriaan de Groot 2020-10-28 17:03:57 +01:00
commit 8fd66933ee
15 changed files with 341 additions and 788 deletions

View File

@ -12,7 +12,8 @@
#include <QString>
struct AdditionalLayoutInfo {
struct AdditionalLayoutInfo
{
QString additionalLayout;
QString additionalVariant;

View File

@ -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,8 +156,8 @@ Config::Config( QObject* parent )
// Connect signals and slots
connect( m_keyboardModelsModel, &KeyboardModelsModel::currentIndexChanged, [&]( int index ) {
m_selectedModel = m_keyboardModelsModel->item( index ).value( "key", "pc105" );
// Set Xorg keyboard model
// Set Xorg keyboard model
m_selectedModel = m_keyboardModelsModel->key( index );
QProcess::execute( "setxkbmap", xkbmap_model_args( m_selectedModel ) );
emit prettyStatusChanged();
} );
@ -166,9 +169,9 @@ Config::Config( QObject* parent )
} );
connect( m_keyboardVariantsModel, &KeyboardVariantsModel::currentIndexChanged, [&]( int index ) {
m_selectedVariant = m_keyboardVariantsModel->item( index )[ "key" ];
// Set Xorg keyboard layout + variant
m_selectedVariant = m_keyboardVariantsModel->key( index );
// Set Xorg keyboard layout
if ( m_setxkbmapTimer.isActive() )
{
m_setxkbmapTimer.stop();
@ -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
{
@ -248,7 +248,7 @@ findLayout( const KeyboardLayoutModel* klm, const QString& currentLayout )
}
void
Config::init()
Config::detectCurrentKeyboardLayout()
{
//### Detect current keyboard layout and variant
QString currentLayout;
@ -269,15 +269,15 @@ Config::init()
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,11 +324,11 @@ Config::prettyStatus() const
{
QString status;
status += tr( "Set keyboard model to %1.<br/>" )
.arg( m_keyboardModelsModel->item( m_keyboardModelsModel->currentIndex() )[ "label" ] );
.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( "<default>" );
status += tr( "Set keyboard layout to %1/%2." ).arg( layout, variant );
@ -380,13 +380,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"
<< m_keyboardVariantsModel->item( variantnumber )[ "key" ] << ' '
<< m_keyboardVariantsModel->item( variantnumber )[ "key" ];
cDebug() << Logger::SubEntry << "matched variant" << *countryPart << ' '
<< m_keyboardVariantsModel->key( variantnumber );
}
}
}
@ -497,8 +496,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 );
}
}

View File

@ -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 <QAbstractListModel>
#include <QMap>
@ -32,7 +32,7 @@ class Config : public QObject
public:
Config( QObject* parent = nullptr );
void init();
void detectCurrentKeyboardLayout();
Calamares::JobList createJobs();
QString prettyStatus() const;
@ -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();
};

View File

@ -10,8 +10,108 @@
#include "KeyboardLayoutModel.h"
#include "utils/Logger.h"
#include <algorithm>
XKBListModel::XKBListModel( QObject* parent )
: QAbstractListModel( parent )
{
}
int
XKBListModel::rowCount( const QModelIndex& ) const
{
return m_list.count();
}
QVariant
XKBListModel::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
XKBListModel::key( int index ) const
{
if ( index < 0 || index >= m_list.count() )
{
return QString();
}
return m_list[ index ].key;
}
QString
XKBListModel::label( int index ) const
{
if ( index < 0 || index >= m_list.count() )
{
return QString();
}
return m_list[ index ].label;
}
QHash< int, QByteArray >
XKBListModel::roleNames() const
{
return { { Qt::DisplayRole, "label" }, { Qt::UserRole, "key" } };
}
void
XKBListModel::setCurrentIndex( int index )
{
if ( index >= m_list.count() || index < 0 )
{
return;
}
if ( m_currentIndex != index )
{
m_currentIndex = index;
emit currentIndexChanged( m_currentIndex );
}
}
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 )
@ -83,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
@ -100,157 +203,21 @@ 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
{
if ( index >= m_list.count() || index < 0 )
{
return QMap< QString, QString >();
}
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 )
: 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();
}

View File

@ -17,6 +17,85 @@
#include <QMetaType>
#include <QObject>
/** @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 XKBListModel : 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 XKBListModel( QObject* parent = nullptr );
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 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 label( int index ) const;
QHash< int, QByteArray > roleNames() const override;
void setCurrentIndex( int index );
int currentIndex() const { return m_currentIndex; }
signals:
void currentIndexChanged( int index );
protected:
struct ModelInfo
{
/// XKB identifier
QString key;
/// Human-readable
QString label;
};
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
};
/** @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
@ -35,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;
@ -51,61 +130,20 @@ signals:
void currentIndexChanged( int index );
};
class KeyboardModelsModel : public QAbstractListModel
/** @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
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
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

View File

@ -15,6 +15,7 @@
#include "KeyboardPage.h"
#include "Config.h"
#include "KeyboardLayoutModel.h"
#include "SetKeyboardLayoutJob.h"
#include "keyboardwidget/keyboardpreview.h"
@ -40,452 +41,64 @@ 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 )
KeyboardPage::KeyboardPage( Config* config, QWidget* parent )
: QWidget( parent )
, ui( new Ui::Page_Keyboard )
, m_keyboardPreview( new KeyBoardPreview( this ) )
, m_defaultIndex( 0 )
, m_config( config )
{
ui->setupUi( this );
// Keyboard Preview
ui->KBPreviewLayout->addWidget( m_keyboardPreview );
m_setxkbmapTimer.setSingleShot( true );
// Connect signals and slots
connect( ui->listVariant, &QListWidget::currentItemChanged, this, &KeyboardPage::onListVariantCurrentItemChanged );
{
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, [this] { ui->comboBoxModel->setCurrentIndex( m_defaultIndex ); } );
ui->buttonRestore, &QPushButton::clicked, [config = config] { config->keyboardModels()->setCurrentIndex(); } );
connect( ui->comboBoxModel, &QComboBox::currentTextChanged, [this]( const QString& text ) {
QString model = m_models.value( text, "pc105" );
connect( ui->physicalModelSelector,
QOverload< int >::of( &QComboBox::currentIndexChanged ),
config->keyboardModels(),
QOverload< int >::of( &XKBListModel::setCurrentIndex ) );
connect( config->keyboardModels(),
&KeyboardModelsModel::currentIndexChanged,
ui->physicalModelSelector,
&QComboBox::setCurrentIndex );
// Set Xorg keyboard model
QProcess::execute( "setxkbmap", QStringList { "-model", model } );
connect( ui->layoutSelector->selectionModel(),
&QItemSelectionModel::currentChanged,
[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]( 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 ); )
}
KeyboardPage::~KeyboardPage()
{
delete ui;
}
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
KeyboardPage::prettyStatus() const
{
QString status;
status += tr( "Set keyboard model to %1.<br/>" ).arg( ui->comboBoxModel->currentText() );
QString layout = ui->listLayout->currentIndex().data().toString();
QString variant = ui->listVariant->currentItem() ? ui->listVariant->currentItem()->text() : QString( "<default>" );
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,
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,
AdditionalLayoutInfo(),
xOrgConfFileName,
convertedKeymapPath,
writeEtcDefaultKeyboard );
list.append( Calamares::job_ptr( j ) );
return list;
}
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 <lang>_<country>
* format to keyboard mappings, which are <country>_<layout>
* 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 <layout>_<country>.
*/
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::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 )
{
// Block signals
ui->listVariant->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();
while ( li.hasNext() )
{
li.next();
LayoutItem* item = new LayoutItem();
item->setText( li.key() );
item->data = li.value();
ui->listVariant->addItem( item );
// currentVariant defaults to QString(). It is only non-empty during the
// initial setup.
if ( li.value() == currentVariant )
{
defaultItem = item;
}
}
// Unblock signals
ui->listVariant->blockSignals( false );
// Set to default value
if ( defaultItem )
{
ui->listVariant->setCurrentItem( defaultItem );
}
}
void
KeyboardPage::onListLayoutCurrentItemChanged( const QModelIndex& current, const QModelIndex& previous )
{
Q_UNUSED( previous )
if ( !current.isValid() )
{
return;
}
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 )
QPersistentModelIndex layoutIndex = ui->listLayout->currentIndex();
LayoutItem* variantItem = dynamic_cast< LayoutItem* >( current );
if ( !layoutIndex.isValid() || !variantItem )
{
return;
}
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 );
}
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;
}

View File

@ -27,42 +27,20 @@ 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;
void init();
QString prettyStatus() const;
Calamares::JobList
createJobs( const QString& xOrgConfFileName, const QString& convertedKeymapPath, bool writeEtcDefaultKeyboard );
void onActivate();
void finalize();
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;
KeyBoardPreview* m_keyboardPreview;
int m_defaultIndex;
QMap< QString, QString > m_models;
QString m_selectedLayout;
QString m_selectedVariant;
QTimer m_setxkbmapTimer;
Config* m_config;
};
#endif // KEYBOARDPAGE_H

View File

@ -76,7 +76,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBoxModel">
<widget class="QComboBox" name="physicalModelSelector">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
@ -110,10 +110,10 @@ SPDX-License-Identifier: GPL-3.0-or-later
<number>9</number>
</property>
<item>
<widget class="QListView" name="listLayout"/>
<widget class="QListView" name="layoutSelector"/>
</item>
<item>
<widget class="QListWidget" name="listVariant"/>
<widget class="QListView" name="variantSelector"/>
</item>
</layout>
</item>
@ -139,9 +139,9 @@ SPDX-License-Identifier: GPL-3.0-or-later
</layout>
</widget>
<tabstops>
<tabstop>comboBoxModel</tabstop>
<tabstop>listLayout</tabstop>
<tabstop>listVariant</tabstop>
<tabstop>physicalModelSelector</tabstop>
<tabstop>layoutSelector</tabstop>
<tabstop>variantSelector</tabstop>
<tabstop>LE_TestKeyboard</tabstop>
<tabstop>buttonRestore</tabstop>
</tabstops>

View File

@ -19,13 +19,11 @@ CALAMARES_PLUGIN_FACTORY_DEFINITION( KeyboardViewStepFactory, registerPlugin< Ke
KeyboardViewStep::KeyboardViewStep( QObject* parent )
: Calamares::ViewStep( parent )
, m_config( new Config(this) )
, m_widget( new KeyboardPage() )
, m_nextEnabled( false )
, m_config( new Config( this ) )
, m_widget( new KeyboardPage( m_config ) )
{
m_widget->init();
m_nextEnabled = true;
emit nextStatusChanged( m_nextEnabled );
m_config->detectCurrentKeyboardLayout();
emit nextStatusChanged( true );
}
@ -48,7 +46,7 @@ KeyboardViewStep::prettyName() const
QString
KeyboardViewStep::prettyStatus() const
{
return m_prettyStatus;
return m_config->prettyStatus();
}
@ -62,7 +60,7 @@ KeyboardViewStep::widget()
bool
KeyboardViewStep::isNextEnabled() const
{
return m_nextEnabled;
return true;
}
@ -97,20 +95,19 @@ KeyboardViewStep::jobs() const
void
KeyboardViewStep::onActivate()
{
m_widget->onActivate();
m_config->onActivate();
}
void
KeyboardViewStep::onLeave()
{
m_widget->finalize();
m_prettyStatus = m_widget->prettyStatus();
m_config->finalize();
}
void
KeyboardViewStep::setConfigurationMap( const QVariantMap& configurationMap )
{
m_config->setConfigurationMap(configurationMap);
m_config->setConfigurationMap( configurationMap );
}

View File

@ -49,8 +49,6 @@ public:
private:
Config* m_config;
KeyboardPage* m_widget;
bool m_nextEnabled;
QString m_prettyStatus;
};
CALAMARES_PLUGIN_FACTORY_DECLARATION( KeyboardViewStepFactory )

View File

@ -30,7 +30,7 @@
#include <QTextStream>
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";

View File

@ -11,9 +11,8 @@
#ifndef SETKEYBOARDLAYOUTJOB_H
#define SETKEYBOARDLAYOUTJOB_H
#include "Job.h"
#include "AdditionalLayoutInfo.h"
#include "Job.h"
class SetKeyboardLayoutJob : public Calamares::Job

View File

@ -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$" );

View File

@ -2,6 +2,7 @@
*
* SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac <teo@kde.org>
* SPDX-FileCopyrightText: 2020 Camilo Higuita <milo.h@aol.com>
* SPDX-FileCopyrightText: 2020 Anke Boersma <demm@kaosx.us>
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Calamares is Free Software: see the License-Identifier above.
@ -10,21 +11,19 @@
#include "KeyboardQmlViewStep.h"
#include "Config.h"
#include "GlobalStorage.h"
#include "JobQueue.h"
#include "utils/Variant.h"
CALAMARES_PLUGIN_FACTORY_DEFINITION( KeyboardQmlViewStepFactory, registerPlugin< KeyboardQmlViewStep >(); )
KeyboardQmlViewStep::KeyboardQmlViewStep( QObject* parent )
: Calamares::QmlViewStep( parent )
, m_config( new Config( this ) )
, m_nextEnabled( false )
, m_writeEtcDefaultKeyboard( true )
{
m_config->init();
m_nextEnabled = true;
emit nextStatusChanged( m_nextEnabled );
m_config->detectCurrentKeyboardLayout();
emit nextStatusChanged( true );
}
QString
@ -36,13 +35,13 @@ KeyboardQmlViewStep::prettyName() const
QString
KeyboardQmlViewStep::prettyStatus() const
{
return m_prettyStatus;
return m_config->prettyStatus();
}
bool
KeyboardQmlViewStep::isNextEnabled() const
{
return m_nextEnabled;
return true;
}
bool
@ -66,7 +65,7 @@ KeyboardQmlViewStep::isAtEnd() const
Calamares::JobList
KeyboardQmlViewStep::jobs() const
{
return m_jobs;
return m_config->createJobs();
}
void
@ -79,8 +78,6 @@ void
KeyboardQmlViewStep::onLeave()
{
m_config->finalize();
// m_jobs = m_config->createJobs( m_xOrgConfFileName, m_convertedKeymapPath, m_writeEtcDefaultKeyboard );
m_prettyStatus = m_config->prettyStatus();
}
QObject*
@ -92,39 +89,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;
}
m_config->setConfigurationMap( configurationMap );
Calamares::QmlViewStep::setConfigurationMap( configurationMap );
}

View File

@ -13,14 +13,12 @@
#include "Config.h"
#include <DllMacro.h>
#include <utils/PluginFactory.h>
#include <viewpages/QmlViewStep.h>
#include "DllMacro.h"
#include "utils/PluginFactory.h"
#include "viewpages/QmlViewStep.h"
#include <QObject>
class KeyboardPage;
class PLUGINDLLEXPORT KeyboardQmlViewStep : public Calamares::QmlViewStep
{
Q_OBJECT
@ -47,14 +45,6 @@ public:
private:
Config* m_config;
bool m_nextEnabled;
QString m_prettyStatus;
QString m_xOrgConfFileName;
QString m_convertedKeymapPath;
bool m_writeEtcDefaultKeyboard;
Calamares::JobList m_jobs;
};
CALAMARES_PLUGIN_FACTORY_DECLARATION( KeyboardQmlViewStepFactory )