multicookmanualwindow.cpp 7.53 KB
#include "multicookmanualwindow.h"
#include "ui_multicookmanualwindow.h"

#include <QKeyEvent>

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

MultiCookManualWindow::MultiCookManualWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MultiCookManualWindow)
{
    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);

    connect(ui->humiditySlider, SIGNAL(sliderMoved(int)), SLOT(updateView()));
    connect(ui->tempSlider, SIGNAL(sliderMoved(int)), SLOT(updateView()));
    connect(ui->timeSlider, SIGNAL(sliderMoved(int)), SLOT(updateView()));

    connect(ui->humiditySlider, SIGNAL(sliderPressed()), SLOT(updateView()));
    connect(ui->tempSlider, SIGNAL(sliderPressed()), SLOT(updateView()));
    connect(ui->timeSlider, SIGNAL(sliderPressed()), SLOT(updateView()));

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

    setFocus();

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

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

    installEventFilter(this);

    pushed = NULL;
}

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

void MultiCookManualWindow::setMode(Define::Mode mode)
{
    switch (mode)
    {
    case Define::SteamMode:
        ui->steamButton->setChecked(true);
        ui->combiButton->setChecked(false);
        ui->dryheatButton->setChecked(false);
        ui->humidityButton->setEnabled(false);
        ui->humiditySlider->setEnabled(false);
        ui->humiditySlider->setValue(100);
        ui->tempSlider->setRange(30, 130);
        ui->tempSlider->setValue(100);
        ui->timeSlider->setValue(0);
        updateView();
        this->mode = mode;
        break;
    case Define::CombiMode:
        ui->steamButton->setChecked(false);
        ui->combiButton->setChecked(true);
        ui->dryheatButton->setChecked(false);
        ui->humidityButton->setEnabled(true);
        ui->humiditySlider->setEnabled(true);
        ui->humiditySlider->setValue(50);
        ui->tempSlider->setRange(30, 300);
        ui->tempSlider->setValue(100);
        ui->timeSlider->setValue(0);
        updateView();
        this->mode = mode;
        break;
    case Define::DryMode:
        ui->steamButton->setChecked(false);
        ui->combiButton->setChecked(false);
        ui->dryheatButton->setChecked(true);
        ui->humidityButton->setEnabled(false);
        ui->humiditySlider->setEnabled(false);
        ui->humiditySlider->setValue(0);
        ui->tempSlider->setRange(30, 300);
        ui->tempSlider->setValue(160);
        ui->timeSlider->setValue(0);
        updateView();
        this->mode = mode;
        break;
    default:
        return;
    }
}

bool MultiCookManualWindow::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 MultiCookManualWindow::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 MultiCookManualWindow::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;
    }
}

int MultiCookManualWindow::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;
}

void MultiCookManualWindow::onEncoderLeft()
{
    focusPreviousChild();
}

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

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

    QPushButton *b = qobject_cast<QPushButton *>(clicked);
    if (b)
        b->click();

    Slider *slider = qobject_cast<Slider *>(clicked);
    if (slider)
    {
        if (slider == ui->humiditySlider)
            ui->humidityButton->setFocus();
        else if (slider == ui->tempSlider)
            ui->tempButton->setFocus();
        else if (slider == ui->timeSlider)
            ui->timeButton->setFocus();

        updateView();
    }
}

void MultiCookManualWindow::updateView()
{
    ui->humidityLabel->setText(QString("%1%").arg(ui->humiditySlider->sliderPosition()));
    ui->tempLabel->setText(Stringer::temperature(ui->tempSlider->sliderPosition(), Stringer::fontSize14));
    ui->timeLabel->setText(Stringer::remainingTime(sliderToTime(ui->timeSlider->sliderPosition()) * 1000, Stringer::fontSize14));

    QWidget *focused = focusWidget();
    ui->humidityButton->setChecked(focused == ui->humiditySlider);
    ui->tempButton->setChecked(focused == ui->tempSlider);
    ui->timeButton->setChecked(focused == ui->timeSlider);
}

void MultiCookManualWindow::afterThreeSecs()
{
    Slider *slider = qobject_cast<Slider *>(focusWidget());
    if (slider)
    {
        if (slider == ui->humiditySlider)
            ui->humidityButton->setFocus();
        else if (slider == ui->tempSlider)
            ui->tempButton->setFocus();
        else if (slider == ui->timeSlider)
            ui->timeButton->setFocus();

        updateView();
    }
}

void MultiCookManualWindow::on_humidityButton_clicked()
{
    ui->humiditySlider->setFocus();
    updateView();
}

void MultiCookManualWindow::on_tempButton_clicked()
{
    ui->tempSlider->setFocus();
    updateView();
}

void MultiCookManualWindow::on_timeButton_clicked()
{
    ui->timeSlider->setFocus();
    updateView();
}

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

void MultiCookManualWindow::on_helpButton_clicked()
{

}

void MultiCookManualWindow::on_okButton_clicked()
{
    MultiManualCook *c = new MultiManualCook;
    c->setMode(mode);
    c->setHumidity(ui->humiditySlider->value());
    c->setTemperature(ui->tempSlider->value());
    c->setTime(sliderToTime(ui->timeSlider->value()));

    emit selected(c);
    close();
}