BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
JobItemUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Data/JobItemUtils.cpp
6 //! @brief Implements class JobItemUtils
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 "Device/Histo/SimulationResult.h"
19 #include "GUI/Util/CoordName.h"
20 
21 namespace {
22 
23 //! Updates axes' titles
24 void updateAxesTitle(DataItem* intensityItem, const ICoordSystem& converter, Coords units)
25 {
26  intensityItem->setXaxisTitle(QString::fromStdString(converter.axisName(0, units)));
27  if (converter.rank() > 1)
28  intensityItem->setYaxisTitle(QString::fromStdString(converter.axisName(1, units)));
29 }
30 
31 } // namespace
32 
33 
34 //! Updates axes of Datafield in IntensityData item to correspond with ::P_AXES_UNITS selection.
35 //! InstrumentItem is used to get domain's detector map for given units.
36 
38  const InstrumentItem* instrumentItem)
39 {
40  ASSERT(intensityItem);
41 
42  if (!instrumentItem) {
43  // special case when reading old project files: project failed on load instrument
44  // but we want to try to recover the rest.
45  return;
46  }
47 
48  if (!intensityItem->getDatafield())
49  return;
50 
51  Coords requested_units = GUI::Util::CoordName::coordFromName(intensityItem->selectedCoords());
52 
53  auto* const converter = instrumentItem->createCoordSystem();
54 
55  auto newData = std::make_unique<Datafield>(converter->convertedAxes(requested_units));
56 
57  newData->setVector(intensityItem->getDatafield()->flatVector());
58 
59  intensityItem->setDatafield(newData.release());
60  intensityItem->setAxesRangeToData();
61  updateAxesTitle(intensityItem, *converter, requested_units);
62 }
63 
64 //! Sets axes units suitable for given instrument.
65 
67  const InstrumentItem* instrumentItem)
68 {
69  auto* const converter = instrumentItem->createCoordSystem();
70  if (!converter)
71  return;
72  setIntensityItemCoords(intensityItem, *converter);
73 }
74 
76  const ICoordSystem& converter)
77 {
78  ComboProperty combo = availableUnits(converter);
79  intensityItem->setAxesUnits(combo);
80 }
81 
83  const Instrument2DItem* instrumentItem)
84 {
85  auto* const converter = instrumentItem->createCoordSystem();
86  auto output_data = std::make_unique<Datafield>(converter->defaultAxes());
87  intensityItem->setDatafield(output_data.release());
88  setIntensityItemCoords(intensityItem, *converter);
89  updateAxesTitle(intensityItem, *converter, converter->defaultUnits());
90 }
91 
92 void GUI::Model::JobItemUtils::setResults(DataItem* intensityItem, const SimulationResult& result)
93 {
94  if (intensityItem->getDatafield() == nullptr) {
95  const auto& converter = result.converter();
96  GUI::Model::JobItemUtils::setIntensityItemCoords(intensityItem, converter);
97  updateAxesTitle(intensityItem, converter, converter.defaultUnits());
98  }
99  auto selected_units = GUI::Util::CoordName::coordFromName(intensityItem->selectedCoords());
100  intensityItem->setDatafield(result.datafield(selected_units));
101 }
102 
104 {
105  ComboProperty result;
106  for (auto units : converter.availableUnits()) {
107  auto unit_name = GUI::Util::CoordName::nameFromCoord(units);
108  if (!unit_name.isEmpty())
109  result << unit_name;
110  }
111 
112  result.setValue(GUI::Util::CoordName::nameFromCoord(converter.defaultUnits()));
113  return result;
114 }
Defines namespace GUI::Util::CoordName.
Declares class DataItem.
Defines class InstrumentItem and all its children.
Defines namespace GUI::Model::JobItemUtils.
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
void setValue(const QString &name)
Abstract base class for IntensityDataItem and SpecularDataItem. Owns one simulated data set of type D...
Definition: DataItem.h:34
virtual void setAxesRangeToData()=0
virtual void setDatafield(Datafield *data)=0
The given pointer becomes owned by this class!!
Definition: DataItem.cpp:20
virtual void setXaxisTitle(const QString &title)=0
Datafield * getDatafield()
Definition: DataItem.h:41
QString selectedCoords() const
Definition: DataItem.cpp:91
void setAxesUnits(const ComboProperty &units)
Definition: DataItem.cpp:96
virtual void setYaxisTitle(const QString &title)=0
Abstract base class for instrument-specific item classes.
virtual ICoordSystem * createCoordSystem() const =0
void updateAxesTitle(Data1DViewItem *view_item)
void createDefaultDetectorMap(DataItem *intensityItem, const Instrument2DItem *instrumentItem)
void setResults(DataItem *intensityItem, const SimulationResult &result)
Sets simulation results into the DataItem.
ComboProperty availableUnits(const ICoordSystem &converter)
void setIntensityItemCoords(DataItem *intensityItem, const InstrumentItem *instrumentItem)
Sets axes units suitable for given instrument.
void updateDataAxes(DataItem *intensityItem, const InstrumentItem *instrumentItem)
updates axes of Datafield in IntensityData item
Coords coordFromName(const QString &name)
Returns domain axes units type from their GUI name.
Definition: CoordName.cpp:46
QString nameFromCoord(Coords units)
Returns axes units names from their domain counterpart.
Definition: CoordName.cpp:36