BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
MaterialInplaceForm.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/SampleDesigner/MaterialInplaceForm.cpp
6 //! @brief Implements class MaterialInplaceForm
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2021
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "Base/Util/Assert.h"
23 #include "GUI/Support/XML/Backup.h"
31 #include <QGridLayout>
32 #include <QLabel>
33 #include <QPushButton>
34 
37  : QWidget(parent)
38  , m_item(item)
39  , m_ec(ec)
40 {
41  m_layout = new QGridLayout(this);
42  m_layout->setContentsMargins(0, 0, 0, 0);
43  createWidgets();
44 
45  connect(itemWithMaterial()->materialItem(), &MaterialItem::dataChanged, this,
47 }
48 
50 {
51  return m_item;
52 }
53 
55 {
56  for (auto* editor : findChildren<DoubleSpinBox*>()) {
57  QSignalBlocker b(editor);
58  editor->setBaseValue(editor->valueDescriptor());
59  }
60  for (auto* editor : findChildren<DoubleLineEdit*>()) {
61  QSignalBlocker b(editor);
62  editor->setBaseValue(editor->valueDescriptor());
63  }
64 }
65 
67 {
68  const auto materialsBackup = GUI::Util::createBackup(&m_ec->sampleItem()->materialItems());
69  const QString newMaterialIdentifier = MaterialEditorDialog::chooseMaterial(
71 
72  if (!newMaterialIdentifier.isEmpty() && newMaterialIdentifier != m_item->materialIdentifier()) {
73  itemWithMaterial()->materialItem()->disconnect(this);
75  m_ec->selectMaterial(m_item, newMaterialIdentifier);
76  createWidgets();
77  connect(itemWithMaterial()->materialItem(), &MaterialItem::dataChanged, this,
79  } else {
80  updateValues(); // necessary, since in the material editor the values could have been
81  // changed without selecting a different material
82 
83  // If the list of materials was edited (e.g. a material added), but the current was not
84  // changed, no modified signal would be sent. Check now for changes and emit if necessary.
85  if (GUI::Util::createBackup(&m_ec->sampleItem()->materialItems()) != materialsBackup)
86  m_ec->modified();
87  }
88 }
89 
91 {
92  auto* material = m_item->materialItem();
93  ASSERT(material);
94 
95  // -- Create UI for delta/beta resp. sldRe/sldIm
96  DoubleDescriptors values;
97  if (material->hasRefractiveIndex())
98  values << material->delta() << material->beta();
99  else
100  values << material->sldRe() << material->sldIm();
101 
102  int col = 0;
103  for (const auto& d : values) {
104  auto* editor = new DoubleLineEdit(this, d);
105  auto* label = new QLabel(d.label, this);
106  label->setBuddy(editor);
107 
108  QObject::connect(editor, &DoubleLineEdit::baseValueChanged,
109  [=](double newValue) { m_ec->setMaterialValue(m_item, newValue, d); });
110 
111  m_layout->addWidget(label, 0, col);
112  m_layout->addWidget(editor, 1, col++);
113  }
114 
115  // -- Create UI for magnetization vector
116  VectorDescriptor mag = material->magnetizationVector();
117  const auto setNewValue = [=](double value, DoubleDescriptor d) {
118  m_ec->setMaterialValue(m_item, value, d);
119  };
120  LayerEditorUtils::addVectorToGrid(m_layout, col, mag, setNewValue, true, false);
121 
122  // -- Create UI for material selection button
123  auto* btn = new QPushButton("...", this);
124  btn->setToolTip("Select material");
125  m_layout->addWidget(btn, 1, m_layout->columnCount());
126  connect(btn, &QPushButton::clicked, this, &MaterialInplaceForm::selectMaterial);
127 
128  m_layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding), 0, m_layout->columnCount());
129 }
130 
132 {
133  updateValues();
134 }
Defines GUI::Util namespace.
Defines class DoubleDescriptor.
QList< DoubleDescriptor > DoubleDescriptors
Defines class DoubleLineEdit.
Defines class DoubleSpinBox.
Defines global pointers.
Defines abstract item with a material property.
Defines class LayerEditorUtils.
Defines namespace GUI::Util::Layout.
Defines class MaterialEditorDialog.
Defines class MaterialInplaceForm.
Defines class MaterialItem.
Defines class MaterialItems.
Defines class MultiLayerItem.
Defines class SampleEditorController.
Defines class VectorDescriptor.
Describes properties of a double value which are necessary to allow GUI representation,...
LineEdit to edit values in a scientific notation, operating on a DoubleDescriptor.
void baseValueChanged(double newBaseValue)
Emitted whenever the value changes.
QString materialIdentifier() const
MaterialItem * materialItem() const
Returns the material item this item links to.
static QString chooseMaterial(QWidget *parent, MultiLayerItem *sample, const QString &identifierOfPreviousMaterial)
Use this to choose a material. identifierOfPreviousMaterial is the material which should be selected ...
ItemWithMaterial * m_item
SampleEditorController * m_ec
ItemWithMaterial * itemWithMaterial() const
MaterialInplaceForm(QWidget *parent, ItemWithMaterial *item, SampleEditorController *ec)
void dataChanged() const
MaterialItems & materialItems()
Class to modify a sample from the layer oriented sample editor.
MultiLayerItem * sampleItem() const
The item on which this controller operates.
void selectMaterial(ItemWithMaterial *item, const QString &newMaterialIdentifier)
void setMaterialValue(ItemWithMaterial *item, double newValue, DoubleDescriptor d)
Describes properties of a 3D vector, consisting of three double values.
static QMainWindow * mainWindow
Definition: Globals.h:22
void clearLayout(QLayout *layout, bool deleteWidgets=true)
Removes content from box layout.
Definition: LayoutUtils.cpp:68
QByteArray createBackup(const T *t)
Definition: Backup.h:24
void addVectorToGrid(QGridLayout *m_gridLayout, int firstCol, const VectorDescriptor &v, SampleEditorController *ec, bool vertically, bool addSpacer)
Create DoubleSpinBoxes for the DoubeDescriptors and connect them to SampleEditorController::setDouble...