// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | bytran@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// The MIT License (MIT)
// Copyright (c) 2016 Dzianis Pliutau
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



// Data object to connect the calculation module with the QML form to specify the instrument function to use
#include "instrumentfunctionqml.h"
#include "auxiliary.h"

InstrumentFunctionQml::InstrumentFunctionQml(QObject *parent, BytranCore *inCore) : QObject(parent)
{
    // Passing the core object into the internal variable
    core = inCore;

    // So that commas are not inserted into numbers
    loc.setNumberOptions(QLocale::OmitGroupSeparator);

    // Setting the internal variable (instrument function index)
    m_instrFunctIndex = core->getInstrFunct()->getInstrFunctIndex();
    m_instrResol = loc.toString(core->getInstrFunct()->getInstrResol(), 'g', 6);
    m_instrFunctWing = loc.toString(core->getInstrFunct()->getInstrFunctWing(), 'g', 6);

    // Connections
    connect(this->core, SIGNAL(valuesUpdated()), this, SLOT(setUpdatedValues()));
}

// Slot is used to update all values in the corresponding QML dialog
void InstrumentFunctionQml::setUpdatedValues()
{
    // Signal emited when the instrumental function is changed
    emit instrFunctIndexChanged();
    emit instrResolChanged();
    emit instrFunctWingChanged();
}

// Methods to get and set the instrumental function indexes
quint8 InstrumentFunctionQml::instrFunctIndex() const { return core->getInstrFunct()->getInstrFunctIndex(); }
void InstrumentFunctionQml::setInstrFunctIndex(const quint8 &instrFunctIndex)
{
    if (instrFunctIndex != m_instrFunctIndex)
    {
        m_instrFunctIndex = instrFunctIndex;
        core->getInstrFunct()->setInstrFunctIndex(m_instrFunctIndex);
        emit instrFunctIndexChanged();
    }
}

// Methods to get and set the instrumental function resolution
QString InstrumentFunctionQml::instrResol() const { return loc.toString(core->getInstrFunct()->getInstrResol(), 'g', 6);; }
void InstrumentFunctionQml::setInstrResol(const QString &instrResol)
{
    if ((instrResol != m_instrResol) && Auxiliary::acceptInput(instrResol))
    {
        m_instrResol = instrResol;
        core->getInstrFunct()->setInstrResol(loc.toDouble(m_instrResol));
        emit instrResolChanged();
    }
}

// Methods to get and set the instrumental function wing
QString InstrumentFunctionQml::instrFunctWing() const { return loc.toString(core->getInstrFunct()->getInstrFunctWing(), 'g', 6); }
void InstrumentFunctionQml::setInstrFunctWing(const QString &instrFunctWing)
{
    if ((instrFunctWing != m_instrFunctWing) && Auxiliary::acceptInput(instrFunctWing))
    {
        m_instrFunctWing = instrFunctWing;
        core->getInstrFunct()->setInstrFunctWing(loc.toDouble(m_instrFunctWing));
        emit instrFunctWingChanged();
    }
}
