calamares/src/modules/keyboard/KeyboardLayoutModel.cpp

275 lines
6.2 KiB
C++
Raw Normal View History

/* === This file is part of Calamares - <https://calamares.io> ===
2016-05-31 19:05:25 +02:00
*
2020-08-22 01:19:58 +02:00
* SPDX-FileCopyrightText: 2016 Teo Mrnjavac <teo@kde.org>
* SPDX-FileCopyrightText: 2017 Adriaan de Groot <groot@kde.org>
* SPDX-License-Identifier: GPL-3.0-or-later
2016-05-31 19:05:25 +02:00
*
* Calamares is Free Software: see the License-Identifier above.
2016-05-31 19:05:25 +02:00
*
*/
#include "KeyboardLayoutModel.h"
#include "utils/Logger.h"
#include "utils/RAII.h"
#include "utils/Retranslator.h"
#include <QTranslator>
2016-05-31 19:05:25 +02:00
#include <algorithm>
static QTranslator* s_kbtranslator = nullptr;
void
retranslateKeyboardModels()
{
if ( !s_kbtranslator )
{
s_kbtranslator = new QTranslator;
}
2020-11-01 14:28:12 +01:00
(void)CalamaresUtils::loadTranslator( QLocale(), QStringLiteral( "kb_" ), s_kbtranslator );
}
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:
if ( s_kbtranslator && !s_kbtranslator->isEmpty() && m_contextname )
{
auto s = s_kbtranslator->translate( m_contextname, item.label.toUtf8().data() );
if ( !s.isEmpty() )
{
return s;
}
}
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 )
{
m_contextname = "kb_models";
// 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";
2020-11-06 01:10:27 +01:00
setCurrentIndex(); // If pc105 was seen, select it now
}
2016-05-31 19:05:25 +02:00
KeyboardLayoutModel::KeyboardLayoutModel( QObject* parent )
2020-03-26 15:57:02 +01:00
: QAbstractListModel( parent )
2016-05-31 19:05:25 +02:00
{
init();
}
int
KeyboardLayoutModel::rowCount( const QModelIndex& parent ) const
{
Q_UNUSED( parent )
2016-05-31 19:05:25 +02:00
return m_layouts.count();
}
QVariant
KeyboardLayoutModel::data( const QModelIndex& index, int role ) const
{
if ( !index.isValid() )
2020-03-26 15:57:02 +01:00
{
2016-05-31 19:05:25 +02:00
return QVariant();
2020-03-26 15:57:02 +01:00
}
2016-05-31 19:05:25 +02:00
switch ( role )
{
2020-03-26 15:57:02 +01:00
case Qt::DisplayRole:
2020-11-01 14:28:12 +01:00
{
auto description = m_layouts.at( index.row() ).second.description;
if ( s_kbtranslator && !s_kbtranslator->isEmpty() )
{
auto s = s_kbtranslator->translate( "kb_layouts", description.toUtf8().data() );
if ( !s.isEmpty() )
{
return s;
}
}
return description;
}
2020-03-26 15:57:02 +01:00
case KeyboardVariantsRole:
return QVariant::fromValue( m_layouts.at( index.row() ).second.variants );
case KeyboardLayoutKeyRole:
return m_layouts.at( index.row() ).first;
2016-05-31 19:05:25 +02:00
}
return QVariant();
}
const QPair< QString, KeyboardGlobal::KeyboardInfo >
2020-03-26 15:57:02 +01:00
KeyboardLayoutModel::item( const int& index ) const
{
2020-03-26 15:57:02 +01:00
if ( index >= m_layouts.count() || index < 0 )
{
return QPair< QString, KeyboardGlobal::KeyboardInfo >();
2020-03-26 15:57:02 +01:00
}
2020-03-26 15:57:02 +01:00
return m_layouts.at( index );
}
2016-05-31 19:05:25 +02:00
QString
KeyboardLayoutModel::key( int index ) const
{
if ( index >= m_layouts.count() || index < 0 )
{
return QString();
}
return m_layouts.at( index ).first;
}
2016-05-31 19:05:25 +02:00
void
KeyboardLayoutModel::init()
{
2020-03-26 15:57:02 +01:00
KeyboardGlobal::LayoutsMap layouts = KeyboardGlobal::getKeyboardLayouts();
for ( KeyboardGlobal::LayoutsMap::const_iterator it = layouts.constBegin(); it != layouts.constEnd(); ++it )
2016-05-31 19:05:25 +02:00
{
2020-03-26 15:57:02 +01:00
m_layouts.append( qMakePair( it.key(), it.value() ) );
}
std::stable_sort( m_layouts.begin(),
m_layouts.end(),
[]( const QPair< QString, KeyboardGlobal::KeyboardInfo >& a,
const QPair< QString, KeyboardGlobal::KeyboardInfo >& b ) {
return a.second.description < b.second.description;
} );
2016-05-31 19:05:25 +02:00
}
2020-03-26 15:57:02 +01:00
QHash< int, QByteArray >
KeyboardLayoutModel::roleNames() const
{
2020-03-26 15:57:02 +01:00
return { { Qt::DisplayRole, "label" }, { KeyboardLayoutKeyRole, "key" }, { KeyboardVariantsRole, "variants" } };
}
void
KeyboardLayoutModel::setCurrentIndex( int index )
{
2020-03-26 15:57:02 +01:00
if ( index >= m_layouts.count() || index < 0 )
{
return;
2020-03-26 15:57:02 +01:00
}
if ( m_currentIndex != index )
{
m_currentIndex = index;
emit currentIndexChanged( m_currentIndex );
}
}
int
KeyboardLayoutModel::currentIndex() const
{
return m_currentIndex;
}
KeyboardVariantsModel::KeyboardVariantsModel( QObject* parent )
: XKBListModel( parent )
{
m_contextname = "kb_variants";
}
void
KeyboardVariantsModel::setVariants( QMap< QString, QString > variants )
{
beginResetModel();
m_list.clear();
m_list.reserve( variants.count() );
for ( const auto& key : variants.keys() )
{
m_list << ModelInfo { variants[ key ], key };
}
m_currentIndex = -1;
endResetModel();
}