BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SpecularDataItem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Data/SpecularDataItem.cpp
6 //! @brief Implements class SpecularDataItem
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/IAxis.h"
20 #include "GUI/Util/Error.h"
21 
22 namespace {
23 
24 const QString x_axis_default_name = "X [nbins]";
25 const QString y_axis_default_name = "Signal [a.u.]";
26 
27 } // namespace
28 
30  : DataItem(M_TYPE)
31 {
32  addProperty(P_TITLE, QString());
33 
34  addProperty<BasicAxisItem>(P_XAXIS);
35 
36  auto* amplitudeAxis = addProperty<AmplitudeAxisItem>(P_YAXIS);
37  amplitudeAxis->visibilityItem()->setValue(true);
38 
41 }
42 
43 void SpecularDataItem::setDatafield(Datafield* data)
44 {
45  if (data != nullptr) {
46  if (data->rank() != 1)
47  throw Error("Error in SpecularDataItem::setDatafield: cannot handle non-1D data");
50  } else {
52  }
53 
55 }
56 
58 {
59  return xAxisItem()->binCount();
60 }
61 
63 {
64  return xAxisItem()->min();
65 }
66 
68 {
69  return xAxisItem()->max();
70 }
71 
72 double SpecularDataItem::xMin() const
73 {
74  const double defaultXmin(0.0);
75  return m_data ? m_data->axis(0).min() : defaultXmin;
76 }
77 
78 double SpecularDataItem::xMax() const
79 {
80  const double defaultXmax(1.0);
81  return m_data ? m_data->axis(0).max() : defaultXmax;
82 }
83 
85 {
86  return yAxisItem()->min();
87 }
88 
90 {
91  return yAxisItem()->max();
92 }
93 
94 double SpecularDataItem::yMin() const
95 {
96  return dataRange().first;
97 }
98 
99 double SpecularDataItem::yMax() const
100 {
101  return dataRange().second;
102 }
103 
105 {
106  return yAxisItem()->isLogScale();
107 }
108 
110 {
111  return xAxisItem()->title();
112 }
113 
115 {
116  return yAxisItem()->title();
117 }
118 
119 void SpecularDataItem::setXaxisTitle(const QString& title)
120 {
121  xAxisItem()->setTitle(title);
122 }
123 
124 void SpecularDataItem::setYaxisTitle(const QString& title)
125 {
126  yAxisItem()->setTitle(title);
127 }
128 
129 //! set zoom range of x,y axes to axes of input data
131 {
132  setLowerX(xMin());
133  setUpperX(xMax());
134  setLowerY(yMin());
135  setUpperY(yMax());
136 }
137 
139 {
141 }
142 
143 std::vector<int> SpecularDataItem::shape() const
144 {
145  return {getNbins()};
146 }
147 
149 {
150  ComboProperty combo = ComboProperty() << data.unitsLabel();
151  setAxesUnits(combo);
152  setXaxisTitle(data.axisLabel(0));
153  setYaxisTitle(data.axisLabel(1));
154  setDatafield(std::move(data).intensityData().release());
156 }
157 
159 {
160  return name == P_XAXIS;
161 }
162 
164 {
165  return name == P_YAXIS;
166 }
167 
168 void SpecularDataItem::setLowerX(double value)
169 {
171 }
172 
173 void SpecularDataItem::setUpperX(double value)
174 {
176 }
177 
178 void SpecularDataItem::setLowerY(double value)
179 {
181 }
182 
183 void SpecularDataItem::setUpperY(double value)
184 {
186 }
187 
188 void SpecularDataItem::setLog(bool log_flag)
189 {
190  yAxisItem()->setLogScale(log_flag);
191 }
192 
193 //! Sets zoom range of X,Y axes, if it was not yet defined.
194 
196 {
197  // set zoom range of x-axis to min, max values if it was not set already
198  if (getUpperX() < getLowerX()) {
199  setLowerX(xMin());
200  setUpperX(xMax());
201  }
202 
203  // set zoom range of y-axis to min, max values if it was not set already
204  if (getUpperY() < getLowerY()) {
205  setLowerY(yMin());
206  setUpperY(yMax());
207  }
208 
209  const int nx = static_cast<int>(m_data->axis(0).size());
210  xAxisItem()->setBinCount(nx);
211 }
212 
213 //! Init ymin, ymax to match the intensity values range.
214 QPair<double, double> SpecularDataItem::dataRange() const
215 {
216  const double default_min = 0.0;
217  const double default_max = 1.0;
218  const Datafield* data = getDatafield();
219  if (!data)
220  return QPair<double, double>(default_min, default_max);
221 
222  const auto vec = data->flatVector();
223  double min(*std::min_element(vec.cbegin(), vec.cend()));
224  double max(*std::max_element(vec.cbegin(), vec.cend()));
225 
226  min /= 2.0;
227  min = std::numeric_limits<double>::epsilon() < min ? min : default_min;
228  max *= 2.0;
229  max = max > min ? max : default_max;
230 
231  return QPair<double, double>(min, max);
232 }
233 
235 {
236  return item<BasicAxisItem>(P_XAXIS);
237 }
238 
240 {
241  return item<BasicAxisItem>(P_XAXIS);
242 }
243 
245 {
246  return item<AmplitudeAxisItem>(P_YAXIS);
247 }
248 
250 {
251  return item<AmplitudeAxisItem>(P_YAXIS);
252 }
253 
254 //! Set axes viewport to original data.
255 
257 {
259 }
Defines various axis items.
Defines error class.
Defines ImportDataInfo helper struct.
const QString y_axis_default_name
const QString x_axis_default_name
Defines namespace GUI::Model::JobItemUtils.
Defines class SpecularDataItem.
bool isLogScale() const
Definition: AxesItems.cpp:178
void setLogScale(bool value)
Definition: AxesItems.cpp:183
DoubleDescriptor min(const QString &unit=QString()) const
Definition: AxesItems.cpp:41
QString title() const
Definition: AxesItems.cpp:80
int binCount() const
Definition: AxesItems.cpp:26
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
DoubleDescriptor max(const QString &unit=QString()) const
Definition: AxesItems.cpp:58
void setTitle(const QString &title)
Definition: AxesItems.cpp:85
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
Abstract base class for IntensityDataItem and SpecularDataItem. Owns one simulated data set of type D...
Definition: DataItem.h:34
virtual void setDatafield(Datafield *data)=0
The given pointer becomes owned by this class!!
Definition: DataItem.cpp:20
Datafield * getDatafield()
Definition: DataItem.h:41
void setAxesUnits(const ComboProperty &units)
Definition: DataItem.cpp:96
std::unique_ptr< Datafield > m_data
simulation results
Definition: DataItem.h:88
Carries information about loaded data.
QString unitsLabel() const
QString axisLabel(size_t axis_index) const
Abstract base class for instrument-specific item classes.
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 value() const
Get value.
void emitDataChanged(int role=Qt::DisplayRole)
Notify model about data changes.
void setYaxisTitle(const QString &title) override
double getLowerY() const
Returns lower and upper zoom ranges of y-axis.
void setDatafield(Datafield *data) override
The given pointer becomes owned by this class!!
QString getXaxisTitle() const
void updateAxesZoomLevel()
Sets zoom range of X,Y axes, if it was not yet defined.
int getNbins() const
Number of bins in data.
void setUpperY(double value)
double yMax() const
static constexpr auto P_TITLE
static bool isXaxisPropertyName(const QString &name)
void setXaxisTitle(const QString &title) override
void setUpperX(double value)
void setLowerX(double value)
static constexpr auto P_XAXIS
std::vector< int > shape() const override
void setLowerY(double value)
void setLog(bool log_flag)
double getUpperY() const
double getLowerX() const
Returns lower and upper zoom ranges of x-axis.
double xMax() const
void setAxesRangeToData() override
set zoom range of x,y axes to axes of input data
QString getYaxisTitle() const
static bool isYaxisPropertyName(const QString &name)
void updateCoords(const InstrumentItem *instrument) override
void resetView()
Set axes viewport to original data.
QPair< double, double > dataRange() const
Init ymin, ymax to match the intensity values range.
double yMin() const
Returns min and max range of y-axis as given by IntensityData.
static constexpr auto P_YAXIS
double getUpperX() const
const AmplitudeAxisItem * yAxisItem() const
void reset(ImportDataInfo data) override
Returns data to the state defined by user (imported) data.
double xMin() const
Returns min and max range of x-axis as given by IntensityData.
const BasicAxisItem * xAxisItem() const
void updateDataAxes(DataItem *intensityItem, const InstrumentItem *instrumentItem)
updates axes of Datafield in IntensityData item
QString const & name(EShape k)
Definition: particles.cpp:20