[keyboard] Factor out a 2-column k-v list
This commit is contained in:
parent
5afe54132b
commit
d536173d66
@ -14,35 +14,19 @@
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
KeyboardModelsModel::KeyboardModelsModel( QObject* parent )
|
||||
XKBListModel::XKBListModel( QObject* parent )
|
||||
: QAbstractListModel( parent )
|
||||
{
|
||||
// The models map is from human-readable names (!) to xkb identifier
|
||||
const auto models = KeyboardGlobal::getKeyboardModels();
|
||||
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";
|
||||
}
|
||||
|
||||
int
|
||||
KeyboardModelsModel::rowCount( const QModelIndex& ) const
|
||||
XKBListModel::rowCount( const QModelIndex& ) const
|
||||
{
|
||||
return m_list.count();
|
||||
}
|
||||
|
||||
QVariant
|
||||
KeyboardModelsModel::data( const QModelIndex& index, int role ) const
|
||||
XKBListModel::data( const QModelIndex& index, int role ) const
|
||||
{
|
||||
if ( !index.isValid() )
|
||||
{
|
||||
@ -67,7 +51,7 @@ KeyboardModelsModel::data( const QModelIndex& index, int role ) const
|
||||
}
|
||||
|
||||
QString
|
||||
KeyboardModelsModel::modelKey( int index ) const
|
||||
XKBListModel::modelKey( int index ) const
|
||||
{
|
||||
if ( index < 0 || index >= m_list.count() )
|
||||
{
|
||||
@ -77,7 +61,7 @@ KeyboardModelsModel::modelKey( int index ) const
|
||||
}
|
||||
|
||||
QString
|
||||
KeyboardModelsModel::modelLabel( int index ) const
|
||||
XKBListModel::modelLabel( int index ) const
|
||||
{
|
||||
if ( index < 0 || index >= m_list.count() )
|
||||
{
|
||||
@ -87,13 +71,13 @@ KeyboardModelsModel::modelLabel( int index ) const
|
||||
}
|
||||
|
||||
QHash< int, QByteArray >
|
||||
KeyboardModelsModel::roleNames() const
|
||||
XKBListModel::roleNames() const
|
||||
{
|
||||
return { { Qt::DisplayRole, "label" }, { Qt::UserRole, "key" } };
|
||||
}
|
||||
|
||||
void
|
||||
KeyboardModelsModel::setCurrentIndex( int index )
|
||||
XKBListModel::setCurrentIndex( int index )
|
||||
{
|
||||
if ( index >= m_list.count() || index < 0 )
|
||||
{
|
||||
@ -106,6 +90,28 @@ KeyboardModelsModel::setCurrentIndex( int index )
|
||||
}
|
||||
}
|
||||
|
||||
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 )
|
||||
|
@ -17,11 +17,12 @@
|
||||
#include <QMetaType>
|
||||
#include <QObject>
|
||||
|
||||
/** @brief A list model of the physical keyboard formats ("models" in xkb)
|
||||
/** @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 KeyboardModelsModel : public QAbstractListModel
|
||||
|
||||
class XKBListModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY( int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged )
|
||||
@ -33,7 +34,7 @@ public:
|
||||
KeyRole = Qt::UserRole ///< xkb identifier
|
||||
};
|
||||
|
||||
explicit KeyboardModelsModel( QObject* parent = nullptr );
|
||||
explicit XKBListModel( QObject* parent = nullptr );
|
||||
|
||||
int rowCount( const QModelIndex& ) const override;
|
||||
QVariant data( const QModelIndex& index, int role ) const override;
|
||||
@ -54,14 +55,12 @@ public:
|
||||
QHash< int, QByteArray > roleNames() const override;
|
||||
|
||||
void setCurrentIndex( int index );
|
||||
/// @brief Set the index back to PC105 (the default physical model)
|
||||
void setCurrentIndex() { setCurrentIndex( m_defaultPC105 ); }
|
||||
int currentIndex() const { return m_currentIndex; }
|
||||
|
||||
signals:
|
||||
void currentIndexChanged( int index );
|
||||
|
||||
private:
|
||||
protected:
|
||||
struct ModelInfo
|
||||
{
|
||||
/// XKB identifier
|
||||
@ -71,6 +70,24 @@ private:
|
||||
};
|
||||
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
|
||||
};
|
||||
|
||||
|
@ -76,7 +76,7 @@ KeyboardPage::KeyboardPage( Config* config, QWidget* parent )
|
||||
connect( ui->physicalModelSelector,
|
||||
QOverload< int >::of( &QComboBox::currentIndexChanged ),
|
||||
config->keyboardModels(),
|
||||
QOverload< int >::of( &KeyboardModelsModel::setCurrentIndex ) );
|
||||
QOverload< int >::of( &XKBListModel::setCurrentIndex ) );
|
||||
connect( config->keyboardModels(),
|
||||
&KeyboardModelsModel::currentIndexChanged,
|
||||
ui->physicalModelSelector,
|
||||
|
Loading…
Reference in New Issue
Block a user