autocookcheckconfigwindow.cpp 6.57 KB
#include "autocookcheckconfigwindow.h"
#include "ui_autocookcheckconfigwindow.h"

#include <QKeyEvent>

#include "soundplayer.h"
#include "stringer.h"

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

    if (!this->cook.isLoaded())
        this->cook.load();

    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);
    afterThreeSecsTimer.start();
    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);

    setFocus();
}

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

bool AutoCookCheckConfigWindow::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 AutoCookCheckConfigWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        onEncoderLeft();
        break;
    case 0x01000031:    // Push
        pushed = focusWidget();
        break;
    case 0x01000030:    // Turn right
        onEncoderRight();
        break;
    }
}

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

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

void AutoCookCheckConfigWindow::setupUi()
{
    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->show();
            cw.minimum->show();
            cw.maximum->show();
            cw.current->show();
            cw.slider->show();

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

            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;

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

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

void AutoCookCheckConfigWindow::onEncoderClicked(QWidget *clicked)
{
    QPushButton *pb = qobject_cast<QPushButton *>(clicked);
    if (pb)
        pb->click();
}

void AutoCookCheckConfigWindow::afterThreeSecs()
{
    close();
}

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