cookpanelbutton.cpp 4.21 KB
#include "cookpanelbutton.h"
#include "ui_cookpanelbutton.h"

#include <QKeyEvent>

#include "soundplayer.h"
#include "manualcooksettingwidget.h"
#include "autocookwindow.h"
#include "manualcookwindow.h"

CookPanelButton::CookPanelButton(CookRecord record, QWidget *parent) :
    QWidget(parent),
    record(record),
    ui(new Ui::CookPanelButton),
    rendered(false),
    longPressEnabled(false),
    emitted(false)
{
    ui->setupUi(this);

    setText(record.name);

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

    longPressedTimer.setSingleShot(true);
    longPressedTimer.setInterval(3000);
    connect(&longPressedTimer, SIGNAL(timeout()), SLOT(emitLongPressed()));
}

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

void CookPanelButton::setText(QString text)
{
    ui->pushButton->setText(text);
}

void CookPanelButton::showInfo()
{
    if (!rendered)
    {
        QPixmap p = CookHistory::render(record);

        label = new QLabel(this);
        label->setPixmap(p);
        label->setGeometry((width() - p.width()) / 2, 65, p.width(), p.height());
    }

    label->show();
    setMinimumHeight(ui->pushButton->height() + label->height());
}

void CookPanelButton::hideInfo()
{
    label->hide();
    setMinimumHeight(ui->pushButton->height());
}

void CookPanelButton::focusBar()
{
    ui->pushButton->setFocus();
}

void CookPanelButton::focusInfoButton()
{
    ui->showInfoButton->setFocus();
}

void CookPanelButton::focusDeleteButton()
{
    ui->deleteButton->setFocus();
}

void CookPanelButton::setLongPressEnabled(bool enabled)
{
    longPressEnabled = enabled;
}

QPushButton *CookPanelButton::bar()
{
    return ui->pushButton;
}

QPushButton *CookPanelButton::infoButton()
{
    return ui->showInfoButton;
}

QPushButton *CookPanelButton::deleteButton()
{
    return ui->deleteButton;
}

void CookPanelButton::setEnabled(bool enabled)
{
    ui->pushButton->setEnabled(enabled);
    ui->showInfoButton->setEnabled(enabled);
    ui->deleteButton->setEnabled(enabled);
}

void CookPanelButton::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        event->ignore();
        break;
    case 0x01000031:    // Push
        pushed = focusWidget();
        if (pushed == ui->pushButton)
            on_pushButton_pressed();
        break;
    case 0x01000030:    // Turn right
        event->ignore();
        break;
    }
}

void CookPanelButton::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        event->ignore();
        break;
    case 0x01000031:    // Push
        if (pushed == ui->pushButton)
            on_pushButton_released();

        if (focusWidget() == pushed)
            onEncoderClicked(pushed);

        pushed = NULL;
        break;
    case 0x01000030:    // Turn right
        event->ignore();
        break;
    }
}

void CookPanelButton::emitLongPressed()
{
    emitted = true;
    emit longPressed(this);
}


void CookPanelButton::on_showInfoButton_clicked()
{
    emit infoClicked(this);
}

void CookPanelButton::on_pushButton_clicked()
{
    if (longPressEnabled && emitted)
        return;

    QMainWindow *w = CookHistory::start(record, parentWidget());

    if (next == NULL)
        return;

    AutoCookWindow *a = qobject_cast<AutoCookWindow *>(w);
    if (a)
    {
        connect(a, SIGNAL(done()), next, SLOT(on_pushButton_clicked()));
        connect(a, SIGNAL(done()), a, SLOT(deleteLater()));
    }

    ManualCookWindow *m = qobject_cast<ManualCookWindow *>(w);
    if (m)
    {
        connect(m, SIGNAL(done()), next, SLOT(on_pushButton_clicked()));
        connect(m, SIGNAL(done()), m, SLOT(deleteLater()));
    }
}

void CookPanelButton::on_deleteButton_clicked()
{
    emit deleteClicked(this);
}

void CookPanelButton::onEncoderLeft()
{

}

void CookPanelButton::onEncoderRight()
{

}

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

void CookPanelButton::on_pushButton_pressed()
{
    longPressedTimer.start();
    emitted = false;
}

void CookPanelButton::on_pushButton_released()
{
    longPressedTimer.stop();
}