//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Numeric/NumWidgetUtil.cpp
//! @brief     Implements widget functions in namespace GUI::Util.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2022
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/Numeric/NumWidgetUtil.h"
#include "GUI/Model/Descriptor/DoubleProperty.h"
#include "GUI/View/Base/CustomEventFilters.h"
#include "GUI/View/Numeric/DoubleSpinBox.h"
#include <QLabel>
#include <QLineEdit>

DoubleSpinBox* GUI::Util::createDoubleSpinBoxRow(QFormLayout* parentLayout, DoubleProperty& d)
{
    auto* sb = new DoubleSpinBox(&d);
    parentLayout->addRow(d.label() + ":", sb);
    return sb;
}

QCheckBox* GUI::Util::createCheckBox(const QString& title, std::function<bool()> getter,
                                     std::function<void(bool)> setter,
                                     QVector<std::function<void()>>* updaters,
                                     const QString& tooltip)
{
    auto* checkBox = new QCheckBox(title);
    checkBox->setToolTip(tooltip);

    QObject::connect(checkBox, &QCheckBox::stateChanged, [=] { setter(checkBox->isChecked()); });

    if (updaters)
        (*updaters) << [=] {
            QSignalBlocker b(checkBox);
            checkBox->setChecked(getter());
        };

    return checkBox;
}

QSpinBox* GUI::Util::createIntSpinBox(std::function<int()> getter, std::function<void(int)> slot,
                                      const RealLimits& limits, QString tooltip,
                                      QVector<std::function<void()>>* updaters, bool easyScrollable)
{
    auto* spin = new QSpinBox;
    spin->setFocusPolicy(Qt::StrongFocus);

    spin->setMinimum(limits.hasLowerLimit() ? limits.min() : -std::numeric_limits<int>::max());
    spin->setMaximum(limits.hasUpperLimit() ? limits.max() : std::numeric_limits<int>::max());
    spin->setToolTip(tooltip);

    if (!easyScrollable)
        WheelEventEater::install(spin);

    QObject::connect(spin, &QSpinBox::valueChanged, [=] { slot(spin->value()); });

    if (updaters)
        (*updaters) << [=] {
            QSignalBlocker b(spin);
            spin->setValue(getter());
        };

    return spin;
}

QDoubleSpinBox* GUI::Util::createDoubleSpinBox(std::function<double()> getter,
                                               std::function<void(double)> slot,
                                               QVector<std::function<void()>>* updaters,
                                               QString tooltip, const RealLimits& limits,
                                               bool easyScrollable)
{
    auto* spin = new QDoubleSpinBox;
    spin->setFocusPolicy(Qt::StrongFocus);
    spin->setMinimum(limits.hasLowerLimit() ? limits.min() : -std::numeric_limits<int>::max());
    spin->setMaximum(limits.hasUpperLimit() ? limits.max() : std::numeric_limits<int>::max());
    spin->setDecimals(3);
    spin->setSingleStep(0.01);
    spin->setToolTip(tooltip);

    if (!easyScrollable)
        WheelEventEater::install(spin);

    QObject::connect(spin, &QDoubleSpinBox::valueChanged, [=] { slot(spin->value()); });

    if (updaters)
        (*updaters) << [=] {
            QSignalBlocker b(spin);
            spin->setValue(getter());
        };

    return spin;
}
