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

#include "soundplayer.h"

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

    m_pSignalMapper->setMapping(ui->pushButton_1, tr("1"));
    m_pSignalMapper->setMapping(ui->pushButton_2, tr("2"));
    m_pSignalMapper->setMapping(ui->pushButton_3, tr("3"));
    m_pSignalMapper->setMapping(ui->pushButton_4, tr("4"));
    m_pSignalMapper->setMapping(ui->pushButton_5, tr("5"));
    m_pSignalMapper->setMapping(ui->pushButton_6, tr("6"));
    m_pSignalMapper->setMapping(ui->pushButton_7, tr("7"));
    m_pSignalMapper->setMapping(ui->pushButton_8, tr("8"));
    m_pSignalMapper->setMapping(ui->pushButton_9, tr("9"));
    m_pSignalMapper->setMapping(ui->pushButton_10, tr("0"));
    m_pSignalMapper->setMapping(ui->pushButton_11, tr("Q"));
    m_pSignalMapper->setMapping(ui->pushButton_12, tr("W"));
    m_pSignalMapper->setMapping(ui->pushButton_13, tr("E"));
    m_pSignalMapper->setMapping(ui->pushButton_14, tr("R"));
    m_pSignalMapper->setMapping(ui->pushButton_15, tr("T"));
    m_pSignalMapper->setMapping(ui->pushButton_16, tr("Y"));
    m_pSignalMapper->setMapping(ui->pushButton_17, tr("U"));
    m_pSignalMapper->setMapping(ui->pushButton_18, tr("I"));
    m_pSignalMapper->setMapping(ui->pushButton_19, tr("O"));
    m_pSignalMapper->setMapping(ui->pushButton_20, tr("P"));
    m_pSignalMapper->setMapping(ui->pushButton_21, tr("A"));
    m_pSignalMapper->setMapping(ui->pushButton_22, tr("S"));
    m_pSignalMapper->setMapping(ui->pushButton_23, tr("D"));
    m_pSignalMapper->setMapping(ui->pushButton_24, tr("F"));
    m_pSignalMapper->setMapping(ui->pushButton_25, tr("G"));
    m_pSignalMapper->setMapping(ui->pushButton_26, tr("H"));
    m_pSignalMapper->setMapping(ui->pushButton_27, tr("J"));
    m_pSignalMapper->setMapping(ui->pushButton_28, tr("K"));
    m_pSignalMapper->setMapping(ui->pushButton_29, tr("L"));
    m_pSignalMapper->setMapping(ui->pushButton_30, tr("Z"));
    m_pSignalMapper->setMapping(ui->pushButton_31, tr("X"));
    m_pSignalMapper->setMapping(ui->pushButton_32, tr("C"));
    m_pSignalMapper->setMapping(ui->pushButton_33, tr("V"));
    m_pSignalMapper->setMapping(ui->pushButton_34, tr("B"));
    m_pSignalMapper->setMapping(ui->pushButton_35, tr("N"));
    m_pSignalMapper->setMapping(ui->pushButton_36, tr("M"));
    m_pSignalMapper->setMapping(ui->pushButton_37, tr("-"));
    m_pSignalMapper->setMapping(ui->pushButton_38, tr(","));
    m_pSignalMapper->setMapping(ui->pushButton_40, tr(" "));


    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(QString)), this, SLOT(onKeyboardClicked(QString)));

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

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

void KeyboardWidget::on_pushButton_41_clicked()
{
    QKeyEvent key(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
    QGuiApplication::sendEvent(QApplication::focusObject(),&key);
    emit onOkKeyClicked();
}

void KeyboardWidget::on_pushButton_42_clicked()
{
    emit onCancelKeyClicked();
}

void KeyboardWidget::on_pushButton_39_clicked()
{
    QKeyEvent key(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
    QGuiApplication::sendEvent(QApplication::focusObject(),&key);
    emit onBackspaceKeyClicked();
}

void KeyboardWidget::onKeyboardClicked(const QString in){
    QInputMethodEvent event;
    event.setCommitString(in);
    QGuiApplication::sendEvent(QApplication::focusObject(),&event);
    emit onKeyboardClickSignal(in);
}

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