BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
data1dplotcontroller.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/view/mvvm/plotting/data1dplotcontroller.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 "qcustomplot.h"
18 #include <stdexcept>
19 
20 namespace {
21 template <typename T> QVector<T> fromStdVector(const std::vector<T>& vec)
22 {
23 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
24  return QVector<T>(vec.begin(), vec.end());
25 #else
26  return QVector<T>::fromStdVector(vec);
27 #endif
28 }
29 } // namespace
30 
31 using namespace ModelView;
32 
34  QCPGraph* m_graph{nullptr};
35  QCPErrorBars* m_errorBars{nullptr};
36 
37  Data1DPlotControllerImpl(QCPGraph* graph) : m_graph(graph)
38  {
39  if (!m_graph)
40  throw std::runtime_error("Uninitialised graph in Data1DPlotController");
41  }
42 
44  {
45  assert(item);
48  }
49 
51  {
52  m_graph->setData(fromStdVector<double>(item->binCenters()),
53  fromStdVector<double>(item->binValues()));
54  customPlot()->replot();
55  }
56 
58  {
59  auto errors = item->binErrors();
60  if (errors.empty()) {
62  return;
63  }
64 
65  if (!m_errorBars)
66  m_errorBars = new QCPErrorBars(customPlot()->xAxis, customPlot()->yAxis);
67 
68  m_errorBars->setData(fromStdVector<double>(errors));
69  m_errorBars->setDataPlottable(m_graph);
70  }
71 
72  void resetGraph()
73  {
74  m_graph->setData(QVector<double>{}, QVector<double>{});
75  customPlot()->replot();
76  }
77 
79  {
80  delete m_errorBars;
81  m_errorBars = nullptr;
82  }
83 
84  QCustomPlot* customPlot()
85  {
86  assert(m_graph);
87  return m_graph->parentPlot();
88  }
89 };
90 
92  : p_impl(std::make_unique<Data1DPlotControllerImpl>(graph))
93 {
94 }
95 
97 
99 {
100  auto on_property_change = [this](SessionItem*, std::string property_name) {
101  if (property_name == Data1DItem::P_VALUES)
102  p_impl->updateGraphPointsFromItem(currentItem());
103  if (property_name == Data1DItem::P_ERRORS)
104  p_impl->updateErrorBarsFromItem(currentItem());
105  };
106  setOnPropertyChange(on_property_change);
107 
108  p_impl->initGraphFromItem(currentItem());
109 }
110 
112 {
113  p_impl->resetGraph();
114 }
Represents one-dimensional data (axis and values).
Definition: data1ditem.h:30
static const std::string P_VALUES
Definition: data1ditem.h:32
static const std::string P_ERRORS
Definition: data1ditem.h:33
std::unique_ptr< Data1DPlotControllerImpl > p_impl
void unsubscribe() override
For necessary manipulations on new item.
void setOnPropertyChange(Callbacks::item_str_t f)
Sets callback to be notified on item's property change.
SessionItem * item() const
For necessary manipulations on unsubscription.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
Defines class CLASS?
Defines class CLASS?
QVector< double > fromStdVector(const std::vector< double > &data)
Definition: GUIHelpers.cpp:225
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81