autocookconfigwindow.cpp 6.45 KB
#include "autocookconfigwindow.h"
#include "ui_autocookconfigwindow.h"

#include "autocookwindow.h"
#include "confirmpopup.h"
#include "stringer.h"
#include "favoritenamepopup.h"

AutoCookConfigWindow::AutoCookConfigWindow(QWidget *parent, Cook cook) :
    QMainWindow(parent),
    ui(new Ui::AutoCookConfigWindow),
    cook(cook)
{
    ui->setupUi(this);

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

    configWidgets.append(
                ConfigWidget {
                    ui->configButton_1,
                    ui->configMinLabel_1,
                    ui->configMaxLabel_1,
                    ui->configCurrentLabel_1,
                    ui->configSlider_1
                });
    configWidgets.append(
                ConfigWidget {
                    ui->configButton_2,
                    ui->configMinLabel_2,
                    ui->configMaxLabel_2,
                    ui->configCurrentLabel_2,
                    ui->configSlider_2
                });
    configWidgets.append(
                ConfigWidget {
                    ui->configButton_3,
                    ui->configMinLabel_3,
                    ui->configMaxLabel_3,
                    ui->configCurrentLabel_3,
                    ui->configSlider_3
                });
    configWidgets.append(
                ConfigWidget {
                    ui->configButton_4,
                    ui->configMinLabel_4,
                    ui->configMaxLabel_4,
                    ui->configCurrentLabel_4,
                    ui->configSlider_4
                });
    configWidgets.append(
                ConfigWidget {
                    ui->configButton_5,
                    ui->configMinLabel_5,
                    ui->configMaxLabel_5,
                    ui->configCurrentLabel_5,
                    ui->configSlider_5
                });

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

    foreach (ConfigWidget w, configWidgets)
    {
        connect(w.button, SIGNAL(pressed()), SLOT(stopTimer()));
        connect(w.button, SIGNAL(released()), SLOT(startTimer()));
        connect(w.slider, SIGNAL(sliderPressed()), SLOT(stopTimer()));
        connect(w.slider, SIGNAL(sliderReleased()), SLOT(startTimer()));
    }

    setupUi();

    startTimer();
}

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

void AutoCookConfigWindow::setupUi()
{
    ui->cookTypeIcon->setPixmap(Define::icon(cook.type));
    ui->selectCookButton->setText(cook.name);

    for (int idx = 0; idx < 5; idx++)
    {
        CookConfig config = cook.configs[idx];
        if (config.type == Define::ConfigNotUsed)
        {
            ConfigWidget cw = configWidgets.at(idx);
            cw.button->hide();
            cw.minimum->hide();
            cw.maximum->hide();
            cw.current->hide();
            cw.slider->hide();
        }
        else
        {
            ConfigWidget cw = configWidgets.at(idx);
            cw.button->setStyleSheet(
                        "QPushButton { image: url("
                         + Define::icon(config.type)
                         + ") } QPushButton::pressed { image: url("
                         + Define::iconOverlay(config.type)
                         + ") }");

            cw.minimum->setText(Define::minimum(config.type));
            cw.maximum->setText(Define::maximum(config.type));
            cw.slider->blockSignals(true);
            cw.slider->setMinimum(1);
            cw.slider->setMaximum(config.maximum);
            cw.slider->setValue(config.current);
            cw.slider->blockSignals(false);

            switch (config.type)
            {
            case Define::Time:
                cw.slider->setProperty("sliderColor", "white");
                break;
            case Define::BurnDegree:
                cw.slider->setProperty("sliderColor", "yellow");
                break;
            case Define::Brightness:
                cw.slider->setProperty("sliderColor", "red");
                break;
            default:
                cw.slider->setProperty("sliderColor", "blue");
                break;
            }

            connect(cw.slider, SIGNAL(valueChanged(int)), SLOT(updateConfig()));
        }
    }

    updateView();
}

void AutoCookConfigWindow::updateView()
{
    for (int idx = 0; idx < 5; idx++)
    {
        CookConfig config = cook.configs[idx];
        if (config.type == Define::ConfigNotUsed)
            continue;

        ConfigWidget cw = configWidgets.at(idx);

        switch (config.type)
        {
        case Define::Time:
            cw.current->setText(Stringer::remainingTime(cook.time() * 1000, Stringer::fontSize14));
            break;
        case Define::BurnDegree:
            cw.current->setText(Stringer::temperature(cook.coreTemp(), Stringer::fontSize14));
            break;
        default:
            cw.current->setText(QString().sprintf(
                    "%d"
                    "<span style=\"font-size:11pt;\">/%d</span>",
                    config.current, config.maximum));
            break;
        }
    }
}

void AutoCookConfigWindow::updateConfig()
{
    cook.setConfig(ui->configSlider_1->value(),
                   ui->configSlider_2->value(),
                   ui->configSlider_3->value(),
                   ui->configSlider_4->value(),
                   ui->configSlider_5->value());

    updateView();
}

void AutoCookConfigWindow::startTimer()
{
    cookStartTimer.start();
}

void AutoCookConfigWindow::stopTimer()
{
    cookStartTimer.stop();
}

void AutoCookConfigWindow::start()
{
    close();

    AutoCookWindow *w = new AutoCookWindow(parentWidget(), cook);
    w->setWindowModality(Qt::WindowModal);
    w->showFullScreen();
    w->raise();
}

void AutoCookConfigWindow::addFavorite()
{
    AutoCookSetting s;
    s.type = cook.type;
    s.root = cook.root;
    for (int i = 0; i < 5; i++)
        s.configs[i] = cook.configs[i].current;

    FavoriteNamePopup *p = new FavoriteNamePopup(this, s);
    p->showFullScreen();
}

void AutoCookConfigWindow::on_backButton_clicked()
{
    close();
}

void AutoCookConfigWindow::on_favoritesButton_clicked()
{
    ConfirmPopup *p = new ConfirmPopup(this, tr("즐겨찾기 항목에 추가하시겠습니까?"));
    p->showFullScreen();

    connect(p, SIGNAL(accepted()), SLOT(addFavorite()));

    if (cookStartTimer.isActive())
    {
        cookStartTimer.stop();
        connect(p, SIGNAL(rejected()), &cookStartTimer, SLOT(start()));
    }
}