programmingautoconfigwindow.cpp 6.89 KB
#include "programmingautoconfigwindow.h"
#include "ui_programmingautoconfigwindow.h"

#include <QKeyEvent>

#include "soundplayer.h"
#include "stringer.h"
#include "cookprogram.h"
#include "configwindow.h"
#include "mainwindow.h"

ProgrammingAutoConfigWindow::ProgrammingAutoConfigWindow(QWidget *parent, Cook cook) :
    QMainWindow(parent),
    ui(new Ui::ProgrammingAutoConfigWindow),
    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
                });

    setupUi();

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

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

void ProgrammingAutoConfigWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000030:    // Turn left
        onEncoderLeft();
        break;
    case 0x01000031:    // Push
        pushed = focusWidget();
        break;
    case 0x01000032:    // Turn right
        onEncoderRight();
        break;
    }
}

void ProgrammingAutoConfigWindow::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000030:    // Turn left
        onEncoderLeft();
        break;
    case 0x01000031:    // Push
        if (focusWidget() == pushed)
            onEncoderClicked(pushed);

        pushed = NULL;
        break;
    case 0x01000032:    // Turn right
        onEncoderRight();
        break;
    }
}

void ProgrammingAutoConfigWindow::onEncoderLeft()
{

}

void ProgrammingAutoConfigWindow::onEncoderRight()
{

}

void ProgrammingAutoConfigWindow::onEncoderClicked(QWidget *clicked)
{

}

void ProgrammingAutoConfigWindow::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));

            switch (config.type)
            {
            case Define::Time:
                cw.slider->setSubPixmap(":/images/slider/sub_white.png");
                break;
            case Define::BurnDegree:
                cw.slider->setSubPixmap(":/images/slider/sub_yellow.png");
                break;
            case Define::Brightness:
                cw.slider->setSubPixmap(":/images/slider/sub_red.png");
                break;
            default:
                cw.slider->setSubPixmap(":/images/slider/sub_blue.png");
                break;
            }

            cw.slider->blockSignals(true);
            cw.slider->setMinimum(1);
            cw.slider->setMaximum(config.maximum);
            cw.slider->setValue(config.current);
            cw.slider->blockSignals(false);
            cw.slider->bigTickInterval = 1;

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

    updateView();
}

void ProgrammingAutoConfigWindow::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 ProgrammingAutoConfigWindow::updateConfig()
{
    cook.setConfig(ui->configSlider_1->sliderPosition(),
                   ui->configSlider_2->sliderPosition(),
                   ui->configSlider_3->sliderPosition(),
                   ui->configSlider_4->sliderPosition(),
                   ui->configSlider_5->sliderPosition());

    updateView();
}

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

void ProgrammingAutoConfigWindow::on_configButton_clicked()
{
    ConfigWindow *w = new ConfigWindow(MainWindow::getInstance());
    w->setWindowModality(Qt::WindowModal);
    w->showFullScreen();
    w->raise();

    MainWindow::jump(w);
}

void ProgrammingAutoConfigWindow::on_helpButton_clicked()
{

}

void ProgrammingAutoConfigWindow::on_okButton_clicked()
{
    if (!cook.isLoaded())
        cook.load();

    AutoCookSetting s;
    s.type = cook.type;
    s.name = cook.name;
    s.root = cook.root;
    for (int i = 0; i < 5; i++)
        s.configs[i] = cook.configs[i].current;

    CookProgram::add(s);

    emit added();
    close();
}