BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jobitem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/model/jobitem.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 Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "gui2/model/jobitem.h"
18 #include "gui2/model/jobmodel.h"
19 #include "gui2/model/modelutils.h"
20 #include "mvvm/model/modelutils.h"
25 #include <QColor>
26 
27 using namespace ModelView;
28 
29 namespace gui2 {
30 
31 namespace {
32 const int row_sim_graph = 0;
33 const int row_reference_graph = 1;
34 
35 GraphItem* create_reference_graph(JobItem* item)
36 {
37  auto model = item->model();
38  return model->insertItem<GraphItem>(item->specularViewport(),
39  {ViewportItem::T_ITEMS, row_reference_graph});
40 }
41 
42 GraphItem* create_difference_graph(JobItem* item)
43 {
44  auto model = item->model();
45  return model->insertItem<GraphItem>(item->diffViewport(), {ViewportItem::T_ITEMS, 0});
46 }
47 
48 //! Creates viewport and data properties. Creates graph, adds data to the graph, and graph
49 //! to viewport.
50 //! TODO consider to replace it with classes, when JobItem structure becomes clear.
51 
52 template <typename Data, typename Graph, typename Viewport>
53 void initViewport(CompoundItem* item, const std::string& data_name,
54  const std::string& viewport_name)
55 {
56  auto data = item->addProperty<Data>(data_name);
57  auto viewport = item->addProperty<Viewport>(viewport_name);
58  auto graph = std::make_unique<GraphItem>();
59  graph->setDataItem(data);
60  viewport->insertItem(graph.release(), {ViewportItem::T_ITEMS, 0});
61 }
62 
63 } // namespace
64 
65 SLDCanvasItem::SLDCanvasItem() : GraphViewportItem(Constants::SLDCanvasItemType) {}
66 
67 std::pair<double, double> SLDCanvasItem::data_yaxis_range() const
68 {
69  auto [ymin, ymax] = GraphViewportItem::data_yaxis_range();
70  double range = ymax - ymin;
71  return {ymin - range / 10.0, ymax + range / 10.0};
72 }
73 
74 // ----------------------------------------------------------------------------
75 
77 {
81 }
82 
84 {
85  return item<Data1DItem>(P_SLD_DATA);
86 }
87 
89 {
90  return item<SLDCanvasItem>(P_SLD_VIEWPORT);
91 }
92 
94 {
95  return item<Data1DItem>(P_SPECULAR_DATA);
96 }
97 
99 {
100  return item<CanvasItem>(P_SPECULAR_VIEWPORT);
101 }
102 
104 {
105  return item<GraphViewportItem>(P_DIFF_VIEWPORT);
106 }
107 
108 //! Updates reference graph in specular viewport from external graph.
109 //! External graph represents user imported data, it will be used to
110 //! is comming from another viewport (i.e. containing user imported data),
111 //! and it is used
112 
114 {
115  if (graph) {
118  } else {
121  }
122 }
123 
124 //! Updates values stored in Data1DItem representing the difference between specular and reference
125 //! graphs.
126 
128 {
129  if (auto reference_graph = referenceGraph(); reference_graph) {
130  const auto reference_data = reference_graph->dataItem();
131  const auto specular_data = specularData();
132  auto diff_data = differenceData();
133  Utils::SetDifference(specular_data, reference_data, diff_data);
134  }
135 }
136 
138 {
139  return item<Data1DItem>(P_DIFF_DATA);
140 }
141 
142 //! Returns specular graph.
143 
145 {
146  auto graphs = specularViewport()->graphItems();
147  return graphs.size() > 0 ? graphs.at(row_sim_graph) : nullptr;
148 }
149 
150 //! Returns reference graph, if exists. It represents imported user data from ExperimentalScanItem.
151 //! Here it is stored in SpecularViewport.
152 
154 {
155  auto graphs = specularViewport()->graphItems();
156  return graphs.size() > 1 ? graphs.at(row_reference_graph) : nullptr;
157 }
158 
159 //! Returns graph representing a numeric difference between simulated and reference curve.
160 
162 {
163  auto graphs = diffViewport()->graphItems();
164  return graphs.size() > 0 ? graphs.at(0) : nullptr;
165 }
166 
168 {
169  assert(graph);
170  auto reference_graph = referenceGraph() ? referenceGraph() : create_reference_graph(this);
171  reference_graph->setFromGraphItem(graph);
172 }
173 
175 {
176  // FIXME rename unused graph
177  if (!differenceGraph()) {
178  create_difference_graph(this);
180  }
181 
183 }
184 
185 //! Removes reference graph from specular viewport.
186 
188 {
189  if (auto graph = referenceGraph(); graph)
191 }
192 
193 //! Removes difference graph from specular viewport.
194 
196 {
197  if (auto graph = differenceGraph(); graph)
199 }
200 
202 {
203  initViewport<Data1DItem, GraphItem, SLDCanvasItem>(this, P_SLD_DATA, P_SLD_VIEWPORT);
204  sldData()->setAxis<FixedBinAxisItem>(1, 0.0, 1.0);
205 }
206 
207 //! Setups a specular viewport together with a single graph in it and corresponding data item.
208 //! Intended to store simulated specular curve, and possibly reference graphs.
209 
211 {
212  initViewport<Data1DItem, GraphItem, CanvasItem>(this, P_SPECULAR_DATA, P_SPECULAR_VIEWPORT);
213  auto graph = specularGraph();
214  graph->setNamedColor("cornflowerblue");
215  specularData()->setAxis<PointwiseAxisItem>(std::vector<double>());
216 }
217 
218 //! Setups viewport, difference graph, and its underlying data to show the difference between
219 //! simulated and reference curves.
220 
222 {
223  initViewport<Data1DItem, GraphItem, GraphViewportItem>(this, P_DIFF_DATA, P_DIFF_VIEWPORT);
224  differenceData()->setAxis<PointwiseAxisItem>(std::vector<double>());
225 }
226 
227 } // namespace gui2
Defines class CLASS?
Complex item holding mixed SessionItem types (single properties and other CompountItems).
Definition: compounditem.h:28
T * addProperty(const std::string &name)
Adds property item of given type.
Definition: compounditem.h:43
Represents one-dimensional data (axis and values).
Definition: data1ditem.h:30
T * setAxis(Args &&... args)
Inserts axis of given type.
Definition: data1ditem.h:56
Item to represent fixed bin axis.
Definition: axisitems.h:75
One-dimensional graph representation of Data1DItem.
Definition: graphitem.h:29
void setDataItem(const Data1DItem *item)
Sets link to the data item.
Definition: graphitem.cpp:34
2D viewport specialized for showing multiple GraphItem's.
std::pair< double, double > data_yaxis_range() const override
Returns lower, upper range on y-axis occupied by all data points of all graphs.
std::vector< GraphItem * > graphItems() const
Returns the selected graph items.
Item to represent pointwise axis.
Definition: axisitems.h:94
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
Definition: sessionmodel.h:104
static const std::string T_ITEMS
Definition: viewportitem.h:31
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:66
Holds a collection of GraphItem's for simultaneous plotting, as well as all information related to pl...
ModelView::GraphItem * specularGraph() const
Returns specular graph.
Definition: jobitem.cpp:144
void setupReferenceGraphFrom(const ModelView::GraphItem *graph)
Definition: jobitem.cpp:167
static const std::string P_SLD_VIEWPORT
Definition: jobitem.h:49
void setupDifferenceGraphFrom(const ModelView::GraphItem *graph)
Definition: jobitem.cpp:174
ModelView::Data1DItem * differenceData() const
Definition: jobitem.cpp:137
CanvasItem * specularViewport() const
Definition: jobitem.cpp:98
ModelView::Data1DItem * specularData() const
Definition: jobitem.cpp:93
ModelView::GraphItem * differenceGraph() const
Returns graph representing a numeric difference between simulated and reference curve.
Definition: jobitem.cpp:161
void removeDifferenceGraph()
Removes difference graph from specular viewport.
Definition: jobitem.cpp:195
void setup_diff_viewport()
Setups viewport, difference graph, and its underlying data to show the difference between simulated a...
Definition: jobitem.cpp:221
void setup_sld_viewport()
Definition: jobitem.cpp:201
static const std::string P_SLD_DATA
Definition: jobitem.h:48
ModelView::GraphViewportItem * diffViewport() const
Definition: jobitem.cpp:103
ModelView::GraphItem * referenceGraph() const
Returns reference graph, if exists.
Definition: jobitem.cpp:153
ModelView::Data1DItem * sldData() const
Definition: jobitem.cpp:83
static const std::string P_DIFF_DATA
Definition: jobitem.h:52
void setup_specular_viewport()
Setups a specular viewport together with a single graph in it and corresponding data item.
Definition: jobitem.cpp:210
static const std::string P_DIFF_VIEWPORT
Definition: jobitem.h:53
void updateDifferenceData()
Updates values stored in Data1DItem representing the difference between specular and reference graphs...
Definition: jobitem.cpp:127
static const std::string P_SPECULAR_VIEWPORT
Definition: jobitem.h:51
void updateReferenceGraph(const ModelView::GraphItem *graph)
Updates reference graph in specular viewport from external graph.
Definition: jobitem.cpp:113
SLDCanvasItem * sldViewport() const
Definition: jobitem.cpp:88
static const std::string P_SPECULAR_DATA
Definition: jobitem.h:50
void removeReferenceGraph()
Removes reference graph from specular viewport.
Definition: jobitem.cpp:187
Viewport intended for showing SLD profile.
Definition: jobitem.h:35
std::pair< double, double > data_yaxis_range() const override
Definition: jobitem.cpp:67
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
MVVM_MODEL_EXPORT void DeleteItemFromModel(SessionItem *item)
Removes and deletes item from its model.
Definition: modelutils.cpp:54
materialitems.h Collection of materials to populate MaterialModel.
const std::string SLDCanvasItemType
const std::string JobItemType
DAREFLCORE_EXPORT void SetDifference(const ModelView::Data1DItem *data1, const ModelView::Data1DItem *data2, ModelView::Data1DItem *target)
Make target item represent difference of two Data1DItems. Target will get an axis as in data1.
Definition: modelutils.cpp:78
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20