BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
graphinfoformatter.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/graphinfoformatter.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 
16 #include "mvvm/utils/stringutils.h"
17 #include <qcustomplot.h>
18 #include <sstream>
19 
20 using namespace ModelView;
21 
22 namespace {
23 
24 QCPGraph* find_graph_nearby(QCustomPlot* custom_plot, double x, double y)
25 {
26  double widget_px = custom_plot->xAxis->coordToPixel(x);
27  double widget_py = custom_plot->yAxis->coordToPixel(y);
28  return dynamic_cast<QCPGraph*>(custom_plot->plottableAt(QPointF(widget_px, widget_py)));
29 }
30 
31 int getBin(const QCPGraph* graph, double x)
32 {
33  const int key_start = graph->findBegin(x);
34  const int key_end = graph->findBegin(x, false); // false = do not expand range
35  if (key_end == key_start || key_end == graph->dataCount())
36  return key_start;
37  return (x - graph->dataSortKey(key_start)) <= (graph->dataSortKey(key_end) - x) ? key_start
38  : key_end;
39 }
40 
41 struct Context {
42  double xpos{0.0};
43  double ypos{0.0};
44  bool close_to_graph{false};
45  int nx{0};
46  double value{0.0};
47 };
48 
49 std::string compose_string(const Context& context)
50 {
51  std::ostringstream ostr;
52  ostr << "[x: " << Utils::DoubleToString(context.xpos, 3) << ", ";
53  ostr << "y: " << Utils::DoubleToString(context.ypos, 3) << "] ";
54  if (context.close_to_graph) {
55  ostr << "[binx: " << context.nx << "] ";
56  ostr << "[value: " << Utils::ScientificDoubleToString(context.value) << "]";
57  }
58  return ostr.str();
59 }
60 
61 } // namespace
62 
63 std::string GraphInfoFormatter::status_string(QCustomPlot* custom_plot, double x, double y) const
64 {
65  Context context{x, y};
66 
67  if (auto qcp_graph = find_graph_nearby(custom_plot, x, y); qcp_graph) {
68  context.close_to_graph = true;
69  context.nx = getBin(qcp_graph, x);
70  context.value = qcp_graph->dataMainValue(context.nx);
71  }
72 
73  return compose_string(context);
74 }
std::string status_string(QCustomPlot *custom_plot, double x, double y) const override
Returns status string representing graph nearby.
Defines class CLASS?
MVVM_MODEL_EXPORT std::string ScientificDoubleToString(double input, int precision=6)
Returns string representation of scientific double.
MVVM_MODEL_EXPORT std::string DoubleToString(double input, int precision=12)
Returns string representation of double with given precision.
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?