washwindow.cpp 8.7 KB
#include "washwindow.h"
#include "ui_washwindow.h"

#include <QSignalMapper>
#include <QKeyEvent>

#include "soundplayer.h"
#include "dirtylevel.h"
#include "configwindow.h"
#include "mainwindow.h"

WashWindow::WashWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::WashWindow),
    selected(false),
    opened(false),
    started(false),
    run(false),
    canceled(false),
    type(0)
{
    ui->setupUi(this);

    ui->clockContainer->setParent(ui->upperStack);
    ui->progressContainer->setParent(ui->upperStack);
    setAttribute(Qt::WA_DeleteOnClose);

    udp = UdpHandler::getInstance();
    connect(udp, SIGNAL(changed()), SLOT(onChanged()));

    ui->animation->load(":/images/animation/wash_04.png");
    ui->closeDoorArrow->hide();

    QSignalMapper *sm = new QSignalMapper(this);
    connect(sm, SIGNAL(mapped(int)), SLOT(start(int)));

    sm->setMapping(ui->washButton_1, 1);
    sm->setMapping(ui->washButton_2, 2);
    sm->setMapping(ui->washButton_3, 3);
    sm->setMapping(ui->washButton_4, 4);
    sm->setMapping(ui->washButton_5, 5);

    connect(ui->washButton_1, SIGNAL(clicked()), sm, SLOT(map()));
    connect(ui->washButton_2, SIGNAL(clicked()), sm, SLOT(map()));
    connect(ui->washButton_3, SIGNAL(clicked()), sm, SLOT(map()));
    connect(ui->washButton_4, SIGNAL(clicked()), sm, SLOT(map()));
    connect(ui->washButton_5, SIGNAL(clicked()), sm, SLOT(map()));

    returnToClockTimer.setSingleShot(true);
    returnToClockTimer.setInterval(10 * 1000);
    connect(&returnToClockTimer, SIGNAL(timeout()), SLOT(returnToClock()));

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

    updateGauge();

    setFocus();
}

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

void WashWindow::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 WashWindow::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 WashWindow::start(int type)
{
    if (selected)
        return;

    if (type < 1 || type > 5)
        return;

    selected = true;

    this->type = type;

    returnToClockTimer.stop();

    ui->animation->clear();
    ui->animation->load(":/images/animation/pull_01.png");
    ui->animation->load(":/images/animation/pull_02.png");
    ui->animation->load(":/images/animation/pull_03.png");
    ui->animation->load(":/images/animation/pull_04.png");
    ui->animation->show();
    ui->animation->start(300);

    udp->set(TG_OVEN_MODE, 2);
}

void WashWindow::stop()
{
    if (!started)
        return;

    if (canceled)
        return;

    canceled = true;
    udp->turnOff(TG_CLEANING);
}

void WashWindow::returnToClock()
{
    ui->upperStack->setCurrentIndex(0);
}

void WashWindow::updateGauge()
{
    ui->dirtySlider->setValue(DirtyLevel::dirty());
    ui->stateSlider->setValue(DirtyLevel::state());
}

void WashWindow::onChanged()
{
    if (!selected)
        return;

    oven_state_t state;
    udp->fillData(state);

    if (!opened)
    {
        if (state.door_state)
        {
            opened = true;

            ui->animation->clear();

            ui->animation->load(":/images/animation/door_big_09.png");
            ui->animation->load(":/images/animation/door_big_08.png");
            ui->animation->load(":/images/animation/door_big_07.png");
            ui->animation->load(":/images/animation/door_big_06.png");
            ui->animation->load(":/images/animation/door_big_05.png");
            ui->animation->load(":/images/animation/door_big_04.png");
            ui->animation->load(":/images/animation/door_big_03.png");
            ui->animation->load(":/images/animation/door_big_02.png");
            ui->animation->load(":/images/animation/door_big_01.png");
            ui->closeDoorArrow->show();
        }
    }
    else if (!started)
    {
        if (!state.door_state)
        {
            started = true;

            SoundPlayer::playStart();

            ui->closeDoorArrow->hide();
            ui->animation->clear();

            ui->animation->load(":/images/animation/wash_01.png");
            ui->animation->load(":/images/animation/wash_02.png");
            ui->animation->load(":/images/animation/wash_03.png");
            ui->animation->load(":/images/animation/wash_04.png");
            ui->washStepGauge->setValue(0);
            ui->titleLabel->setText(tr("기기의 내부를 세척 중입니다"));
            ui->descLabel->setText(tr("완료될 때까지 문을 열지 마세요.\n기기의 내부의 자동 세척 기능을 실행 중입니다."));
            ui->washStepTypeLabel->setText("");
            ui->washStepCountLabel->setText("");

            ui->upperStack->setCurrentIndex(1);

            udp->set(TG_CLEAN_TYPE, type);
            udp->turnOn(TG_CLEANING);
        }
    }
    else if (state.cleaning_sate)
    {
        if (!run)
            run = true;

        oven_control_t control;
        udp->fillControl(control);

        if (control.clean_total != 0 && control.clean_step != 0 && control.clean_step_type != 0)
        {
            ui->washStepGauge->setMaximum(control.clean_total);
            ui->washStepGauge->setValue(control.clean_step);
            ui->washStepCountLabel->setText(QString().sprintf("%d/%d", control.clean_step, control.clean_total));

            switch (control.clean_step_type)
            {
            case 1:
                ui->washStepTypeLabel->setText(tr("내부 헹굼 진행 중입니다."));
                break;
            case 2:
                ui->washStepTypeLabel->setText(tr("스팀 급수 진행 중입니다."));
                break;
            case 3:
                ui->washStepTypeLabel->setText(tr("내부 팬 세척 진행 중입니다."));
                break;
            case 4:
                ui->washStepTypeLabel->setText(tr("내부 스팀 불림 진행 중입니다."));
                break;
            case 5:
                ui->washStepTypeLabel->setText(tr("내부 강 세척 진행 중입니다."));
                break;
            case 6:
                ui->washStepTypeLabel->setText(tr("내부 상부 세척 진행 중입니다."));
                break;
            case 7:
                ui->washStepTypeLabel->setText(tr("내부 스팀 세척 진행 중입니다."));
                break;
            case 8:
                ui->washStepTypeLabel->setText(tr("세척 종료 진행 중입니다."));
                break;
            case 9:
                ui->washStepTypeLabel->setText(tr("세제 세척수 만들기 진행 중입니다."));
                break;
            case 10:
                ui->washStepTypeLabel->setText(tr("세제 세척수 헹굼 진행 중입니다."));
                break;
            case 11:
                ui->washStepTypeLabel->setText(tr("하부 탱크 세척수 만들기 진행 중입니다."));
                break;
            }
        }
    }
    else if (canceled)
    {
        SoundPlayer::playStop();

        close();
    }
    else if (run)
    {
        SoundPlayer::playStop();
        DirtyLevel::wash(type);

        ui->titleLabel->setText(tr("세척이 종료되었습니다"));
        ui->descLabel->setText("");
        ui->washStepTypeLabel->setText("");
        ui->washStepCountLabel->setText("");

        ui->animation->stop();
        ui->animation->clear();
        ui->animation->load(":/images/animation/wash_04.png");

        returnToClockTimer.start();

        selected = false;
        opened = false;
        started = false;
        run = false;

        updateGauge();
    }
}

void WashWindow::on_backButton_clicked()
{
    if (started)
        stop();
    else
        close();
}

void WashWindow::on_configButton_clicked()
{
    if (started)
        stop();
    else
    {
        ConfigWindow *w = new ConfigWindow(MainWindow::getInstance());
        w->setWindowModality(Qt::WindowModal);
        w->showFullScreen();
        w->raise();

        MainWindow::jump(w);
    }
}

void WashWindow::on_helpButton_clicked()
{

}

void WashWindow::onEncoderLeft()
{
    focusPreviousChild();
}

void WashWindow::onEncoderRight()
{
    focusNextChild();
}

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