cooldownpopup.cpp 7.86 KB
#include "cooldownpopup.h"
#include "ui_cooldownpopup.h"

#include <QKeyEvent>

#include "soundplayer.h"
#include "stringer.h"

CooldownPopup::CooldownPopup(QWidget *parent, int target) :
    QWidget(parent),
    ui(new Ui::CooldownPopup),
    showingCurrentTemp(false),
    needCookStarting(false)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_DeleteOnClose);

    oven = Oven::getInstance();

    lastDisplayedFanLevel = -1;
    lastDisplayedHumidification = !oven->humidification();

    ui->openDoorAnimation->load(":/images/animation/door_big_01.png");
    ui->openDoorAnimation->load(":/images/animation/door_big_02.png");
    ui->openDoorAnimation->load(":/images/animation/door_big_03.png");
    ui->openDoorAnimation->load(":/images/animation/door_big_04.png");
    ui->openDoorAnimation->load(":/images/animation/door_big_05.png");
    ui->openDoorAnimation->load(":/images/animation/door_big_06.png");
    ui->openDoorAnimation->load(":/images/animation/door_big_07.png");
    ui->openDoorAnimation->load(":/images/animation/door_big_08.png");
    ui->openDoorAnimation->load(":/images/animation/door_big_09.png");
    ui->openDoorAnimation->start(300);

    ui->tempSlider->setSubPixmap(":/images/slider/sub_red.png");
    ui->tempSlider->setRange(30, 300);
    ui->tempSlider->setValue(target);

    cookingFanLevel = oven->fan();
    expectingFanLevel = oven->maxFan();
    started = false;
    opened = false;
    needCookStarting = oven->cooking();
    if (needCookStarting)
        oven->stopCooking();

    oven->setFan(expectingFanLevel);

    connect(oven, SIGNAL(changed(Oven*)), SLOT(updateView()));

    cooldownStartTimer.setSingleShot(true);
    cooldownStartTimer.setInterval(3000);
    connect(&cooldownStartTimer, SIGNAL(timeout()), SLOT(start()));

    showCurrentTempTimer.setSingleShot(true);
    showCurrentTempTimer.setInterval(1000);
    connect(&showCurrentTempTimer, SIGNAL(timeout()), SLOT(showCurrentTemp()));

    checkOvenTimer.setInterval(100);
    connect(&checkOvenTimer, SIGNAL(timeout()), SLOT(checkOven()));

    connect(ui->tempSlider, SIGNAL(sliderMoved(int)), SLOT(updateView()));

    updateView();

    cooldownStartTimer.start();

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

    focusTempButtonTimer.setSingleShot(true);
    focusTempButtonTimer.setInterval(3000);
    connect(&focusTempButtonTimer, SIGNAL(timeout()), SLOT(focusTempButton()));

    connect(ui->tempSlider, SIGNAL(sliderMoved(int)), &focusTempButtonTimer, SLOT(start()));

    ui->background->setFocus();
}

CooldownPopup::~CooldownPopup()
{
    if (needCookStarting)
        oven->startCooking();

    delete ui;
}

void CooldownPopup::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 CooldownPopup::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 CooldownPopup::start()
{
    started = true;
    opened = false;

    oven->setFan(expectingFanLevel);
    oven->startCooldown();

    checkOvenTimer.start();

    updateView();
}

void CooldownPopup::stop()
{
    started = false;

    oven->stopCooldown();
    oven->setFan(cookingFanLevel);

    close();
}

void CooldownPopup::showCurrentTemp()
{
    showingCurrentTemp = true;
    updateView();
}

void CooldownPopup::updateView()
{
    QWidget *focused = focusWidget();
    ui->tempButton->setChecked(focused == ui->tempSlider);

    int temp;
    if (showingCurrentTemp)
        temp = oven->currentTemp();
    else if (ui->tempSlider->isSliderDown() || focusWidget() == ui->tempSlider)
        temp = ui->tempSlider->sliderPosition();
    else
        temp = ui->tempSlider->value();

    if (ui->tempSlider->sliderPosition() != temp)
    {
        ui->tempSlider->blockSignals(true);
        ui->tempSlider->setSliderPosition(temp);
        ui->tempSlider->blockSignals(false);
    }

    ui->tempCurrentLabel->setText(Stringer::temperature(temp, Stringer::fontSize14));

    if (lastDisplayedFanLevel != expectingFanLevel)
    {
        lastDisplayedFanLevel = expectingFanLevel;

        switch (expectingFanLevel)
        {
        case 1:
            ui->fanButton->setStyleSheet("background-image: url(:/images/cooldown/fan1.png);");
            break;
        case 2:
            ui->fanButton->setStyleSheet("background-image: url(:/images/cooldown/fan2.png);");
            break;
        case 3:
            ui->fanButton->setStyleSheet("background-image: url(:/images/cooldown/fan3.png);");
            break;
        case 4:
            ui->fanButton->setStyleSheet("background-image: url(:/images/cooldown/fan4.png);");
            break;
        case 5:
            ui->fanButton->setStyleSheet("background-image: url(:/images/cooldown/fan5.png);");
            break;
        }
    }

    if (lastDisplayedHumidification != oven->humidification())
    {
        lastDisplayedHumidification = oven->humidification();
        ui->humidificationButton->setChecked(oven->humidification());
    }

    if (started && !oven->door())
        ui->openDoorWidget->show();
    else
        ui->openDoorWidget->hide();
}

void CooldownPopup::checkOven()
{
    updateView();

    if (!started)
        return;

    if (!opened)
    {
        if (oven->door())
        {
            opened = true;
        }
    }
    else
    {
        if (oven->currentTemp() <= ui->tempSlider->value())
        {
            stop();
        }
    }
}

void CooldownPopup::on_closeButton_clicked()
{
    stop();
    close();
}

void CooldownPopup::on_closeButton_2_clicked()
{
    stop();
    close();
}

void CooldownPopup::on_tempButton_pressed()
{
    showCurrentTempTimer.start();
}

void CooldownPopup::on_tempButton_released()
{
    if (showCurrentTempTimer.isActive())
        showCurrentTempTimer.stop();

    if (showingCurrentTemp)
    {
        showingCurrentTemp = false;
        updateView();
    }
}

void CooldownPopup::on_runButton_clicked()
{
    if (cooldownStartTimer.isActive())
    {
        cooldownStartTimer.stop();
        start();
    }
}

void CooldownPopup::on_fanButton_clicked()
{
    expectingFanLevel--;
    if (expectingFanLevel < oven->minFan())
        expectingFanLevel = oven->maxFan();

    oven->setFan(expectingFanLevel);

    if (cooldownStartTimer.isActive())
        cooldownStartTimer.start();
}

void CooldownPopup::on_humidificationButton_clicked()
{
    if (oven->humidification())
        oven->stopHumidification();
    else
        oven->startHumidification();
}

void CooldownPopup::on_tempButton_clicked()
{
    ui->tempSlider->setFocus();
    focusTempButtonTimer.start();
}

void CooldownPopup::focusTempButton()
{
    if (focusWidget() == ui->tempSlider)
        ui->tempButton->setFocus();
}

void CooldownPopup::onEncoderLeft()
{
    QWidget *focused = focusWidget();
    if (focused == ui->background)
        ui->humidificationButton->setFocus();
    else
        focusPreviousChild();
}

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

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

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

    Slider *slider = qobject_cast<Slider *>(clicked);
    if (slider)
    {
        ui->tempButton->setFocus();
    }
}