manualcookfinishpopup.cpp 2.25 KB
#include "manualcookfinishpopup.h"
#include "ui_manualcookfinishpopup.h"

#include <QKeyEvent>

#include "soundplayer.h"

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

    setAttribute(Qt::WA_DeleteOnClose);

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

    ui->warmupButton->setEnabled(keepWarmStartable);

    ui->background->setFocus();
}

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

void ManualCookFinishPopup::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 ManualCookFinishPopup::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 ManualCookFinishPopup::onEncoderLeft()
{
    QWidget *focused = focusWidget();
    if (focused == ui->background)
        ui->okButton->setFocus();
    else
        focusPreviousChild();
}

void ManualCookFinishPopup::onEncoderRight()
{
    if (focusWidget() == ui->okButton)
        ui->background->setFocus();
    else
        focusNextChild();
}

void ManualCookFinishPopup::onEncoderClicked(QWidget *clicked)
{
    if (clicked == ui->background)
    {
        close();
        return;
    }

    QPushButton *b = qobject_cast<QPushButton *>(clicked);
    if (b)
    {
        b->click();
        return;
    }
}

void ManualCookFinishPopup::on_closeButton_clicked()
{
    close();
}

void ManualCookFinishPopup::on_closeButton_2_clicked()
{
    close();
}

void ManualCookFinishPopup::on_warmupButton_clicked()
{
    close();

    emit keepWarm();
}

void ManualCookFinishPopup::on_okButton_clicked()
{
    close();
}