BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
colorscaleplotcontroller.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/colorscaleplotcontroller.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 <stdexcept>
20 
21 using namespace ModelView;
22 
24 
26  QCPColorScale* color_scale{nullptr};
27  QCPLayoutGrid* layout_grid{new QCPLayoutGrid};
28  std::unique_ptr<ViewportAxisPlotController> axisController;
29  QCPMarginGroup* margin_group{nullptr};
30 
33  {
34  if (!color_scale)
35  throw std::runtime_error("ColorScalePlotController: axis is not initialized.");
36 
37  axisController = std::make_unique<ViewportAxisPlotController>(color_scale->axis());
38  }
39 
41  {
45  setup_margins();
46  }
47 
48  //! Updates color scale for log10.
49 
51  {
52  const bool is_log = controller->currentItem()->is_in_log();
53  color_scale->setDataScaleType(is_log ? QCPAxis::stLogarithmic : QCPAxis::stLinear);
54  }
55 
57  {
58  if (!layout_grid->hasElement(0, 0))
59  layout_grid->addElement(0, 0, color_scale);
60 
61  layout_grid->setVisible(true);
62  customPlot()->plotLayout()->addElement(0, 1, layout_grid);
63  }
64 
66  {
67  layout_grid->setVisible(false);
68  customPlot()->plotLayout()->take(layout_grid);
69  customPlot()->plotLayout()->simplify();
70  }
71 
72  //! Setup margins of color scale to match top/bottom margins of axis rectangle.
73 
75  {
76  if (margin_group)
77  return;
78 
79  if (!customPlot()->axisRect())
80  return;
81  margin_group = new QCPMarginGroup(customPlot());
82  customPlot()->axisRect()->setMarginGroup(QCP::msBottom | QCP::msTop, margin_group);
83  color_scale->setMarginGroup(QCP::msBottom | QCP::msTop, margin_group);
84  }
85 
86  QCustomPlot* customPlot() { return color_scale->parentPlot(); }
87 };
88 
90  : p_impl(std::make_unique<ColorScalePlotControllerImpl>(this, color_scale))
91 
92 {
93 }
94 
96 
98 {
99  auto on_property_change = [this](SessionItem*, std::string property_name) {
100  if (property_name == ViewportAxisItem::P_IS_LOG)
101  p_impl->update_log_scale();
102  };
103  setOnPropertyChange(on_property_change);
104 
105  p_impl->setup_components();
106 }
Defines class CLASS?
Establishes communication between QCPColorScale and ViewportAxisItem.
ColorScalePlotController(QCPColorScale *color_scale)
std::unique_ptr< ColorScalePlotControllerImpl > p_impl
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
static const std::string P_IS_LOG
Definition: axisitems.h:46
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
void setup_margins()
Setup margins of color scale to match top/bottom margins of axis rectangle.
ColorScalePlotControllerImpl(ColorScalePlotController *controller, QCPColorScale *color_scale)
Defines class CLASS?