gastestwindow.cpp 1.69 KB
#include "gastestwindow.h"
#include "ui_gastestwindow.h"

#include <QKeyEvent>

#include "soundplayer.h"

GasTestWindow::GasTestWindow(QWidget *parent, UdpHandler *udp) :
    QMainWindow(parent),
    ui(new Ui::GasTestWindow), udp(udp)
{
    ui->setupUi(this);

    ui->clockContainer->setParent(ui->upperStack);
    setAttribute(Qt::WA_DeleteOnClose);

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

    connect(udp, SIGNAL(changed()), this, SLOT(onDataChanged()));
}

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

void GasTestWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        onEncoderLeft();
        break;
    case 0x01000031:    // Push
        pushed = focusWidget();
        break;
    case 0x01000030:    // Turn right
        onEncoderRight();
        break;
    }
}

void GasTestWindow::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        onEncoderLeft();
        break;
    case 0x01000031:    // Push
        if (focusWidget() == pushed)
            onEncoderClicked(pushed);

        pushed = NULL;
        break;
    case 0x01000030:    // Turn right
        onEncoderRight();
        break;
    }
}

void GasTestWindow::onDataChanged()
{

}

void GasTestWindow::on_backButton_clicked()
{
    close();
}

void GasTestWindow::onEncoderLeft()
{
    focusPreviousChild();
}

void GasTestWindow::onEncoderRight()
{
    focusNextChild();
}

void GasTestWindow::onEncoderClicked(QWidget *clicked)
{
    QPushButton *b = qobject_cast<QPushButton *>(clicked);
    if (b)
        b->click();
}