programmingautoconfigwindow.cpp 9.53 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();

    afterThreeSecsTimer.setSingleShot(true);
    afterThreeSecsTimer.setInterval(3000);
    connect(&afterThreeSecsTimer, SIGNAL(timeout()), SLOT(afterThreeSecs()));

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

    foreach (QWidget *w, findChildren<QWidget *>())
        w->installEventFilter(this);

    installEventFilter(this);

    setFocus();
}

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

bool ProgrammingAutoConfigWindow::eventFilter(QObject */*watched*/, QEvent *event)
{
    switch (event->type())
    {
    case QEvent::KeyPress:
    case QEvent::KeyRelease:
    case QEvent::MouseButtonPress:
    case QEvent::MouseButtonRelease:
    case QEvent::MouseMove:
        afterThreeSecsTimer.start();
        break;
    default:
        break;
    }

    return false;
}

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()
{
    focusPreviousChild();
}

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

void ProgrammingAutoConfigWindow::onEncoderClicked(QWidget *clicked)
{
    if (clicked == NULL)
        return;

    if (clicked->inherits("QPushButton"))
    {
        QPushButton *pb = qobject_cast<QPushButton *>(clicked);
        if (pb)
            pb->click();
    }
    else if (clicked->inherits("Slider"))
    {
        Slider *slider = qobject_cast<Slider *>(clicked);
        if (slider)
        {
            if (slider == ui->configSlider_1)
                ui->configButton_1->setFocus();
            else if (slider == ui->configSlider_2)
                ui->configButton_2->setFocus();
            else if (slider == ui->configSlider_3)
                ui->configButton_3->setFocus();
            else if (slider == ui->configSlider_4)
                ui->configButton_4->setFocus();
            else if (slider == ui->configSlider_5)
                ui->configButton_5->setFocus();
        }
    }
}

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, QPushButton:focus { 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::afterThreeSecs()
{
    Slider *slider = qobject_cast<Slider *>(focusWidget());
    if (slider)
    {
        if (slider == ui->configSlider_1)
            ui->configButton_1->setFocus();
        else if (slider == ui->configSlider_2)
            ui->configButton_2->setFocus();
        else if (slider == ui->configSlider_3)
            ui->configButton_3->setFocus();
        else if (slider == ui->configSlider_4)
            ui->configButton_4->setFocus();
        else if (slider == ui->configSlider_5)
            ui->configButton_5->setFocus();
    }
}

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

void ProgrammingAutoConfigWindow::on_configButton_1_clicked()
{
    ui->configSlider_1->setFocus();
}

void ProgrammingAutoConfigWindow::on_configButton_2_clicked()
{
    ui->configSlider_2->setFocus();
}

void ProgrammingAutoConfigWindow::on_configButton_3_clicked()
{
    ui->configSlider_3->setFocus();
}

void ProgrammingAutoConfigWindow::on_configButton_4_clicked()
{
    ui->configSlider_4->setFocus();
}

void ProgrammingAutoConfigWindow::on_configButton_5_clicked()
{
    ui->configSlider_5->setFocus();
}