BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
colormapplotcontroller.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/colormapplotcontroller.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 
20 #include "qcustomplot.h"
21 #include <map>
22 
23 namespace {
24 using gradient_map_t = std::map<std::string, QCPColorGradient::GradientPreset>;
25 gradient_map_t createGradientMap()
26 {
27  gradient_map_t result;
28 
29  result["Grayscale"] = QCPColorGradient::gpGrayscale;
30  result["Hot"] = QCPColorGradient::gpHot;
31  result["Cold"] = QCPColorGradient::gpCold;
32  result["Night"] = QCPColorGradient::gpNight;
33  result["Candy"] = QCPColorGradient::gpCandy;
34  result["Geography"] = QCPColorGradient::gpGeography;
35  result["Ion"] = QCPColorGradient::gpIon;
36  result["Thermal"] = QCPColorGradient::gpThermal;
37  result["Polar"] = QCPColorGradient::gpPolar;
38  result["Spectrum"] = QCPColorGradient::gpSpectrum;
39  result["Jet"] = QCPColorGradient::gpJet;
40  result["Hues"] = QCPColorGradient::gpHues;
41 
42  return result;
43 }
44 
45 QCPColorGradient getGradient(const std::string& gradientName)
46 {
47  static gradient_map_t gradient_map = createGradientMap();
48  auto it = gradient_map.find(gradientName);
49  return it != gradient_map.end() ? QCPColorGradient(it->second) : QCPColorGradient::gpSpectrum;
50 }
51 
52 } // namespace
53 
54 using namespace ModelView;
55 
58  QCustomPlot* custom_plot{nullptr};
59  QCPColorMap* color_map{nullptr};
60  std::unique_ptr<Data2DPlotController> data_controller;
61 
63  QCPColorScale* color_scale)
64  : master(master), custom_plot(plot)
65  {
66  color_map = new QCPColorMap(custom_plot->xAxis, custom_plot->yAxis);
67  data_controller = std::make_unique<Data2DPlotController>(color_map);
68 
69  if (color_scale)
70  color_map->setColorScale(color_scale);
71  }
72 
74 
76 
78  {
82  custom_plot->replot();
83  }
84 
85  void update_data_controller() { data_controller->setItem(colormap_item()->dataItem()); }
86 
87  //! Updates QCPColorMap's interpolation when corresponding property of ColorMapItem changed.
88 
90  {
91  auto is_interpolated = colormap_item()->property<bool>(ColorMapItem::P_INTERPOLATION);
92  color_map->setInterpolate(is_interpolated);
93  }
94 
96  {
98  color_map->setGradient(getGradient(combo.value()));
99  }
100 };
101 
102 ColorMapPlotController::ColorMapPlotController(QCustomPlot* custom_plot, QCPColorScale* color_scale)
103  : p_impl(std::make_unique<ColorMapPlotControllerImpl>(this, custom_plot, color_scale))
104 {
105 }
106 
108 {
109  auto on_property_change = [this](SessionItem*, std::string property_name) {
110  if (property_name == ColorMapItem::P_INTERPOLATION)
111  p_impl->update_interpolation();
112 
113  if (property_name == ColorMapItem::P_GRADIENT)
114  p_impl->update_gradient();
115 
116  if (property_name == ColorMapItem::P_LINK)
117  p_impl->update_data_controller();
118 
119  p_impl->custom_plot->replot();
120  };
121  setOnPropertyChange(on_property_change);
122 
123  p_impl->update_colormap();
124 }
125 
127 {
128  p_impl->data_controller->setItem(nullptr);
129 }
130 
QMap< QString, QCPColorGradient::GradientPreset > gradient_map_t
Two-dimensional color map representation of Data2DItem.
Definition: colormapitem.h:28
static const std::string P_INTERPOLATION
Definition: colormapitem.h:33
static const std::string P_LINK
Definition: colormapitem.h:30
static const std::string P_GRADIENT
Definition: colormapitem.h:32
Establish communication between QCPColorMap and ColorMapItem.
std::unique_ptr< ColorMapPlotControllerImpl > p_impl
ColorMapPlotController(QCustomPlot *plot, QCPColorScale *color_scale=nullptr)
void unsubscribe() override
For necessary manipulations on new item.
Custom property to define list of string values with multiple selections.
Definition: comboproperty.h:27
void setOnPropertyChange(Callbacks::item_str_t f)
Sets callback to be notified on item's property change.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
T property(const std::string &tag) const
Returns data stored in property item.
Definition: sessionitem.h:181
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
QCPColorGradient getGradient(const QString &gradientName)
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
ColorMapPlotControllerImpl(ColorMapPlotController *master, QCustomPlot *plot, QCPColorScale *color_scale)
void update_interpolation()
Updates QCPColorMap's interpolation when corresponding property of ColorMapItem changed.