Newer
Older
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/SampleDesigner/MaterialInplaceForm.cpp
//! @brief Implements class MaterialInplaceForm
//!
//! @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/MaterialInplaceForm.h"
#include "GUI/Model/Material/MaterialModel.h"
#include "GUI/Model/Sample/ItemWithMaterial.h"
#include "GUI/Model/Session/ModelPath.h"
#include "GUI/Model/Types/DoubleDescriptor.h"
#include "GUI/Model/Types/VectorDescriptor.h"

Wuttke, Joachim
committed
#include "GUI/View/Edit/DoubleLineEdit.h"
#include "GUI/View/Edit/DoubleSpinBox.h"
#include "GUI/View/MaterialEditor/MaterialEditorDialog.h"
#include "GUI/View/SampleDesigner/LayerEditorUtils.h"
#include "GUI/View/SampleDesigner/SampleEditorController.h"
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
MaterialInplaceForm::MaterialInplaceForm(QWidget* parent, ItemWithMaterial* item,
SampleEditorController* ec)
: QWidget(parent), m_item(item), m_ec(ec)
{
m_layout = new QGridLayout(this);
m_layout->setContentsMargins(0, 0, 0, 0);
createWidgets();
connect(ec->materialModel(), &MaterialModel::materialChanged, this,
&MaterialInplaceForm::onMaterialChanged);
Matthias Puchner
committed
ItemWithMaterial* MaterialInplaceForm::itemWithMaterial() const
{
return m_item;
}
void MaterialInplaceForm::updateValues()
{
for (auto* editor : findChildren<DoubleSpinBox*>()) {
QSignalBlocker b(editor);
editor->setBaseValue(editor->valueDescriptor());
}
for (auto* editor : findChildren<DoubleLineEdit*>()) {
QSignalBlocker b(editor);
editor->setBaseValue(editor->valueDescriptor());
}
}
void MaterialInplaceForm::selectMaterial()
{
const QString newMaterialIdentifier = MaterialEditorDialog::chooseMaterial(
baWin, m_ec->projectDocument(), m_item->materialIdentifier());
if (!newMaterialIdentifier.isEmpty() && newMaterialIdentifier != m_item->materialIdentifier()) {
GUI::Util::Layout::clearLayout(m_layout, true);
m_ec->selectMaterial(m_item, newMaterialIdentifier);
createWidgets();
Matthias Puchner
committed
} else
updateValues(); // necessary, since in the material editor the values could have been
// changed without selecting a different material
}
void MaterialInplaceForm::createWidgets()
{
auto* material = m_item->materialItem();
Matthias Puchner
committed
// -- Create UI for delta/beta resp. sldRe/sldIm
DoubleDescriptors values;
if (material->hasRefractiveIndex())
values << material->delta() << material->beta();
else
values << material->sldRe() << material->sldIm();
int col = 0;

Wuttke, Joachim
committed
for (const auto& d : values) {
auto* editor = new DoubleLineEdit(this, d);
auto* label = new QLabel(d.label, this);
label->setBuddy(editor);
QObject::connect(editor, &DoubleLineEdit::baseValueChanged,
[=](double newValue) { m_ec->setMaterialValue(m_item, newValue, d); });
m_layout->addWidget(label, 0, col);
m_layout->addWidget(editor, 1, col++);
}
Matthias Puchner
committed
// -- Create UI for magnetization vector
VectorDescriptor mag = material->magnetizationVector();
Matthias Puchner
committed
const auto setNewValue = [=](double value, DoubleDescriptor d) {
m_ec->setMaterialValue(m_item, value, d);
};
Matthias Puchner
committed
LayerEditorUtils::addVectorToGrid(m_layout, col, mag, setNewValue, true, false);
Matthias Puchner
committed
// -- Create UI for material selection button
auto* btn = new QPushButton("...", this);
btn->setToolTip("Select material");
m_layout->addWidget(btn, 1, m_layout->columnCount());
connect(btn, &QPushButton::clicked, this, &MaterialInplaceForm::selectMaterial);
m_layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding), 0, m_layout->columnCount());
}
Matthias Puchner
committed
void MaterialInplaceForm::onMaterialChanged(MaterialItem* materialItem)
{
if (materialItem->identifier() == m_item->materialIdentifier())
updateValues();
}