Newer
Older
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/SampleDesigner/SampleEditor.cpp
//! @brief Implements class SampleEditor
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "GUI/View/SampleDesigner/SampleEditor.h"
#include "GUI/Model/Sample/SampleItem.h"
#include "GUI/View/SampleDesigner/LayerEditorUtil.h"
#include "GUI/View/SampleDesigner/SampleEditorController.h"
#include <QBoxLayout>
Matthias Puchner
committed
#include <QPushButton>
SampleEditor::SampleEditor(QWidget* parent, ProjectDocument* document)
: QWidget(parent)
, m_document(document)
{
m_scrollArea = new QScrollArea(this);
m_scrollArea->setWidgetResizable(true);
m_scrollArea->setWidget(createEmptyWidget());
auto* mainLayout = new QVBoxLayout(this);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->setSpacing(0);
mainLayout->addWidget(m_scrollArea);
}
SampleEditor::~SampleEditor()
{
qDeleteAll(m_editControllers.values());
}
void SampleEditor::setCurrentSample(SampleItem* sampleItem)
m_editControllers[m_currentSample]->setSampleForm(nullptr);
delete m_scrollArea->takeWidget();
m_currentSample = sampleItem;
if (m_currentSample == nullptr) {
m_scrollArea->setWidget(createEmptyWidget());
return;
}
if (!m_editControllers.contains(m_currentSample))
m_editControllers.insert(m_currentSample,
new SampleEditorController(m_document, m_currentSample));
auto* ec = m_editControllers[m_currentSample];
connect(ec, &SampleEditorController::requestViewInRealspace, this,
&SampleEditor::requestViewInRealspace);
connect(ec, &SampleEditorController::aboutToRemoveItem, this, &SampleEditor::aboutToRemoveItem);
connect(ec, &SampleEditorController::modified, this, &SampleEditor::modified);
createLayerColors();
m_currentSampleWidget = new SampleForm(this, m_currentSample, ec);
ec->setSampleForm(m_currentSampleWidget);
m_currentSampleWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
m_scrollArea->setWidget(m_currentSampleWidget);
void SampleEditor::createLayerColors() // #baLayerEditor move to better place
return;
int col = 0;
for (auto* l : m_currentSample->layerItems()) {
if (l->color().isValid())
continue;
l->setColor(LayerEditorUtil::predefinedLayerColors()[col]);
if (col == LayerEditorUtil::predefinedLayerColors().size())
col = 0;
}
}
QWidget* SampleEditor::createEmptyWidget()
auto* emptyWidget = new QWidget(this);
Matthias Puchner
committed
auto* btn = new QPushButton("Create sample", emptyWidget);
connect(btn, &QPushButton::clicked, this, &SampleEditor::requestCreateNewSample);
Matthias Puchner
committed
auto* layout = new QHBoxLayout(emptyWidget);
layout->setContentsMargins(10, 20, 10, 20);
layout->addStretch();
layout->addWidget(btn);
layout->addStretch();
layout->setAlignment(Qt::AlignTop);
return emptyWidget;
}