keyboardwidget.cpp 12 KB
#include <QKeyEvent>
#include <QDebug>
#include "keyboardwidget.h"
#include "ui_keyboardwidget.h"

#include "soundplayer.h"

static const QString m_mapKeyboard[] = {
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "0",
    "Q",
    "W",
    "E",
    "R",
    "T",
    "Y",
    "U",
    "I",
    "O",
    "P",
    "A",
    "S",
    "D",
    "F",
    "G",
    "H",
    "J",
    "K",
    "L",
    "Z",
    "X",
    "C",
    "V",
    "B",
    "N",
    "M",
    "-",
    ",",
    " "
};



KeyboardWidget::KeyboardWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::KeyboardWidget)
{
    ui->setupUi(this);

    m_pCurCheckedBtn = NULL;
    m_vectorTabOrder.append(ui->pushButton_1);
    m_vectorTabOrder.append(ui->pushButton_2);
    m_vectorTabOrder.append(ui->pushButton_3);
    m_vectorTabOrder.append(ui->pushButton_4);
    m_vectorTabOrder.append(ui->pushButton_5);
    m_vectorTabOrder.append(ui->pushButton_6);
    m_vectorTabOrder.append(ui->pushButton_7);
    m_vectorTabOrder.append(ui->pushButton_8);
    m_vectorTabOrder.append(ui->pushButton_9);
    m_vectorTabOrder.append(ui->pushButton_10);
    m_vectorTabOrder.append(ui->pushButton_11);
    m_vectorTabOrder.append(ui->pushButton_12);
    m_vectorTabOrder.append(ui->pushButton_13);
    m_vectorTabOrder.append(ui->pushButton_14);
    m_vectorTabOrder.append(ui->pushButton_15);
    m_vectorTabOrder.append(ui->pushButton_16);
    m_vectorTabOrder.append(ui->pushButton_17);
    m_vectorTabOrder.append(ui->pushButton_18);
    m_vectorTabOrder.append(ui->pushButton_19);
    m_vectorTabOrder.append(ui->pushButton_20);
    m_vectorTabOrder.append(ui->pushButton_21);
    m_vectorTabOrder.append(ui->pushButton_22);
    m_vectorTabOrder.append(ui->pushButton_23);
    m_vectorTabOrder.append(ui->pushButton_24);
    m_vectorTabOrder.append(ui->pushButton_25);
    m_vectorTabOrder.append(ui->pushButton_26);
    m_vectorTabOrder.append(ui->pushButton_27);
    m_vectorTabOrder.append(ui->pushButton_28);
    m_vectorTabOrder.append(ui->pushButton_29);
    m_vectorTabOrder.append(ui->pushButton_30);
    m_vectorTabOrder.append(ui->pushButton_31);
    m_vectorTabOrder.append(ui->pushButton_32);
    m_vectorTabOrder.append(ui->pushButton_33);
    m_vectorTabOrder.append(ui->pushButton_34);
    m_vectorTabOrder.append(ui->pushButton_35);
    m_vectorTabOrder.append(ui->pushButton_36);
    m_vectorTabOrder.append(ui->pushButton_37);
    m_vectorTabOrder.append(ui->pushButton_38);
    m_vectorTabOrder.append(ui->pushButton_39);
    m_vectorTabOrder.append(ui->pushButton_40);
    m_vectorTabOrder.append(ui->pushButton_41);
    m_vectorTabOrder.append(ui->pushButton_42);



    m_pSignalMapper = new QSignalMapper(this);

    m_pSignalMapper->setMapping(ui->pushButton_1, 0);
    m_pSignalMapper->setMapping(ui->pushButton_2, 1);
    m_pSignalMapper->setMapping(ui->pushButton_3, 2);
    m_pSignalMapper->setMapping(ui->pushButton_4, 3);
    m_pSignalMapper->setMapping(ui->pushButton_5, 4);
    m_pSignalMapper->setMapping(ui->pushButton_6, 5);
    m_pSignalMapper->setMapping(ui->pushButton_7, 6);
    m_pSignalMapper->setMapping(ui->pushButton_8, 7);
    m_pSignalMapper->setMapping(ui->pushButton_9, 8);
    m_pSignalMapper->setMapping(ui->pushButton_10, 9);
    m_pSignalMapper->setMapping(ui->pushButton_11, 10);
    m_pSignalMapper->setMapping(ui->pushButton_12, 11);
    m_pSignalMapper->setMapping(ui->pushButton_13, 12);
    m_pSignalMapper->setMapping(ui->pushButton_14, 13);
    m_pSignalMapper->setMapping(ui->pushButton_15, 14);
    m_pSignalMapper->setMapping(ui->pushButton_16, 15);
    m_pSignalMapper->setMapping(ui->pushButton_17, 16);
    m_pSignalMapper->setMapping(ui->pushButton_18, 17);
    m_pSignalMapper->setMapping(ui->pushButton_19, 18);
    m_pSignalMapper->setMapping(ui->pushButton_20, 19);
    m_pSignalMapper->setMapping(ui->pushButton_21, 20);
    m_pSignalMapper->setMapping(ui->pushButton_22, 21);
    m_pSignalMapper->setMapping(ui->pushButton_23, 22);
    m_pSignalMapper->setMapping(ui->pushButton_24, 23);
    m_pSignalMapper->setMapping(ui->pushButton_25, 24);
    m_pSignalMapper->setMapping(ui->pushButton_26, 25);
    m_pSignalMapper->setMapping(ui->pushButton_27, 26);
    m_pSignalMapper->setMapping(ui->pushButton_28, 27);
    m_pSignalMapper->setMapping(ui->pushButton_29, 28);
    m_pSignalMapper->setMapping(ui->pushButton_30, 29);
    m_pSignalMapper->setMapping(ui->pushButton_31, 30);
    m_pSignalMapper->setMapping(ui->pushButton_32, 31);
    m_pSignalMapper->setMapping(ui->pushButton_33, 32);
    m_pSignalMapper->setMapping(ui->pushButton_34, 33);
    m_pSignalMapper->setMapping(ui->pushButton_35, 34);
    m_pSignalMapper->setMapping(ui->pushButton_36, 35);
    m_pSignalMapper->setMapping(ui->pushButton_37, 36);
    m_pSignalMapper->setMapping(ui->pushButton_38, 37);
    m_pSignalMapper->setMapping(ui->pushButton_40, 38);




    connect(ui->pushButton_1,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_2,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_3,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_4,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_5,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_6,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_7,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_8,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_9,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_10,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_11,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_12,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_13,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_14,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_15,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_16,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_17,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_18,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_19,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_20,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_21,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_22,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_23,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_24,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_25,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_26,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_27,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_28,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_29,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_30,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_31,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_32,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_33,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_34,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_35,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_36,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_37,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_38,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_40,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));

    connect(m_pSignalMapper,SIGNAL(mapped(int)), this, SLOT(onKeyboardClicked(int)));

    foreach (QPushButton *button, findChildren<QPushButton *>())
        connect(button, &QPushButton::pressed, SoundPlayer::playClick);
}

KeyboardWidget::~KeyboardWidget()
{
    delete ui;
}

void KeyboardWidget::on_pushButton_41_clicked()
{
    if(m_pCurCheckedBtn != NULL) m_pCurCheckedBtn->setChecked(false);
    m_nCurTabPos = 40;
    m_pCurCheckedBtn = m_vectorTabOrder[m_nCurTabPos];
    m_pCurCheckedBtn->setChecked(true);
    QKeyEvent key(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
    QGuiApplication::sendEvent(QApplication::focusObject(),&key);
    emit onOkKeyClicked();
}

void KeyboardWidget::on_pushButton_42_clicked()
{
    if(m_pCurCheckedBtn != NULL) m_pCurCheckedBtn->setChecked(false);
    m_nCurTabPos = 41;
    m_pCurCheckedBtn = m_vectorTabOrder[m_nCurTabPos];
    m_pCurCheckedBtn->setChecked(true);
    if(m_pCurCheckedBtn != NULL) m_pCurCheckedBtn->setChecked(false);
    emit onCancelKeyClicked();
}

void KeyboardWidget::on_pushButton_39_clicked()
{
    qDebug() << qApp->focusObject();
    if(m_pCurCheckedBtn != NULL) m_pCurCheckedBtn->setChecked(false);
    m_nCurTabPos = 38;
    m_pCurCheckedBtn = m_vectorTabOrder[m_nCurTabPos];
    m_pCurCheckedBtn->setChecked(true);
    QKeyEvent key(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
    QGuiApplication::sendEvent(QApplication::focusObject(),&key);
    emit onBackspaceKeyClicked();
}

void KeyboardWidget::onKeyboardClicked(const int in){
    QInputMethodEvent event;
    qDebug()<< "keyboard clicked" << in;
    event.setCommitString(m_mapKeyboard[in]);
    if(m_pCurCheckedBtn != NULL) m_pCurCheckedBtn->setChecked(false);
    if( in < 38) m_nCurTabPos = in;
    else if( in == 38) m_nCurTabPos = 39;
    m_pCurCheckedBtn = m_vectorTabOrder[m_nCurTabPos];
    m_pCurCheckedBtn->setChecked(true);
    QGuiApplication::sendEvent(QApplication::focusObject(),&event);
    emit onKeyboardClickSignal(m_mapKeyboard[in]);
}

void KeyboardWidget::on_pushButton_40_clicked()
{
    emit onSpaceKeyClicked();
}

void KeyboardWidget::keyPressEvent(QKeyEvent *event){
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        if(m_nCurTabPos > 0 ) m_nCurTabPos--;
        else m_nCurTabPos = m_vectorTabOrder.size()-1;
        if(m_pCurCheckedBtn !=NULL) m_pCurCheckedBtn->setChecked(false);
        m_pCurCheckedBtn = m_vectorTabOrder[m_nCurTabPos];
        m_pCurCheckedBtn->setChecked(true);
        break;
    case 0x01000030:    // Turn right
        if(m_nCurTabPos < (m_vectorTabOrder.size() -1)) m_nCurTabPos++;
        else m_nCurTabPos=0;
        if(m_pCurCheckedBtn !=NULL) m_pCurCheckedBtn->setChecked(false);
        m_pCurCheckedBtn = m_vectorTabOrder[m_nCurTabPos];
        m_pCurCheckedBtn->setChecked(true);
        break;
    }
}

void KeyboardWidget::keyReleaseEvent(QKeyEvent *event){
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        if(m_nCurTabPos > 0 ) m_nCurTabPos--;
        else m_nCurTabPos = m_vectorTabOrder.size()-1;
        if(m_pCurCheckedBtn !=NULL) m_pCurCheckedBtn->setChecked(false);
        m_pCurCheckedBtn = m_vectorTabOrder[m_nCurTabPos];
        m_pCurCheckedBtn->setChecked(true);
        break;
    case 0x01000031:    // Push
    {
        if(m_pCurCheckedBtn != NULL){
            m_pCurCheckedBtn->click();
        }
        break;
    }
    case 0x01000030:    // Turn right
        if(m_nCurTabPos < (m_vectorTabOrder.size() -1)) m_nCurTabPos++;
        else m_nCurTabPos=0;
        if(m_pCurCheckedBtn !=NULL) m_pCurCheckedBtn->setChecked(false);
        m_pCurCheckedBtn = m_vectorTabOrder[m_nCurTabPos];
        m_pCurCheckedBtn->setChecked(true);
        break;
    }
}

void KeyboardWidget::focusInKeyboard(){
    m_nCurTabPos = 0;
    if(m_pCurCheckedBtn != NULL) m_pCurCheckedBtn->setChecked(false);
    m_pCurCheckedBtn = m_vectorTabOrder[0];
    m_pCurCheckedBtn->setChecked(true);
    this->grabKeyboard();
}

void KeyboardWidget::focusOutKeyboard(){
    m_nCurTabPos = 0;
    if(m_pCurCheckedBtn != NULL) m_pCurCheckedBtn->setChecked(false);
    this->releaseKeyboard();
}