programmingmanualwindow.cpp 9.87 KB
#include "programmingmanualwindow.h"
#include "ui_programmingmanualwindow.h"

#include <QKeyEvent>

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

ProgrammingManualWindow::ProgrammingManualWindow(QWidget *parent, Define::Mode mode) :
    QMainWindow(parent),
    ui(new Ui::ProgrammingManualWindow)
{
    ui->setupUi(this);

    ui->clockContainer->setParent(ui->upperStack);
    setAttribute(Qt::WA_DeleteOnClose);

    ui->humiditySlider->setSubPixmap(":/images/slider/humidity.png");
    ui->humiditySlider->setRange(0, 100);
    ui->humiditySlider->bigTickInterval = 50;
    ui->humiditySlider->tickInterval = 10;

    ui->tempSlider->setSubPixmap(":/images/slider/temp.png");
    ui->tempSlider->bigTickInterval = 50;
    ui->tempSlider->tickInterval = 10;

    ui->timeSlider->setSubPixmap(":/images/slider/time.png");
    ui->timeSlider->setRange(0, 342);
    ui->timeSlider->bigTicks.append(0);
    ui->timeSlider->bigTicks.append(180);
    ui->timeSlider->bigTicks.append(270);
    ui->timeSlider->bigTicks.append(342);
    ui->timeSlider->ticks.append(60);
    ui->timeSlider->ticks.append(120);
    ui->timeSlider->ticks.append(180 + 30);
    ui->timeSlider->ticks.append(180 + 60);
    ui->timeSlider->ticks.append(270 + 4 * 6);
    ui->timeSlider->ticks.append(270 + 4 * 12);

    ui->interTempSlider->setSubPixmap(":/images/slider/core.png");
    ui->interTempSlider->setRange(30, 99);

    Oven *oven = Oven::getInstance();

    QList<int> &bigTicks = ui->interTempSlider->bigTicks;
    bigTicks.append(oven->minInterTemp());
    bigTicks.append(oven->minInterTemp() + (oven->maxInterTemp() - oven->minInterTemp()) / 2);
    bigTicks.append(oven->maxInterTemp());

    QList<int> &ticks = ui->interTempSlider->ticks;
    for (int i = 1; i < 3; i++)
        ticks.append(bigTicks.at(0) + i * (bigTicks.at(1) - bigTicks.at(0)) / 3);
    for (int i = 1; i < 3; i++)
        ticks.append(bigTicks.at(1) + i * (bigTicks.at(2) - bigTicks.at(1)) / 3);

    connect(ui->humiditySlider, SIGNAL(sliderMoved(int)), SLOT(updateHumidityLabel()));
    connect(ui->tempSlider, SIGNAL(sliderMoved(int)), SLOT(updateTempLabel()));
    connect(ui->timeSlider, SIGNAL(sliderMoved(int)), SLOT(updateTimeLabel()));
    connect(ui->interTempSlider, SIGNAL(sliderMoved(int)), SLOT(updateCoreTempLabel()));

    setDefault(mode);

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

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

void ProgrammingManualWindow::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 ProgrammingManualWindow::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;
    }
}

int ProgrammingManualWindow::sliderToTime(int value)
{
    if (value <= 180)
        return value * 60;
    if (value <= 270)
        return 180 * 60 + (value - 180) * 2 * 60;
    return 360 * 60 + (value - 270) * 15 * 60;
}

int ProgrammingManualWindow::timeToSlider(int secs)
{
    if (secs <= 180 * 60)
        return secs / 60;
    if (secs <= 360 * 60)
        return 180 + (secs - 180 * 60) / 2 / 60;
    return 270 + (secs - 360 * 60) / 15 / 60;
}

void ProgrammingManualWindow::onEncoderLeft()
{

}

void ProgrammingManualWindow::onEncoderRight()
{

}

void ProgrammingManualWindow::onEncoderClicked(QWidget *clicked)
{

}

void ProgrammingManualWindow::setDefault(Define::Mode mode)
{
    switch (mode)
    {
    case Define::SteamMode:
        ui->steamButton->setChecked(true);
        ui->humiditySlider->setEnabled(false);
        ui->humiditySlider->setValue(100);
        ui->tempSlider->setRange(30, 130);
        ui->tempSlider->setValue(100);
        ui->timeSlider->setValue(0);
        ui->interTempSlider->setEnabled(false);
        ui->interTempSlider->setSubVisible(false);
        ui->interTempSlider->setValue(30);
        setFan(4);
        updateCoreTempButton();
        updateCoreTempLabel();
        this->mode = mode;
        break;
    case Define::CombiMode:
        ui->combiButton->setChecked(true);
        ui->humiditySlider->setEnabled(true);
        ui->humiditySlider->setValue(50);
        ui->tempSlider->setRange(30, 300);
        ui->tempSlider->setValue(100);
        ui->timeSlider->setValue(0);
        ui->interTempSlider->setEnabled(false);
        ui->interTempSlider->setSubVisible(false);
        ui->interTempSlider->setValue(30);
        setFan(4);
        updateCoreTempButton();
        updateCoreTempLabel();
        this->mode = mode;
        break;
    case Define::DryMode:
        ui->dryheatButton->setChecked(true);
        ui->humiditySlider->setEnabled(false);
        ui->humiditySlider->setValue(0);
        ui->tempSlider->setRange(30, 300);
        ui->tempSlider->setValue(160);
        ui->timeSlider->setValue(0);
        ui->interTempSlider->setEnabled(false);
        ui->interTempSlider->setSubVisible(false);
        ui->interTempSlider->setValue(30);
        setFan(4);
        updateCoreTempButton();
        updateCoreTempLabel();
        this->mode = mode;
        break;
    default:
        return;
    }
}

void ProgrammingManualWindow::setFan(int level)
{
    fan = level;

    updateFanButton();
}

void ProgrammingManualWindow::updateHumidityLabel()
{
    ui->humidityLabel->setText(QString("%1%").arg(ui->humiditySlider->sliderPosition()));
}

void ProgrammingManualWindow::updateTempLabel()
{
    ui->tempLabel->setText(Stringer::temperature(ui->tempSlider->sliderPosition(), Stringer::fontSize14));
}

void ProgrammingManualWindow::updateTimeLabel()
{
    ui->timeLabel->setText(Stringer::remainingTime(sliderToTime(ui->timeSlider->sliderPosition()) * 1000, Stringer::fontSize14));
}

void ProgrammingManualWindow::updateCoreTempButton()
{
    if (ui->interTempSlider->isEnabled())
        ui->interTempButton->setStyleSheet("\
QPushButton {\
    image: url(:/images/slider_icon/core_temp_enabled.png);\
}\
QPushButton:pressed {\
    image: url(:/images/slider_icon/core_temp_ov.png);\
}");
    else
       ui->interTempButton->setStyleSheet("\
QPushButton {\
   image: url(:/images/slider_icon/core_temp.png);\
}\
QPushButton:pressed {\
   image: url(:/images/slider_icon/core_temp_ov.png);\
}");
}

void ProgrammingManualWindow::updateCoreTempLabel()
{
    if (ui->interTempSlider->isEnabled())
        ui->interTempLabel->setText(Stringer::temperature(ui->interTempSlider->sliderPosition(), Stringer::fontSize14));
    else
        ui->interTempLabel->setText(Stringer::unusedTemperature(Stringer::fontSize14));
}

void ProgrammingManualWindow::updateFanButton()
{
    switch (fan)
    {
    case 1:
        ui->fanButton->setStyleSheet(
                    "background-image: url(:/images/manual_button/fan_1.png)");
        break;
    case 2:
        ui->fanButton->setStyleSheet(
                    "background-image: url(:/images/manual_button/fan_2.png)");
        break;
    case 3:
        ui->fanButton->setStyleSheet(
                    "background-image: url(:/images/manual_button/fan_3.png)");
        break;
    case 4:
        ui->fanButton->setStyleSheet(
                    "background-image: url(:/images/manual_button/fan_4.png)");
        break;
    case 5:
        ui->fanButton->setStyleSheet(
                    "background-image: url(:/images/manual_button/fan_5.png)");
        break;
    default:
        ui->fanButton->setStyleSheet(
                    "background-image: url(:/images/manual_button/fan_1.png)");
        break;
    }
}

void ProgrammingManualWindow::onCoreTempEnabled(int celsius)
{
    ui->interTempSlider->setEnabled(true);
    ui->interTempSlider->setSubVisible(true);
    ui->interTempSlider->setValue(celsius);
    updateCoreTempButton();
    updateCoreTempLabel();
}

void ProgrammingManualWindow::on_steamButton_clicked()
{
    setDefault(Define::SteamMode);
}

void ProgrammingManualWindow::on_combiButton_clicked()
{
    setDefault(Define::CombiMode);
}

void ProgrammingManualWindow::on_dryheatButton_clicked()
{
    setDefault(Define::DryMode);
}

void ProgrammingManualWindow::on_interTempButton_clicked()
{
    if (ui->interTempSlider->isEnabled())
    {
        ui->interTempSlider->setEnabled(false);
        ui->interTempSlider->setSubVisible(false);
        updateCoreTempButton();
        updateCoreTempLabel();
    }
    else
    {
        ProgrammingManualCoreTempPopup *p = new ProgrammingManualCoreTempPopup(this);
        connect(p, SIGNAL(coreTempEnabled(int)), SLOT(onCoreTempEnabled(int)));
        p->showFullScreen();
    }
}

void ProgrammingManualWindow::on_fanButton_clicked()
{
    fan++;
    if (fan > 5)
        fan = 1;

    updateFanButton();
}

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

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

    MainWindow::jump(w);
}

void ProgrammingManualWindow::on_helpButton_clicked()
{

}

void ProgrammingManualWindow::on_okButton_clicked()
{
    ManualCookSetting s;
    s.mode = mode;
    s.humidity = ui->humiditySlider->value();
    s.temp = ui->tempSlider->value();
    s.time = sliderToTime(ui->timeSlider->value());
    s.coreTempEnabled = ui->interTempSlider->isEnabled();
    s.coreTemp = ui->interTempSlider->value();
    s.fan = fan;

    CookProgram::add(s);

    emit added();
    close();
}