BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
customplot_test_utils.h
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/tests/testview/customplot_test_utils.h
6 //! @brief Defines 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 #ifndef BORNAGAIN_MVVM_TESTS_TESTVIEW_CUSTOMPLOT_TEST_UTILS_H
16 #define BORNAGAIN_MVVM_TESTS_TESTVIEW_CUSTOMPLOT_TEST_UTILS_H
17 
18 #include <algorithm>
19 #include <qcustomplot.h>
20 #include <vector>
21 
22 //! Various common utils for unit tests.
23 
24 namespace TestUtils {
25 
26 //! Returns vector representing bin centers/values on QCPGraph.
27 
28 template <typename G, typename T> std::vector<double> get_values(const G* graph, T operand)
29 {
30  std::vector<double> result;
31  auto graph_data = *graph->data();
32  std::transform(std::begin(graph_data), std::end(graph_data), std::back_inserter(result),
33  [operand](const auto& point) { return operand(point); });
34  return result;
35 }
36 
37 //! Returns vector representing bin centers on QCPgraph.
38 std::vector<double> binCenters(const QCPGraph* graph);
39 
40 //! Returns vector representing y-values on QCPgraph.
41 std::vector<double> binValues(const QCPGraph* graph);
42 
43 //! Returns vector representing bin errors of QCPGraph.
44 std::vector<double> binErrors(const QCPGraph* graph);
45 
46 //! Finds and returns specific plottable in QCustomPlot canvas.
47 template <typename T> T* GetPlottable(QCustomPlot* custom_plot)
48 {
49  for (int i = 0; i < custom_plot->plottableCount(); ++i) {
50  if (auto plottable = dynamic_cast<T*>(custom_plot->plottable()); plottable)
51  return plottable;
52  }
53  return nullptr;
54 }
55 } // namespace TestUtils
56 
57 Q_DECLARE_METATYPE(QCPRange)
58 
59 #endif // BORNAGAIN_MVVM_TESTS_TESTVIEW_CUSTOMPLOT_TEST_UTILS_H
Various common utils for unit tests.
Definition: test_utils.h:34
std::vector< double > binValues(const QCPGraph *graph)
Returns vector representing y-values on QCPgraph.
std::vector< double > binErrors(const QCPGraph *graph)
Returns vector representing bin errors of QCPGraph.
std::vector< double > binCenters(const QCPGraph *graph)
Returns vector representing bin centers on QCPgraph.
std::vector< double > get_values(const G *graph, T operand)
Returns vector representing bin centers/values on QCPGraph.
T * GetPlottable(QCustomPlot *custom_plot)
Finds and returns specific plottable in QCustomPlot canvas.