BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
graphviewportitem.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/model/mvvm/standarditems/graphviewportitem.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 <algorithm>
18 #include <vector>
19 
20 using namespace ModelView;
21 
22 namespace {
23 
24 const double failback_min = 0.0;
25 const double failback_max = 1.0;
26 
27 //! Find min and max values along all data points in all graphs.
28 //! Function 'func' is used to run either through binCenters or binValues.
29 
30 template <typename T> auto get_min_max(const std::vector<GraphItem*>& graphs, T func)
31 {
32  std::vector<double> values;
33  for (auto graph : graphs) {
34  const auto array = func(graph);
35  std::copy(std::begin(array), std::end(array), std::back_inserter(values));
36  }
37 
38  auto [xmin, xmax] = std::minmax_element(std::begin(values), std::end(values));
39  return xmin != xmax ? std::make_pair(*xmin, *xmax) : std::make_pair(failback_min, failback_max);
40 }
41 
42 } // namespace
43 
45 {
48 }
49 
50 //! Returns the selected graph items.
51 
52 std::vector<GraphItem*> GraphViewportItem::graphItems() const
53 {
54  return items<GraphItem>(T_ITEMS);
55 }
56 
57 //! Returns the selected graph items.
58 
59 std::vector<GraphItem*> GraphViewportItem::visibleGraphItems() const
60 {
61  std::vector<GraphItem*> all_items = items<GraphItem>(T_ITEMS);
62  std::vector<GraphItem*> visible_items;
63  std::copy_if(all_items.begin(), all_items.end(), std::back_inserter(visible_items),
64  [](const GraphItem* graph_item) {
65  return graph_item->property<bool>(GraphItem::P_DISPLAYED);
66  });
67  return visible_items;
68 }
69 
70 //! Set the graph selection.
71 
72 void GraphViewportItem::setVisible(const std::vector<GraphItem*>& visible_graph_items)
73 {
74  std::vector<GraphItem*> output;
75  for (auto graph_item : items<GraphItem>(T_ITEMS)) {
76  if (std::find(visible_graph_items.begin(), visible_graph_items.end(), graph_item)
77  != visible_graph_items.end())
78  graph_item->setProperty(GraphItem::P_DISPLAYED, true);
79  else
80  graph_item->setProperty(GraphItem::P_DISPLAYED, false);
81  }
82 }
83 
84 //! Reset the graph selection.
85 
87 {
88  for (auto graph_item : items<GraphItem>(T_ITEMS))
89  graph_item->setProperty(GraphItem::P_DISPLAYED, true);
90 }
91 
92 //! Returns lower, upper range on x-axis occupied by all data points of all graphs.
93 
94 std::pair<double, double> GraphViewportItem::data_xaxis_range() const
95 {
96  return get_min_max(visibleGraphItems(), [](GraphItem* graph) { return graph->binCenters(); });
97 }
98 
99 //! Returns lower, upper range on y-axis occupied by all data points of all graphs.
100 
101 std::pair<double, double> GraphViewportItem::data_yaxis_range() const
102 {
103  return get_min_max(visibleGraphItems(), [](GraphItem* graph) { return graph->binValues(); });
104 }
One-dimensional graph representation of Data1DItem.
Definition: graphitem.h:29
static const std::string P_DISPLAYED
Definition: graphitem.h:34
std::vector< double > binValues() const
Definition: graphitem.cpp:64
std::vector< double > binCenters() const
Definition: graphitem.cpp:59
void setAllVisible()
Reset the graph selection.
std::vector< GraphItem * > visibleGraphItems() const
Returns the selected graph items.
void setVisible(const std::vector< GraphItem * > &visible_graph_items)
Set the graph selection.
GraphViewportItem(const std::string &model_type=Constants::GraphViewportItemType)
std::pair< double, double > data_yaxis_range() const override
Returns lower, upper range on y-axis occupied by all data points of all graphs.
std::pair< double, double > data_xaxis_range() const override
Returns lower, upper range on x-axis occupied by all data points of all graphs.
std::vector< GraphItem * > graphItems() const
Returns the selected graph items.
void registerTag(const TagInfo &tagInfo, bool set_as_default=false)
Registers tag to hold items under given name.
static TagInfo universalTag(std::string name, std::vector< std::string > modelTypes={})
Constructs universal tag intended for unlimited amount of various items.
Definition: taginfo.cpp:34
Base class to represent 2D viewport.
Definition: viewportitem.h:27
static const std::string T_ITEMS
Definition: viewportitem.h:31
Defines class CLASS?
Defines class CLASS?
const model_type GraphItemType
Definition: mvvm_types.h:53
materialitems.h Collection of materials to populate MaterialModel.
std::string model_type
Definition: types.h:23
Definition: filesystem.h:81