BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
Data1DViewItem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Data/Data1DViewItem.cpp
6 //! @brief Implements class Data1DViewItem
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 
21 #include "GUI/Model/Job/JobItem.h"
22 #include "GUI/Util/ComboProperty.h"
23 #include "GUI/Util/Error.h"
24 
25 namespace {
26 
27 const QString x_axis_default_name = "X [nbins]";
28 const QString y_axis_default_name = "Signal [a.u.]";
29 
30 const double default_min = 0.0;
31 const double default_max = 1.0;
32 
33 } // namespace
34 
36  : SessionItem(M_TYPE)
37  , m_job_item(nullptr)
38 {
39  addProperty(P_TITLE, QString());
40 
41  auto* basicAxis = addProperty<BasicAxisItem>(P_XAXIS);
42  basicAxis->binsItem();
43 
44  auto* amplitudeAxis = addProperty<AmplitudeAxisItem>(P_YAXIS);
45  amplitudeAxis->binsItem();
46  amplitudeAxis->titleItem();
47  amplitudeAxis->visibilityItem()->setValue(true);
48  amplitudeAxis->visibilityItem();
49 
51 
52  ComboProperty combo = ComboProperty() << "nbins";
53  addProperty(P_AXES_UNITS, combo.variant());
54 
55  mapper()->setOnPropertyChange([this](const QString& name) {
56  if (name != P_AXES_UNITS)
57  return;
60  });
61 
62  setLowerX(default_min);
63  setUpperX(default_max);
64  setLowerY(default_min);
65  setUpperY(default_max);
68 }
69 
71 {
72  return xAxisItem()->binCount();
73 }
74 
76 {
77  return xAxisItem()->min();
78 }
79 
81 {
82  return xAxisItem()->max();
83 }
84 
86 {
87  return yAxisItem()->min();
88 }
89 
91 {
92  return yAxisItem()->max();
93 }
94 
96 {
97  return yAxisItem()->isLogScale();
98 }
99 
101 {
102  return xAxisItem()->title();
103 }
104 
106 {
107  return yAxisItem()->title();
108 }
109 
110 void Data1DViewItem::setXaxisTitle(const QString& title)
111 {
112  xAxisItem()->setTitle(title);
113 }
114 
115 void Data1DViewItem::setYaxisTitle(const QString& title)
116 {
117  yAxisItem()->setTitle(title);
118 }
119 
120 //! set zoom range of x,y axes to axes of input data
122 {
124  this, propertyContainerItem()->basicDataItem());
125 
126  // For data loading from disk: items appear earlier than
127  // actual data.
128  if (!data)
129  return;
130 
131  setLowerX(data->axis(0).min());
132  setUpperX(data->axis(0).max());
133 
134  auto data_range = dataRange(data.get());
135  setLowerY(data_range.first);
136  setUpperY(data_range.second);
137 }
138 
140 {
141  return getItemValue(P_AXES_UNITS).value<ComboProperty>();
142 }
143 
145 {
147 }
148 
150 {
152 }
153 
155 {
156  return name == P_AXES_UNITS;
157 }
158 
160 {
161  // TODO: implement when applying DataITem1DView in ImportView
162  throw Error("Error in DataItem1DView::resetToDefault: not implemented");
163 }
164 
165 QPair<QVector<double>, QVector<double>> Data1DViewItem::graphData(Data1DProperties* property_item)
166 {
167  const auto data = GUI::Model::DataViewUtils::getTranslatedData(this, property_item->dataItem());
168  if (!data)
169  return {};
170 
171 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
172  auto centers = data->axis(0).binCenters();
173  auto values = data->flatVector();
174  return {QVector<double>(centers.cbegin(), centers.cend()),
175  QVector<double>(values.cbegin(), values.cend())};
176 #else
177  return {QVector<double>::fromStdVector(data->axis(0).binCenters()),
178  QVector<double>::fromStdVector(data->flatVector())};
179 #endif
180 }
181 
183 {
184  if (m_job_item != nullptr)
185  return m_job_item; // returning preset job item
186 
187  auto* item = parentItem();
188  do {
189  if (item->modelType() == JobItem::M_TYPE) {
190  m_job_item = dynamic_cast<JobItem*>(item);
191  return m_job_item;
192  }
193  } while ((item = item->parentItem()));
194  throw Error("Error in DataItem1DView::jobItem: passed item is not owned by any job item");
195 }
196 
197 void Data1DViewItem::setLowerX(double value)
198 {
200 }
201 
202 void Data1DViewItem::setUpperX(double value)
203 {
205 }
206 
207 void Data1DViewItem::setLowerY(double value)
208 {
210 }
211 
212 void Data1DViewItem::setUpperY(double value)
213 {
215 }
216 
217 void Data1DViewItem::setLog(bool log_flag)
218 {
219  yAxisItem()->setLogScale(log_flag);
220 }
221 
223 {
224  return item<DataPropertyContainer>(T_DATA_PROPERTIES);
225 }
226 
228 {
230 }
231 
233 {
234  return item<BasicAxisItem>(P_XAXIS);
235 }
236 
238 {
239  return item<BasicAxisItem>(P_XAXIS);
240 }
241 
243 {
244  return item<AmplitudeAxisItem>(P_YAXIS);
245 }
246 
248 {
249  return item<AmplitudeAxisItem>(P_YAXIS);
250 }
251 
253 {
254  return name == P_XAXIS;
255 }
256 
258 {
259  return name == P_YAXIS;
260 }
261 
262 //! Set axes viewport to original data.
263 
265 {
267 }
268 
269 //! Init ymin, ymax to match the intensity values range.
270 QPair<double, double> Data1DViewItem::dataRange(const Datafield* data) const
271 {
272  if (!data)
273  return QPair<double, double>(default_min, default_max);
274 
275  const auto vec =
276  data->flatVector(); // TODO merge code here, SpecularDataItem, IntensityDataItem
277  double min(*std::min_element(vec.cbegin(), vec.cend()));
278  double max(*std::max_element(vec.cbegin(), vec.cend()));
279 
280  min /= 2.0;
281  min = std::numeric_limits<double>::epsilon() < min ? min : default_min;
282  max *= 2.0;
283  max = max > min ? max : default_max;
284 
285  return QPair<double, double>(min, max);
286 }
Defines various axis items.
Defines class ComboProperty.
Defines class Data1DViewItem.
Declares class DataItem.
Defines class DataProperties and its descendants.
Defines class DataPropertyContainer.
Defines namespace GUI::Model::DataViewUtils.
Defines error class.
const QString y_axis_default_name
const QString x_axis_default_name
Defines class JobItem.
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 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
QVariant variant() const
Constructs variant enclosing given ComboProperty.
Holds data required for 1D DataItem representation.
JobItem * jobItem()
Returns either parenting JobItem or job item set with DataItem1DView::setJobItem.
void setLog(bool log_flag)
static bool isXAxisPropertyName(const QString &name)
static bool isAxesUnitsPropertyName(const QString &name)
JobItem * m_job_item
static bool isYAxisPropertyName(const QString &name)
double getUpperY() const
bool isLog() const
const AmplitudeAxisItem * yAxisItem() const
int getNbins() const
Number of bins in data.
QPair< double, double > dataRange(const Datafield *data) const
Init ymin, ymax to match the intensity values range.
static constexpr auto T_DATA_PROPERTIES
static constexpr auto P_TITLE
void setAxesRangeToData()
set zoom range of x,y axes to axes of input data
static constexpr auto P_AXES_UNITS
DataPropertyContainer * propertyContainerItem()
void resetView()
Set axes viewport to original data.
double getLowerY() const
Returns lower and upper zoom ranges of y-axis.
const BasicAxisItem * xAxisItem() const
QString getYaxisTitle() const
static constexpr auto P_YAXIS
void setUpperY(double value)
QString getXaxisTitle() const
double getLowerX() const
Returns lower and upper zoom ranges of x-axis.
QPair< QVector< double >, QVector< double > > graphData(Data1DProperties *property_item)
Returns point data for drawing.
void setLowerX(double value)
void setLowerY(double value)
void setUpperX(double value)
void setXaxisTitle(const QString &title)
DataPropertyContainer * createPropertyContainerItem()
double getUpperX() const
static constexpr auto P_XAXIS
void setYaxisTitle(const QString &title)
void resetToDefault()
Returns data view to default state (no dimensional units, default axes' names)
SelectionDescriptor< QString > axesUnitsDescriptor() const
void setAxesUnits(const ComboProperty &units)
ComboProperty axesUnits() const
DataItem * dataItem()
static constexpr auto M_TYPE
static constexpr auto M_TYPE
Definition: JobItem.h:62
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=nullptr)
Definition: ModelMapper.cpp:39
Describes a selection (various possibilities and the current one).
Base class for a GUI data item.
Definition: SessionItem.h:204
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....
bool registerTag(const QString &name, int min=0, int max=-1, QStringList modelTypes={})
Add new tag to this item with given name, min, max and types. max = -1 -> unlimited,...
QVariant value() const
Get value.
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.
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:60
T * item(const QString &tag) const
Definition: SessionItem.h:353
SessionItem * parentItem() const
Returns parent of this item.
Definition: SessionItem.cpp:67
SessionItem * getItem(const QString &tag="", int row=0) const
Returns item in given row of given tag.
T * insertItem(SessionItem *parent=nullptr, int row=-1, QString tag="")
Definition: SessionModel.h:137
std::unique_ptr< Datafield > getTranslatedData(Data1DViewItem *view_item, DataItem *data_item)
void updateAxesTitle(Data1DViewItem *view_item)
QString const & name(EShape k)
Definition: particles.cpp:20