BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
data2dplotcontroller.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/data2dplotcontroller.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 
18 #include "qcustomplot.h"
19 #include <algorithm>
20 #include <stdexcept>
21 
22 using namespace ModelView;
23 
24 namespace {
25 //! Returns QCPRange of axis.
26 QCPRange qcpRange(const BinnedAxisItem* axis)
27 {
28  auto centers = axis->binCenters(); // QCPColorMapData expects centers of bin
29  return centers.empty() ? QCPRange() : QCPRange(centers.front(), centers.back());
30 }
31 } // namespace
32 
35  QCPColorMap* color_map{nullptr};
38  {
39  if (!color_map)
40  throw std::runtime_error("Uninitialised colormap in Data2DPlotController");
41  }
42 
44 
46  {
48 
49  if (auto data_item = dataItem(); data_item) {
50  auto xAxis = data_item->xAxis();
51  auto yAxis = data_item->yAxis();
52  if (xAxis && yAxis) {
53  const int nbinsx = xAxis->size();
54  const int nbinsy = yAxis->size();
55 
56  color_map->data()->setSize(nbinsx, nbinsy);
57  color_map->data()->setRange(qcpRange(xAxis), qcpRange(yAxis));
58 
59  auto values = data_item->content();
60  for (int ix = 0; ix < nbinsx; ++ix)
61  for (int iy = 0; iy < nbinsy; ++iy)
62  color_map->data()->setCell(ix, iy,
63  values[static_cast<size_t>(ix + iy * nbinsx)]);
64 
65  auto [min, max] = std::minmax_element(std::begin(values), std::end(values));
66  color_map->setDataRange(QCPRange(*min, *max));
67  }
68  }
69  color_map->parentPlot()->replot();
70  }
71 
72  void reset_colormap() { color_map->data()->clear(); }
73 };
74 
76  : p_impl(std::make_unique<Data2DPlotControllerImpl>(this, color_map))
77 {
78 }
79 
81 
83 {
84  auto on_data_change = [this](SessionItem*, int) { p_impl->update_data_points(); };
85  setOnDataChange(on_data_change);
86 
87  p_impl->update_data_points();
88 }
89 
91 {
92  p_impl->reset_colormap();
93 }
Defines class CLASS?
Item to represent an axis with arbitrary binning.
Definition: axisitems.h:61
virtual std::vector< double > binCenters() const =0
Represents two-dimensional data (axes definition and 2d array of values).
Definition: data2ditem.h:29
Establish communication between QCPColorMap and Data2DItem.
Data2DPlotController(QCPColorMap *color_map)
void unsubscribe() override
For necessary manipulations on new item.
std::unique_ptr< Data2DPlotControllerImpl > p_impl
void setOnDataChange(Callbacks::item_int_t f)
Sets callback to be notified on item's data change.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
Data2DPlotControllerImpl(Data2DPlotController *master, QCPColorMap *color_map)