BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
data2ditem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/standarditems/data2ditem.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include <stdexcept>
18 
19 using namespace ModelView;
20 
21 namespace {
22 size_t total_bin_count(Data2DItem* item)
23 {
24  if (auto xaxis = item->xAxis(); xaxis)
25  if (auto yaxis = item->yAxis(); yaxis)
26  return static_cast<size_t>(xaxis->size() * yaxis->size());
27  return 0;
28 }
29 } // namespace
30 
32 {
35  setContent(std::vector<double>()); // prevent editing in widgets, since there is no
36  // corresponding editor
37 }
38 
39 //! Sets axes and put data points to zero.
40 
41 void Data2DItem::setAxes(std::unique_ptr<BinnedAxisItem> x_axis,
42  std::unique_ptr<BinnedAxisItem> y_axis)
43 {
44  insert_axis(std::move(x_axis), T_XAXIS);
45  insert_axis(std::move(y_axis), T_YAXIS);
46  setContent(std::vector<double>(total_bin_count(this), 0.0));
47 }
48 
49 //! Returns x-axis (nullptr if it doesn't exist).
50 
52 {
53  return item<BinnedAxisItem>(T_XAXIS);
54 }
55 
56 //! Returns y-axis (nullptr if it doesn't exist).
57 
59 {
60  return item<BinnedAxisItem>(T_YAXIS);
61 }
62 
63 void Data2DItem::setContent(const std::vector<double>& data)
64 {
65  if (total_bin_count(this) != data.size())
66  throw std::runtime_error("Data1DItem::setContent() -> Data doesn't match size of axis");
67 
68  setData(data);
69 }
70 
71 //! Returns 2d vector representing 2d data.
72 
73 std::vector<double> Data2DItem::content() const
74 {
75  return data<std::vector<double>>();
76 }
77 
78 //! Insert axis under given tag. Previous axis will be deleted and data points invalidated.
79 
80 void Data2DItem::insert_axis(std::unique_ptr<BinnedAxisItem> axis, const std::string& tag)
81 {
82  // removing current axis
83  if (getItem(tag, 0))
84  delete takeItem({tag, 0});
85 
86  insertItem(axis.release(), {tag, 0});
87 }
Defines class CLASS?
Item to represent an axis with arbitrary binning.
Definition: axisitems.h:61
Complex item holding mixed SessionItem types (single properties and other CompountItems).
Definition: compounditem.h:28
Represents two-dimensional data (axes definition and 2d array of values).
Definition: data2ditem.h:29
static const std::string T_XAXIS
Definition: data2ditem.h:31
BinnedAxisItem * xAxis() const
Returns x-axis (nullptr if it doesn't exist).
Definition: data2ditem.cpp:51
BinnedAxisItem * yAxis() const
Returns y-axis (nullptr if it doesn't exist).
Definition: data2ditem.cpp:58
static const std::string T_YAXIS
Definition: data2ditem.h:32
void setAxes(std::unique_ptr< BinnedAxisItem > x_axis, std::unique_ptr< BinnedAxisItem > y_axis)
Sets axes and put data points to zero.
Definition: data2ditem.cpp:41
std::vector< double > content() const
Returns 2d vector representing 2d data.
Definition: data2ditem.cpp:73
void insert_axis(std::unique_ptr< BinnedAxisItem > axis, const std::string &tag)
Insert axis under given tag. Previous axis will be deleted and data points invalidated.
Definition: data2ditem.cpp:80
void setContent(const std::vector< double > &data)
Definition: data2ditem.cpp:63
SessionItem * takeItem(const TagRow &tagrow)
Removes item from given row from given tag, returns it to the caller.
SessionItem * getItem(const std::string &tag, int row=0) const
Returns item at given row of given tag.
void registerTag(const TagInfo &tagInfo, bool set_as_default=false)
Registers tag to hold items under given name.
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Definition: sessionitem.h:141
T data(int role=ItemDataRole::DATA) const
Returns data of given type T for given role.
Definition: sessionitem.h:148
bool insertItem(SessionItem *item, const TagRow &tagrow)
Insert item into given tag under the given row.
Holds info about single tag for SessionItem.
Definition: taginfo.h:28
Defines class CLASS?
const model_type Data2DItemType
Definition: mvvm_types.h:51
const model_type FixedBinAxisItemType
Definition: mvvm_types.h:52
materialitems.h Collection of materials to populate MaterialModel.