BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SelectionDescriptor.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Descriptor/SelectionDescriptor.h
6 //! @brief Defines class SelectionDescriptor
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_MODEL_DESCRIPTOR_SELECTIONDESCRIPTOR_H
16 #define BORNAGAIN_GUI_MODEL_DESCRIPTOR_SELECTIONDESCRIPTOR_H
17 
19 #include "GUI/Util/ComboProperty.h"
20 #include <QString>
21 #include <QStringList>
22 #include <functional>
23 
24 using std::function;
25 
26 //! Abstract base class for SelectionDescriptor to ease referencing.
28 public:
29  virtual ~AbstractSelectionDescriptor() = default;
30 
31  //! Set currently selected option
32  virtual void setCurrentIndex(int newIndex) const = 0;
33 
34  //! Get currently selected option
35  virtual int currentIndex() const = 0;
36 };
37 
38 //! Describes a selection (various possibilities and the current one).
39 //!
40 //! Usually a selection is presented as a combo box.
41 //! Right now with SessionModel still in place, in many cases a selection changes the current item
42 //! of a group item, i.e. it changes the class of a certain child item (e.g.
43 //! XRotationItem*/YRotationItem*/...).
44 //! Also a SessionItem holding only a ComboProperty (but not switching children like GroupItem) can
45 //! be wrapped. For example IntensityDataItem::gradient() is simply a list of QStrings.
46 //!
47 //! With the migration, more and more SelectionDescriptor operate on a SelectionProperty. Please
48 //! refer to this class for more information.
49 //!
50 //! The template parameter defines the type of the current item. This can be a pointer to a common
51 //! base class (like RotationItem*), but it also can be a std::variant<...>, which is useful if
52 //! no dedicated common base class exists (like for the roughness items LayerZeroRoughnessItem and
53 //! LayerBasicRoughnessItem).
54 //! If not used with a GroupItem, but with a "normal" ComboProperty holder, the template parameter
55 //! can be a QString, so currentItem() returns the currently selected string.
56 //!
57 //! Note that this class does not provide (*implement*) a selection, but *provide
58 //! information* about a selection. For implementing a selection, please see SelectionProperty.
59 //!
60 //! By using this class, the underlying data scheme is hidden from the user of the data. This e.g.
61 //! eases SessionItem migration. The underlying implementation can be a GroupItem, a simple pointer
62 //! member, a std::variant or any other construction to define a selection.
63 //!
64 //! For easy UI creation, there are functions like GUI:Util::createSelectionCombo() which take a
65 //! descriptor and fully initialize the created combo box.
66 //!
67 //! \sa SelectionProperty
68 //!
69 template <typename T>
71 public:
72  SelectionDescriptor() = default;
73 
74  //! Initialize the members by means of a SessionItem containing a ComboProperty. This can be
75  //! a GroupItem or any other property.
76  //!
77  //! currentItem can only be initialized if the template parameter is a pointer (like
78  //! RotationItem*). If it is e.g. a std::variant<>, the currentItem has to be initialized by the
79  //! caller. Only for easier migration. Should be removed after SessionItem refactoring.
81  {
82  label = item->displayName();
83  tooltip = item->toolTip();
84  options = item->value().value<ComboProperty>().getValues();
85  currentIndexSetter = [=](int index) {
86  auto comboProperty = item->value().value<ComboProperty>();
87 
88  if (comboProperty.currentIndex() != index) {
89  comboProperty.setCurrentIndex(index);
90  item->setValue(QVariant::fromValue<ComboProperty>(comboProperty));
91  }
92  };
93  currentIndexGetter = [=] { return item->value().value<ComboProperty>().currentIndex(); };
94 
95  if constexpr (std::is_pointer<T>::value) {
96  if (auto* groupItem = dynamic_cast<GroupItem*>(item))
97  currentItem = [=] { return dynamic_cast<T>(groupItem->currentItem()); };
98  } else if constexpr (std::is_same<T, QString>::value)
99  currentItem = [=] { return item->value().value<ComboProperty>().getValue(); };
100  }
101 
102  operator T() const { return currentItem(); }
103 
104  void setCurrentIndex(int newIndex) const override { currentIndexSetter(newIndex); }
105 
106  int currentIndex() const override { return currentIndexGetter(); }
107 
108  QString label; //!< A label text (short, no trailing colon)
109  QString tooltip; //!< Tooltip text
110  QStringList options; //!< List of options, usually presented as combo entries
111  function<void(int)> currentIndexSetter; //!< Function to set currently selected option
112  function<int()> currentIndexGetter; //!< Function to get currently selected option
113  function<T()> currentItem; //!< Function to get currently selected item
114 };
115 
116 #endif // BORNAGAIN_GUI_MODEL_DESCRIPTOR_SELECTIONDESCRIPTOR_H
Defines class ComboProperty.
Defines class GroupItem.
Abstract base class for SelectionDescriptor to ease referencing.
virtual ~AbstractSelectionDescriptor()=default
virtual int currentIndex() const =0
Get currently selected option.
virtual void setCurrentIndex(int newIndex) const =0
Set currently selected option.
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
void setCurrentIndex(int index)
Describes a selection (various possibilities and the current one).
int currentIndex() const override
Get currently selected option.
SelectionDescriptor()=default
function< int()> currentIndexGetter
Function to get currently selected option.
QString label
A label text (short, no trailing colon)
function< T()> currentItem
Function to get currently selected item.
function< void(int)> currentIndexSetter
Function to set currently selected option.
QStringList options
List of options, usually presented as combo entries.
void setCurrentIndex(int newIndex) const override
Set currently selected option.
SelectionDescriptor(SessionItem *item)
Initialize the members by means of a SessionItem containing a ComboProperty. This can be a GroupItem ...
QString tooltip
Tooltip text.
Base class for a GUI data item.
Definition: SessionItem.h:204
QString displayName() const
Get display name of item, append index if ambigue.
QVariant value() const
Get value.
bool setValue(QVariant value)
Set value, ensure that variant types match.
QString toolTip() const