BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
FitComparisonViewController.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/PlotComparison/FitComparisonViewController.cpp
6 //! @brief Implements classes DiffItemController, FitComparison1DViewController
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/DiffUtil.h"
22 #include "GUI/Model/Job/JobItem.h"
23 #include "GUI/Util/ComboProperty.h"
25 
26 namespace {
27 
28 const double relative_diff_min_1d = 1e-05;
29 const double relative_diff_max_1d = 4.0;
30 
31 } // namespace
32 
33 
35  : QObject(parent)
36  , m_diff_item_controller(new DiffItemController(SpecularDataItem::M_TYPE, this))
37  , m_diff_view_item(nullptr)
38  , m_appearanceRepeater(new PropertyRepeater(this))
39  , m_xAxisRepeater(new PropertyRepeater(this))
40 {
41 }
42 
44 {
45  return m_diff_view_item;
46 }
47 
49 {
50  ASSERT(job_item);
51 
52  clear();
54  createDiffViewItem(job_item);
55 
56  auto* job_data_view = job_item->dataItemView();
57 
58  m_appearanceRepeater->addItem(job_data_view);
60 
61  m_xAxisRepeater->addItem(job_data_view->xAxisItem());
62  m_xAxisRepeater->addItem(diffItemView()->xAxisItem());
63 
64  m_diff_view_item->setXaxisTitle(job_data_view->getXaxisTitle());
65  m_diff_view_item->setYaxisTitle("Relative difference");
66  m_diff_view_item->setLowerY(relative_diff_min_1d);
67  m_diff_view_item->setUpperY(relative_diff_max_1d);
68 }
69 
71 {
73 }
74 
76 {
77  if (!m_diff_view_item)
78  return;
80  m_diff_view_item->setLowerY(relative_diff_min_1d);
81  m_diff_view_item->setUpperY(relative_diff_max_1d);
82 }
83 
85 {
89  if (m_diff_view_item)
91 }
92 
94 {
96  auto* container = m_diff_view_item->createPropertyContainerItem();
97  container->addItem(m_diff_item_controller->diffItem());
98 
99  m_diff_view_item->setJobItem(job_item);
100  auto* job_data_view = job_item->dataItemView();
101  ComboProperty units_value = job_data_view->axesUnits();
102  m_diff_view_item->setAxesUnits(units_value);
103 }
104 
106 {
107  auto* parent = m_diff_view_item->parentItem();
108  auto* old_view_item = parent->takeRow(parent->rowOfChild(m_diff_view_item));
109  ASSERT(old_view_item == m_diff_view_item);
110  delete (old_view_item);
111  m_diff_view_item = nullptr;
112 }
113 
114 DiffItemController::DiffItemController(const QString& data_type, QObject* parent)
115  : QObject(parent)
116  , m_current_item(nullptr)
117  , m_private_model(new SessionModel("TempIntensityDataModel", this))
118  , m_diff_item(dynamic_cast<DataItem*>(m_private_model->insertNewItem(data_type)))
119 {
120  ASSERT(m_diff_item);
121 }
122 
124 {
125  unsubscribe();
126 }
127 
129 {
130  ASSERT(job_item);
131  if (m_current_item)
132  unsubscribe();
133  m_current_item = job_item;
134  subscribe();
135  updateDiffData();
136 }
137 
139 {
140  ASSERT(m_current_item);
141 
142  auto* sim_data = m_current_item->dataItem();
143  auto* real_data = m_current_item->realDataItem()->dataItem();
144  ASSERT(sim_data && real_data);
145 
146  if (!sim_data->getDatafield()) // job failed
147  return;
148 
150  DiffUtil::relativeDifferenceField(*sim_data->getDatafield(), *real_data->getDatafield()));
151 }
152 
154 {
155  if (!m_current_item) {
156  ASSERT(false);
157  return;
158  }
159 
160  // on simulation data change
162  [=](Datafield*) { updateDiffData(); });
163 }
164 
166 {
167  if (!m_current_item)
168  return;
169  disconnect(m_current_item->dataItem(), nullptr, this, nullptr);
170  m_current_item = nullptr;
171 }
Defines various axis items.
Defines class ComboProperty.
Defines class Data1DViewItem.
Defines class DataPropertyContainer.
Defines classes DiffItemController, FitComparison1DViewController.
Defines class JobItem.
Defines class PropertyRepeater.
Defines class RealDataItem.
Defines class SpecularDataItem.
Custom property to define list of string values with multiple selections. Intended for QVariant.
Definition: ComboProperty.h:25
View model for 1D DataItem. Can represent several items at once. In current implementation the first ...
void resetView()
Set axes viewport to original data.
void setJobItem(JobItem *job_item)
Sets job item (for unit conversion) if DataItemView is dynamically created outside of JobModel.
void setUpperY(double value)
void setLowerY(double value)
void setXaxisTitle(const QString &title)
DataPropertyContainer * createPropertyContainerItem()
void setYaxisTitle(const QString &title)
void setAxesUnits(const ComboProperty &units)
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
void datafieldChanged(Datafield *data)
void setJobItem(JobItem *job_item)
DiffItemController(const QString &data_type, QObject *parent)
Data1DViewItem * dataItemView()
Definition: JobItem.cpp:305
DataItem * dataItem()
Definition: JobItem.cpp:101
RealDataItem * realDataItem()
Definition: JobItem.cpp:290
Tracks property change (axes range, units etc) for the collection of IntensityDataItems and sets same...
void addItem(SessionItem *sessionItem)
DataItem * dataItem()
SessionItem * takeRow(int row)
Removes row from item and returns the item.
SessionItem * parentItem() const
Returns parent of this item.
Definition: SessionItem.cpp:67
Base class for a GUI data collection. A collection is e.g. all real data (RealDataModel)....
Definition: SessionModel.h:42
T * insertItem(SessionItem *parent=nullptr, int row=-1, QString tag="")
Definition: SessionModel.h:137