calamares/src/modules/keyboard/KeyboardLayoutModel.h

139 lines
3.6 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-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
*
*/
#ifndef KEYBOARDLAYOUTMODEL_H
#define KEYBOARDLAYOUTMODEL_H
#include "keyboardwidget/keyboardglobal.h"
#include <QAbstractListModel>
#include <QMap>
#include <QMetaType>
#include <QObject>
2016-05-31 19:05:25 +02:00
/** @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
2016-05-31 19:05:25 +02:00
{
Q_OBJECT
2020-03-26 15:57:02 +01:00
Q_PROPERTY( int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged )
2016-05-31 19:05:25 +02:00
public:
enum
2016-05-31 19:05:25 +02:00
{
LabelRole = Qt::DisplayRole, ///< Human-readable
KeyRole = Qt::UserRole ///< xkb identifier
2016-05-31 19:05:25 +02:00
};
explicit XKBListModel( QObject* parent = nullptr );
2016-05-31 19:05:25 +02:00
int rowCount( const QModelIndex& = QModelIndex() ) const override;
2016-05-31 19:05:25 +02:00
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;
2016-05-31 19:05:25 +02:00
2020-03-26 15:57:02 +01:00
QHash< int, QByteArray > roleNames() const override;
void setCurrentIndex( int index );
int currentIndex() const { return m_currentIndex; }
signals:
2020-03-26 15:57:02 +01:00
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
2016-05-31 19:05:25 +02:00
};
class KeyboardLayoutModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY( int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged )
public:
enum Roles : int
{
KeyboardVariantsRole = Qt::UserRole,
KeyboardLayoutKeyRole
};
KeyboardLayoutModel( QObject* parent = nullptr );
int rowCount( const QModelIndex& parent = QModelIndex() ) const override;
QVariant data( const QModelIndex& index, int role ) const override;
void setCurrentIndex( const int& index );
int currentIndex() const;
const QPair< QString, KeyboardGlobal::KeyboardInfo > item( const int& index ) const;
protected:
QHash< int, QByteArray > roleNames() const override;
private:
void init();
int m_currentIndex = -1;
QList< QPair< QString, KeyboardGlobal::KeyboardInfo > > m_layouts;
signals:
void currentIndexChanged( int index );
};
class KeyboardVariantsModel : public XKBListModel
{
Q_OBJECT
public:
explicit KeyboardVariantsModel( QObject* parent = nullptr );
void setVariants( QMap< QString, QString > variants );
};
2020-03-26 15:57:02 +01:00
#endif // KEYBOARDLAYOUTMODEL_H