BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
graphplotcontroller.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/graphplotcontroller.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 
22 #include "qcustomplot.h"
23 
24 using namespace ModelView;
25 
28  QCustomPlot* m_customPlot{nullptr};
29  QCPGraph* m_graph{nullptr};
30  std::unique_ptr<Data1DPlotController> m_dataController;
31  std::unique_ptr<PenController> m_penController;
32 
33  GraphItemControllerImpl(GraphPlotController* master, QCustomPlot* plot)
34  : m_self(master), m_customPlot(plot)
35  {
36  }
37 
38  //! Setups controllers and updates graph properties.
39 
40  void init_graph()
41  {
42  m_graph = m_customPlot->addGraph();
43  m_dataController = std::make_unique<Data1DPlotController>(m_graph);
44  m_penController = std::make_unique<PenController>(m_graph);
45 
49  }
50 
52  {
53  if (m_graph)
54  m_customPlot->removePlottable(m_graph);
55  }
56 
58 
59  void update_data_controller() { m_dataController->setItem(graph_item()->dataItem()); }
60 
61  //! Updates graph pen from GraphItem.
62 
63  void update_graph_pen() { m_penController->setItem(graph_item()->penItem()); }
64 
65  //! Update visible
67  {
68  m_graph->setVisible(graph_item()->property<bool>(GraphItem::P_DISPLAYED));
69  m_customPlot->replot();
70  }
71 
72  void reset_graph()
73  {
74  m_dataController->setItem(nullptr);
75  m_penController->setItem(nullptr);
76  m_customPlot->removePlottable(m_graph);
77  m_graph = nullptr;
78  m_customPlot->replot();
79  }
80 };
81 
83  : p_impl(std::make_unique<GraphItemControllerImpl>(this, custom_plot))
84 {
85 }
86 
88 {
89  auto on_property_change = [this](SessionItem*, const std::string& property_name) {
90  if (property_name == GraphItem::P_LINK)
91  p_impl->update_data_controller();
92 
93  if (property_name == GraphItem::P_DISPLAYED)
94  p_impl->update_visible();
95  };
96  setOnPropertyChange(on_property_change);
97 
98  p_impl->init_graph();
99 }
100 
102 {
103  p_impl->reset_graph();
104 }
105 
One-dimensional graph representation of Data1DItem.
Definition: graphitem.h:29
static const std::string P_DISPLAYED
Definition: graphitem.h:34
static const std::string P_LINK
Definition: graphitem.h:31
Establish communication between QCPGraph and GraphItem.
std::unique_ptr< GraphItemControllerImpl > 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.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
std::unique_ptr< Data1DPlotController > m_dataController
void init_graph()
Setups controllers and updates graph properties.
void update_graph_pen()
Updates graph pen from GraphItem.
GraphItemControllerImpl(GraphPlotController *master, QCustomPlot *plot)