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

#include <QKeyEvent>

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

CooldownPopup::CooldownPopup(QWidget *parent, Oven *oven) :
    QWidget(parent),
    ui(new Ui::CooldownPopup),
    oven(oven),
    showingCurrentTemp(false)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_DeleteOnClose);

    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);

    cookingFanLevel = oven->fan();
    expectingFanLevel = oven->maxFan();
    started = false;
    opened = false;

    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()
{
    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;

    checkOvenTimer.start();

    updateView();
}

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

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

    close();
}

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

void CooldownPopup::updateView()
{
    int temp;
    if (showingCurrentTemp)
        temp = oven->currentTemp();
    else
        temp = ui->tempSlider->sliderPosition();

    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;
            oven->setFan(expectingFanLevel);
            oven->startCooldown();
        }
    }
    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();

    if (oven->cooldown())
        oven->setFan(expectingFanLevel);
    else
        updateView();

    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();
    else if (clicked->inherits("QPushButton"))
    {
        QPushButton *b = qobject_cast<QPushButton *>(clicked);
        if (b)
            b->click();
    }
    else if (clicked->inherits("Slider"))
    {
        Slider *slider = qobject_cast<Slider *>(clicked);
        if (slider == ui->tempSlider)
            ui->tempButton->setFocus();
    }
}