haccpdownloaddlg.cpp 4.57 KB
#include <QSignalMapper>
#include <QDate>
#include <QKeyEvent>
#include <QDebug>
#include "haccpdownloaddlg.h"
#include "ui_haccpdownloaddlg.h"
#include "soundplayer.h"
#include "fileprocessdlg.h"
#include "config.h"

HaccpDownloadDlg::HaccpDownloadDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::HaccpDownloadDlg)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_DeleteOnClose);
    qApp->setActiveWindow(this);
    this->setFocus();

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

    ui->ctrSpBxYearStart->setFormatterWidth(4);
    ui->ctrSpBxYearEnd->setFormatterWidth(4);
    QDate date = QDate::currentDate();
    ui->ctrSpBxDayEnd->setValue(date.day());
    ui->ctrSpBxDayStart->setValue(date.day());
    ui->ctrSpBxMonthEnd->setValue(date.month());
    ui->ctrSpBxMonthStart->setValue(date.month());
    ui->ctrSpBxYearStart->setValue(date.year());
    ui->ctrSpBxYearEnd->setValue(date.year());

    ui->ctrSpBxYearStart->setFocus();



    connect(ui->keyboardwidget, SIGNAL(onOkKeyClicked()), this, SLOT(keyEnter_clicked()));
    connect(ui->keyboardwidget, SIGNAL(onCancelKeyClicked()), this, SLOT(keyCancel_clicked()));

    QSignalMapper* mapper = new QSignalMapper(this);
    mapper->setMapping(ui->ctrSpBxYearStart, ui->ctrSpBxYearStart);
    mapper->setMapping(ui->ctrSpBxYearEnd, ui->ctrSpBxYearEnd);
    mapper->setMapping(ui->ctrSpBxMonthStart, ui->ctrSpBxMonthStart);
    mapper->setMapping(ui->ctrSpBxMonthEnd, ui->ctrSpBxMonthEnd);
    mapper->setMapping(ui->ctrSpBxDayStart, ui->ctrSpBxDayStart);

    connect(ui->ctrSpBxDayEnd, SIGNAL(focusInEdit()), mapper,SLOT(map()));
    connect(ui->ctrSpBxDayStart, SIGNAL(focusInEdit()), mapper,SLOT(map()));
    connect(ui->ctrSpBxMonthEnd, SIGNAL(focusInEdit()), mapper,SLOT(map()));
    connect(ui->ctrSpBxMonthStart, SIGNAL(focusInEdit()), mapper,SLOT(map()));
    connect(ui->ctrSpBxYearStart, SIGNAL(focusInEdit()), mapper,SLOT(map()));
    connect(ui->ctrSpBxYearEnd, SIGNAL(focusInEdit()), mapper,SLOT(map()));

    connect(mapper, SIGNAL(mapped(QWidget*)), this, SLOT(focusInSpinBox(QWidget*)));


}

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

void HaccpDownloadDlg::on_ctrBtnCancel_clicked()
{
    deleteLater();
}

void HaccpDownloadDlg::on_ctrBtnOk_clicked()
{
    FileProcessDlg* dlg = new FileProcessDlg(QDate(ui->ctrSpBxYearStart->value(), ui->ctrSpBxMonthStart->value(), ui->ctrSpBxDayStart->value()),
                                             QDate(ui->ctrSpBxYearEnd->value(),ui->ctrSpBxMonthEnd->value(), ui->ctrSpBxDayEnd->value()),
                                             this);
    connect(dlg, SIGNAL(destroyed(QObject*)), SLOT(deleteLater()));
    dlg->setWindowModality(Qt::WindowModal);
    dlg->show();
    dlg->exec();
}

void HaccpDownloadDlg::keyCancel_clicked()
{
    QSpinBox *spbx = qobject_cast<QSpinBox*>(focusWidget());
    if(spbx != NULL){
        spbx->setValue(m_nFocusValue);
    }
    ui->keyboardwidget->focusOutKeyboard();
}

void HaccpDownloadDlg::keyEnter_clicked(){
    ui->keyboardwidget->focusOutKeyboard();
}


void HaccpDownloadDlg::focusInSpinBox(QWidget *widget){
    QSpinBox *spbx = qobject_cast<QSpinBox*>(widget);
    if(spbx != NULL){
        m_nFocusValue = spbx->value();
    }
}

void HaccpDownloadDlg::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        if(focusWidget() == ui->ctrSpBxYearStart) ui->ctrBtnCancel->setFocus();
        else focusPreviousChild();
        break;
    case 0x01000031:    // Push
    {
        QPushButton *btn = qobject_cast<QPushButton*>(focusWidget());
        if(btn != NULL){
            btn->click();
        }
        else{
            QSpinBox *spbx = qobject_cast<QSpinBox*>(focusWidget());
            qDebug() << "grab keyboard";
            if(spbx != NULL) ui->keyboardwidget->focusInKeyboard();
        }
        break;
    }
    case 0x01000030:    // Turn right
        if(focusWidget() == ui->ctrBtnCancel) ui->ctrSpBxYearStart->setFocus();
        else focusNextChild();
        break;
    }
}

void HaccpDownloadDlg::keyPressEvent(QKeyEvent *event){
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        if(focusWidget() == ui->ctrSpBxYearStart) ui->ctrBtnCancel->setFocus();
        else focusPreviousChild();
        break;
    case 0x01000031:    // Push
        break;
    case 0x01000030:    // Turn right
        if(focusWidget() == ui->ctrBtnCancel) ui->ctrSpBxYearStart->setFocus();
        else focusNextChild();
        break;
    }
}