//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/Views/InstrumentWidgets/InstrumentView.cpp
//! @brief     Implements class InstrumentView
//!
//! @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/Views/InstrumentWidgets/InstrumentView.h"
#include "GUI/Models/InstrumentItems.h"
#include "GUI/Models/InstrumentModel.h"
#include "GUI/Views/CommonWidgets/StyledToolBar.h"
#include "GUI/Views/InstrumentWidgets/InstrumentEditor.h"
#include "GUI/Views/InstrumentWidgets/InstrumentSelectorWidget.h"
#include "GUI/mainwindow/projectdocument.h"
#include <QBoxLayout>

InstrumentView::InstrumentView(QWidget* parent, ProjectDocument* document)
    : QWidget(parent), m_instrumentEditor(new InstrumentEditor), m_document(document)
{
    auto horizontalLayout = new QHBoxLayout;
    m_instrumentSelector = new InstrumentSelectorWidget(document, this);
    horizontalLayout->addWidget(m_instrumentSelector);
    horizontalLayout->addWidget(m_instrumentEditor, 1);

    StyledToolBar* toolbar = new StyledToolBar(this);
    toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolbar->addActions(m_instrumentSelector->toolbarActions());

    auto mainLayout = new QVBoxLayout;
    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);
    mainLayout->addWidget(toolbar);
    mainLayout->addLayout(horizontalLayout);
    setLayout(mainLayout);

    connect(m_instrumentSelector, &InstrumentSelectorWidget::selectionChanged, this,
            &InstrumentView::onItemSelectionChanged);

    onItemSelectionChanged(nullptr);
}

void InstrumentView::onItemSelectionChanged(SessionItem* instrumentItem)
{
    m_instrumentEditor->setItem(dynamic_cast<InstrumentItem*>(instrumentItem));
}

void InstrumentView::showEvent(QShowEvent*)
{
    m_instrumentSelector->updateSelection();
}