BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
PointwiseAxisItem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Device/PointwiseAxisItem.cpp
6 //! @brief Implements pointwise axis item
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 
16 #include "Base/Axis/PointwiseAxis.h"
17 #include "Device/Coord/ICoordSystem.h"
18 #include "Device/Data/Datafield.h"
19 #include "Device/IO/IOFactory.h"
20 #include "Device/IO/ReadWriteINT.h"
23 
25  : BasicAxisItem(M_TYPE)
26  , m_instrumentItem(nullptr)
27 {
28  minItem()->setEnabled(false);
29  binsItem()->setEnabled(false);
30  maxItem()->setEnabled(false);
32 }
33 
35 
37 {
38  QByteArray a;
39  if (s.xmlWriter())
40  a = serializeBinaryData();
41 
42  s.assertVersion(0);
43  Serialize::rwBaseClass<BasicAxisItem>(s, "base", this);
44  Serialize::rwSessionItem<QString>(s, "nativeUnits", getItem(P_NATIVE_AXIS_UNITS));
45  Serialize::rwValue(s, "axisData", a);
46 
47  if (s.xmlReader())
49 }
50 
51 void PointwiseAxisItem::init(const IAxis& axis, const QString& units_label)
52 {
53  m_axis = std::unique_ptr<IAxis>(axis.clone());
54  setItemValue(P_NATIVE_AXIS_UNITS, units_label);
55 }
56 
58 {
59  // only specular instruments are relevant
60  m_instrumentItem = dynamic_cast<const SpecularInstrumentItem*>(instrument);
61 }
62 
63 const IAxis* PointwiseAxisItem::axis() const
64 {
65  return m_axis.get();
66 }
67 
69 {
70  return getItemValue(P_NATIVE_AXIS_UNITS).toString();
71 }
72 
73 std::unique_ptr<IAxis> PointwiseAxisItem::createAxis(double scale) const
74 {
75  if (!checkValidity())
76  return nullptr;
77 
78  auto* const converter = m_instrumentItem->createCoordSystem();
79  std::unique_ptr<IAxis> converted_axis(converter->createConvertedAxis(0, Coords::DEGREES));
80 
81  // applying scaling
82  std::vector<double> centers = converted_axis->binCenters();
83  for (double& e : centers)
84  e *= scale;
85 
86  return std::make_unique<PointwiseAxis>(converted_axis->axisName(), std::move(centers));
87 }
88 
90 {
91  if (!m_axis)
92  return {};
93  Datafield axisData({m_axis->clone()});
94 
95  std::stringstream ss;
96  ReadWriteINT().writeDatafield(axisData, ss);
97  return QByteArray(ss.str().c_str(), static_cast<int>(ss.str().size()));
98 }
99 
100 void PointwiseAxisItem::deserializeBinaryData(const QByteArray& data)
101 {
102  if (data.isEmpty())
103  return;
104 
105  std::istringstream str(data.toStdString());
106  std::unique_ptr<Datafield> d(ReadWriteINT().readDatafield(str));
107  m_axis = std::unique_ptr<IAxis>(d->axis(0).clone());
108 }
109 
111 {
112  return m_axis && m_instrumentItem && getUnitsLabel() != "nbins";
113 }
114 
116 {
117  if (!checkValidity())
118  return;
119 
120  auto* const converter = m_instrumentItem->createCoordSystem();
121  setLowerBound(converter->calculateMin(0, Coords::DEGREES));
122  setUpperBound(converter->calculateMax(0, Coords::DEGREES));
123  setBinCount(static_cast<int>(m_axis->size()));
124 
125  emitDataChanged();
126 }
Defines class InstrumentItem and all its children.
Defines pointwise axis item.
Defines class Streamer.
SessionItem * maxItem() const
Definition: AxesItems.cpp:70
SessionItem * binsItem() const
Definition: AxesItems.cpp:36
SessionItem * minItem() const
Definition: AxesItems.cpp:53
void setBinCount(size_t value)
Definition: AxesItems.cpp:31
void setLowerBound(double value)
Definition: AxesItems.cpp:48
void setUpperBound(double value)
Definition: AxesItems.cpp:65
Abstract base class for instrument-specific item classes.
void deserializeBinaryData(const QByteArray &data) override
~PointwiseAxisItem() override
bool checkValidity() const
const SpecularInstrumentItem * m_instrumentItem
std::unique_ptr< IAxis > createAxis(double scale) const override
static constexpr auto P_NATIVE_AXIS_UNITS
void serialize(Streamer &s) override
void setOwningInstrument(const InstrumentItem *instrument)
std::unique_ptr< IAxis > m_axis
QByteArray serializeBinaryData() const override
const IAxis * axis() const
QString getUnitsLabel() const
void init(const IAxis &axis, const QString &units_label)
SessionItem * addProperty(const QString &name, const QVariant &variant)
Add new property item and register new tag. name is the tag name and the display name....
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
void setItemValue(const QString &tag, const QVariant &variant) const
Directly set value of item under given tag.
void emitDataChanged(int role=Qt::DisplayRole)
Notify model about data changes.
void setEnabled(bool enabled)
Flags accessors.
SessionItem * getItem(const QString &tag="", int row=0) const
Returns item in given row of given tag.
ICoordSystem * createCoordSystem() const override
Supports serialization to or deserialization from QXmlStream.
Definition: Streamer.h:36
QXmlStreamWriter * xmlWriter()
Returns stream writer or nullptr.
Definition: Streamer.h:47
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
void rwValue(Streamer &s, const QString &tag, bool &val)
Definition: Serialize.cpp:19