From 1a46e08cc28277d21d0c4fd5da8c9e087177008a Mon Sep 17 00:00:00 2001 From: Camilo Higuita Date: Fri, 3 Apr 2020 10:18:07 +0200 Subject: [PATCH] [keyboard] changes to the keyboardmodel to work with qml --- src/modules/keyboard/Config.cpp | 10 ++-- src/modules/keyboard/Config.h | 6 ++- src/modules/keyboard/KeyboardLayoutModel.cpp | 53 +++++++++++++++----- src/modules/keyboard/KeyboardLayoutModel.h | 14 +++++- 4 files changed, 63 insertions(+), 20 deletions(-) diff --git a/src/modules/keyboard/Config.cpp b/src/modules/keyboard/Config.cpp index f8abb01d4..e617216f9 100644 --- a/src/modules/keyboard/Config.cpp +++ b/src/modules/keyboard/Config.cpp @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2019-2020, Adriaan de Groot + * Copyright 2020, Camilo Higuita * * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,11 +19,6 @@ #include "Config.h" -#include -#include -#include -#include - #include "keyboardwidget/keyboardpreview.h" #include "SetKeyboardLayoutJob.h" @@ -31,6 +27,10 @@ #include "utils/Logger.h" #include "utils/Retranslator.h" +#include +#include +#include + KeyboardModelsModel::KeyboardModelsModel(QObject* parent) : QAbstractListModel(parent) { detectModels(); diff --git a/src/modules/keyboard/Config.h b/src/modules/keyboard/Config.h index bb1447cda..e2881e9c3 100644 --- a/src/modules/keyboard/Config.h +++ b/src/modules/keyboard/Config.h @@ -1,6 +1,7 @@ /* === This file is part of Calamares - === * * Copyright 2019-2020, Adriaan de Groot + * Copyright 2020, Camilo Higuita * * Calamares is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,13 +20,14 @@ #ifndef KEYBOARD_CONFIG_H #define KEYBOARD_CONFIG_H +#include "Job.h" +#include "KeyboardLayoutModel.h" + #include #include #include #include -#include "Job.h" #include -#include "KeyboardLayoutModel.h" class KeyboardModelsModel : public QAbstractListModel { diff --git a/src/modules/keyboard/KeyboardLayoutModel.cpp b/src/modules/keyboard/KeyboardLayoutModel.cpp index 0abd89ae2..cd43a801b 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.cpp +++ b/src/modules/keyboard/KeyboardLayoutModel.cpp @@ -23,12 +23,11 @@ KeyboardLayoutModel::KeyboardLayoutModel( QObject* parent ) - : QAbstractListModel( parent ) +: QAbstractListModel( parent ) { init(); } - int KeyboardLayoutModel::rowCount( const QModelIndex& parent ) const { @@ -45,30 +44,60 @@ KeyboardLayoutModel::data( const QModelIndex& index, int role ) const switch ( role ) { - case Qt::DisplayRole: - return m_layouts.at( index.row() ).second.description; - case KeyboardVariantsRole: - return QVariant::fromValue( m_layouts.at( index.row() ).second.variants ); - case KeyboardLayoutKeyRole: - return m_layouts.at( index.row() ).first; + case Qt::DisplayRole: + return m_layouts.at( index.row() ).second.description; + case KeyboardVariantsRole: + return QVariant::fromValue( m_layouts.at( index.row() ).second.variants ); + case KeyboardLayoutKeyRole: + return m_layouts.at( index.row() ).first; } return QVariant(); } +const QPair< QString, KeyboardGlobal::KeyboardInfo > +KeyboardLayoutModel::item(const int &index) const +{ + if(index >= m_layouts.count() || index < 0) + return QPair< QString, KeyboardGlobal::KeyboardInfo >(); + + return m_layouts.at(index); +} void KeyboardLayoutModel::init() { KeyboardGlobal::LayoutsMap layouts = - KeyboardGlobal::getKeyboardLayouts(); + KeyboardGlobal::getKeyboardLayouts(); for ( KeyboardGlobal::LayoutsMap::const_iterator it = layouts.constBegin(); - it != layouts.constEnd(); ++it ) - m_layouts.append( qMakePair( it.key(), it.value() ) ); + it != layouts.constEnd(); ++it ) + m_layouts.append( qMakePair( it.key(), it.value() ) ); std::stable_sort( m_layouts.begin(), m_layouts.end(), []( const QPair< QString, KeyboardGlobal::KeyboardInfo >& a, - const QPair< QString, KeyboardGlobal::KeyboardInfo >& b ) + const QPair< QString, KeyboardGlobal::KeyboardInfo >& b ) { return a.second.description < b.second.description; } ); } + +QHash +KeyboardLayoutModel::roleNames() const +{ + return {{Qt::DisplayRole, "label"}, {KeyboardLayoutKeyRole, "key"}, {KeyboardVariantsRole, "variants"}}; +} + +void +KeyboardLayoutModel::setCurrentIndex(const int &index) +{ + if(index >= m_layouts.count() || index < 0) + return; + + m_currentIndex = index; + emit currentIndexChanged(m_currentIndex); +} + +int +KeyboardLayoutModel::currentIndex() const +{ + return m_currentIndex; +} diff --git a/src/modules/keyboard/KeyboardLayoutModel.h b/src/modules/keyboard/KeyboardLayoutModel.h index 27cb1d031..f3a6c6630 100644 --- a/src/modules/keyboard/KeyboardLayoutModel.h +++ b/src/modules/keyboard/KeyboardLayoutModel.h @@ -24,10 +24,12 @@ #include #include #include +#include class KeyboardLayoutModel : public QAbstractListModel { Q_OBJECT + Q_PROPERTY(int currentIndex WRITE setCurrentIndex READ currentIndex NOTIFY currentIndexChanged ) public: enum Roles : int @@ -42,10 +44,20 @@ public: 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 roleNames() const override; + private: void init(); - + int m_currentIndex =-1; QList< QPair< QString, KeyboardGlobal::KeyboardInfo > > m_layouts; + +signals: + void currentIndexChanged(int index); }; #endif // KEYBOARDLAYOUTMODEL_H