[keyboard] Factor out a 2-column k-v list
This commit is contained in:
parent
5afe54132b
commit
d536173d66
@ -14,35 +14,19 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
KeyboardModelsModel::KeyboardModelsModel( QObject* parent )
|
XKBListModel::XKBListModel( QObject* parent )
|
||||||
: QAbstractListModel( 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
|
int
|
||||||
KeyboardModelsModel::rowCount( const QModelIndex& ) const
|
XKBListModel::rowCount( const QModelIndex& ) const
|
||||||
{
|
{
|
||||||
return m_list.count();
|
return m_list.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant
|
QVariant
|
||||||
KeyboardModelsModel::data( const QModelIndex& index, int role ) const
|
XKBListModel::data( const QModelIndex& index, int role ) const
|
||||||
{
|
{
|
||||||
if ( !index.isValid() )
|
if ( !index.isValid() )
|
||||||
{
|
{
|
||||||
@ -67,7 +51,7 @@ KeyboardModelsModel::data( const QModelIndex& index, int role ) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
QString
|
QString
|
||||||
KeyboardModelsModel::modelKey( int index ) const
|
XKBListModel::modelKey( int index ) const
|
||||||
{
|
{
|
||||||
if ( index < 0 || index >= m_list.count() )
|
if ( index < 0 || index >= m_list.count() )
|
||||||
{
|
{
|
||||||
@ -77,7 +61,7 @@ KeyboardModelsModel::modelKey( int index ) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
QString
|
QString
|
||||||
KeyboardModelsModel::modelLabel( int index ) const
|
XKBListModel::modelLabel( int index ) const
|
||||||
{
|
{
|
||||||
if ( index < 0 || index >= m_list.count() )
|
if ( index < 0 || index >= m_list.count() )
|
||||||
{
|
{
|
||||||
@ -87,13 +71,13 @@ KeyboardModelsModel::modelLabel( int index ) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
QHash< int, QByteArray >
|
QHash< int, QByteArray >
|
||||||
KeyboardModelsModel::roleNames() const
|
XKBListModel::roleNames() const
|
||||||
{
|
{
|
||||||
return { { Qt::DisplayRole, "label" }, { Qt::UserRole, "key" } };
|
return { { Qt::DisplayRole, "label" }, { Qt::UserRole, "key" } };
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
KeyboardModelsModel::setCurrentIndex( int index )
|
XKBListModel::setCurrentIndex( int index )
|
||||||
{
|
{
|
||||||
if ( index >= m_list.count() || index < 0 )
|
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 )
|
KeyboardLayoutModel::KeyboardLayoutModel( QObject* parent )
|
||||||
: QAbstractListModel( parent )
|
: QAbstractListModel( parent )
|
||||||
|
@ -17,11 +17,12 @@
|
|||||||
#include <QMetaType>
|
#include <QMetaType>
|
||||||
#include <QObject>
|
#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.
|
* This model acts like it has a single selection, as well.
|
||||||
*/
|
*/
|
||||||
class KeyboardModelsModel : public QAbstractListModel
|
|
||||||
|
class XKBListModel : public QAbstractListModel
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY( int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged )
|
Q_PROPERTY( int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged )
|
||||||
@ -33,7 +34,7 @@ public:
|
|||||||
KeyRole = Qt::UserRole ///< xkb identifier
|
KeyRole = Qt::UserRole ///< xkb identifier
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit KeyboardModelsModel( QObject* parent = nullptr );
|
explicit XKBListModel( QObject* parent = nullptr );
|
||||||
|
|
||||||
int rowCount( const QModelIndex& ) const override;
|
int rowCount( const QModelIndex& ) const override;
|
||||||
QVariant data( const QModelIndex& index, int role ) const override;
|
QVariant data( const QModelIndex& index, int role ) const override;
|
||||||
@ -54,14 +55,12 @@ public:
|
|||||||
QHash< int, QByteArray > roleNames() const override;
|
QHash< int, QByteArray > roleNames() const override;
|
||||||
|
|
||||||
void setCurrentIndex( int index );
|
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; }
|
int currentIndex() const { return m_currentIndex; }
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void currentIndexChanged( int index );
|
void currentIndexChanged( int index );
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
struct ModelInfo
|
struct ModelInfo
|
||||||
{
|
{
|
||||||
/// XKB identifier
|
/// XKB identifier
|
||||||
@ -71,6 +70,24 @@ private:
|
|||||||
};
|
};
|
||||||
QVector< ModelInfo > m_list;
|
QVector< ModelInfo > m_list;
|
||||||
int m_currentIndex = -1;
|
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
|
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,
|
connect( ui->physicalModelSelector,
|
||||||
QOverload< int >::of( &QComboBox::currentIndexChanged ),
|
QOverload< int >::of( &QComboBox::currentIndexChanged ),
|
||||||
config->keyboardModels(),
|
config->keyboardModels(),
|
||||||
QOverload< int >::of( &KeyboardModelsModel::setCurrentIndex ) );
|
QOverload< int >::of( &XKBListModel::setCurrentIndex ) );
|
||||||
connect( config->keyboardModels(),
|
connect( config->keyboardModels(),
|
||||||
&KeyboardModelsModel::currentIndexChanged,
|
&KeyboardModelsModel::currentIndexChanged,
|
||||||
ui->physicalModelSelector,
|
ui->physicalModelSelector,
|
||||||
|
Loading…
Reference in New Issue
Block a user