configresttimeformatdlg.cpp 4.64 KB
#include "configresttimeformatdlg.h"
#include "ui_configresttimeformatdlg.h"
#include "config.h"
#include "soundplayer.h"

using namespace Define;

configResttimeFormatDlg::configResttimeFormatDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::configResttimeFormatDlg)
{
    Config* cfg = Config::getInstance();
    config_item item;
    item = cfg->getConfigValue(config_resttime_format);
    m_nCurSel = item.d32;
    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->pushButton_1->setText(QCoreApplication::translate("Config",rest_time_type_menu[0]));
    ui->pushButton_2->setText(QCoreApplication::translate("Config", rest_time_type_menu[1]));

    reloadUi();

    m_pSignalMapper = new QSignalMapper(this);
    m_pSignalMapper->setMapping(ui->pushButton_1,0);
    m_pSignalMapper->setMapping(ui->pushButton_2,1);

    connect(ui->pushButton_1,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_2,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));

    connect(m_pSignalMapper,SIGNAL(mapped(int)),this,SLOT(onConfigBtnClicked(int)));

    m_vectorTabOrder.append(this);
    m_vectorTabOrder.append(ui->pushButton_1);
    m_vectorTabOrder.append(ui->pushButton_2);
    m_vectorTabOrder.append(ui->ctrBtnOk);
    m_vectorTabOrder.append(ui->ctrBtnCancel);
}

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

void configResttimeFormatDlg::on_ctrBtnOk_clicked()
{
    Config* cfg = Config::getInstance();
    config_item item;
    item.d32 = m_nCurSel;
    cfg->setConfigValue(config_resttime_format,item);
    accept();
}

void configResttimeFormatDlg::on_ctrBtnCancel_clicked()
{
    reject();
}

void configResttimeFormatDlg::onConfigBtnClicked(const int sel){
      m_nCurSel = sel;
}

void configResttimeFormatDlg::reloadUi(){
    switch(m_nCurSel){
    case 0:
        ui->pushButton_1->setChecked(true);
        break;
   case 1:
        ui->pushButton_2->setChecked(true);
        break;
    default:
        break;
    }
}

void configResttimeFormatDlg::keyPressEvent(QKeyEvent *event){
    int i = 0;
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        for(i = 0; i < m_vectorTabOrder.size();i++){
            if(focusWidget() == m_vectorTabOrder[i]) break;
        }

        if(i==0){
            i = m_vectorTabOrder.size()-1;
            m_vectorTabOrder[i]->setFocus();
        }
        else if(i < m_vectorTabOrder.size()) {
            i = i - 1;
            m_vectorTabOrder[i]->setFocus();
        }
        else{
            i=0;
            m_vectorTabOrder[i]->setFocus();
        }
        break;
    case 0x01000031:    // Push

        break;
    case 0x01000030:    // Turn right
        for(i = 0; i < m_vectorTabOrder.size();i++){
            if(focusWidget() == m_vectorTabOrder[i]) break;
        }

        if(i<m_vectorTabOrder.size()-1){
            i+=1;
            m_vectorTabOrder[i]->setFocus();
        }
        else if(i== (m_vectorTabOrder.size()-1)){
            i=0;
            m_vectorTabOrder[i]->setFocus();
        }
        else{
            i=0;
            m_vectorTabOrder[i]->setFocus();
        }
        break;
    }
}

void configResttimeFormatDlg::keyReleaseEvent(QKeyEvent *event){
    int i = 0;
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        for(i = 0; i < m_vectorTabOrder.size();i++){
            if(focusWidget() == m_vectorTabOrder[i]) break;
        }

        if(i==0){
            i = m_vectorTabOrder.size()-1;
            m_vectorTabOrder[i]->setFocus();
        }
        else if(i < m_vectorTabOrder.size()) {
            i = i - 1;
            m_vectorTabOrder[i]->setFocus();
        }
        else{
            i=0;
            m_vectorTabOrder[i]->setFocus();
        }
        break;
    case 0x01000031:    // Push
    {
        QPushButton *btn = qobject_cast<QPushButton*>(focusWidget());
        if(btn != NULL){
            btn->click();
        }
        break;
    }
    case 0x01000030:    // Turn right
        for(i = 0; i < m_vectorTabOrder.size();i++){
            if(focusWidget() == m_vectorTabOrder[i]) break;
        }

        if(i<m_vectorTabOrder.size()-1){
            i+=1;
            m_vectorTabOrder[i]->setFocus();
        }
        else if(i== (m_vectorTabOrder.size()-1)){
            i=0;
            m_vectorTabOrder[i]->setFocus();
        }
        else{
            i=0;
            m_vectorTabOrder[i]->setFocus();
        }
        break;
    }
}