/***************************************************************************
                          mainwindowinputqso.cpp  -  description
                             -------------------
    begin                : may 2021
    copyright            : (C) 2021 by Jaime Robles
    email                : jaime@robles.es
 ***************************************************************************/

/*****************************************************************************
 * This file is part of KLog.                                                *
 *                                                                           *
 *    KLog is free software: you can redistribute it and/or modify           *
 *    it under the terms of the GNU General Public License as published by   *
 *    the Free Software Foundation, either version 3 of the License, or      *
 *    (at your option) any later version.                                    *
 *                                                                           *
 *    KLog is distributed in the hope that it will be useful,                *
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *    GNU General Public License for more details.                           *
 *                                                                           *
 *    You should have received a copy of the GNU General Public License      *
 *    along with KLog.  If not, see <https://www.gnu.org/licenses/>.          *
 *                                                                           *
 *****************************************************************************/
//
// This class implement the tab of the mainwindow that supports the QSL options
//

#include "mainwindowinputqso.h"

MainWindowInputQSO::MainWindowInputQSO(DataProxy_SQLite *dp, QWidget *parent) :
    QWidget(parent)
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    dataProxy = dp;
    //locator = new Locator();

    nameLineEdit = new QLineEdit;
    qthLineEdit = new QLineEdit;
    locatorLineEdit = new QLineEdit;
    rstTXLineEdit = new QLineEdit;
    rstRXLineEdit = new QLineEdit;
    txFreqSpinBox = new QDoubleSpinBox;
    rxFreqSpinBox = new QDoubleSpinBox;
    splitCheckBox = new QCheckBox;
    rxPowerSpinBox = new QDoubleSpinBox;
    dataProxy = dp;

    createUI();
    setDefaultData();
    //installEventFilter (this);
    clear();
    //qDebug() << "MainWindowInputQSO::MainWindowInputQSO - END"  ;
}

MainWindowInputQSO::~MainWindowInputQSO()
{
   //delete(dataProxy);
}

void MainWindowInputQSO::setModifying(const bool _m)
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    modify = _m;
}

void MainWindowInputQSO::createUI()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    rstTXLineEdit->setToolTip(tr("TX RST."));
    rstRXLineEdit->setToolTip(tr("RX RST."));
    txFreqSpinBox->setToolTip(tr("TX Frequency in MHz."));
    rxFreqSpinBox->setToolTip(tr("RX Frequency in MHz."));
    rxPowerSpinBox->setToolTip(tr("Power used by the contacted station."));
    nameLineEdit->setToolTip(tr("Name of the contacted operator."));
    qthLineEdit->setToolTip(tr("QTH of the contacted station."));
    locatorLineEdit->setToolTip(tr("Locator of the contacted station."));

    rxPowerSpinBox->setDecimals(2);
    rxPowerSpinBox->setMaximum(9999);
    rxPowerSpinBox->setSuffix(" " + tr("Watts"));

    txFreqSpinBox->setDecimals(6);
    txFreqSpinBox->setMaximum(99999);
    txFreqSpinBox->setSuffix(" " + tr("MHz"));
    txFreqSpinBox->setKeyboardTracking(false);
    txFreqSpinBox->setSingleStep(0.001);

    rxFreqSpinBox->setDecimals(6);
    rxFreqSpinBox->setMaximum(99999);
    rxFreqSpinBox->setSuffix(" " + tr("MHz"));
    rxFreqSpinBox->setKeyboardTracking(false);
    rxFreqSpinBox->setSingleStep(0.001);

    splitCheckBox->setText(tr("Split", "Translator: Split is a common hamradio term. Do not translate unless you are sure."));
    splitCheckBox->setChecked(false);

    QLabel *nameLabel = new QLabel(this);
    nameLabel->setText(tr("Name"));
    nameLabel->setAlignment(Qt::AlignCenter);

    QLabel *qthLabel = new QLabel(this);
    qthLabel->setText(tr("QTH"));
    qthLabel->setAlignment(Qt::AlignCenter);

    QLabel *locLabel = new QLabel(this);
    locLabel->setText(tr("DX Locator"));
    locLabel->setAlignment(Qt::AlignCenter);

    QLabel *rxPowerSpinBoxLabelN = new QLabel(this);
    rxPowerSpinBoxLabelN->setText(tr("Power(rx)"));
    rxPowerSpinBoxLabelN->setAlignment(Qt::AlignCenter);

    QLabel *rstLabelN = new QLabel(this);
    rstLabelN->setText(tr("RST"));
    rstLabelN->setAlignment(Qt::AlignCenter);

    QLabel *rstTxLabelN = new QLabel(this);
    rstTxLabelN->setText(tr("TX"));
    rstTxLabelN->setAlignment(Qt::AlignCenter);

    QLabel *rstRxLabelN = new QLabel(this);
    rstRxLabelN->setText(tr("RX"));
    rstRxLabelN->setAlignment(Qt::AlignCenter);

    QHBoxLayout *RSTLabelsLayout = new QHBoxLayout;
    RSTLabelsLayout->addWidget (rstTxLabelN);
    RSTLabelsLayout->addWidget (rstLabelN);
    RSTLabelsLayout->addWidget (rstRxLabelN);

    QHBoxLayout *RSTValuesLayout = new QHBoxLayout;
    RSTValuesLayout->addWidget (rstTXLineEdit);
    RSTValuesLayout->addWidget (rstRXLineEdit);

    QVBoxLayout *RSTLayout = new QVBoxLayout;
    RSTLayout->addLayout (RSTLabelsLayout);
    RSTLayout->addLayout (RSTValuesLayout);

    /*
     * QGridLayout *RSTLayout = new QGridLayout;
    RSTLayout->addWidget(rstTxLabelN, 0, 0);
    RSTLayout->addWidget(rstTXLineEdit, 1, 0);
    RSTLayout->addWidget(rstRxLabelN, 0, 1);
    RSTLayout->addWidget(rstRXLineEdit, 1, 1);
    */

    QVBoxLayout *qthLayout = new QVBoxLayout;
    qthLayout->addWidget(qthLabel);
    qthLayout->addWidget(qthLineEdit);

    QVBoxLayout *rstQTHLayout = new QVBoxLayout;
    rstQTHLayout->addLayout(RSTLayout);
    rstQTHLayout->addLayout(qthLayout);

    QLabel *freqLabelsN = new QLabel(this);
    freqLabelsN->setText(tr("Frequency"));
    freqLabelsN->setAlignment(Qt::AlignCenter);
    //freqLabelsN->setAlignment(Qt::AlignCenter);

    QLabel *txfreqLabelN = new QLabel(this);
    txfreqLabelN->setText(tr("TX"));
    txfreqLabelN->setAlignment(Qt::AlignCenter);
    //txfreqLabelN->setAlignment(Qt::AlignLeft);

    QLabel *rxfreqLabelN = new QLabel(this);
    rxfreqLabelN->setText(tr("RX"));
    rxfreqLabelN->setAlignment(Qt::AlignCenter);
    //rxfreqLabelN->setAlignment(Qt::AlignRight);

    QHBoxLayout *freqTXLayout = new QHBoxLayout;
    freqTXLayout->addWidget (txfreqLabelN);
    freqTXLayout->addWidget (splitCheckBox);

    QHBoxLayout *freqTitleLayout = new QHBoxLayout;
    freqTitleLayout->addLayout(freqTXLayout);
    freqTitleLayout->addWidget(freqLabelsN);
    freqTitleLayout->addWidget(rxfreqLabelN);

    QHBoxLayout *freqDataLayout = new QHBoxLayout;
    freqDataLayout->addWidget(txFreqSpinBox);
    freqDataLayout->addWidget(rxFreqSpinBox);

    QVBoxLayout *freqLayout = new QVBoxLayout;
    freqLayout->addLayout(freqTitleLayout);
    freqLayout->addLayout(freqDataLayout);

    QVBoxLayout *locVLayout = new QVBoxLayout;
    locVLayout->addWidget(locLabel);
    locVLayout->addWidget(locatorLineEdit);

    QVBoxLayout *freqLocLayout = new QVBoxLayout;
    freqLocLayout->addLayout(freqLayout);
    freqLocLayout->addLayout(locVLayout);

    QHBoxLayout *rstfreqLayout = new QHBoxLayout;
    rstfreqLayout->addLayout(rstQTHLayout);
    rstfreqLayout->addLayout(freqLocLayout);

    QVBoxLayout *rxPowerLayout = new QVBoxLayout;
    rxPowerLayout->addWidget(rxPowerSpinBoxLabelN);
    rxPowerLayout->addWidget(rxPowerSpinBox);

    QVBoxLayout *nameLayout = new QVBoxLayout;
    nameLayout->addWidget(nameLabel);
    nameLayout->addWidget(nameLineEdit);

    QVBoxLayout *rxPwrLayout = new QVBoxLayout;
    rxPwrLayout->addWidget(rxPowerSpinBoxLabelN);
    rxPwrLayout->addWidget(rxPowerSpinBox);

    QHBoxLayout *namePwrHLayout = new QHBoxLayout;
    namePwrHLayout->addLayout(nameLayout);
    namePwrHLayout->addLayout(rxPwrLayout);

    QVBoxLayout *namePwrLayout = new QVBoxLayout;
    namePwrLayout->addStretch(1);
    namePwrLayout->addLayout(namePwrHLayout);
    namePwrLayout->addStretch(5);

    QVBoxLayout *qsoInputTabWidgetMainLayout = new QVBoxLayout;
    qsoInputTabWidgetMainLayout->addLayout(rstfreqLayout);
    qsoInputTabWidgetMainLayout->addLayout(namePwrLayout);
    // qsoInputTabWidgetMainLayout->setSizeConstraint(QLayout::SetFixedSize);
    setLayout(qsoInputTabWidgetMainLayout);

    connect(rstTXLineEdit, SIGNAL(returnPressed()), this, SLOT(slotReturnPressed() ) );
    connect(rstRXLineEdit, SIGNAL(returnPressed()), this, SLOT(slotReturnPressed() ) );
    connect(locatorLineEdit, SIGNAL(returnPressed()), this, SLOT(slotReturnPressed() ) );
    connect(qthLineEdit, SIGNAL(returnPressed()), this, SLOT(slotReturnPressed() ) );
    connect(nameLineEdit, SIGNAL(returnPressed()), this, SLOT(slotReturnPressed() ) );
    //connect(nameLineEdit, SIGNAL(paletteChanged(QPalette)), this, SLOT(slotPaletteChanged(QPalette)) );
    connect(locatorLineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotLocatorTextChanged() ) );
    connect(txFreqSpinBox, SIGNAL(valueChanged(double)), this, SLOT(slotFreqTXChanged(double)));
    connect(rxFreqSpinBox, SIGNAL(valueChanged(double)), this, SLOT(slotFreqRXChanged(double)));
    connect(splitCheckBox, SIGNAL(checkStateChanged(Qt::CheckState)), this, SLOT(slotSplitClicked()) ) ;

    QWidget::setTabOrder (rstTXLineEdit, rstRXLineEdit);
    QWidget::setTabOrder (rstRXLineEdit, nameLineEdit);
    QWidget::setTabOrder (nameLineEdit, qthLineEdit);
    QWidget::setTabOrder (qthLineEdit, locatorLineEdit);
    QWidget::setTabOrder (locatorLineEdit, rxPowerSpinBox);
}

QSO MainWindowInputQSO::getQSOData(QSO _qso)
{
    QSO qso = _qso;
    qso.setGridSquare(getDXLocator());
    qso.setName(getName());
    qso.setQTH(getQTH());
    qso.setRSTRX(getRSTRX());
    qso.setRSTTX(getRSTTX());
    qso.setFreq(getTXFreq().toDouble());
    qso.setFreqRX(getRXFreq().toDouble());
    qso.setBandRX(dataProxy->getBandNameFromFreq(getRXFreq().toDouble()));
    qso.setRXPwr(getRXPwr());
    setRSTToMode(qso.getSubmode(), true);

    qso.setBandRX (dataProxy->getBandNameFromFreq (getRXFreq()));
    qso.setBand(dataProxy->getBandNameFromFreq (getTXFreq()));
    return qso;
}

void MainWindowInputQSO::setQSOData(const QSO &_qso)
{
    fillingQSO = true;
    setModifying(true);

    QSO qso(_qso);
    setDXLocator(qso.getGridSquare());
    setName(qso.getName());
    setQTH(qso.getQTH());
    setRSTRX(qso.getRSTRX());
    setRSTTX(qso.getRSTTX());
    setTXFreq(qso.getFreqTX());
    setRXFreq(qso.getFreqRX());
    setRXPwr(qso.getRXPwr());
    fillingQSO = false;
}

void MainWindowInputQSO::setDefaultData()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    palRed.setColor(QPalette::Text, Qt::red);
    palBlack.setColor(QPalette::Text, Qt::black);
    palWhite.setColor(QPalette::Text, Qt::white);
    completedWithPreviousName = false;
    completedWithPreviousQTH = false;
    completedWithPreviousLocator = false;
    rxFreqBeingAutoChanged = false;
    txFreqBeingAutoChanged = false;
    isSATPropagation = false;
    propMode = QString();
    freqTX = Frequency(0.0);
    freqRX = Frequency(0.0);
    modify = false;
    readDarkMode();
}

void MainWindowInputQSO::readDarkMode()
{
    QSettings settings(util->getCfgFile (), QSettings::IniFormat);
    settings.beginGroup ("Colors");
    setDarkMode(settings.value("DarkMode", false).toBool ());
    settings.endGroup ();
}

void MainWindowInputQSO::clear()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    qthLineEdit->clear();
    nameLineEdit->clear();
    locatorLineEdit->clear();
    rxPowerSpinBox->setValue(0);
    modify = false;
    fillingQSO = false;
}

void MainWindowInputQSO::cleanQRZCOM(const bool _dataFromQRZCOM)
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    if (_dataFromQRZCOM)
    {
        qthLineEdit->clear();
        nameLineEdit->clear();
        locatorLineEdit->clear();
    }
}

void MainWindowInputQSO::clearName()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    nameLineEdit->clear();
    completedWithPreviousName = false;
}

void MainWindowInputQSO::clearQTH()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    qthLineEdit->clear();
    completedWithPreviousQTH = false;
}

void MainWindowInputQSO::clearDXLocator()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    locatorLineEdit->clear ();
    completedWithPreviousLocator = false;
}

void MainWindowInputQSO::slotReturnPressed()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    emit returnPressed();
}

void MainWindowInputQSO::slotLocatorTextChanged()
{//TO BE REMOVED ONCE InfoWidget is FINISHED - At least modified
   //qDebug() << Q_FUNC_INFO << " - Start";
    int cursorP = locatorLineEdit->cursorPosition();

    locatorLineEdit->setText((util->getClearSQLi(locatorLineEdit->text())).toUpper());

    if ( util->isValidGrid((locatorLineEdit->text()).toUpper()) || locatorLineEdit->text ().isEmpty ())
    {
        //qDebug() << Q_FUNC_INFO << ": VALID: " << locatorLineEdit->text();
        setPaletteRightDXLocator(true);
        emit dxLocatorChanged (locatorLineEdit->text());

        locatorLineEdit->setToolTip(tr("DX QTH locator."));
        //qDebug() << Q_FUNC_INFO << ": " << locator->getLat(locatorLineEdit->text());
        //qDebug() << Q_FUNC_INFO << ": LON: " << locator->getLon(locatorLineEdit->text());
    }
    else
    {
        //qDebug() << Q_FUNC_INFO << ": NOT VALID: " << locatorLineEdit->text();
        setPaletteRightDXLocator(false);
        locatorLineEdit->setToolTip(tr("DX QTH locator. Format should be Maidenhead like IN70AA up to 10 characters."));
        locatorLineEdit->setCursorPosition(cursorP);
        return;
    }
    locatorLineEdit->setCursorPosition(cursorP);
}

QString MainWindowInputQSO::getDXLocator()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    return locatorLineEdit->text();
}

void MainWindowInputQSO::setDXLocator(const QString &_loc, bool _completing)
{
  //qDebug() << Q_FUNC_INFO << " - Start";
    if (!util->isValidGrid(_loc))
       return;
    if (_completing)
    {
        locatorLineEdit->setText (_loc.toUpper ());
    }

    if ((_loc.length()>=0) && ((getDXLocator()).length()<=0) )
    {
        setPaletteRightDXLocator (true);
        locatorLineEdit->setText (_loc.toUpper ());
        completedWithPreviousLocator=true;
    }
    else if (completedWithPreviousLocator && (_loc != getDXLocator()))
    {
        completedWithPreviousLocator = false;
        setPaletteRightDXLocator(false);
    }
}

QString MainWindowInputQSO::getName()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    return nameLineEdit->text ();
}

void MainWindowInputQSO::setName(const QString &_st, bool _completing)
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    if (!_completing)
    {
        nameLineEdit->setText (_st);
        completedWithPreviousName = false;
        return;
    }

    if ((_st.length()>=0) && (getName().length()<=0) )
    {
        //qDebug() << Q_FUNC_INFO << ": name: 1" ;
        nameLineEdit->setText (_st);
        setPaletteRightName (true);
        completedWithPreviousName = true;
    }
    else if (completedWithPreviousName && (_st != getName()))
    {
        //qDebug() << Q_FUNC_INFO << ": name: 2" ;
        completedWithPreviousName = false;
        setPaletteRightName (false);
    }
}

Frequency MainWindowInputQSO::getTXFreq()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
   return Frequency(txFreqSpinBox->value());
}

void MainWindowInputQSO::setTXFreq(const Frequency &_ft)
{
   //qDebug() << Q_FUNC_INFO << " - Start - " << _ft.toDouble();

   if ((_ft.isValid()) && (_ft.toDouble(MHz) <= txFreqSpinBox->maximum())) {
       //qDebug() << Q_FUNC_INFO << ": defining FR: " << QString::number(_ft);
       txFreqSpinBox->setValue(_ft.toDouble(MHz));
   } else {
       //qDebug() << Q_FUNC_INFO << ": defining FR: 0" ;
       txFreqSpinBox->setValue(0.0);
   }
    //qDebug() << Q_FUNC_INFO;
}

Frequency MainWindowInputQSO::getRXFreq()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
   return Frequency(rxFreqSpinBox->value());
}

void MainWindowInputQSO::setRXFreq(const Frequency &_ft)
{
  //qDebug() << Q_FUNC_INFO << " - Start - " << _ft.toDouble();
   if ((_ft.isValid()) && (_ft.toDouble(MHz) <= rxFreqSpinBox->maximum())) {
       rxFreqSpinBox->setValue(_ft.toDouble(MHz));
      //qDebug() << Q_FUNC_INFO << " - *** FREQ VALID ***" << _ft.toDouble();
   } else {
       rxFreqSpinBox->setValue(0.0);
   }
}

void MainWindowInputQSO::setSplit(const bool _split)
{
    splitCheckBox->blockSignals(true);
    splitCheckBox->setChecked(_split);
    splitCheckBox->blockSignals(false);
    // Sync the freq RX if the radio disables split
    if (!_split)
        rxFreqSpinBox->setValue(txFreqSpinBox->value());
}

double MainWindowInputQSO::getRXPwr()
{
    return rxPowerSpinBox->value();
}

void MainWindowInputQSO::setRXPwr(const double _pw)
{
   //qDebug() << Q_FUNC_INFO << " - Start: " << _pw;
    if (_pw>=0)
    {
        rxPowerSpinBox->setValue(_pw);
    }
}

QString MainWindowInputQSO::getQTH()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    return qthLineEdit->text();
}

void MainWindowInputQSO::setQTH(const QString &_st, bool _completing)
{// if _completing, I a completing from a previous QSO
   //qDebug() << Q_FUNC_INFO << " - Start";
    if (!_completing)
    {
        qthLineEdit->setText(_st);
        return;
    }

    if ((_st.length()>=0) && (getQTH().length()<=0) )
    {
        setPaletteRightQTH (true);
        completedWithPreviousQTH = true;
        qthLineEdit->setText(_st);
    }
    else if (completedWithPreviousQTH && (_st != getQTH()))
    {
        completedWithPreviousQTH = false;
        setPaletteRightQTH (false);
    }
}

QString MainWindowInputQSO::getRSTTX()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    return rstTXLineEdit->text();
}

void MainWindowInputQSO::setRSTTX(const QString &_st)
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    rstTXLineEdit->setText (_st);
}

QString MainWindowInputQSO::getRSTRX()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    return rstRXLineEdit->text();
}

void MainWindowInputQSO::setRSTRX(const QString &_st)
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    rstRXLineEdit->setText (_st);
}


void MainWindowInputQSO::setRSTToMode(const QString &_m, const bool _reading)
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    //qDebug() << Q_FUNC_INFO << ": " << _m ;

    if ((_m == "SSB") || (_m== "LSB") || (_m=="USB") || (_m == "FM") || (_m == "AM") )
    {
         //qDebug() << Q_FUNC_INFO << ": Detected SSB/LSB/USB" ;
        rstTXLineEdit->setInputMask("#DD");
        rstRXLineEdit->setInputMask("#DD");

        //rstTXLineEdit->setText("59");
        //rstRXLineEdit->setText("59");
        rstTXLineEdit->setMaxLength(2);
        rstRXLineEdit->setMaxLength(2);
    }
    else if ((_m == "CW") || (_m == "RTTY"))
    {
        rstTXLineEdit->setInputMask("DDD");
        rstRXLineEdit->setInputMask("DDD");
        //rstTXLineEdit->setText("599");
        //rstRXLineEdit->setText("599");
        rstTXLineEdit->setMaxLength(3);
        rstRXLineEdit->setMaxLength(3);
    }
    else if (_m == "PSK31")
    {
        rstTXLineEdit->setInputMask("#DD0");
        rstRXLineEdit->setInputMask("#DD0");
        //rstTXLineEdit->setText("599");
        //rstRXLineEdit->setText("599");
        rstTXLineEdit->setMaxLength(4);
        rstRXLineEdit->setMaxLength(4);
    }
    else if ( (_m.contains("FT", Qt::CaseInsensitive)) || (_m.contains("JT", Qt::CaseInsensitive)) || (_m.contains("QRA64", Qt::CaseInsensitive)) || (_m.contains("JS", Qt::CaseInsensitive)))
    {
        rstTXLineEdit->setInputMask("#99");
        rstRXLineEdit->setInputMask("#99");
        //rstTXLineEdit->setText("0");
        //rstRXLineEdit->setText("0");
        rstTXLineEdit->setMaxLength(3);
        rstRXLineEdit->setMaxLength(3);
    }
    else
    { // By default SSB RST is configured but anything could be added
        rstTXLineEdit->setInputMask("#xxx");
        rstRXLineEdit->setInputMask("#xxx");
        //rstTXLineEdit->setText("59");
        //rstRXLineEdit->setText("59");
        rstTXLineEdit->setMaxLength(4);
        rstRXLineEdit->setMaxLength(4);
    }
    if (!_reading)
    {
         //qDebug() << Q_FUNC_INFO << ": reading the UI";
        rstTXLineEdit->setText(util->getDefaultRST(_m));
        rstRXLineEdit->setText(util->getDefaultRST(_m));
    }
}

void MainWindowInputQSO::setPaletteRightName(const bool _ok)
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    if (_ok)
    {
        if (darkMode)
        {
            nameLineEdit->setPalette (palWhite);
        }
        else
        {
            nameLineEdit->setPalette (palBlack);
        }
    }
    else
    {
        nameLineEdit->setPalette (palRed);
    }
}

void MainWindowInputQSO::setPaletteRightQTH(const bool _ok)
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    if (_ok)
    {
        if (darkMode)
        {
            qthLineEdit->setPalette (palWhite);
        }
        else
        {
            qthLineEdit->setPalette (palBlack);
        }
    }
    else
    {
        qthLineEdit->setPalette (palRed);
    }
}

void MainWindowInputQSO::setPaletteRightDXLocator(const bool _ok)
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    if (_ok)
    {
        if (darkMode)
        {
            locatorLineEdit->setPalette (palWhite);
        }
        else
        {
            locatorLineEdit->setPalette (palBlack);
        }
    }
    else
    {
        locatorLineEdit->setPalette (palRed);
    }
}

void MainWindowInputQSO::setDarkMode (const bool _dm)
{
    darkMode = _dm;

    if (darkMode) {
        txFreqSpinBox->setPalette(palWhite);
        rxFreqSpinBox->setPalette(palWhite);
        //qDebug() << Q_FUNC_INFO << " - DarkMode: ON";
    } else {
        //qDebug() << Q_FUNC_INFO << " - DarkMode: OFF";
        txFreqSpinBox->setPalette(palBlack);
        rxFreqSpinBox->setPalette(palBlack);
    }
}

void MainWindowInputQSO::setPropModeFromSat(const QString &_p)
{
    //qDebug() << Q_FUNC_INFO << ": " << _p;
    propMode = _p;
    if (propMode == "SAT")
    {
        splitCheckBox->setChecked (true);
    }
    else
    {
        splitCheckBox->setChecked (false);
    }
}

void MainWindowInputQSO::slotFreqTXChanged(const double _f)
{
   //qDebug() << Q_FUNC_INFO << ": " << QString::number(_f);
    Frequency f1(_f);
    if (!f1.isValid())
        return;
    if (f1 == freqTX)
        return;
    freqTX = f1;

   int bandId = dataProxy->getBandIdFromFreq(f1);
   if (bandId > 1) { // If the freq belongs to one ham band
       txFreqSpinBox->setToolTip(tr("TX Frequency in MHz."));
       if (darkMode) {
           //qDebug() << Q_FUNC_INFO << " - We are in darkmode";
           txFreqSpinBox->setPalette(palWhite);
       } else {
           //qDebug() << Q_FUNC_INFO << " - We are NOT in darkmode";
           txFreqSpinBox->setPalette(palBlack);
       }
       //qDebug() << Q_FUNC_INFO << ": emitting: " << QString::number(_f);
        emit txFreqChanged (f1);
   } else {
       txFreqSpinBox->setToolTip(tr("TX Frequency in MHz.\nFrequency is not in a hamradio band!"));
       txFreqSpinBox->setPalette(palRed);
       //qDebug() << Q_FUNC_INFO << ":RED - Not in band " ;
   }
    if ((!splitCheckBox->isChecked()) && !modify)
    {
        rxFreqSpinBox->setValue(_f);
        //qDebug() << Q_FUNC_INFO << ": copying to RX Freq " ;
    }
   //qDebug() << Q_FUNC_INFO << " - END" ;
    //splitCheckBox->setChecked(txFreqSpinBox->value() != rxFreqSpinBox->value());
    //setSplitCheckBox();
}

void MainWindowInputQSO::slotSplitClicked()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    bool checked = splitCheckBox->isChecked();
    if (!checked)
        rxFreqSpinBox->setValue(txFreqSpinBox->value());
    emit splitChanged(checked);
}

void MainWindowInputQSO::slotFreqRXChanged(const double _f)
{
   //qDebug() << Q_FUNC_INFO << " - Start";
   Frequency f1(_f);
   if (!f1.isValid())
       return;

   if (f1 == freqRX)
       return;

   freqRX = f1;
   int bandId = dataProxy->getBandIdFromFreq(f1);
   if (bandId > 1) { // If the freq belongs to one ham band
       if (darkMode) {
           //qDebug() << Q_FUNC_INFO << " - We are in darkmode";
           rxFreqSpinBox->setPalette(palWhite);
       } else {
           //qDebug() << Q_FUNC_INFO << " - We are NOT in darkmode";
           rxFreqSpinBox->setPalette(palBlack);
       }
       rxFreqSpinBox->setToolTip(tr("RX Frequency in MHz."));
       emit rxFreqChanged(Frequency(rxFreqSpinBox->value()));
    }
    else
    {
        rxFreqSpinBox->setToolTip(tr("RX Frequency in MHz.\nFrequency is not in a hamradio band!"));
        rxFreqSpinBox->setPalette(palRed);
        //qDebug() << Q_FUNC_INFO << " - Freq is not in ANY ham band";
    }
    if ((!splitCheckBox->isChecked()) && !modify)
    {
        txFreqSpinBox->setValue(_f);
    }

    splitCheckBox->setChecked(txFreqSpinBox->value() != rxFreqSpinBox->value());
    //qDebug() << Q_FUNC_INFO << " - END";

}

//void MainWindowInputQSO::setSplitCheckBox()
//{
//qDebug() << Q_FUNC_INFO << " - Start";
//if (util->isSameFreq(txFreqSpinBox->value(), rxFreqSpinBox->value()))
// {
//     splitCheckBox->setChecked(false);
// }
// else
// {
//     splitCheckBox->setChecked(true);
// }
//}

void MainWindowInputQSO::receiveFocus()
{
   //qDebug() << Q_FUNC_INFO << " - Start";
    rstTXLineEdit->setFocus ();
}

bool MainWindowInputQSO::eventFilter (QObject *object, QEvent *event)
{
    Q_UNUSED(object);
   //qDebug() << Q_FUNC_INFO << " - Start";
    if (!(event->type() == QEvent::Paint ))
    {
        //qDebug() << Q_FUNC_INFO << ": " << QString::number(event->type ());
    }
    //qDebug() << Q_FUNC_INFO << ": " << object->objectName();

    if ((event->type() == QEvent::KeyPress) || (event->type() == QEvent::ShortcutOverride)) {
        //qDebug() << Q_FUNC_INFO << "KEY PRESSED";
        QKeyEvent *ke = static_cast<QKeyEvent *>(event);
        if (ke->key() == Qt::Key_Tab) {
            //qDebug() << Q_FUNC_INFO << "KEY PRESSED TAB";
            if (rxPowerSpinBox->hasFocus())
            {
                //qDebug() << Q_FUNC_INFO << "emitting to hand over to mainQSO Input";
                emit handOverFocusSignal();
            }
            // special tab handling here
            return true;
        }
    }
    return QWidget::event(event);
}

