//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Frame/Plot1DFrame.cpp
//! @brief     Implements class Plot1DFrame.
//!
//! @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/View/Frame/Plot1DFrame.h"
#include "GUI/Model/Project/DataSource.h"
#include "GUI/View/Canvas/SpecularDataCanvas.h"
#include "GUI/View/Setup/AxisPanel.h"
#include "GUI/View/Setup/FrameActions.h"
#include <QBoxLayout>
#include <QMenu>

namespace {

void execContextMenu(const QPoint& point)
{
    QMenu menu;
    menu.addAction(gActions->reset_view);
    menu.addAction(gActions->save_plot);
    menu.addAction(gActions->toggle_properties_panel);
    menu.exec(point);
}

} // namespace


Plot1DFrame::Plot1DFrame()
    : m_specular_canvas(new SpecularDataCanvas)
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    auto* hlayout = new QHBoxLayout(this);
    hlayout->setContentsMargins(0, 0, 0, 0);
    hlayout->setSpacing(0);
    hlayout->addWidget(m_specular_canvas);

    connect(m_specular_canvas, &SpecularDataCanvas::customContextMenuRequested,
            [](const QPoint& point) { execContextMenu(point); });

    m_data_source = std::make_unique<DataFromData>();
    auto* axis_panel = new AxisPanel(m_data_source.get());
    hlayout->addWidget(axis_panel);
    QObject::connect(gActions->toggle_properties_panel, &QAction::triggered, axis_panel,
                     &QWidget::setVisible);
    axis_panel->setVisible(false);
}
