BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
viewmodeldelegate.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/testviewmodel/viewmodeldelegate.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 "google_test.h"
22 #include "widgetbasedtest.h"
23 #include <QDataWidgetMapper>
24 #include <QStyleOptionViewItem>
25 
26 using namespace ModelView;
27 
28 //! Tests of ViewModelDelegate class.
29 
31 public:
33 
34  struct TestData {
35  SessionModel model{};
38  QDataWidgetMapper mapper;
39 
40  TestData() : view_model(&model)
41  {
42  mapper.setModel(&view_model);
43  mapper.setItemDelegate(&delegate);
44  }
45 
46  std::unique_ptr<CustomEditor> create_editor(const QModelIndex& index)
47  {
48  return std::unique_ptr<CustomEditor>(dynamic_cast<CustomEditor*>(
49  delegate.createEditor(nullptr, QStyleOptionViewItem(), index)));
50  }
51 
52  void map_to_index(QWidget* widget, const QModelIndex& index)
53  {
54  mapper.setRootIndex(index.parent());
55  mapper.setCurrentModelIndex(index.sibling(index.row(), 0));
56  mapper.addMapping(widget, 1);
57  }
58  };
59 
60  std::unique_ptr<TestData> test_data() { return std::make_unique<TestData>(); }
61 };
62 
64 
66 {
67  TestData test_data;
68  test_data.model.insertItem<VectorItem>();
69 
70  auto parent_index = test_data.view_model.index(0, 0);
71  auto x_value_index = test_data.view_model.index(0, 1, parent_index);
72 
73  EXPECT_TRUE(test_data.create_editor(x_value_index).get() != nullptr);
74 }
75 
76 //! Check that ViewModelDelegate can work with widget mapper.
77 
79 {
80  TestData test_data;
81  auto vector_item = test_data.model.insertItem<VectorItem>();
82  auto x_item = vector_item->getItem(VectorItem::P_X);
83 
84  // accessing to index list (index of label field and index of data field)
85  // of PropertyItem corresponding to x-coordinate.
86  auto x_value_index = test_data.view_model.indexOfSessionItem(x_item).at(1);
87  auto editor = test_data.create_editor(x_value_index);
88 
89  test_data.map_to_index(editor.get(), x_value_index);
90 
91  editor->setData(43.0);
92  editor->dataChanged(editor->data());
93  EXPECT_EQ(x_item->data<double>(), 43.0);
94 }
Base class for all custom variant editors.
Definition: customeditor.h:26
View model to show content of SessionModel in Qt widgets: two column tree with label/data.
SessionItem * getItem(const std::string &tag, int row=0) const
Returns item at given row of given tag.
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Definition: sessionitem.h:141
T data(int role=ItemDataRole::DATA) const
Returns data of given type T for given role.
Definition: sessionitem.h:148
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
Vector item with three x,y,z property items.
Definition: vectoritem.h:24
static const std::string P_X
Definition: vectoritem.h:26
Model delegate to provide editing/painting for custom variants.
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Tests of ViewModelDelegate class.
std::unique_ptr< TestData > test_data()
Convenience class to setup QApplication for tests involving QWidget creation.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
Defines class CLASS?
Defines class CLASS?
std::unique_ptr< CustomEditor > create_editor(const QModelIndex &index)
void map_to_index(QWidget *widget, const QModelIndex &index)
Defines class CLASS?
Defines class CLASS?
TEST_F(ViewModelDelegateTest, createEditor)
Defines class CLASS?