BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
mousemovereporter.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/mousemovereporter.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 
17 #include <QMouseEvent>
18 #include <qcustomplot.h>
19 #include <stdexcept>
20 
21 using namespace ModelView;
22 
25  QCustomPlot* custom_plot{nullptr};
30  {
31  if (!custom_plot)
32  throw std::runtime_error("MouseMoveReporter: not initialized custom plot.");
33 
34  custom_plot->setMouseTracking(true);
35  set_connected();
36  }
37 
39  {
40  auto on_mouse_move = [this](QMouseEvent* event) {
41  double x = pixelToXaxisCoord(event->pos().x());
42  double y = pixelToYaxisCoord(event->pos().y());
43  if (callback)
44  callback({x, y, axesRangeContains(x, y)});
45  };
46 
47  QObject::connect(custom_plot, &QCustomPlot::mouseMove, on_mouse_move);
48  }
49 
50  double pixelToXaxisCoord(double pixel) const { return custom_plot->xAxis->pixelToCoord(pixel); }
51 
52  double pixelToYaxisCoord(double pixel) const { return custom_plot->yAxis->pixelToCoord(pixel); }
53 
54  bool axesRangeContains(double xpos, double ypos) const
55  {
56  return custom_plot->xAxis->range().contains(xpos)
57  && custom_plot->yAxis->range().contains(ypos);
58  }
59 };
60 
61 MouseMoveReporter::MouseMoveReporter(QCustomPlot* custom_plot, callback_t callback)
62  : p_impl(std::make_unique<MouseMoveReporterImpl>(this, custom_plot, callback))
63 {
64 }
65 
Tracks mouse moves in QCustomPlot canvas.
std::function< void(const MousePosInfo &pos_info)> callback_t
MouseMoveReporter(QCustomPlot *custom_plot, callback_t callback)
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Definition: filesystem.h:81
bool axesRangeContains(double xpos, double ypos) const
MouseMoveReporterImpl(MouseMoveReporter *reporter, QCustomPlot *custom_plot, callback_t callback)