//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Model/Axis/AmplitudeAxisItem.cpp
//! @brief     Implements various axis items.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/Model/Axis/AmplitudeAxisItem.h"
#include "GUI/Model/Util/UtilXML.h"

namespace {
namespace Tag {

const QString IsVisible("IsVisible");
const QString Nbins("Nbins");
const QString MinDeg("MinDeg");
const QString MaxDeg("MaxDeg");
const QString BaseData("BaseData");
const QString LockMinMax("LockMinMax");
const QString LogScale("LogScale");
const QString LogRangeOrders("LogRangeOrders");

} // namespace Tag
} // namespace

AmplitudeAxisItem::AmplitudeAxisItem(QObject* parent)
    : BasicAxisItem(parent)
    , m_lock_min_max(false)
    , m_log_scale(true)
    , m_log_range_orders(6.0)
{
}

void AmplitudeAxisItem::writeTo(QXmlStreamWriter* w) const
{
    XML::writeAttribute(w, XML::Attrib::version, uint(2));

    // parameters from base class
    w->writeStartElement(Tag::BaseData);
    BasicAxisItem::writeTo(w);
    w->writeEndElement();

    // lock min/max
    w->writeStartElement(Tag::LockMinMax);
    XML::writeAttribute(w, XML::Attrib::value, m_lock_min_max);
    w->writeEndElement();

    // log scale
    w->writeStartElement(Tag::LogScale);
    XML::writeAttribute(w, XML::Attrib::value, m_log_scale);
    w->writeEndElement();

    // log range, orders of magniture
    w->writeStartElement(Tag::LogRangeOrders);
    XML::writeAttribute(w, XML::Attrib::value, m_log_range_orders);
    w->writeEndElement();
}

void AmplitudeAxisItem::readFrom(QXmlStreamReader* r)
{
    const uint version = XML::readUIntAttribute(r, XML::Attrib::version);
    Q_UNUSED(version);

    while (r->readNextStartElement()) {
        QString tag = r->name().toString();

        // parameters from base class
        if (tag == Tag::BaseData) {
            BasicAxisItem::readFrom(r);
            XML::gotoEndElementOfTag(r, tag);

            // lock min/max
        } else if (tag == Tag::LockMinMax) {
            XML::readAttribute(r, XML::Attrib::value, &m_lock_min_max);
            XML::gotoEndElementOfTag(r, tag);

            // log range, orders of magniture
        } else if (tag == Tag::LogRangeOrders) {
            XML::readAttribute(r, XML::Attrib::value, &m_log_range_orders);
            XML::gotoEndElementOfTag(r, tag);

        } else
            r->skipCurrentElement();
    }
}
void AmplitudeAxisItem::setLogScale(bool value)
{
    m_log_scale = value;
    emit logScaleChanged(value);
}
void AmplitudeAxisItem::setLocked(bool locked)
{
    m_lock_min_max = locked;
}
void AmplitudeAxisItem::setLogRangeOrders(double v)
{
    m_log_range_orders = v;
    setMin(max() / std::pow(10, m_log_range_orders));
}

void AmplitudeAxisItem::adjustLogRangeOrders()
{
    if (min() > 0 && max() > 0)
        m_log_range_orders = std::log10(max() / min());
}
