[keyboard] Move all the keyboard (AbstractList)Models to the models file

This commit is contained in:
Adriaan de Groot 2020-10-12 14:33:09 +02:00
parent 1d696253c3
commit b54273f904
4 changed files with 212 additions and 211 deletions

View File

@ -23,160 +23,6 @@
#include <QProcess>
#include <QTimer>
KeyboardModelsModel::KeyboardModelsModel( QObject* parent )
: QAbstractListModel( parent )
{
detectModels();
}
void
KeyboardModelsModel::detectModels()
{
beginResetModel();
const auto models = KeyboardGlobal::getKeyboardModels();
auto index = -1;
for ( const auto& key : models.keys() )
{
index++;
m_list << QMap< QString, QString > { { "label", key }, { "key", models[ key ] } };
if ( models[ key ] == "pc105" )
{
this->setCurrentIndex( index );
}
}
endResetModel();
}
void
KeyboardModelsModel::refresh()
{
m_list.clear();
setCurrentIndex( -1 );
detectModels();
}
QVariant
KeyboardModelsModel::data( const QModelIndex& index, int role ) const
{
if ( !index.isValid() )
{
return QVariant();
}
const auto item = m_list.at( index.row() );
return role == Qt::DisplayRole ? item[ "label" ] : item[ "key" ];
}
int
KeyboardModelsModel::rowCount( const QModelIndex& ) const
{
return m_list.count();
}
QHash< int, QByteArray >
KeyboardModelsModel::roleNames() const
{
return { { Qt::DisplayRole, "label" }, { Qt::UserRole, "key" } };
}
int
KeyboardModelsModel::currentIndex() const
{
return m_currentIndex;
}
const QMap< QString, QString >
KeyboardModelsModel::item( const int& index ) const
{
if ( index >= m_list.count() || index < 0 )
{
return QMap< QString, QString >();
}
return m_list.at( index );
}
const QMap< QString, QString >
KeyboardVariantsModel::item( const int& index ) const
{
if ( index >= m_list.count() || index < 0 )
{
return QMap< QString, QString >();
}
return m_list.at( index );
}
void
KeyboardModelsModel::setCurrentIndex( const int& index )
{
if ( index >= m_list.count() || index < 0 )
{
return;
}
m_currentIndex = index;
emit currentIndexChanged( m_currentIndex );
}
KeyboardVariantsModel::KeyboardVariantsModel( QObject* parent )
: QAbstractListModel( parent )
{
}
int
KeyboardVariantsModel::currentIndex() const
{
return m_currentIndex;
}
void
KeyboardVariantsModel::setCurrentIndex( const int& index )
{
if ( index >= m_list.count() || index < 0 )
{
return;
}
m_currentIndex = index;
emit currentIndexChanged( m_currentIndex );
}
QVariant
KeyboardVariantsModel::data( const QModelIndex& index, int role ) const
{
if ( !index.isValid() )
{
return QVariant();
}
const auto item = m_list.at( index.row() );
return role == Qt::DisplayRole ? item[ "label" ] : item[ "key" ];
}
int
KeyboardVariantsModel::rowCount( const QModelIndex& ) const
{
return m_list.count();
}
QHash< int, QByteArray >
KeyboardVariantsModel::roleNames() const
{
return { { Qt::DisplayRole, "label" }, { Qt::UserRole, "key" } };
}
void
KeyboardVariantsModel::setVariants( QMap< QString, QString > variants )
{
m_list.clear();
beginResetModel();
for ( const auto& key : variants.keys() )
{
const auto item = QMap< QString, QString > { { "label", key }, { "key", variants[ key ] } };
m_list << item;
}
endResetModel();
}
/* Returns stringlist with suitable setxkbmap command-line arguments
* to set the given @p layout and @p variant.
*/

View File

@ -20,63 +20,6 @@
#include <QTimer>
#include <QUrl>
class KeyboardModelsModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY( int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged )
public:
explicit KeyboardModelsModel( QObject* parent = nullptr );
int rowCount( const QModelIndex& = QModelIndex() ) const override;
QVariant data( const QModelIndex& index, int role ) const override;
void setCurrentIndex( const int& index );
int currentIndex() const;
const QMap< QString, QString > item( const int& index ) const;
public slots:
void refresh();
protected:
QHash< int, QByteArray > roleNames() const override;
private:
int m_currentIndex = -1;
QVector< QMap< QString, QString > > m_list;
void detectModels();
signals:
void currentIndexChanged( int index );
};
class KeyboardVariantsModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY( int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged )
public:
explicit KeyboardVariantsModel( QObject* parent = nullptr );
void setVariants( QMap< QString, QString > variants );
int rowCount( const QModelIndex& = QModelIndex() ) const override;
QVariant data( const QModelIndex& index, int role ) const override;
void setCurrentIndex( const int& index );
int currentIndex() const;
const QMap< QString, QString > item( const int& index ) const;
protected:
QHash< int, QByteArray > roleNames() const override;
private:
int m_currentIndex = -1;
QVector< QMap< QString, QString > > m_list;
signals:
void currentIndexChanged( int index );
};
class Config : public QObject
{
Q_OBJECT

View File

@ -99,3 +99,158 @@ KeyboardLayoutModel::currentIndex() const
{
return m_currentIndex;
}
KeyboardModelsModel::KeyboardModelsModel( QObject* parent )
: QAbstractListModel( parent )
{
detectModels();
}
void
KeyboardModelsModel::detectModels()
{
beginResetModel();
const auto models = KeyboardGlobal::getKeyboardModels();
auto index = -1;
for ( const auto& key : models.keys() )
{
index++;
m_list << QMap< QString, QString > { { "label", key }, { "key", models[ key ] } };
if ( models[ key ] == "pc105" )
{
this->setCurrentIndex( index );
}
}
endResetModel();
}
void
KeyboardModelsModel::refresh()
{
m_list.clear();
setCurrentIndex( -1 );
detectModels();
}
QVariant
KeyboardModelsModel::data( const QModelIndex& index, int role ) const
{
if ( !index.isValid() )
{
return QVariant();
}
const auto item = m_list.at( index.row() );
return role == Qt::DisplayRole ? item[ "label" ] : item[ "key" ];
}
int
KeyboardModelsModel::rowCount( const QModelIndex& ) const
{
return m_list.count();
}
QHash< int, QByteArray >
KeyboardModelsModel::roleNames() const
{
return { { Qt::DisplayRole, "label" }, { Qt::UserRole, "key" } };
}
int
KeyboardModelsModel::currentIndex() const
{
return m_currentIndex;
}
const QMap< QString, QString >
KeyboardModelsModel::item( const int& index ) const
{
if ( index >= m_list.count() || index < 0 )
{
return QMap< QString, QString >();
}
return m_list.at( index );
}
const QMap< QString, QString >
KeyboardVariantsModel::item( const int& index ) const
{
if ( index >= m_list.count() || index < 0 )
{
return QMap< QString, QString >();
}
return m_list.at( index );
}
void
KeyboardModelsModel::setCurrentIndex( const int& index )
{
if ( index >= m_list.count() || index < 0 )
{
return;
}
m_currentIndex = index;
emit currentIndexChanged( m_currentIndex );
}
KeyboardVariantsModel::KeyboardVariantsModel( QObject* parent )
: QAbstractListModel( parent )
{
}
int
KeyboardVariantsModel::currentIndex() const
{
return m_currentIndex;
}
void
KeyboardVariantsModel::setCurrentIndex( const int& index )
{
if ( index >= m_list.count() || index < 0 )
{
return;
}
m_currentIndex = index;
emit currentIndexChanged( m_currentIndex );
}
QVariant
KeyboardVariantsModel::data( const QModelIndex& index, int role ) const
{
if ( !index.isValid() )
{
return QVariant();
}
const auto item = m_list.at( index.row() );
return role == Qt::DisplayRole ? item[ "label" ] : item[ "key" ];
}
int
KeyboardVariantsModel::rowCount( const QModelIndex& ) const
{
return m_list.count();
}
QHash< int, QByteArray >
KeyboardVariantsModel::roleNames() const
{
return { { Qt::DisplayRole, "label" }, { Qt::UserRole, "key" } };
}
void
KeyboardVariantsModel::setVariants( QMap< QString, QString > variants )
{
m_list.clear();
beginResetModel();
for ( const auto& key : variants.keys() )
{
const auto item = QMap< QString, QString > { { "label", key }, { "key", variants[ key ] } };
m_list << item;
}
endResetModel();
}

View File

@ -51,4 +51,61 @@ signals:
void currentIndexChanged( int index );
};
class KeyboardModelsModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY( int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged )
public:
explicit KeyboardModelsModel( QObject* parent = nullptr );
int rowCount( const QModelIndex& = QModelIndex() ) const override;
QVariant data( const QModelIndex& index, int role ) const override;
void setCurrentIndex( const int& index );
int currentIndex() const;
const QMap< QString, QString > item( const int& index ) const;
public slots:
void refresh();
protected:
QHash< int, QByteArray > roleNames() const override;
private:
int m_currentIndex = -1;
QVector< QMap< QString, QString > > m_list;
void detectModels();
signals:
void currentIndexChanged( int index );
};
class KeyboardVariantsModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY( int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged )
public:
explicit KeyboardVariantsModel( QObject* parent = nullptr );
void setVariants( QMap< QString, QString > variants );
int rowCount( const QModelIndex& = QModelIndex() ) const override;
QVariant data( const QModelIndex& index, int role ) const override;
void setCurrentIndex( const int& index );
int currentIndex() const;
const QMap< QString, QString > item( const int& index ) const;
protected:
QHash< int, QByteArray > roleNames() const override;
private:
int m_currentIndex = -1;
QVector< QMap< QString, QString > > m_list;
signals:
void currentIndexChanged( int index );
};
#endif // KEYBOARDLAYOUTMODEL_H