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

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

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

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

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

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

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

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

    updateView();

    cooldownStartTimer.start();

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

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

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

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

        if (oven->humidification())
            ui->humidificationButton->setStyleSheet("background-image: url(:/images/cooldown/side_nozzle_ov.png);");
        else
            ui->humidificationButton->setStyleSheet("background-image: url(:/images/cooldown/side_nozzle.png);");
    }

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