BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SelectionContainerForm.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/SampleDesigner/SelectionContainerForm.h
6 //! @brief Defines class SelectionContainerForm
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 
15 #ifndef BORNAGAIN_GUI_VIEW_SAMPLEDESIGNER_SELECTIONCONTAINERFORM_H
16 #define BORNAGAIN_GUI_VIEW_SAMPLEDESIGNER_SELECTIONCONTAINERFORM_H
17 
22 #include <QComboBox>
23 #include <QGridLayout>
24 
25 //! Abstract widget base class to contain a selection, defined by a SelectionDescriptor.
26 //!
27 //! This abstract base class contains only the selection combo box and the infrastructure to
28 //! call the SampleEditorController to change the current selection. A derived class has to
29 //! implement the showing of the contents (method createContent).
30 class AbstractSelectionContainerForm : public QWidget {
31 public:
32  virtual void createContent() = 0;
33 
34 protected:
36  : QWidget(parent)
37  , m_ec(ec)
38  {
39  }
40 
41  template <typename T>
43  {
44  m_gridLayout = new QGridLayout(this);
45  m_gridLayout->setContentsMargins(0, 0, 0, 0);
46  m_gridLayout->setSpacing(6);
47 
48  m_combo = new QComboBox;
50  m_combo->addItems(d.options);
51  m_combo->setCurrentIndex(d.currentIndex());
52  m_combo->setMaxVisibleItems(m_combo->count());
53 
54  QObject::connect(m_combo, QOverload<int>::of(&QComboBox::currentIndexChanged),
55  [=](int current) {
56  clear();
57  m_ec->setCurrentIndex(this, current, d);
58  });
59 
60  m_gridLayout->addWidget(m_combo, 1, 0);
61  createContent();
62  }
63 
64 private:
65  //! Remove all descriptors from the layout
66  void clear()
67  {
68  auto* layoutItemOfComboBox = m_gridLayout->itemAtPosition(1, 0);
69  m_gridLayout->takeAt(m_gridLayout->indexOf(layoutItemOfComboBox));
71  m_gridLayout->addWidget(layoutItemOfComboBox->widget(), 1, 0);
72  }
73 
74 protected:
75  QGridLayout* m_gridLayout;
76  QComboBox* m_combo;
78 };
79 
80 //! A widget to contain a selection, defined by a SelectionDescriptor.
81 //!
82 //! This SelectionContainerForm is limited to contain the selection combo box and a list of double
83 //! values represented by DoubleDescriptors. The list of DoubleDescriptors is queried by calling
84 //! LayerEditorUtils::doubleDescriptorsOfItem(). To have the correct DoubleDescriptors on this form,
85 //! you may have to overload this method according to your class and your needs. The overload will
86 //! expect the template type you defined for SelectionDescriptor.
87 //!
88 //! Example:
89 //! SelectionDescriptor<RotationItem*> => LayerEditorUtils::doubleDescriptorsOfItem(RotationItem*)
90 //!
91 //! The connection from selection combo -> SelectionDescriptor is made via
92 //! SampleEditorController::setCurrentIndex(), where a command can be used for undo purposes.
93 //!
94 //! For each DoubleDescriptor, a unit-aware DoubleSpinBox is created. The connection from each
95 //! spinbox to the DoubleDescriptor is made via SampleEditorController::setDouble(), where a command
96 //! can be used for undo purposes.
97 //!
98 //! If a more complex selection shall be realized (e.g. with
99 //! sub-selections or different value types), this class is not sufficient.
101 public:
102  template <typename T>
105  : AbstractSelectionContainerForm(parent, ec)
106  {
107  if (d.currentItem != nullptr)
108  currentValues = [=] {
110  };
111  else
112  currentValues = [] { return DoubleDescriptors(); };
113 
114  initUI(d);
115  }
116 
117  virtual void createContent()
118  {
119  if (currentValues != nullptr)
121  }
122 
123 private:
124  function<DoubleDescriptors()> currentValues = nullptr;
125 };
126 
127 #endif // BORNAGAIN_GUI_VIEW_SAMPLEDESIGNER_SELECTIONCONTAINERFORM_H
Defines classes releted to event filtering.
QList< DoubleDescriptor > DoubleDescriptors
Defines class LayerEditorUtils.
Defines namespace GUI::Util::Layout.
Defines class SampleEditorController.
Abstract widget base class to contain a selection, defined by a SelectionDescriptor.
void clear()
Remove all descriptors from the layout.
AbstractSelectionContainerForm(QWidget *parent, SampleEditorController *ec)
void initUI(const SelectionDescriptor< T > &d)
virtual void createContent()=0
Class to modify a sample from the layer oriented sample editor.
void setCurrentIndex(AbstractSelectionContainerForm *widget, int index, const AbstractSelectionDescriptor &d)
A widget to contain a selection, defined by a SelectionDescriptor.
function< DoubleDescriptors()> currentValues
SelectionContainerForm(QWidget *parent, const SelectionDescriptor< T > &d, SampleEditorController *ec)
Describes a selection (various possibilities and the current one).
int currentIndex() const override
Get currently selected option.
function< T()> currentItem
Function to get currently selected item.
QStringList options
List of options, usually presented as combo entries.
static void install(QObject *obj)
void clearLayout(QLayout *layout, bool deleteWidgets=true)
Removes content from box layout.
Definition: LayoutUtils.cpp:68
DoubleDescriptors doubleDescriptorsOfItem(LayerBasicRoughnessItem *item)
void addMultiPropertyToGrid(QGridLayout *m_gridLayout, int firstCol, const DoubleDescriptors &valueDescriptors, SampleEditorController *ec, bool vertically, bool addSpacer)
Create DoubleSpinBoxes for the DoubeDescriptors and connect them to SampleEditorController::setDouble...