BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
graphviewportplotcontroller.test.cpp File Reference

Implements class CLASS? More...

Include dependency graph for graphviewportplotcontroller.test.cpp:

Go to the source code of this file.

Classes

class  GraphViewportPlotControllerTest
 Testing GraphViewportPlotController. More...
 

Functions

 TEST_F (GraphViewportPlotControllerTest, addAndRemoveGraphs)
 Checks consequitive graph adding/removal. More...
 
 TEST_F (GraphViewportPlotControllerTest, addGraphAndSetItem)
 Check ::setItem() method when ViewPortItem contains graphs. More...
 
 TEST_F (GraphViewportPlotControllerTest, addGraphUndoRedo)
 Adding graph, then undo, then redo again. More...
 
 TEST_F (GraphViewportPlotControllerTest, addGraphUndoRedoMacro)
 Adding graph together with data in macro, then undo, then redo again. More...
 
 TEST_F (GraphViewportPlotControllerTest, addMoreGraphs)
 Checks consequitive graph adding/removal. More...
 
 TEST_F (GraphViewportPlotControllerTest, checkVisible)
 Checks The fucntionality of selection in the viewport. More...
 
 TEST_F (GraphViewportPlotControllerTest, initialState)
 Initial state. More...
 
 TEST_F (GraphViewportPlotControllerTest, setItem)
 Check ::setItem() method when no graphs exist. More...
 
 TEST_F (GraphViewportPlotControllerTest, switchBetweenTwoViewports)
 Two GraphViewportItem's and switch between them. More...
 

Detailed Description

Implements class CLASS?

Homepage:\n http://www.bornagainproject.org
License:\n GNU General Public License v3 or higher (see COPYING)
Authors
Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)

Definition in file graphviewportplotcontroller.test.cpp.

Function Documentation

◆ TEST_F() [1/9]

TEST_F ( GraphViewportPlotControllerTest  ,
addAndRemoveGraphs   
)

Checks consequitive graph adding/removal.

Definition at line 101 of file graphviewportplotcontroller.test.cpp.

102 {
103  auto custom_plot = std::make_unique<QCustomPlot>();
104  GraphViewportPlotController controller(custom_plot.get());
105 
106  // setting up controller with viewport item
107  SessionModel model;
108  auto viewport_item = model.insertItem<GraphViewportItem>();
109  controller.setItem(viewport_item);
110 
111  // No graphs yet.
112  EXPECT_EQ(custom_plot->graphCount(), 0);
113 
114  // Populating with data items
115  auto data1 = model.insertItem<Data1DItem>();
116  const std::vector<double> expected_values1 = {1.0, 2.0, 3.0};
117  const std::vector<double> expected_centers = {0.5, 1.5, 2.5};
118  data1->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
119  data1->setValues(expected_values1);
120 
121  auto data2 = model.insertItem<Data1DItem>();
122  const std::vector<double> expected_values2 = {4.0, 5.0, 6.0};
123  data2->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
124  data2->setValues(expected_values2);
125 
126  // adding graph item to viewport
127  auto graph_item1 = model.insertItem<GraphItem>(viewport_item, {"", 0});
128 
129  // check that QCustomPlot knows about graph
130  EXPECT_EQ(custom_plot->graphCount(), 1);
131 
132  graph_item1->setDataItem(data1);
133 
134  // check that QCustomPlot knows about graph
135  EXPECT_EQ(custom_plot->graphCount(), 1);
136 
137  // adding secong graph
138  auto graph_item2 = model.insertItem<GraphItem>(viewport_item, {"", 1});
139  graph_item2->setDataItem(data2);
140 
141  // check that QCustomPlot knows about two graph
142  EXPECT_EQ(custom_plot->graphCount(), 2);
143 
144  // Checking that viewport min, max adjusted to both graphs when manually call update_viewport()
145  viewport_item->setViewportToContent();
146  EXPECT_DOUBLE_EQ(custom_plot->xAxis->range().lower, expected_centers[0]);
147  EXPECT_DOUBLE_EQ(custom_plot->xAxis->range().upper, expected_centers[2]);
148  EXPECT_DOUBLE_EQ(custom_plot->yAxis->range().lower, expected_values1[0]);
149  EXPECT_DOUBLE_EQ(custom_plot->yAxis->range().upper, expected_values2[2]);
150 
151  // removing one GraphItem
152  model.removeItem(viewport_item, {ViewportItem::T_ITEMS, 1});
153 
154  // only single graph should remain on QCustomPlot3
155  EXPECT_EQ(custom_plot->graphCount(), 1);
156 }
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
2D viewport specialized for showing multiple GraphItem's.
Establishes communications and mutual updates for GraphViewportItem and QCutomPlot.
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.

References ModelView::SessionModel::insertItem(), ModelView::SessionModel::removeItem(), ModelView::Data1DItem::setAxis(), ModelView::GraphItem::setDataItem(), ModelView::ItemListenerBase::setItem(), and ModelView::ViewportItem::T_ITEMS.

Here is the call graph for this function:

◆ TEST_F() [2/9]

TEST_F ( GraphViewportPlotControllerTest  ,
addGraphAndSetItem   
)

Check ::setItem() method when ViewPortItem contains graphs.

Definition at line 70 of file graphviewportplotcontroller.test.cpp.

71 {
72  auto custom_plot = std::make_unique<QCustomPlot>();
73  GraphViewportPlotController controller(custom_plot.get());
74 
75  // setting up controller with viewport item
76  SessionModel model;
77  auto viewport_item = model.insertItem<GraphViewportItem>();
78 
79  auto data_item = model.insertItem<Data1DItem>();
80  const std::vector<double> expected_values = {1.0, 2.0, 3.0};
81  const std::vector<double> expected_centers = {0.5, 1.5, 2.5};
82  data_item->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
83  data_item->setValues(expected_values);
84 
85  auto graph_item = model.insertItem<GraphItem>(viewport_item);
86  graph_item->setDataItem(data_item);
87  controller.setItem(viewport_item);
88 
89  // single graph on custom plot.
90  EXPECT_EQ(custom_plot->graphCount(), 1);
91 
92  // QCustomPlot axis should correspond to
93  EXPECT_DOUBLE_EQ(custom_plot->xAxis->range().lower, expected_centers[0]);
94  EXPECT_DOUBLE_EQ(custom_plot->xAxis->range().upper, expected_centers[2]);
95  EXPECT_DOUBLE_EQ(custom_plot->yAxis->range().lower, expected_values[0]);
96  EXPECT_DOUBLE_EQ(custom_plot->yAxis->range().upper, expected_values[2]);
97 }

References ModelView::SessionModel::insertItem(), ModelView::GraphItem::setDataItem(), and ModelView::ItemListenerBase::setItem().

Here is the call graph for this function:

◆ TEST_F() [3/9]

TEST_F ( GraphViewportPlotControllerTest  ,
addGraphUndoRedo   
)

Adding graph, then undo, then redo again.

Definition at line 258 of file graphviewportplotcontroller.test.cpp.

259 {
260  auto custom_plot = std::make_unique<QCustomPlot>();
261  GraphViewportPlotController controller(custom_plot.get());
262 
263  // setting up controller with viewport item
264  SessionModel model;
265 
266  auto viewport_item = model.insertItem<GraphViewportItem>();
267  controller.setItem(viewport_item);
268 
269  // No graphs yet.
270  EXPECT_EQ(custom_plot->graphCount(), 0);
271 
272  // Populating with data items
273  auto data1 = model.insertItem<Data1DItem>();
274  const std::vector<double> expected_values = {1.0, 2.0, 3.0};
275  const std::vector<double> expected_centers = {0.5, 1.5, 2.5};
276  data1->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
277  data1->setValues(expected_values);
278 
279  // beginning of our undo/redo story
280  model.setUndoRedoEnabled(true);
281  EXPECT_EQ(model.undoStack()->index(), 0);
282  EXPECT_EQ(model.undoStack()->count(), 0);
283 
284  // adding graph item to viewport
285  auto graph_item = model.insertItem<GraphItem>(viewport_item, {"", 0});
286  EXPECT_EQ(model.undoStack()->index(), 1);
287  EXPECT_EQ(model.undoStack()->count(), 1);
288 
289  // assigning data to graph
290  graph_item->setDataItem(data1);
291  EXPECT_EQ(model.undoStack()->index(), 2);
292  EXPECT_EQ(model.undoStack()->count(), 2);
293 
294  // validating graph in custom plot
295  EXPECT_EQ(custom_plot->graphCount(), 1);
296  EXPECT_EQ(TestUtils::binCenters(custom_plot->graph()), expected_centers);
297  EXPECT_EQ(TestUtils::binValues(custom_plot->graph()), expected_values);
298 
299  // undoing data assignment
300  model.undoStack()->undo();
301  EXPECT_EQ(model.undoStack()->index(), 1);
302  EXPECT_EQ(model.undoStack()->count(), 2);
303 
304  // graph is still there, but empty
305  EXPECT_EQ(custom_plot->graphCount(), 1);
306  EXPECT_EQ(TestUtils::binCenters(custom_plot->graph()), std::vector<double>());
307  EXPECT_EQ(TestUtils::binValues(custom_plot->graph()), std::vector<double>());
308  EXPECT_EQ(graph_item->dataItem(), nullptr);
309 
310  // redoing data assignment
311  model.undoStack()->redo();
312  EXPECT_EQ(model.undoStack()->index(), 2);
313  EXPECT_EQ(model.undoStack()->count(), 2);
314 
315  // graph is complete again
316  EXPECT_EQ(graph_item->dataItem(), data1);
317  EXPECT_EQ(custom_plot->graphCount(), 1);
318  EXPECT_EQ(TestUtils::binCenters(custom_plot->graph()), expected_centers);
319  EXPECT_EQ(TestUtils::binValues(custom_plot->graph()), expected_values);
320 }
void setUndoRedoEnabled(bool value)
Sets undo/redo either enabled or disabled. By default undo/redo is disabled.
UndoStackInterface * undoStack() const
Returns command stack to perform undo/redo.
virtual int index() const =0
virtual int count() const =0
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.

References TestUtils::binCenters(), TestUtils::binValues(), ModelView::UndoStackInterface::count(), ModelView::UndoStackInterface::index(), ModelView::SessionModel::insertItem(), ModelView::UndoStackInterface::redo(), ModelView::Data1DItem::setAxis(), ModelView::ItemListenerBase::setItem(), ModelView::SessionModel::setUndoRedoEnabled(), ModelView::UndoStackInterface::undo(), and ModelView::SessionModel::undoStack().

Here is the call graph for this function:

◆ TEST_F() [4/9]

TEST_F ( GraphViewportPlotControllerTest  ,
addGraphUndoRedoMacro   
)

Adding graph together with data in macro, then undo, then redo again.

Definition at line 324 of file graphviewportplotcontroller.test.cpp.

325 {
326  auto custom_plot = std::make_unique<QCustomPlot>();
327  GraphViewportPlotController controller(custom_plot.get());
328 
329  // setting up controller with viewport item
330  SessionModel model;
331 
332  auto viewport_item = model.insertItem<GraphViewportItem>();
333  controller.setItem(viewport_item);
334 
335  // No graphs yet.
336  EXPECT_EQ(custom_plot->graphCount(), 0);
337 
338  // beginning of our undo/redo story
339  model.setUndoRedoEnabled(true);
340  EXPECT_EQ(model.undoStack()->index(), 0);
341  EXPECT_EQ(model.undoStack()->count(), 0);
342 
343  // adding data and graph as macro
344  model.undoStack()->beginMacro("addGraph");
345  // Populating with data items
346  auto data1 = model.insertItem<Data1DItem>();
347  const std::vector<double> expected_values = {1.0, 2.0, 3.0};
348  const std::vector<double> expected_centers = {0.5, 1.5, 2.5};
349  data1->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
350  data1->setValues(expected_values);
351  auto data_identifier = data1->identifier();
352  // adding graph item to viewport
353  auto graph_item = model.insertItem<GraphItem>(viewport_item, {"", 0});
354  // assigning data to graph
355  graph_item->setDataItem(data1);
356  model.undoStack()->endMacro();
357 
358  EXPECT_EQ(viewport_item->graphItems()[0]->dataItem(), model.topItem<Data1DItem>());
359 
360  EXPECT_EQ(model.undoStack()->index(), 1);
361  EXPECT_EQ(model.undoStack()->count(), 1);
362 
363  // validating graph in custom plot
364  EXPECT_EQ(custom_plot->graphCount(), 1);
365  EXPECT_EQ(TestUtils::binCenters(custom_plot->graph()), expected_centers);
366  EXPECT_EQ(TestUtils::binValues(custom_plot->graph()), expected_values);
367 
368  // undoing macro
369  model.undoStack()->undo();
370  EXPECT_EQ(model.undoStack()->index(), 0);
371  EXPECT_EQ(model.undoStack()->count(), 1);
372 
373  // no graph and no items
374  EXPECT_EQ(viewport_item->graphItems().size(), 0);
375  EXPECT_EQ(model.topItem<Data1DItem>(), nullptr);
376  EXPECT_EQ(custom_plot->graphCount(), 0);
377 
378  // redoing macro
379  model.undoStack()->redo();
380  EXPECT_EQ(custom_plot->graphCount(), 1);
381  EXPECT_EQ(viewport_item->graphItems().size(), 1);
382 
383  EXPECT_EQ(model.topItem<Data1DItem>()->identifier(), data_identifier);
384  EXPECT_EQ(viewport_item->graphItems()[0]->dataItem(), model.topItem<Data1DItem>());
385 
386  EXPECT_EQ(TestUtils::binCenters(custom_plot->graph()), expected_centers);
387  EXPECT_EQ(TestUtils::binValues(custom_plot->graph()), expected_values);
388 }
Data1DItem * dataItem() const
Returns data item linked to the given GraphItem.
Definition: graphitem.cpp:54
std::string identifier() const
Returns unique identifier.
Definition: sessionitem.cpp:87
T * topItem() const
Returns top item of the given type.
Definition: sessionmodel.h:126
virtual void beginMacro(const std::string &name)=0

References ModelView::UndoStackInterface::beginMacro(), TestUtils::binCenters(), TestUtils::binValues(), ModelView::UndoStackInterface::count(), ModelView::GraphItem::dataItem(), ModelView::UndoStackInterface::endMacro(), ModelView::SessionItem::identifier(), ModelView::UndoStackInterface::index(), ModelView::SessionModel::insertItem(), ModelView::UndoStackInterface::redo(), ModelView::Data1DItem::setAxis(), ModelView::GraphItem::setDataItem(), ModelView::ItemListenerBase::setItem(), ModelView::SessionModel::setUndoRedoEnabled(), ModelView::SessionModel::topItem(), ModelView::UndoStackInterface::undo(), and ModelView::SessionModel::undoStack().

Here is the call graph for this function:

◆ TEST_F() [5/9]

TEST_F ( GraphViewportPlotControllerTest  ,
addMoreGraphs   
)

Checks consequitive graph adding/removal.

Definition at line 160 of file graphviewportplotcontroller.test.cpp.

161 {
162  auto custom_plot = std::make_unique<QCustomPlot>();
163  GraphViewportPlotController controller(custom_plot.get());
164 
165  // setting up controller with viewport item
166  SessionModel model;
167  auto viewport_item = model.insertItem<GraphViewportItem>();
168  controller.setItem(viewport_item);
169 
170  // No graphs yet.
171  EXPECT_EQ(custom_plot->graphCount(), 0);
172 
173  // adding graph item to viewport
174  model.insertItem<GraphItem>(viewport_item);
175  EXPECT_EQ(custom_plot->graphCount(), 1);
176 
177  model.insertItem<GraphItem>(viewport_item);
178  EXPECT_EQ(custom_plot->graphCount(), 2);
179 
180  model.insertItem<GraphItem>(viewport_item);
181  EXPECT_EQ(custom_plot->graphCount(), 3);
182 }

References ModelView::SessionModel::insertItem(), and ModelView::ItemListenerBase::setItem().

Here is the call graph for this function:

◆ TEST_F() [6/9]

TEST_F ( GraphViewportPlotControllerTest  ,
checkVisible   
)

Checks The fucntionality of selection in the viewport.

Definition at line 186 of file graphviewportplotcontroller.test.cpp.

187 {
188  // Convenience
189  struct FindVisible {
190  static std::vector<QCPAbstractPlottable*> findVisible(const QCustomPlot* custom_plot)
191  {
192  std::vector<QCPAbstractPlottable*> output;
193  for (int i = 0; i < custom_plot->graphCount(); ++i) {
194  if (custom_plot->graph(i)->visible())
195  output.push_back(custom_plot->graph(i));
196  }
197  return output;
198  }
199  };
200 
201  // custom plot setup
202  auto custom_plot = std::make_unique<QCustomPlot>();
203  GraphViewportPlotController controller(custom_plot.get());
204 
205  // setting up controller with viewport item
206  SessionModel model;
207  auto viewport_item = model.insertItem<GraphViewportItem>();
208  controller.setItem(viewport_item);
209 
210  // adding graph item to viewport
211  auto first_plot = model.insertItem<GraphItem>(viewport_item);
212  auto second_plot = model.insertItem<GraphItem>(viewport_item);
213  auto third_plot = model.insertItem<GraphItem>(viewport_item);
214  EXPECT_EQ(custom_plot->graphCount(), 3);
215 
216  viewport_item->setVisible(std::vector<GraphItem*>{first_plot});
217  EXPECT_EQ(FindVisible::findVisible(custom_plot.get()).size(), 1);
218 
219  viewport_item->setVisible(std::vector<GraphItem*>{second_plot, third_plot});
220  EXPECT_EQ(FindVisible::findVisible(custom_plot.get()).size(), 2);
221 
222  viewport_item->setAllVisible();
223  EXPECT_EQ(FindVisible::findVisible(custom_plot.get()).size(), 3);
224 }

References ModelView::SessionModel::insertItem(), and ModelView::ItemListenerBase::setItem().

Here is the call graph for this function:

◆ TEST_F() [7/9]

TEST_F ( GraphViewportPlotControllerTest  ,
initialState   
)

Initial state.

Definition at line 39 of file graphviewportplotcontroller.test.cpp.

40 {
41  auto custom_plot = std::make_unique<QCustomPlot>();
42  GraphViewportPlotController controller(custom_plot.get());
43  EXPECT_EQ(custom_plot->graphCount(), 0);
44 }

◆ TEST_F() [8/9]

TEST_F ( GraphViewportPlotControllerTest  ,
setItem   
)

Check ::setItem() method when no graphs exist.

Definition at line 48 of file graphviewportplotcontroller.test.cpp.

49 {
50  auto custom_plot = std::make_unique<QCustomPlot>();
51  GraphViewportPlotController controller(custom_plot.get());
52 
53  // setting up controller with viewport item
54  SessionModel model;
55  auto item = model.insertItem<GraphViewportItem>();
56  controller.setItem(item);
57 
58  // no graphs in empty GraphViewportItem
59  EXPECT_EQ(custom_plot->graphCount(), 0);
60 
61  // axis should be [0, 1] as in defaule ViewportAxisItem
62  EXPECT_DOUBLE_EQ(custom_plot->xAxis->range().lower, 0.0);
63  EXPECT_DOUBLE_EQ(custom_plot->xAxis->range().upper, 1.0);
64  EXPECT_DOUBLE_EQ(custom_plot->yAxis->range().lower, 0.0);
65  EXPECT_DOUBLE_EQ(custom_plot->yAxis->range().upper, 1.0);
66 }

References ModelView::SessionModel::insertItem(), and ModelView::ItemListenerBase::setItem().

Here is the call graph for this function:

◆ TEST_F() [9/9]

TEST_F ( GraphViewportPlotControllerTest  ,
switchBetweenTwoViewports   
)

Two GraphViewportItem's and switch between them.

Definition at line 228 of file graphviewportplotcontroller.test.cpp.

229 {
230  auto custom_plot = std::make_unique<QCustomPlot>();
231  GraphViewportPlotController controller(custom_plot.get());
232 
233  // setting up controller with viewport item
234  SessionModel model;
235  auto viewport_item0 = model.insertItem<GraphViewportItem>();
236  auto viewport_item1 = model.insertItem<GraphViewportItem>();
237 
238  auto data_item = model.insertItem<Data1DItem>();
239  const std::vector<double> expected_values = {1.0, 2.0, 3.0};
240  const std::vector<double> expected_centers = {0.5, 1.5, 2.5};
241  data_item->setAxis<FixedBinAxisItem>(3, 0.0, 3.0);
242  data_item->setValues(expected_values);
243 
244  auto graph_item = model.insertItem<GraphItem>(viewport_item0);
245  graph_item->setDataItem(data_item);
246  controller.setItem(viewport_item0);
247 
248  // single graph on custom plot.
249  EXPECT_EQ(custom_plot->graphCount(), 1);
250 
251  // switch to second (empty) viewport, QCustomPlot should have no graphs
252  controller.setItem(viewport_item1);
253  EXPECT_EQ(custom_plot->graphCount(), 0);
254 }

References ModelView::SessionModel::insertItem(), ModelView::GraphItem::setDataItem(), and ModelView::ItemListenerBase::setItem().

Here is the call graph for this function: