calamares/src/modules/keyboard/KeyboardLayoutModel.cpp

102 lines
2.5 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 <algorithm>
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:
return m_layouts.at( index.row() ).second.description;
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
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
2020-03-26 15:57:02 +01:00
KeyboardLayoutModel::setCurrentIndex( const 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
}
m_currentIndex = index;
2020-03-26 15:57:02 +01:00
emit currentIndexChanged( m_currentIndex );
}
int
KeyboardLayoutModel::currentIndex() const
{
return m_currentIndex;
}