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

Description

template<typename T>
class SelectionProperty< T >

Class for representing a selection, its attributes and its accessors.

A "selection" in this context is a class instance out of a range of possible class instances.

Example: A distribution can be represented by DistributionGateItem, DistributionLorentzItem and more. The common base class is DistributionItem. To store the currently distribution, you may use a member of DistributionItem* which than holds an instance of the currently selected distribution (e.g. DistributionGateItem*). To select between distributions, you need a combo box filled with the distribution names, a label for the combo box, and so on.

This class provides many of these attributes and functionalities:

  • A pointer to the common base class (given as the template argument T). The lifetime of this pointer is handled in here.
  • label: a label of e.g. a spin box
  • tooltip: tooltip for e.g. a spin box
  • persistent tag: a name to serialize this property. Do not change this string, since it is serialized!
  • A list of available options (e.g. the names of the distributions)
  • A descriptor which provides some of the given information, as well as getters/setters.

For the initialization of a SelectionProperty there are helper methods. They are not necessarily to be called if they are not convenient. They are only there to help initialization e.g by using a catalog. If no catalog exists for the given case, the initialization can be done by any means.

This means especially that this class is not relying on the existence of a related catalog class - a catalog helps but is not mandatory.

See also
SelectionDescriptor

Definition at line 52 of file SelectionProperty.h.

Public Member Functions

 ~SelectionProperty ()
 
SelectionDescriptor< T > descriptor () const
 Returns a descriptor working on this property. More...
 
get () const
 Direct access to the stored pointer. More...
 
template<typename Catalog , typename... ArgsForCreation>
void init (const QString &label, const QString &tooltip, const QString &persistentTag, ArgsForCreation... argsForCreation)
 Initialize by means of a catalog class and optional creation arguments. More...
 
template<typename Catalog >
void initWithInitializer (const QString &label, const QString &tooltip, const QString &persistentTag, const QVector< typename Catalog::Type > &types, std::function< void(T newItem, const T oldItem)> initializer)
 Initialize by means of a catalog class, a subsection of allowed types and an initializer function. More...
 
template<typename Catalog >
void initWithInitializer (const QString &label, const QString &tooltip, const QString &persistentTag, std::function< void(T newItem, const T oldItem)> initializer)
 Initialize by means of a catalog class and an initializer function. More...
 
 operator SelectionDescriptor< T > () const
 Cast to a descriptor working on this property. More...
 
operator-> () const
 Direct access to the stored pointer. More...
 
QString persistentTag () const
 Persistent tag for serializing. More...
 
template<typename S , typename... ArgsForConstructor>
S * set (ArgsForConstructor... argsForConstructor)
 Directly set the new item. More...
 
void set (T t, bool callInitializer=false)
 Directly set the new item. More...
 

Private Member Functions

template<typename Catalog , typename... ArgsForCreation>
void initDescriptor (const QString &label, const QString &tooltip, const QVector< typename Catalog::Type > &types, ArgsForCreation... argsForCreation)
 

Private Attributes

SelectionDescriptor< T > m_descriptor
 descriptor, holding attributes like label, tooltip More...
 
std::function< void(T newItem, const T oldItem)> m_initializer
 initializer function. Can be empty. The first argument is the new item, the second is the old one if present; can be null. This is intended to maybe copy values from the old to the new selection. oldItem also can be ignored if not relevant, More...
 
m_p = nullptr
 Current selection. More...
 
QString m_persistentTag
 Persistent tag for serializing. More...
 

Constructor & Destructor Documentation

◆ ~SelectionProperty()

template<typename T >
SelectionProperty< T >::~SelectionProperty ( )
inline

Definition at line 54 of file SelectionProperty.h.

54 { delete m_p; }
T m_p
Current selection.

References SelectionProperty< T >::m_p.

Member Function Documentation

◆ descriptor()

template<typename T >
SelectionDescriptor<T> SelectionProperty< T >::descriptor ( ) const
inline

Returns a descriptor working on this property.

Definition at line 120 of file SelectionProperty.h.

120 { return m_descriptor; }
SelectionDescriptor< T > m_descriptor
descriptor, holding attributes like label, tooltip

References SelectionProperty< T >::m_descriptor.

◆ get()

◆ init()

template<typename T >
template<typename Catalog , typename... ArgsForCreation>
void SelectionProperty< T >::init ( const QString &  label,
const QString &  tooltip,
const QString &  persistentTag,
ArgsForCreation...  argsForCreation 
)
inline

Initialize by means of a catalog class and optional creation arguments.

The current selection will be initialized with the first type in the catalog types. The optional arguments are the arguments which may be necessary for the creation method in the catalog.

Definition at line 62 of file SelectionProperty.h.

64  {
66  initDescriptor<Catalog>(label, tooltip, Catalog::types(), argsForCreation...);
67  m_descriptor.setCurrentIndex(0);
68  }
QString m_persistentTag
Persistent tag for serializing.
QString persistentTag() const
Persistent tag for serializing.

References SelectionProperty< T >::m_descriptor, SelectionProperty< T >::m_persistentTag, and SelectionProperty< T >::persistentTag().

Referenced by Instrument2DItem::Instrument2DItem(), InstrumentItem::InstrumentItem(), Interference1DLatticeItem::Interference1DLatticeItem(), Interference2DAbstractLatticeItem::Interference2DAbstractLatticeItem(), Interference2DLatticeItem::Interference2DLatticeItem(), Interference2DParaCrystalItem::Interference2DParaCrystalItem(), InterferenceRadialParaCrystalItem::InterferenceRadialParaCrystalItem(), ItemWithParticles::ItemWithParticles(), MesoCrystalItem::MesoCrystalItem(), ParticleItem::ParticleItem(), ParticleLayoutItem::ParticleLayoutItem(), and SpecularBeamItem::SpecularBeamItem().

Here is the call graph for this function:

◆ initDescriptor()

template<typename T >
template<typename Catalog , typename... ArgsForCreation>
void SelectionProperty< T >::initDescriptor ( const QString &  label,
const QString &  tooltip,
const QVector< typename Catalog::Type > &  types,
ArgsForCreation...  argsForCreation 
)
inlineprivate

Definition at line 157 of file SelectionProperty.h.

161  {
162  auto setCurrentIndex = [=](int current) {
163  auto* p = Catalog::create(types[current], argsForCreation...);
164  if (m_initializer)
165  m_initializer(p, m_p);
166  m_p = p;
167  };
168 
169  m_descriptor.label = label;
170  m_descriptor.tooltip = tooltip;
171  for (const auto type : types)
172  m_descriptor.options << Catalog::uiInfo(type).menuEntry;
173  m_descriptor.currentIndexSetter = setCurrentIndex;
174  m_descriptor.currentIndexGetter = [=]() { return types.indexOf(Catalog::type(m_p)); };
175  m_descriptor.currentItem = [=] { return m_p; };
176  }
std::function< void(T newItem, const T oldItem)> m_initializer
initializer function. Can be empty. The first argument is the new item, the second is the old one if ...

References SelectionProperty< T >::m_descriptor, SelectionProperty< T >::m_initializer, and SelectionProperty< T >::m_p.

◆ initWithInitializer() [1/2]

template<typename T >
template<typename Catalog >
void SelectionProperty< T >::initWithInitializer ( const QString &  label,
const QString &  tooltip,
const QString &  persistentTag,
const QVector< typename Catalog::Type > &  types,
std::function< void(T newItem, const T oldItem)>  initializer 
)
inline

Initialize by means of a catalog class, a subsection of allowed types and an initializer function.

Same as before, but only a subset of types available in a catalog shall be used. The current selection will be initialized with the first type in the given types subset. Each newly created item can be initialized with the given initializer method. Note that the item creation will take place also after a call to this method, so take care that the given initializer stays valid throughout the lifetime of this SelectionProperty instance.

The initializer function takes two arguments: The first is the new item, the second is the old one (if present; can be null). This is intended to maybe copy values from the old to the new selection. The old item also can be ignored, always according to the current needs.

Definition at line 107 of file SelectionProperty.h.

111  {
112  m_initializer = initializer;
114 
115  initDescriptor<Catalog>(label, tooltip, types);
116  m_descriptor.setCurrentIndex(0);
117  }

References SelectionProperty< T >::m_descriptor, SelectionProperty< T >::m_initializer, SelectionProperty< T >::m_persistentTag, and SelectionProperty< T >::persistentTag().

Here is the call graph for this function:

◆ initWithInitializer() [2/2]

template<typename T >
template<typename Catalog >
void SelectionProperty< T >::initWithInitializer ( const QString &  label,
const QString &  tooltip,
const QString &  persistentTag,
std::function< void(T newItem, const T oldItem)>  initializer 
)
inline

Initialize by means of a catalog class and an initializer function.

The current selection will be initialized with the first type in the catalog types. Each newly created item can be initialized with the given initializer method. Note that the item creation will take place also after a call to this method, so take care that the given initializer stays valid throughout the lifetime of this SelectionProperty instance.

The initializer function takes two arguments: The first is the new item, the second is the old one (if present; can be null). This is intended to maybe copy values from the old to the new selection. The old item also can be ignored, always according to the current needs.

Definition at line 82 of file SelectionProperty.h.

85  {
86  m_initializer = initializer;
88 
89  initDescriptor<Catalog>(label, tooltip, Catalog::types());
90  m_descriptor.setCurrentIndex(0);
91  }

References SelectionProperty< T >::m_descriptor, SelectionProperty< T >::m_initializer, SelectionProperty< T >::m_persistentTag, and SelectionProperty< T >::persistentTag().

Referenced by BeamAzimuthalAngleItem::BeamAzimuthalAngleItem(), BeamInclinationAngleItem::BeamInclinationAngleItem(), BeamWavelengthItem::BeamWavelengthItem(), RectangularDetectorItem::RectangularDetectorItem(), SpecularBeamInclinationItem::SpecularBeamInclinationItem(), and SphericalDetectorItem::SphericalDetectorItem().

Here is the call graph for this function:

◆ operator SelectionDescriptor< T >()

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

Cast to a descriptor working on this property.

Definition at line 123 of file SelectionProperty.h.

123 { return m_descriptor; }

◆ operator->()

template<typename T >
T SelectionProperty< T >::operator-> ( ) const
inline

Direct access to the stored pointer.

Definition at line 126 of file SelectionProperty.h.

126 { return m_p; }

References SelectionProperty< T >::m_p.

◆ persistentTag()

template<typename T >
QString SelectionProperty< T >::persistentTag ( ) const
inline

Persistent tag for serializing.

Definition at line 152 of file SelectionProperty.h.

152 { return m_persistentTag; }

References SelectionProperty< T >::m_persistentTag.

Referenced by SelectionProperty< T >::init(), SelectionProperty< T >::initWithInitializer(), and Serialize::rwSelected().

◆ set() [1/2]

template<typename T >
template<typename S , typename... ArgsForConstructor>
S* SelectionProperty< T >::set ( ArgsForConstructor...  argsForConstructor)
inline

Directly set the new item.

Definition at line 142 of file SelectionProperty.h.

143  {
144  S* s = new S(argsForConstructor...);
145  if (s != nullptr && m_initializer)
146  m_initializer(s, m_p);
147  m_p = s;
148  return s;
149  }

References SelectionProperty< T >::m_initializer, and SelectionProperty< T >::m_p.

◆ set() [2/2]

Member Data Documentation

◆ m_descriptor

◆ m_initializer

template<typename T >
std::function<void(T newItem, const T oldItem)> SelectionProperty< T >::m_initializer
private

initializer function. Can be empty. The first argument is the new item, the second is the old one if present; can be null. This is intended to maybe copy values from the old to the new selection. oldItem also can be ignored if not relevant,

Definition at line 187 of file SelectionProperty.h.

Referenced by SelectionProperty< T >::initDescriptor(), SelectionProperty< T >::initWithInitializer(), and SelectionProperty< T >::set().

◆ m_p

◆ m_persistentTag

template<typename T >
QString SelectionProperty< T >::m_persistentTag
private

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