BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
graphplotcontroller.test.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/tests/testview/graphplotcontroller.test.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 "customplot_test_utils.h"
16 #include "google_test.h"
24 #include "qcustomplot.h"
25 #include <QSignalSpy>
26 
27 using namespace ModelView;
28 
29 //! Testing GraphPlotController.
30 
31 class GraphPlotControllerTest : public ::testing::Test {
32 public:
34 };
35 
37 
38 //! Initial state.
39 
41 {
42  auto custom_plot = std::make_unique<QCustomPlot>();
43  GraphPlotController controller(custom_plot.get());
44  EXPECT_EQ(controller.currentItem(), nullptr);
45  EXPECT_EQ(custom_plot->graphCount(), 0);
46 }
47 
48 //! Setting GraphItem with data and checking that plottable contains correct data.
49 
51 {
52  auto custom_plot = std::make_unique<QCustomPlot>();
53  GraphPlotController controller(custom_plot.get());
54 
55  // setup model and single data item in it
56  SessionModel model;
57  auto data_item = model.insertItem<Data1DItem>();
58  data_item->setAxis<FixedBinAxisItem>(2, 0.0, 2.0);
59  std::vector<double> expected_centers = {0.5, 1.5};
60  std::vector<double> expected_values = {42.0, 43.0};
61  data_item->setValues(expected_values);
62 
63  // setup graph item
64  auto graph_item = model.insertItem<GraphItem>();
65  graph_item->setDataItem(data_item);
66 
67  // initializing controller
68  controller.setItem(graph_item);
69 
70  // Checking resulting plottables
71  EXPECT_EQ(custom_plot->graphCount(), 1);
72  auto graph = custom_plot->graph();
73  EXPECT_EQ(TestUtils::binCenters(graph), expected_centers);
74  EXPECT_EQ(TestUtils::binValues(graph), expected_values);
75  EXPECT_EQ(graph->pen().color(), QColor(Qt::black));
76  EXPECT_EQ(graph->pen().style(), Qt::SolidLine);
77  EXPECT_EQ(graph->pen().width(), 1);
78 }
79 
80 TEST_F(GraphPlotControllerTest, changeGraphAppearance)
81 {
82  auto custom_plot = std::make_unique<QCustomPlot>();
83  GraphPlotController controller(custom_plot.get());
84 
85  // setup model and single data item in it
86  SessionModel model;
87  auto data_item = model.insertItem<Data1DItem>();
88  data_item->setAxis<FixedBinAxisItem>(2, 0.0, 2.0);
89  std::vector<double> expected_centers = {0.5, 1.5};
90  std::vector<double> expected_values = {42.0, 43.0};
91  data_item->setValues(expected_values);
92 
93  // setup graph item
94  auto graph_item = model.insertItem<GraphItem>();
95  graph_item->setDataItem(data_item);
96 
97  // initializing controller
98  controller.setItem(graph_item);
99 
100  // changing appearance properties
101  auto pen_item = graph_item->penItem();
102  pen_item->setProperty(PenItem::P_COLOR, QColor(Qt::red));
103 
104  auto styleCombo = pen_item->property<ComboProperty>(PenItem::P_STYLE);
105  styleCombo.setCurrentIndex(2);
106  pen_item->setProperty(PenItem::P_STYLE, styleCombo);
107  pen_item->setProperty(PenItem::P_WIDTH, 2);
108 
109  auto graph = custom_plot->graph();
110  EXPECT_EQ(graph->pen().color(), QColor(Qt::red));
111  EXPECT_EQ(graph->pen().style(), Qt::DashLine);
112  EXPECT_EQ(graph->pen().width(), 2);
113 }
114 
115 //! Setting GraphItem with data and checking that plottable contains correct data.
116 //! Same as aboe, except that the data is based on PointWiseAxis.
117 
118 TEST_F(GraphPlotControllerTest, setPointwiseItem)
119 {
120  auto custom_plot = std::make_unique<QCustomPlot>();
121  GraphPlotController controller(custom_plot.get());
122 
123  // setup model and single data item in it
124  const std::vector<double> expected_centers = {1.0, 2.0, 3.0};
125  const std::vector<double> expected_values = {42.0, 43.0, 44.0};
126 
127  SessionModel model;
128  auto data_item = model.insertItem<Data1DItem>();
129  data_item->setAxis<PointwiseAxisItem>(expected_centers);
130  data_item->setValues(expected_values);
131 
132  // setup graph item
133  auto graph_item = model.insertItem<GraphItem>();
134  auto pen_item = graph_item->penItem();
135  pen_item->setProperty(PenItem::P_COLOR, QColor(Qt::red));
136  graph_item->setDataItem(data_item);
137 
138  // initializing controller
139  controller.setItem(graph_item);
140 
141  // Checking resulting plottables
142  EXPECT_EQ(custom_plot->graphCount(), 1);
143  auto graph = custom_plot->graph();
144  EXPECT_EQ(TestUtils::binCenters(graph), expected_centers);
145  EXPECT_EQ(TestUtils::binValues(graph), expected_values);
146  EXPECT_EQ(graph->pen().color(), QColor(Qt::red));
147 }
148 
149 //! Setting data to graph after.
150 
152 {
153  auto custom_plot = std::make_unique<QCustomPlot>();
154  GraphPlotController controller(custom_plot.get());
155 
156  SessionModel model;
157  auto graph_item = model.insertItem<GraphItem>();
158 
159  controller.setItem(graph_item);
160 
161  // without data QCustomPlot has a graph without points
162  EXPECT_EQ(custom_plot->graphCount(), 1);
163  auto graph = custom_plot->graph();
164  EXPECT_EQ(TestUtils::binCenters(graph), std::vector<double>());
165  EXPECT_EQ(TestUtils::binValues(graph), std::vector<double>());
166 
167  // setup Data1DItem and assign to GraphItem
168  auto data_item = model.insertItem<Data1DItem>();
169  data_item->setAxis<FixedBinAxisItem>(2, 0.0, 2.0);
170  std::vector<double> expected_centers = {0.5, 1.5};
171  std::vector<double> expected_values = {42.0, 43.0};
172  data_item->setValues(expected_values);
173 
174  graph_item->setDataItem(data_item);
175 
176  // Checking resulting plottables
177  EXPECT_EQ(custom_plot->graphCount(), 1);
178  EXPECT_EQ(TestUtils::binCenters(graph), expected_centers);
179  EXPECT_EQ(TestUtils::binValues(graph), expected_values);
180 }
181 
182 //! Unlinking from Data1DItem or GraphItem.
183 
185 {
186  auto custom_plot = std::make_unique<QCustomPlot>();
187  GraphPlotController controller(custom_plot.get());
188 
189  // setup model and single data item in it
190  SessionModel model;
191  auto data_item = model.insertItem<Data1DItem>();
192  data_item->setAxis<FixedBinAxisItem>(2, 0.0, 2.0);
193  std::vector<double> expected_centers = {0.5, 1.5};
194  std::vector<double> expected_values = {42.0, 43.0};
195  data_item->setValues(expected_values);
196 
197  // setup graph item
198  auto graph_item = model.insertItem<GraphItem>();
199  auto pen_item = graph_item->penItem();
200  pen_item->setProperty(PenItem::P_COLOR, QColor(Qt::red));
201  graph_item->setDataItem(data_item);
202 
203  // initializing controller
204  controller.setItem(graph_item);
205 
206  // unlinking from data item
207  graph_item->setDataItem(nullptr);
208 
209  // Checking resulting plottables
210  // Current convention is that graph stays intact, but points disappear.
211  EXPECT_EQ(custom_plot->graphCount(), 1);
212  auto graph = custom_plot->graph();
213  EXPECT_EQ(TestUtils::binCenters(graph), std::vector<double>());
214  EXPECT_EQ(TestUtils::binValues(graph), std::vector<double>());
215  EXPECT_EQ(graph->pen().color(), QColor(Qt::red));
216 
217  // unlinking from graph item should remove Graph from CustomPlot
218  controller.setItem(nullptr);
219  EXPECT_EQ(custom_plot->graphCount(), 0);
220 }
221 
222 //! Deletion of controller should lead to graph removal.
223 
224 TEST_F(GraphPlotControllerTest, controllerDelete)
225 {
226  auto custom_plot = std::make_unique<QCustomPlot>();
227  auto controller = std::make_unique<GraphPlotController>(custom_plot.get());
228 
229  // setup model and single data item in it
230  SessionModel model;
231  auto data_item = model.insertItem<Data1DItem>();
232 
233  // setup graph item
234  auto graph_item = model.insertItem<GraphItem>();
235  graph_item->setDataItem(data_item);
236 
237  // initializing controller
238  controller->setItem(graph_item);
239  EXPECT_EQ(custom_plot->graphCount(), 1);
240 
241  // deleting controller should lead to graph removal
242  controller.reset();
243  EXPECT_EQ(custom_plot->graphCount(), 0);
244 }
245 
246 //! Deletion of graphItem should lead to the dissapearance of graph.
247 
249 {
250  auto custom_plot = std::make_unique<QCustomPlot>();
251  auto controller = std::make_unique<GraphPlotController>(custom_plot.get());
252 
253  // setup model and single data item in it
254  SessionModel model;
255  auto data_item = model.insertItem<Data1DItem>();
256 
257  // setup graph item
258  auto graph_item = model.insertItem<GraphItem>();
259  graph_item->setDataItem(data_item);
260 
261  // initializing controller
262  controller->setItem(graph_item);
263  EXPECT_EQ(custom_plot->graphCount(), 1);
264 
265  model.removeItem(graph_item->parent(), graph_item->tagRow());
266  EXPECT_EQ(custom_plot->graphCount(), 0);
267 }
Defines class CLASS?
Testing GraphPlotController.
Custom property to define list of string values with multiple selections.
Definition: comboproperty.h:27
void setCurrentIndex(int index)
Represents one-dimensional data (axis and values).
Definition: data1ditem.h:30
T * setAxis(Args &&... args)
Inserts axis of given type.
Definition: data1ditem.h:56
Item to represent fixed bin axis.
Definition: axisitems.h:75
One-dimensional graph representation of Data1DItem.
Definition: graphitem.h:29
void setDataItem(const Data1DItem *item)
Sets link to the data item.
Definition: graphitem.cpp:34
PenItem * penItem() const
Definition: graphitem.cpp:89
Establish communication between QCPGraph and GraphItem.
void setItem(SessionItem *item)
static const std::string P_STYLE
static const std::string P_COLOR
static const std::string P_WIDTH
Item to represent pointwise axis.
Definition: axisitems.h:94
SessionItem * parent() const
Returns parent item. Will return nullptr if item doesn't have a parent.
TagRow tagRow() const
Returns TagRow of this item under which it is accessible through its parent.
void setProperty(const std::string &tag, const T &value)
Sets value to property item.
Definition: sessionitem.h:190
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
Definition: sessionmodel.h:104
void removeItem(SessionItem *parent, const TagRow &tagrow)
Removes given row from parent.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
TEST_F(GraphPlotControllerTest, initialState)
Initial state.
materialitems.h Collection of materials to populate MaterialModel.
std::vector< double > binValues(const QCPGraph *graph)
Returns vector representing y-values on QCPgraph.
std::vector< double > binCenters(const QCPGraph *graph)
Returns vector representing bin centers on QCPgraph.
Defines class CLASS?
Defines class CLASS?