washtestwindow.cpp 3.36 KB
#include "washtestwindow.h"
#include "ui_washtestwindow.h"

#include <QKeyEvent>

#include "soundplayer.h"

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

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

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

    udp = UdpHandler::getInstance();
    connect(udp, SIGNAL(changed()), this, SLOT(onDataChanged()));

    udp->set(TG_OVEN_MODE, 4);
    udp->turnOn(TG_MANUAL_RELAY);

    onDataChanged();
}

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

void WashTestWindow::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 WashTestWindow::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 WashTestWindow::onDataChanged()
{
    if (udp->dp())
        ui->steamPumpButton->setText("STOP");
    else
        ui->steamPumpButton->setText("START");

    if (udp->ssp())
        ui->cleanserPumpButton->setText("STOP");
    else
        ui->cleanserPumpButton->setText("START");

    if (udp->unp())
        ui->upperPumpButton->setText("STOP");
    else
        ui->upperPumpButton->setText("START");
}

void WashTestWindow::on_steamPumpButton_clicked()
{
    if (udp->dp())
        steamPumpOff();
    else
        steamPumpOn();
}

void WashTestWindow::on_cleanserPumpButton_clicked()
{
    if (udp->ssp())
        cleanserPumpOff();
    else
        cleanserPumpOn();
}

void WashTestWindow::on_upperPumpButton_clicked()
{
    if (udp->unp())
        upperPumpOff();
    else
        upperPumpOn();
}

void WashTestWindow::on_drainValveStartButton_clicked()
{
    drainValveOpen();
}

void WashTestWindow::on_drainValveStopButton_clicked()
{
    drainValveClose();
}

void WashTestWindow::on_backButton_clicked()
{
    steamPumpOff();
    cleanserPumpOff();
    upperPumpOff();
    drainValveClose();

    udp->turnOff(TG_MANUAL_RELAY);

    close();
}

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

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

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

void WashTestWindow::steamPumpOn()
{
    udp->turnOn(TG_DP);
}

void WashTestWindow::steamPumpOff()
{
    udp->turnOff(TG_DP);
}

void WashTestWindow::cleanserPumpOn()
{
    udp->turnOn(TG_SSP);
}

void WashTestWindow::cleanserPumpOff()
{
    udp->turnOff(TG_SSP);
}

void WashTestWindow::upperPumpOn()
{
    udp->turnOn(TG_UNP);
}

void WashTestWindow::upperPumpOff()
{
    udp->turnOff(TG_UNP);
}

void WashTestWindow::drainValveOpen()
{
    udp->turnOn(TG_DV);
}

void WashTestWindow::drainValveClose()
{
    udp->turnOff(TG_DV);
}