/* MSHV Mode Switcher Buttons
 * Copyright 2022 Hrisimir Hristov, LZ2HV
 * (Edited by Harper Innes, VK1TTY - to remove Gendered Language and Replace with Non-Gendered language)  NOTE:May be used under the terms of the GNU General Public License (GPL)
 */
#include "hvmodbtsw.h"

//#include <QtGui>

#include "../config_str_all.h"

CbModeSw::CbModeSw(int id,QString n,QWidget *parent)
        : QCheckBox(parent)
{
    sid = id;
    setText(n);
    connect(this, SIGNAL(clicked(bool)), this, SLOT(sclicked(bool)));
}
CbModeSw::~CbModeSw()
{}
void CbModeSw::sclicked(bool f)
{
    emit cbclicked(sid,f);
}
HvDialogModBtSw::HvDialogModBtSw(QWidget *parent)
        : QDialog(parent)
{
    setWindowTitle(tr("Mode Switcher Buttons"));
    setWindowFlags(windowFlags() ^ Qt::WindowContextHelpButtonHint);
    setMinimumSize(200,400);//235,400
    LV = new QVBoxLayout();
    LV->setContentsMargins(10,2,5,2);
    LV->setSpacing(0);
    QGroupBox *GB_M = new QGroupBox(tr("Choose Modes")+":");
    QVBoxLayout *LV_D = new QVBoxLayout(this);
    LV_D->setContentsMargins(10,10,10,10);
    LV_D->setSpacing(10);
    for (int i = 0; i < COUNT_MODE; ++i)
    {
        CbModeSw *cb = new CbModeSw(i,ModeStr(pos_mods[i]));
        LV->addWidget(cb);              
        connect(cb, SIGNAL(cbclicked(int,bool)), this, SIGNAL(clicked(int,bool)));
    }
    //LV->setAlignment(Qt::AlignTop);
    cb220 = new CbModeSw(COUNT_MODE,tr("USE MODE SWITCHER"));//"USE MODE SWITCHER BUTTONS"
    connect(cb220, SIGNAL(cbclicked(int,bool)), this, SIGNAL(clicked(int,bool)));
    GB_M->setLayout(LV);
    LV_D->addWidget(GB_M);
    LV_D->addWidget(cb220);
    setLayout(LV_D);    
}
HvDialogModBtSw::~HvDialogModBtSw()
{}
void HvDialogModBtSw::SetDsettings(int i,bool f)
{
    if (i<COUNT_MODE)
    {
        CbModeSw *cb = (CbModeSw*)LV->itemAt(i)->widget();
        cb->setChecked(f);
    }
    if (i==COUNT_MODE) cb220->setChecked(f);
}
////////////////////////////////////////////////////////////////////////////
HvButton_ID::HvButton_ID(int id, QString n, QWidget * parent)
        : QPushButton(parent)
{
    sid = id;
    setText(n);
    connect(this, SIGNAL(clicked(bool)), this, SLOT(sclicked()));
}
HvButton_ID::~HvButton_ID()
{}
void HvButton_ID::sclicked()
{
    emit btclicked(sid);
}
HvWModBtSw::HvWModBtSw(QWidget *dp,QWidget *parent)
        : QWidget(parent)
{
    H_bt_mode = new QHBoxLayout();
    H_bt_mode->setContentsMargins(0,0,0,0);
    H_bt_mode->setSpacing(0);
    for (int i = 0; i < COUNT_MODE; ++i)
    {
        HvButton_ID *bt = new HvButton_ID(pos_mods[i],ModeStr(pos_mods[i]));
        bt->setFixedHeight(19);
        bt->setHidden(true);//default
        H_bt_mode->addWidget(bt);
        connect(bt, SIGNAL(btclicked(int)), this, SIGNAL(clicked(int)));
    }
    setHidden(true);//default
    setLayout(H_bt_mode);
    D_mode_bt_sw = new HvDialogModBtSw(dp);
    connect(D_mode_bt_sw, SIGNAL(clicked(int,bool)), this, SLOT(SetHidden(int,bool)));
    SetSettings("0#2#13#11#14#15#7#8#222");//default
}
HvWModBtSw::~HvWModBtSw()
{}
/*void HvWModBtSw::SetDefault()
{
	SetSettings("0#2#13#11#14#15#7#8#222");//default
}*/
void HvWModBtSw::dexec()
{
	D_mode_bt_sw->exec();
}
void HvWModBtSw::SetFont(QFont f)
{
    QFontMetrics fm(f);
    for (int i = 0; i < COUNT_MODE; ++i)
    {
        HvButton_ID *bt = (HvButton_ID*)H_bt_mode->itemAt(i)->widget();
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)//2.56
        int fcor = fm.horizontalAdvance(ModeStr(pos_mods[i]))+10;
#else
        int fcor = fm.width(ModeStr(pos_mods[i]))+10;
#endif
        bt->setMinimumWidth(fcor);
        bt->setFixedHeight(fm.height()+2);
    }
}
void HvWModBtSw::SetHidden(int i,bool f)
{
    bool f0 = true;
    if (f) f0 = false;   
    if (i<COUNT_MODE)
    {
    	if (H_bt_mode->isEmpty() && !isHidden()) emit clicked(220);
        HvButton_ID *bt = (HvButton_ID*)H_bt_mode->itemAt(i)->widget();
        bt->setHidden(f0);
    }
    if (i==COUNT_MODE) 
    {
    	if (!H_bt_mode->isEmpty() && isHidden()) emit clicked(220);
    	setHidden(f0);
   	}    	
}
void HvWModBtSw::SetSettings(QString s)
{
    QStringList l = s.split("#");
    bool f = false;
    for (int i = 0; i < COUNT_MODE; ++i)
    {
    	f = false;
        HvButton_ID *bt = (HvButton_ID*)H_bt_mode->itemAt(i)->widget();
        for (int j = 0; j < l.count(); ++j)
        {
            if (bt->sid == l.at(j).toInt())
            {
                bt->setHidden(false);
                D_mode_bt_sw->SetDsettings(i,true);
                f = true;
                break;
            }
        }
        if (!f)
        {
        	bt->setHidden(true);
        	D_mode_bt_sw->SetDsettings(i,false);        	
       	} 
    }
    f = false;
    for (int j = 0; j < l.count(); ++j)
    {
        if (l.at(j) == "220")
        {
            setHidden(false);
            D_mode_bt_sw->SetDsettings(COUNT_MODE,true);
            f = true;
            break;
        }
    }
    if (!f)
    {
    	setHidden(true);
    	D_mode_bt_sw->SetDsettings(COUNT_MODE,false);
   	} 
}
QString HvWModBtSw::GetSettings()
{
    QString str;
    for (int i = 0; i < COUNT_MODE; ++i)
    {
        HvButton_ID *bt = (HvButton_ID*)H_bt_mode->itemAt(i)->widget();
        if (!bt->isHidden()) str.append(QString("%1").arg(bt->sid)+"#");
    }
    if (!isHidden()) str.append("220#");
    str.append("222");//id all is off
    return str;
}



