[keyboard] changes to the keyboardmodel to work with qml

This commit is contained in:
Camilo Higuita 2020-04-03 10:18:07 +02:00 committed by Adriaan de Groot
parent 0872de7910
commit 1a46e08cc2
4 changed files with 63 additions and 20 deletions

View File

@ -1,6 +1,7 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2019-2020, Adriaan de Groot <groot@kde.org>
* Copyright 2020, Camilo Higuita <milo.h@aol.com> *
*
* 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 <QDebug>
#include <QProcess>
#include <QApplication>
#include <QTimer>
#include "keyboardwidget/keyboardpreview.h"
#include "SetKeyboardLayoutJob.h"
@ -31,6 +27,10 @@
#include "utils/Logger.h"
#include "utils/Retranslator.h"
#include <QProcess>
#include <QApplication>
#include <QTimer>
KeyboardModelsModel::KeyboardModelsModel(QObject* parent) : QAbstractListModel(parent)
{
detectModels();

View File

@ -1,6 +1,7 @@
/* === This file is part of Calamares - <https://github.com/calamares> ===
*
* Copyright 2019-2020, Adriaan de Groot <groot@kde.org>
* Copyright 2020, Camilo Higuita <milo.h@aol.com>
*
* 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 <QObject>
#include <QUrl>
#include <QTimer>
#include <QMap>
#include "Job.h"
#include <QAbstractListModel>
#include "KeyboardLayoutModel.h"
class KeyboardModelsModel : public QAbstractListModel
{

View File

@ -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<int, QByteArray>
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;
}

View File

@ -24,10 +24,12 @@
#include <QAbstractListModel>
#include <QMap>
#include <QMetaType>
#include <QObject>
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<int, QByteArray> 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