BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
graphviewportplotcontroller.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/graphviewportplotcontroller.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 
21 #include <list>
22 #include <qcustomplot.h>
23 #include <stdexcept>
24 
25 using namespace ModelView;
26 
29  QCustomPlot* custom_plot{nullptr};
30  std::list<std::unique_ptr<GraphPlotController>> graph_controllers;
31  std::unique_ptr<ViewportAxisPlotController> xAxisController;
32  std::unique_ptr<ViewportAxisPlotController> yAxisController;
33 
35  : master(master), custom_plot(plot)
36  {
37  }
38 
40 
41  //! Setup controller components.
43  {
46  }
47 
48  //! Creates axes controllers.
49 
51  {
52  auto viewport = viewport_item();
53 
54  xAxisController = std::make_unique<ViewportAxisPlotController>(custom_plot->xAxis);
55  xAxisController->setItem(viewport->xAxis());
56 
57  yAxisController = std::make_unique<ViewportAxisPlotController>(custom_plot->yAxis);
58  yAxisController->setItem(viewport->yAxis());
59  }
60 
61  //! Run through all GraphItem's and create graph controllers for QCustomPlot.
62 
64  {
65  graph_controllers.clear();
66  auto viewport = viewport_item();
67  for (auto graph_item : viewport->graphItems()) {
68  auto controller = std::make_unique<GraphPlotController>(custom_plot);
69  controller->setItem(graph_item);
70  graph_controllers.push_back(std::move(controller));
71  }
72  viewport->setViewportToContent();
73  }
74 
75  //! Adds controller for item.
76  void add_controller_for_item(SessionItem* parent, const TagRow& tagrow)
77  {
78  auto added_child = dynamic_cast<GraphItem*>(parent->getItem(tagrow.tag, tagrow.row));
79 
80  for (auto& controller : graph_controllers)
81  if (controller->currentItem() == added_child)
82  throw std::runtime_error("Attempt to create second controller");
83 
84  auto controller = std::make_unique<GraphPlotController>(custom_plot);
85  controller->setItem(added_child);
86  graph_controllers.push_back(std::move(controller));
87  custom_plot->replot();
88  }
89 
90  //! Remove GraphPlotController corresponding to GraphItem.
91 
92  void remove_controller_for_item(SessionItem* parent, const TagRow& tagrow)
93  {
94  auto child_about_to_be_removed = parent->getItem(tagrow.tag, tagrow.row);
95  auto if_func = [&](const std::unique_ptr<GraphPlotController>& cntrl) -> bool {
96  return cntrl->currentItem() == child_about_to_be_removed;
97  };
98  graph_controllers.remove_if(if_func);
99  custom_plot->replot();
100  }
101 };
102 
104  : p_impl(std::make_unique<GraphViewportPlotControllerImpl>(this, custom_plot))
105 {
106 }
107 
109 {
110  auto on_item_inserted = [this](SessionItem* parent, TagRow tagrow) {
111  p_impl->add_controller_for_item(parent, tagrow);
112  };
113  setOnItemInserted(on_item_inserted);
114 
115  auto on_about_to_remove_item = [this](SessionItem* parent, TagRow tagrow) {
116  p_impl->remove_controller_for_item(parent, tagrow);
117  };
118  setOnAboutToRemoveItem(on_about_to_remove_item);
119 
120  p_impl->setup_components();
121 }
122 
Defines class CLASS?
One-dimensional graph representation of Data1DItem.
Definition: graphitem.h:29
2D viewport specialized for showing multiple GraphItem's.
Establishes communications and mutual updates for GraphViewportItem and QCutomPlot.
std::unique_ptr< GraphViewportPlotControllerImpl > p_impl
void setOnItemInserted(Callbacks::item_tagrow_t f)
Sets callback to be notified on child insertion.
void setOnAboutToRemoveItem(Callbacks::item_tagrow_t f)
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
SessionItem * getItem(const std::string &tag, int row=0) const
Returns item at given row of given tag.
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
std::string tag
Definition: tagrow.h:27
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
void remove_controller_for_item(SessionItem *parent, const TagRow &tagrow)
Remove GraphPlotController corresponding to GraphItem.
void add_controller_for_item(SessionItem *parent, const TagRow &tagrow)
Adds controller for item.
void create_graph_controllers()
Run through all GraphItem's and create graph controllers for QCustomPlot.
GraphViewportPlotControllerImpl(GraphViewportPlotController *master, QCustomPlot *plot)
Defines class CLASS?