diff --git a/src/modules/keyboard/Config.cpp b/src/modules/keyboard/Config.cpp index 11a20bbb7..e5574f186 100644 --- a/src/modules/keyboard/Config.cpp +++ b/src/modules/keyboard/Config.cpp @@ -23,160 +23,6 @@ #include #include -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. */ diff --git a/src/modules/keyboard/Config.h b/src/modules/keyboard/Config.h index 44a893faa..f02c08877 100644 --- a/src/modules/keyboard/Config.h +++ b/src/modules/keyboard/Config.h @@ -20,63 +20,6 @@ #include #include -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 diff --git a/src/modules/keyboard/KeyboardLayoutModel.cpp b/src/modules/keyboard/KeyboardLayoutModel.cpp index 5b92678f6..2a4ffa4d6 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.cpp +++ b/src/modules/keyboard/KeyboardLayoutModel.cpp @@ -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(); +} + diff --git a/src/modules/keyboard/KeyboardLayoutModel.h b/src/modules/keyboard/KeyboardLayoutModel.h index f4699c9f8..0fd662263 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.h +++ b/src/modules/keyboard/KeyboardLayoutModel.h @@ -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