BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
DetectorAlignmentEditor.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Instrument/DetectorAlignmentEditor.cpp
6 //! @brief Implements class DetectorAlignmentEditor
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
19 #include <QComboBox>
20 #include <QFormLayout>
21 #include <QLabel>
22 
23 namespace {
24 QString alignmentDescription(RectangularDetector::EDetectorArrangement a)
25 {
26  switch (a) {
27  case RectangularDetector::GENERIC:
28  return "Intersection of normal and detector";
29  case RectangularDetector::PERPENDICULAR_TO_SAMPLE:
30  return "Intersection of sample x-axis and detector";
31  case RectangularDetector::PERPENDICULAR_TO_DIRECT_BEAM:
32  return "Intersection of direct beam and detector";
33  case RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM:
34  return "Intersection of reflected beam and detector";
35  case RectangularDetector::PERPENDICULAR_TO_REFLECTED_BEAM_DPOS:
36  return "Intersection of direct beam and detector";
37  default:
38  ASSERT(false);
39  }
40 }
41 
42 } // namespace
43 
45  : QWidget(parent)
46  , m_item(item)
47 {
48  ASSERT(m_item);
49  m_formLayout = new QFormLayout(this);
50  m_formLayout->setContentsMargins(0, 15, 0, 0);
51  m_formLayout->setSpacing(8);
52 
53  auto* m_combo =
55  createAligmentWidgets();
56  emit dataChanged();
57  });
58  m_formLayout->addRow("Alignment:", m_combo);
59 
61 }
62 
64  const DoubleDescriptor& d)
65 {
66  auto* sb = GUI::Util::createSpinBox(parentFormLayout, d);
67  connect(sb, &DoubleSpinBox::baseValueChanged, [=](double v) {
68  if (d.get() != v) {
69  d.set(v);
70  emit dataChanged();
71  }
72  });
73  return sb;
74 }
75 
77 {
78  auto* sb = new DoubleSpinBox(parent, d);
79  connect(sb, &DoubleSpinBox::baseValueChanged, [=](double v) {
80  if (d.get() != v) {
81  d.set(v);
82  emit dataChanged();
83  }
84  });
85  return sb;
86 }
87 
88 void DetectorAlignmentEditor::addVector(QFormLayout* parentLayout, const VectorDescriptor& d)
89 {
90  auto* layout = new QHBoxLayout;
91 
92  const auto add = [&](const DoubleDescriptor& d) {
93  layout->addWidget(new QLabel(GUI::Util::labelWithUnit(d) + ":"));
94  layout->addWidget(createSpinBox(parentLayout->parentWidget(), d));
95  };
96 
97  add(d.x);
98  add(d.y);
99  add(d.z);
100 
101  layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding));
102 
103  parentLayout->addRow(d.label + ":", layout);
104 }
105 
107 {
108  while (m_formLayout->rowCount() > 1)
109  m_formLayout->removeRow(1);
110 
111  const QString descr = alignmentDescription(m_item->detectorAlignment());
112  auto* layout = new QFormLayout;
113  layout->setFieldGrowthPolicy(QFormLayout::FieldsStayAtSizeHint);
114  layout->setContentsMargins(0, 0, 0, 0);
115  m_formLayout->addRow("", layout);
116 
117  if (m_item->detectorAlignment() == RectangularDetector::GENERIC) {
118  addVector(layout, m_item->normalVector());
119  addVector(layout, m_item->directionVector());
120  auto* layoutUV = new QFormLayout;
121  createSpinBox(layoutUV, m_item->u0());
122  createSpinBox(layoutUV, m_item->v0());
123  layout->addRow(descr + ":", layoutUV);
124  } else {
125  auto* layoutUVD = new QFormLayout;
126  createSpinBox(layoutUVD, m_item->u0());
127  createSpinBox(layoutUVD, m_item->v0());
128  createSpinBox(layoutUVD, m_item->distance());
129  layout->addRow(descr + ":", layoutUVD);
130  }
131 }
A widget for editing the alignment of a detector.
Defines class DoubleSpinBox.
Defines class RectangularDetectorItem.
Defines GUI::Util namespace.
RectangularDetectorItem * m_item
DetectorAlignmentEditor(QWidget *parent, RectangularDetectorItem *item)
DoubleSpinBox * createSpinBox(QFormLayout *parentFormLayout, const DoubleDescriptor &d)
void addVector(QFormLayout *parentLayout, const VectorDescriptor &d)
Describes properties of a double value which are necessary to allow GUI representation,...
function< double()> get
function to get the current value
SpinBox for DoubleDescriptors, supporting units.
Definition: DoubleSpinBox.h:22
void baseValueChanged(double newBaseValue)
Emitted whenever the value changes.
SelectionDescriptor< RectangularDetector::EDetectorArrangement > detectorAlignmentSelection() const
RectangularDetector::EDetectorArrangement detectorAlignment() const
Describes properties of a 3D vector, consisting of three double values.
DoubleDescriptor y
DoubleDescriptor x
QString label
A label text (short, no trailing colon)
DoubleDescriptor z
QComboBox * createSelectionCombo(QWidget *parent, const SelectionDescriptor< T > d, std::function< void(int)> slot=nullptr)
Create a combo box with the information found in a selection descriptor.
Definition: WidgetUtils.h:45
QSpinBox * createSpinBox(QWidget *parent, const UIntDescriptor &d, std::function< void(uint)> slot=nullptr)
Create a spin box with the information found in a UIntDescriptor.
Definition: WidgetUtils.cpp:24
QString labelWithUnit(const QString &label, std::variant< QString, Unit > unit)
Create a label with an optional unit in brackets.