BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
customplotsceneadapter.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/customplotsceneadapter.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 
15 #include "customplotsceneadapter.h"
16 #include "qcustomplot.h"
17 #include <QObject>
18 
19 using namespace ModelView;
20 
22  QCustomPlot* custom_plot{nullptr};
23  std::unique_ptr<QMetaObject::Connection> conn_to_customplot;
25  {
26  conn_to_customplot = std::make_unique<QMetaObject::Connection>();
27  }
28 
29  double toSceneX(double customplot_x) const
30  {
31  return custom_plot ? custom_plot->xAxis->coordToPixel(customplot_x) : customplot_x;
32  }
33 
34  double toSceneY(double customplot_y) const
35  {
36  return custom_plot ? custom_plot->yAxis->coordToPixel(customplot_y) : customplot_y;
37  }
38 
39  double fromSceneX(double scene_x) const
40  {
41  return custom_plot ? custom_plot->xAxis->pixelToCoord(scene_x) : scene_x;
42  }
43 
44  double fromSceneY(double scene_y) const
45  {
46  return custom_plot ? custom_plot->yAxis->pixelToCoord(scene_y) : scene_y;
47  }
48 
49  QRectF viewportRectangle() const
50  {
51  if (!custom_plot)
52  return {};
53 
54  auto xrange = custom_plot->xAxis->range();
55  auto yrange = custom_plot->yAxis->range();
56 
57  return QRectF(toSceneX(xrange.lower), toSceneY(yrange.upper),
58  toSceneX(xrange.upper) - toSceneX(xrange.lower),
59  toSceneY(yrange.lower) - toSceneY(yrange.upper));
60  }
61 };
62 
64  : p_impl(std::make_unique<CustomPlotSceneAdapterImpl>(custom_plot))
65 {
66  auto on_customplot_destroy = [this]() { p_impl->custom_plot = nullptr; };
67  *p_impl->conn_to_customplot =
68  QObject::connect(custom_plot, &QCustomPlot::destroyed, on_customplot_destroy);
69 }
70 
72 {
73  if (p_impl->custom_plot)
74  QObject::disconnect(*p_impl->conn_to_customplot);
75 }
76 
77 double CustomPlotSceneAdapter::toSceneX(double customplot_x) const
78 {
79  return p_impl->toSceneX(customplot_x);
80 }
81 
82 double CustomPlotSceneAdapter::toSceneY(double customplot_y) const
83 {
84  return p_impl->toSceneY(customplot_y);
85 }
86 
87 double CustomPlotSceneAdapter::fromSceneX(double scene_x) const
88 {
89  return p_impl->fromSceneX(scene_x);
90 }
91 
92 double CustomPlotSceneAdapter::fromSceneY(double scene_y) const
93 {
94  return p_impl->fromSceneY(scene_y);
95 }
96 
98 {
99  return p_impl->viewportRectangle();
100 }
double toSceneY(double customplot_y) const override
convert local y-coordinate to scene coordinate
double toSceneX(double customplot_x) const override
convert local x-coordinate to scene coordinate
double fromSceneY(double scene_y) const override
convert scene y-coordinate to local axis coordinate
std::unique_ptr< CustomPlotSceneAdapterImpl > p_impl
QRectF viewportRectangle() const override
returns viewport rectangle in scene coordinates
CustomPlotSceneAdapter(QCustomPlot *custom_plot)
double fromSceneX(double scene_x) const override
convert scene x-coordinate to local axis coordinate
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81