BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
BeamWavelengthItem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Device/BeamWavelengthItem.cpp
6 //! @brief Implements class BeamWavelengthItem
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 
18 
19 namespace {
20 
21 const double default_wl = 0.1;
22 
23 void initDistribution(DistributionItem* newDistribution,
24  const DistributionItem* currentDistribution)
25 {
26  newDistribution->setLimits(RealLimits::positive());
27  newDistribution->setUnit(Unit::nanometer);
28 
29  double meanVal = default_wl;
30  if (auto* cd = dynamic_cast<const SymmetricResolutionItem*>(currentDistribution))
31  meanVal = cd->mean();
32 
33  if (auto* nd = dynamic_cast<SymmetricResolutionItem*>(newDistribution)) {
34  nd->setMean(meanVal);
35  nd->setMeanDecimals(4);
36  }
37 
38  newDistribution->initDistribution(meanVal);
39 };
40 
41 } // namespace
42 
44  : BeamWavelengthItem(false)
45 {
46 }
47 
48 BeamWavelengthItem::BeamWavelengthItem(bool onlySymmetricDistributions)
49 {
51  "Distribution", "", "distribution",
52  onlySymmetricDistributions ? DistributionItemCatalog::symmetricTypes()
54  initDistribution);
55 }
56 
58 {
59  s.assertVersion(0);
60  Serialize::rwSelected<DistributionItemCatalog>(s, m_distribution);
61 
62  if (s.xmlReader()) {
63  m_distribution->setLimits(RealLimits::positive());
65  }
66 }
67 
69 {
71 }
72 
73 // ------------------------------------------------------------------------------------------------
74 
76  : BeamWavelengthItem(true)
77 {
78 }
79 
80 void SpecularBeamWavelengthItem::setToRange(const RealLimits& limits)
81 {
82  if (auto* symmetricDistribution = dynamic_cast<SymmetricResolutionItem*>(distribution())) {
83  if (!limits.isInRange(wavelength())) {
84  const double new_value =
85  limits.isLimited() ? (limits.upperLimit() - limits.lowerLimit()) / 2. : default_wl;
86  symmetricDistribution->setMean(new_value);
87  }
88  symmetricDistribution->setLimits(limits);
89  }
90 }
Defines class BeamWavelengthItem.
Defines class DistributionItemCatalog.
Defines class Streamer.
@ nanometer
DistributionItem * distribution() const
SelectionProperty< DistributionItem * > m_distribution
virtual double meanValue() const
double wavelength() const
Returns wavelength. In the case of distribution applied, returns its mean.
void serialize(Streamer &s) override
static QVector< Type > symmetricTypes()
Types of symmetric distributions.
static QVector< Type > types()
Available types of items.
virtual void setLimits(const RealLimits &limits)
virtual void initDistribution(double)
virtual void setUnit(const variant< QString, Unit > &unit)=0
Set the unit of the distributed value.
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.
void setToRange(const RealLimits &limits)
Supports serialization to or deserialization from QXmlStream.
Definition: Streamer.h:36
QXmlStreamReader * xmlReader()
Returns stream reader or nullptr.
Definition: Streamer.h:48
void assertVersion(unsigned expectedVersion) const
As reader, throws DeserializationException unless the expected version is read. As writer,...
Definition: Streamer.cpp:26