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

#include "autocookwindow.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;
            }

//            cw.slider->style()->unpolish(cw.slider);
//            cw.slider->style()->polish(cw.slider);
//            cw.slider->update();

            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:
        {
            int time = cook.time();
            if (time >= 3600)
                cw.current->setText(QString().sprintf(
                        "%d"
                        "<span style=\"font-size:11pt;\">시간</span>"
                        " %02d"
                        "<span style=\"font-size:11pt;\">분</span>",
                        time / 3600,
                        (time % 3600) / 60));
            else if (time >= 60)
                cw.current->setText(QString().sprintf(
                        "%d"
                        "<span style=\"font-size:11pt;\">분</span>"
                        " %02d"
                        "<span style=\"font-size:11pt;\">초</span>",
                        time / 60,
                        time % 60));
            else
                cw.current->setText(QString().sprintf(
                        "%d"
                        "<span style=\"font-size:11pt;\">초</span>",
                        time));
            break;
        }
        case Define::BurnDegree:
            cw.current->setText(QString().sprintf(
                    "%d"
                    "<span style=\"font-size:11pt;\">℃</span>",
                    cook.coreTemp()));
            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::on_backButton_clicked()
{
    close();
}