BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
colormapplotcontroller.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/colormapplotcontroller.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"
23 #include "qcustomplot.h"
24 #include <QSignalSpy>
25 
26 using namespace ModelView;
27 
28 //! Testing ColorMapPlotController.
29 
30 class ColorMapPlotControllerTest : public ::testing::Test {
31 public:
33 };
34 
36 
37 //! Initial state.
38 
40 {
41  auto custom_plot = std::make_unique<QCustomPlot>();
42  ColorMapPlotController controller(custom_plot.get());
43  EXPECT_EQ(controller.currentItem(), nullptr);
44 
45  // creation of controller leads to the creation of QCPColorMap
46  auto color_map = TestUtils::GetPlottable<QCPColorMap>(custom_plot.get());
47  ASSERT_TRUE(color_map != nullptr);
48 }
49 
50 //! Setting ColorMapItem with data and checking that QCPColorMap appeared among plottables.
51 
53 {
54  auto custom_plot = std::make_unique<QCustomPlot>();
55  ColorMapPlotController controller(custom_plot.get());
56 
57  // creating data item
58  SessionModel model;
59  auto data_item = model.insertItem<Data2DItem>();
60  const int nx = 3, ny = 2;
61  data_item->setAxes(FixedBinAxisItem::create(nx, 0.0, 3.0),
62  FixedBinAxisItem::create(ny, 0.0, 2.0));
63  std::vector<double> expected = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
64  data_item->setContent(expected);
65 
66  // creating colormap item
67  auto colormap_item = model.insertItem<ColorMapItem>();
68  colormap_item->setDataItem(data_item);
69 
70  // initializing controller
71  controller.setItem(colormap_item);
72 
73  // checking that QCPColorMap has been created
74  EXPECT_EQ(custom_plot->plottableCount(), 1);
75  auto color_map = TestUtils::GetPlottable<QCPColorMap>(custom_plot.get());
76  ASSERT_TRUE(color_map != nullptr);
77  EXPECT_EQ(color_map->data()->keySize(), nx);
78  EXPECT_EQ(color_map->data()->valueSize(), ny);
79  EXPECT_EQ(color_map->data()->cell(0, 0), 1.0);
80  EXPECT_EQ(color_map->data()->cell(nx - 1, ny - 1), 6.0);
81 
82  // checking interpolation flag
83  EXPECT_TRUE(color_map->interpolate());
84 }
85 
86 //! Setting data to graph after.
87 
89 {
90  auto custom_plot = std::make_unique<QCustomPlot>();
91  ColorMapPlotController controller(custom_plot.get());
92 
93  SessionModel model;
94  auto colormap_item = model.insertItem<ColorMapItem>();
95 
96  controller.setItem(colormap_item);
97 
98  // without data QCustomPlot has QCPColorMap without default settings
99  EXPECT_EQ(custom_plot->plottableCount(), 1);
100  auto color_map = TestUtils::GetPlottable<QCPColorMap>(custom_plot.get());
101  ASSERT_TRUE(color_map != nullptr);
102  const int qcpmap_internal_default(10);
103  EXPECT_EQ(color_map->data()->keySize(), qcpmap_internal_default);
104  EXPECT_EQ(color_map->data()->valueSize(), qcpmap_internal_default);
105 
106  // setup Data2DItem and assign to ColorMapItem
107  auto data_item = model.insertItem<Data2DItem>();
108  const int nx = 3, ny = 2;
109  data_item->setAxes(FixedBinAxisItem::create(nx, 0.0, 3.0),
110  FixedBinAxisItem::create(ny, 0.0, 2.0));
111  std::vector<double> expected = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
112  data_item->setContent(expected);
113 
114  colormap_item->setDataItem(data_item);
115 
116  // colormap should get the shape of Data2DItem
117  EXPECT_EQ(color_map->data()->keySize(), nx);
118  EXPECT_EQ(color_map->data()->valueSize(), ny);
119  EXPECT_EQ(color_map->data()->cell(0, 0), 1.0);
120  EXPECT_EQ(color_map->data()->cell(nx - 1, ny - 1), 6.0);
121 }
122 
123 //! Unlinking from Data2DItem or ColorMapItem.
124 
126 {
127  auto custom_plot = std::make_unique<QCustomPlot>();
128  ColorMapPlotController controller(custom_plot.get());
129 
130  // setup model and single data item in it
131  SessionModel model;
132  auto data_item = model.insertItem<Data2DItem>();
133  const int nx = 3, ny = 2;
134  data_item->setAxes(FixedBinAxisItem::create(nx, 0.0, 3.0),
135  FixedBinAxisItem::create(ny, 0.0, 2.0));
136  std::vector<double> expected = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
137  data_item->setContent(expected);
138 
139  // creating colormap item
140  auto colormap_item = model.insertItem<ColorMapItem>();
141  colormap_item->setDataItem(data_item);
142 
143  controller.setItem(colormap_item);
144 
145  auto color_map = TestUtils::GetPlottable<QCPColorMap>(custom_plot.get());
146  EXPECT_EQ(color_map->data()->keySize(), nx);
147  EXPECT_EQ(color_map->data()->valueSize(), ny);
148 
149  // unlinking from data item
150  colormap_item->setDataItem(nullptr);
151 
152  EXPECT_EQ(custom_plot->plottableCount(), 1);
153 
154  // QCPColorMap should be there, but its shapre should be (0,0)
155  EXPECT_EQ(color_map->data()->keySize(), 0);
156  EXPECT_EQ(color_map->data()->valueSize(), 0);
157 
158  // unlinking from ColorMapItem leave QCPColorMap intact
159  controller.setItem(nullptr);
160  EXPECT_EQ(custom_plot->plottableCount(), 1);
161  EXPECT_EQ(color_map->data()->keySize(), 0);
162  EXPECT_EQ(color_map->data()->valueSize(), 0);
163 }
164 
165 //! Deletion of controller should lead to graph removal.
166 
168 {
169  auto custom_plot = std::make_unique<QCustomPlot>();
170  auto controller = std::make_unique<ColorMapPlotController>(custom_plot.get());
171 
172  // setup model and single data item in it
173  SessionModel model;
174  auto data_item = model.insertItem<Data2DItem>();
175 
176  // setup graph item
177  auto colormap_item = model.insertItem<ColorMapItem>();
178  colormap_item->setDataItem(data_item);
179 
180  // initializing controller
181  controller->setItem(colormap_item);
182  EXPECT_EQ(custom_plot->plottableCount(), 1);
183 
184  // deleting controller should lead to QCPColorMap removal
185  controller.reset();
186  EXPECT_EQ(custom_plot->plottableCount(), 0);
187 }
188 
189 //! Deletion of controller should lead to graph removal.
190 
192 {
193  auto custom_plot = std::make_unique<QCustomPlot>();
194  auto controller = std::make_unique<ColorMapPlotController>(custom_plot.get());
195 
196  // setup model and single data item in it
197  SessionModel model;
198  auto data_item = model.insertItem<Data2DItem>();
199 
200  // creating colormap item
201  auto colormap_item = model.insertItem<ColorMapItem>();
202  colormap_item->setDataItem(data_item);
203 
204  controller->setItem(colormap_item);
205 
206  auto color_map = TestUtils::GetPlottable<QCPColorMap>(custom_plot.get());
207 
208  EXPECT_EQ(color_map->gradient(), QCPColorGradient::gpPolar);
209 
210  auto combo = colormap_item->property<ComboProperty>(ColorMapItem::P_GRADIENT);
211  combo.setValue("Hot");
212  colormap_item->setProperty(ColorMapItem::P_GRADIENT, combo);
213  EXPECT_EQ(color_map->gradient(), QCPColorGradient::gpHot);
214 }
Defines class CLASS?
Testing ColorMapPlotController.
Two-dimensional color map representation of Data2DItem.
Definition: colormapitem.h:28
void setDataItem(const Data2DItem *item)
Sets link to the data item.
static const std::string P_GRADIENT
Definition: colormapitem.h:32
Establish communication between QCPColorMap and ColorMapItem.
Custom property to define list of string values with multiple selections.
Definition: comboproperty.h:27
void setValue(const std::string &name)
Represents two-dimensional data (axes definition and 2d array of values).
Definition: data2ditem.h:29
void setAxes(std::unique_ptr< BinnedAxisItem > x_axis, std::unique_ptr< BinnedAxisItem > y_axis)
Sets axes and put data points to zero.
Definition: data2ditem.cpp:41
static std::unique_ptr< FixedBinAxisItem > create(int nbins, double xmin, double xmax)
Definition: axisitems.cpp:81
void setItem(SessionItem *item)
T property(const std::string &tag) const
Returns data stored in property item.
Definition: sessionitem.h:181
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
Defines class CLASS?
Defines class CLASS?
TEST_F(ColorMapPlotControllerTest, initialState)
Initial state.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?