configdatetimedlg.cpp 4.38 KB
#include <QDateTime>
#include <QKeyEvent>
#include "configdatetimedlg.h"
#include "ui_configdatetimedlg.h"
#include "system.h"
#include "soundplayer.h"


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

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

    ui->ctrSpBxYear->setFormatterWidth(4);
    QDateTime dt_tm = QDateTime::currentDateTime();
    QDate dt_ = dt_tm.date();
    QTime tm_ = dt_tm.time();
    ui->ctrSpBxYear->setValue(dt_.year());
    ui->ctrSpBxMonth->setValue(dt_.month());
    ui->ctrSpBxDay->setValue(dt_.day());
    ui->ctrSpBxHour->setValue(tm_.hour());
    ui->ctrSpBxMin->setValue(tm_.minute());

    ui->ctrSpBxYear->setFocus();
    m_bInputState = true;


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

    m_pMapperSpins = new QSignalMapper(this);
    m_pMapperSpins->setMapping(ui->ctrSpBxYear, ui->ctrSpBxYear);
    m_pMapperSpins->setMapping(ui->ctrSpBxMonth, ui->ctrSpBxMonth);
    m_pMapperSpins->setMapping(ui->ctrSpBxDay, ui->ctrSpBxDay);
    m_pMapperSpins->setMapping(ui->ctrSpBxHour, ui->ctrSpBxHour);
    m_pMapperSpins->setMapping(ui->ctrSpBxMin, ui->ctrSpBxMin);

    connect(ui->ctrSpBxYear, SIGNAL(focusInEdit()), m_pMapperSpins, SLOT(map()));
    connect(ui->ctrSpBxMonth, SIGNAL(focusInEdit()), m_pMapperSpins, SLOT(map()));
    connect(ui->ctrSpBxDay, SIGNAL(focusInEdit()), m_pMapperSpins, SLOT(map()));
    connect(ui->ctrSpBxHour, SIGNAL(focusInEdit()), m_pMapperSpins, SLOT(map()));
    connect(ui->ctrSpBxMin, SIGNAL(focusInEdit()), m_pMapperSpins, SLOT(map()));

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

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

void ConfigDateTimeDlg::on_ctrBtnOk_clicked()
{
    time_t tm_t;
    QDate dt_(ui->ctrSpBxYear->value(), ui->ctrSpBxMonth->value(), ui->ctrSpBxDay->value());
    QTime tm_(ui->ctrSpBxHour->value(), ui->ctrSpBxMin->value());
    QDateTime dt_tm;
    dt_tm.setTime(tm_);
    dt_tm.setDate(dt_);
    tm_t = (time_t)dt_tm.toTime_t();
    System::setRtcTime(dt_tm);
    stime(&tm_t);
    this->accept();
}

void ConfigDateTimeDlg::on_spinBox_valueChanged(int arg1)
{

}

void ConfigDateTimeDlg::on_ctrBtnCancel_clicked()
{
    this->reject();
}

void ConfigDateTimeDlg::keyPressEvent(QKeyEvent *event){
    int i = 0;
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        if(focusWidget() == ui->ctrSpBxYear) ui->ctrBtnCancel->setFocus();
        else focusPreviousChild();
        break;
    case 0x01000031:    // Push

        break;
    case 0x01000030:    // Turn right
        if(focusWidget() == ui->ctrBtnCancel) ui->ctrSpBxYear->setFocus();
        else focusNextChild();

        break;
    }
}

void ConfigDateTimeDlg::keyReleaseEvent(QKeyEvent *event){
    int i = 0;
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        if(focusWidget() == ui->ctrSpBxYear) 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->ctrSpBxYear->setFocus();
        else focusNextChild();
        break;
    }
}

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

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

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