operationtimeparts.cpp 3.9 KB
#include <QKeyEvent>
#include <qdebug.h>
#include "operationtimeparts.h"
#include "ui_operationtimeparts.h"
#include "ovenstatics.h"
#include "soundplayer.h"

OperationTimeParts::OperationTimeParts(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::OperationTimeParts)
{
    int i=0;
    OvenStatistics* ovs = OvenStatistics::getInstance();

    ui->setupUi(this);

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

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

    m_arrTimeLabels[0] = ui->m_ctrlLbListTime_1;
    m_arrTimeLabels[1] = ui->m_ctrlLbListTime_2;
    m_arrTimeLabels[2] = ui->m_ctrlLbListTime_3;
    m_arrTimeLabels[3] = ui->m_ctrlLbListTime_4;
    m_arrTimeLabels[4] = ui->m_ctrlLbListTime_5;
    m_arrTimeLabels[5] = ui->m_ctrlLbListTime_6;
    m_arrTimeLabels[6] = ui->m_ctrlLbListTime_7;
    m_arrTimeLabels[7] = ui->m_ctrlLbListTime_8;
    m_arrTimeLabels[8] = ui->m_ctrlLbListTime_9;
    m_arrTimeLabels[9] = ui->m_ctrlLbListTime_10;
    m_arrTimeLabels[10] = ui->m_ctrlLbListTime_11;

    m_arrResetBtn[0] = ui->btnReset_1;
    m_arrResetBtn[1] = ui->btnReset_2;
    m_arrResetBtn[2] = ui->btnReset_3;
    m_arrResetBtn[3] = ui->btnReset_4;
    m_arrResetBtn[4] = ui->btnReset_5;
    m_arrResetBtn[5] = ui->btnReset_6;
    m_arrResetBtn[6] = ui->btnReset_7;
    m_arrResetBtn[7] = ui->btnReset_8;
    m_arrResetBtn[8] = ui->btnReset_9;
    m_arrResetBtn[9] = ui->btnReset_10;
    m_arrResetBtn[10] = ui->btnReset_11;


    m_btnGrops = new QButtonGroup();

    for(i=0;i<OPERATION_TIME_CONTROL_MAX;i++){
        m_btnGrops->addButton(m_arrResetBtn[i],i);
    }

    connect(m_btnGrops,SIGNAL(buttonClicked(int)),this,SLOT(on_btnResets_clicked(int)));

    if(ovs != NULL){
        for( i =0;i<OPERATION_TIME_PARTS_MAX_ITEM;i++){
            if(m_arrPartItemIdxs[i] != 0xffff){
                m_arrUseItems[i] = &(ovs->srvdata->use_log.values[m_arrPartItemIdxs[i]]);
            }
            else m_arrUseItems[i] = NULL;
        }
    }
    reloadUi();
}

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

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

void OperationTimeParts::reloadUi(void){
    uint32_t timetemp;
    OvenStatistics* ovs = OvenStatistics::getInstance();
    QString strTemp="";
    int i = 0;

    if(ovs != NULL){
        for(i = 0; i<OPERATION_TIME_PARTS_MAX_ITEM;i++){
            if(m_arrUseItems[i] != NULL){
                timetemp = *m_arrUseItems[i];
                strTemp.sprintf("%d min", timetemp/60);
                m_arrTimeLabels[i]->setText(strTemp);
            }
            else{
                m_arrTimeLabels[i]->setText("-");
            }
        }
    }
    else{
        for(i=0;i<OPERATION_TIME_PARTS_MAX_ITEM;i++){
            m_arrTimeLabels[i]->setText("-");
        }
    }
}

void OperationTimeParts::on_btnReset_2_clicked()
{

}

void OperationTimeParts::on_btnResets_clicked(int idx){
    qDebug() << "PUSH BUTTON " << idx;
    if(idx < OPERATION_TIME_CONTROL_MAX){
        if(m_arrUseItems[idx] !=NULL){
            *m_arrUseItems[idx]=0;
            reloadUi();
        }
    }

}

void OperationTimeParts::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000030:    // Turn left
        focusPreviousChild();
        break;
    case 0x01000031:    // Push
        break;
    case 0x01000032:    // Turn right
        focusNextChild();
        break;
    }
}

void OperationTimeParts::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000030:    // Turn left
        focusPreviousChild();
        break;
    case 0x01000031:    // Push
    {
        QPushButton *btn = qobject_cast<QPushButton*>(focusWidget());
        if(btn != NULL){
            btn->click();
        }
        break;
    }
    case 0x01000032:    // Turn right
        focusNextChild();
        break;
    }
}