BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SelectionDescriptor< T > Class Template Reference

Description

template<typename T>
class SelectionDescriptor< T >

Describes a selection (various possibilities and the current one).

Usually a selection is presented as a combo box. Right now with SessionModel still in place, in many cases a selection changes the current item of a group item, i.e. it changes the class of a certain child item (e.g. XRotationItem*‍/YRotationItem*‍/...). Also a SessionItem holding only a ComboProperty (but not switching children like GroupItem) can be wrapped. For example IntensityDataItem::gradient() is simply a list of QStrings.

With the migration, more and more SelectionDescriptor operate on a SelectionProperty. Please refer to this class for more information.

The template parameter defines the type of the current item. This can be a pointer to a common base class (like RotationItem*), but it also can be a std::variant<...>, which is useful if no dedicated common base class exists (like for the roughness items LayerZeroRoughnessItem and LayerBasicRoughnessItem). If not used with a GroupItem, but with a "normal" ComboProperty holder, the template parameter can be a QString, so currentItem() returns the currently selected string.

Note that this class does not provide (implement) a selection, but provide information about a selection. For implementing a selection, please see SelectionProperty.

By using this class, the underlying data scheme is hidden from the user of the data. This e.g. eases SessionItem migration. The underlying implementation can be a GroupItem, a simple pointer member, a std::variant or any other construction to define a selection.

For easy UI creation, there are functions like GUI:Util::createSelectionCombo() which take a descriptor and fully initialize the created combo box.

See also
SelectionProperty

Definition at line 70 of file SelectionDescriptor.h.

Inheritance diagram for SelectionDescriptor< T >:
[legend]
Collaboration diagram for SelectionDescriptor< T >:
[legend]

Public Member Functions

 SelectionDescriptor ()=default
 
 SelectionDescriptor (SessionItem *item)
 Initialize the members by means of a SessionItem containing a ComboProperty. This can be a GroupItem or any other property. More...
 
int currentIndex () const override
 Get currently selected option. More...
 
 operator T () const
 
void setCurrentIndex (int newIndex) const override
 Set currently selected option. More...
 

Public Attributes

function< int()> currentIndexGetter
 Function to get currently selected option. More...
 
function< void(int)> currentIndexSetter
 Function to set currently selected option. More...
 
function< T()> currentItem
 Function to get currently selected item. More...
 
QString label
 A label text (short, no trailing colon) More...
 
QStringList options
 List of options, usually presented as combo entries. More...
 
QString tooltip
 Tooltip text. More...
 

Constructor & Destructor Documentation

◆ SelectionDescriptor() [1/2]

template<typename T >
SelectionDescriptor< T >::SelectionDescriptor ( )
default

◆ SelectionDescriptor() [2/2]

template<typename T >
SelectionDescriptor< T >::SelectionDescriptor ( SessionItem item)
inlineexplicit

Initialize the members by means of a SessionItem containing a ComboProperty. This can be a GroupItem or any other property.

currentItem can only be initialized if the template parameter is a pointer (like RotationItem*). If it is e.g. a std::variant<>, the currentItem has to be initialized by the caller. Only for easier migration. Should be removed after SessionItem refactoring.

Definition at line 80 of file SelectionDescriptor.h.

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  }
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
void setCurrentIndex(int index)
int currentIndex() const override
Get currently selected option.
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.
QString tooltip
Tooltip text.
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

References SelectionDescriptor< T >::currentIndex(), SelectionDescriptor< T >::currentIndexGetter, SelectionDescriptor< T >::currentIndexSetter, SelectionDescriptor< T >::currentItem, SessionItem::displayName(), SelectionDescriptor< T >::label, SelectionDescriptor< T >::options, ComboProperty::setCurrentIndex(), SessionItem::setValue(), SessionItem::toolTip(), SelectionDescriptor< T >::tooltip, and SessionItem::value().

Here is the call graph for this function:

Member Function Documentation

◆ currentIndex()

◆ operator T()

template<typename T >
SelectionDescriptor< T >::operator T ( ) const
inline

Definition at line 102 of file SelectionDescriptor.h.

102 { return currentItem(); }

References SelectionDescriptor< T >::currentItem.

◆ setCurrentIndex()

template<typename T >
void SelectionDescriptor< T >::setCurrentIndex ( int  newIndex) const
inlineoverridevirtual

Member Data Documentation

◆ currentIndexGetter

template<typename T >
function<int()> SelectionDescriptor< T >::currentIndexGetter

◆ currentIndexSetter

template<typename T >
function<void(int)> SelectionDescriptor< T >::currentIndexSetter

◆ currentItem

template<typename T >
function<T()> SelectionDescriptor< T >::currentItem

◆ label

template<typename T >
QString SelectionDescriptor< T >::label

◆ options

◆ tooltip


The documentation for this class was generated from the following file: