autocooksettingwidget.cpp 4.46 KB
#include "autocooksettingwidget.h"
#include "ui_autocooksettingwidget.h"

#include "stringer.h"

AutoCookSettingWidget::AutoCookSettingWidget(AutoCookSetting setting, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AutoCookSettingWidget)
{
    ui->setupUi(this);

    Cook cook(setting.type, setting.root, setting.name);
    cook.setConfig(setting.configs[0],
                   setting.configs[1],
                   setting.configs[2],
                   setting.configs[3],
                   setting.configs[4]);

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

    setupUi(cook);
}

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

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

    QString styleSheet("\
QPushButton { image: url(%1); }\
QPushButton:pressed,\
QPushButton:focus { image: url(%2); }\
QPushButton:checked { image: url(%3); }");

    for (int idx = 0; idx < 5; idx++)
    {
        ConfigWidget cw = configWidgets.at(idx);

        CookConfig config = cook.configs[idx];
        if (config.type == Define::ConfigNotUsed)
        {
            cw.button->hide();
            cw.minimum->hide();
            cw.maximum->hide();
            cw.current->hide();
            cw.slider->hide();
        }
        else
        {
            cw.button->setStyleSheet(styleSheet
                                     .arg(Define::icon(config.type))
                                     .arg(Define::iconOverlay(config.type))
                                     .arg(Define::iconActiveted(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;
            }

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