configsoundselelectdlg.cpp 7.12 KB
#include <QDebug>
#include "configsoundselelectdlg.h"
#include "ui_configsoundselelectdlg.h"
#include "soundplayer.h"


ConfigSoundSelelectDlg::ConfigSoundSelelectDlg(QWidget *parent, ConfigType cfgtype) :
    QDialog(parent),
    ui(new Ui::ConfigSoundSelelectDlg)
{
    QString strTemp;
    Config* cfg = Config::getInstance();
    config_item item;
    ui->setupUi(this);
    setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_NoSystemBackground);
    setAttribute(Qt::WA_TranslucentBackground);
    qApp->setActiveWindow(this);
    this->setFocus();

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

    this->setAttribute( Qt::WA_DeleteOnClose);


    m_nCfgType = cfgtype;

    ui->pushButton_1->setText(tr("Media 01"));
    ui->pushButton_2->setText(tr("Media 02"));
    ui->pushButton_3->setText(tr("Media 03"));
    ui->pushButton_4->setText(tr("Media 04"));
    ui->pushButton_5->setText(tr("Media 05"));
    ui->pushButton_6->setText(tr("Media 06"));
    ui->pushButton_7->setText(tr("Media 07"));
    ui->pushButton_8->setText(tr("Media 08"));
    ui->pushButton_9->setText(tr("Media 09"));
    ui->pushButton_10->setText(tr("Media 10"));

    strTemp = cfg->getTitleString(cfgtype);

    ui->ctrLbTitle->setText(strTemp);


    item = cfg->getConfigValue(cfgtype);

   m_nCurSel = item.d32;
   qDebug() << "m_nCurSel is " << m_nCurSel;

    reloadUi();

    m_pSignalMapper = new QSignalMapper(this);
    m_pSignalMapper->setMapping(ui->pushButton_1,0);
    m_pSignalMapper->setMapping(ui->pushButton_2,1);
    m_pSignalMapper->setMapping(ui->pushButton_3,2);
    m_pSignalMapper->setMapping(ui->pushButton_4,3);
    m_pSignalMapper->setMapping(ui->pushButton_5,4);
    m_pSignalMapper->setMapping(ui->pushButton_6,5);
    m_pSignalMapper->setMapping(ui->pushButton_7,6);
    m_pSignalMapper->setMapping(ui->pushButton_8,7);
    m_pSignalMapper->setMapping(ui->pushButton_9,8);
    m_pSignalMapper->setMapping(ui->pushButton_10,9);


    connect(ui->pushButton_1,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_2,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_3,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_4,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_5,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_6,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_7,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_8,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_9,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));
    connect(ui->pushButton_10,SIGNAL(clicked(bool)),m_pSignalMapper,SLOT(map()));

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

    m_vectorTabOrder.append(ui->pushButton_1);
    m_vectorTabOrder.append(ui->pushButton_2);
    m_vectorTabOrder.append(ui->pushButton_3);
    m_vectorTabOrder.append(ui->pushButton_4);
    m_vectorTabOrder.append(ui->pushButton_5);
    m_vectorTabOrder.append(ui->pushButton_6);
    m_vectorTabOrder.append(ui->pushButton_7);
    m_vectorTabOrder.append(ui->pushButton_8);
    m_vectorTabOrder.append(ui->pushButton_9);
    m_vectorTabOrder.append(ui->pushButton_10);
    m_vectorTabOrder.append(ui->ctrBtnOk);
    m_vectorTabOrder.append(ui->ctrBtnCancel);
}

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

void ConfigSoundSelelectDlg::on_ctrBtnOk_clicked()
{
    Config* cfg = Config::getInstance();
    config_item item;
    item.d32 = m_nCurSel;
    qDebug() << item.d32 << "cur sel  " << m_nCurSel;
    cfg->setConfigValue(m_nCfgType,item);
    accept();
}

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

void ConfigSoundSelelectDlg::reloadUi(){
    switch(m_nCurSel){
    case 1:
        ui->pushButton_2->setChecked(true);
        break;
    case 2:
        ui->pushButton_3->setChecked(true);
        break;
    case 3:
        ui->pushButton_4->setChecked(true);
        break;
    case 4:
        ui->pushButton_5->setChecked(true);
        break;
    case 5:
        ui->pushButton_6->setChecked(true);
        break;
    case 6:
        ui->pushButton_7->setChecked(true);
        break;
    case 7:
        ui->pushButton_8->setChecked(true);
        break;
    case 8:
        ui->pushButton_9->setChecked(true);
        break;
    case 9:
        ui->pushButton_10->setChecked(true);
        break;
    case 0:
    default:
        ui->pushButton_1->setChecked(true);
        break;
    }
}

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


void ConfigSoundSelelectDlg::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 ConfigSoundSelelectDlg::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;
    }
}