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

Implements class CLASS? More...

Include dependency graph for viewmodelutils.test.cpp:

Go to the source code of this file.

Classes

class  ViewModelUtilsTest
 

Functions

 TEST_F (ViewModelUtilsTest, itemCheckStateRole)
 Testing check state role of item. More...
 
 TEST_F (ViewModelUtilsTest, itemDecorationRole)
 Testing decoration role of the item. More...
 
 TEST_F (ViewModelUtilsTest, ItemRoleToQtRole)
 Translation of item role to Qt roles. More...
 
 TEST_F (ViewModelUtilsTest, itemsFromIndex)
 Check ItemsFromIndex in PropertyTableViewModel context. More...
 
 TEST_F (ViewModelUtilsTest, itemTextColorRole)
 Testing color role of item. More...
 
 TEST_F (ViewModelUtilsTest, itemToolTipRole)
 Testing tooltip role of the item. More...
 
 TEST_F (ViewModelUtilsTest, iterate)
 
 TEST_F (ViewModelUtilsTest, parentItemsFromIndex)
 Check ParentItemsFromIndex in PropertyTableViewModel context. More...
 
 TEST_F (ViewModelUtilsTest, UniqueItemsFromIndex)
 Check UniqueItemsFromIndex for artificially constructed viewmodel. 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 viewmodelutils.test.cpp.

Function Documentation

◆ TEST_F() [1/9]

TEST_F ( ViewModelUtilsTest  ,
itemCheckStateRole   
)

Testing check state role of item.

Definition at line 114 of file viewmodelutils.test.cpp.

115 {
116  SessionItem item("Something");
117 
118  // no color defined for item by default
119  auto variant = Utils::CheckStateRole(item);
120  EXPECT_FALSE(variant.isValid());
121 
122  item.setData(QVariant::fromValue(true));
123  EXPECT_EQ(Utils::CheckStateRole(item).value<int>(), Qt::Checked);
124 
125  item.setData(QVariant::fromValue(false));
126  EXPECT_EQ(Utils::CheckStateRole(item).value<int>(), Qt::Unchecked);
127 }
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
QVariant CheckStateRole(const SessionItem &item)
Returns check state for given item.

References ModelView::Utils::CheckStateRole(), and ModelView::SessionItem::setData().

Here is the call graph for this function:

◆ TEST_F() [2/9]

TEST_F ( ViewModelUtilsTest  ,
itemDecorationRole   
)

Testing decoration role of the item.

Definition at line 131 of file viewmodelutils.test.cpp.

132 {
133  SessionItem item("Something");
134 
135  // no color defined for item by default
136  auto variant = Utils::DecorationRole(item);
137  EXPECT_FALSE(variant.isValid());
138 
139  QColor expected(Qt::green);
140  item.setData(expected);
141  EXPECT_EQ(Utils::DecorationRole(item).value<QColor>(), expected);
142 }
QVariant DecorationRole(const SessionItem &item)
Returns tooltip for given item.

References ModelView::Utils::DecorationRole(), and ModelView::SessionItem::setData().

Here is the call graph for this function:

◆ TEST_F() [3/9]

TEST_F ( ViewModelUtilsTest  ,
ItemRoleToQtRole   
)

Translation of item role to Qt roles.

Definition at line 75 of file viewmodelutils.test.cpp.

76 {
77  // DATA role of SessionItem should be translated to two Qt roles (edit and display)
79  QVector<int> expected = {Qt::DisplayRole, Qt::EditRole};
80  EXPECT_EQ(roles, expected);
81 
82  // APPEARANCE roles of SessionItem on Qt site means color
84 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
85  expected = {Qt::ForegroundRole};
86 #else
87  expected = {Qt::TextColorRole};
88 #endif
89  EXPECT_EQ(roles, expected);
90 
91  // tooltip role
93  expected = {Qt::ToolTipRole};
94  EXPECT_EQ(roles, expected);
95 }
const int TOOLTIP
tooltip for item's data
Definition: mvvm_types.h:34
const int DATA
main data role
Definition: mvvm_types.h:30
const int APPEARANCE
appearance flag
Definition: mvvm_types.h:32
MVVM_VIEWMODEL_EXPORT QVariant TextColorRole(const SessionItem &item)
Returns text color for given item.
MVVM_VIEWMODEL_EXPORT QVector< int > ItemRoleToQtRole(int role)
Returns vector of Qt roles corresponding to given ItemDataRole.
QVariant ToolTipRole(const SessionItem &item, int ncol=0)
Returns tooltip for given item.
QVariant ForegroundRole(const SessionItem &item)
Returns text color for given item.

References ModelView::ItemDataRole::APPEARANCE, ModelView::ItemDataRole::DATA, SessionItemUtils::ForegroundRole(), ModelView::Utils::ItemRoleToQtRole(), ModelView::Utils::TextColorRole(), ModelView::ItemDataRole::TOOLTIP, and SessionItemUtils::ToolTipRole().

Here is the call graph for this function:

◆ TEST_F() [4/9]

TEST_F ( ViewModelUtilsTest  ,
itemsFromIndex   
)

Check ItemsFromIndex in PropertyTableViewModel context.

ViewItem with its three property x, y, z forms one row. All corresponding indices of (x,y,z) should give us pointers to VectorItem's properties.

Definition at line 161 of file viewmodelutils.test.cpp.

162 {
163  // creating VectorItem and viewModel to see it as a table
164  SessionModel model;
165  auto parent = model.insertItem<VectorItem>();
166  PropertyTableViewModel viewModel(&model);
167 
168  // it's a table with one row and x,y,z columns
169  EXPECT_EQ(viewModel.rowCount(), 1);
170  EXPECT_EQ(viewModel.columnCount(), 3);
171 
172  // empty index list doesn't lead to SessionItem's
173  QModelIndexList index_list;
174  EXPECT_EQ(Utils::ItemsFromIndex(index_list).size(), 0);
175 
176  // index list populated with column of properties
177  index_list.push_back(viewModel.index(0, 0));
178  index_list.push_back(viewModel.index(0, 1));
179  index_list.push_back(viewModel.index(0, 2));
180 
181  std::vector<SessionItem*> expected = {parent->getItem(VectorItem::P_X),
182  parent->getItem(VectorItem::P_Y),
183  parent->getItem(VectorItem::P_Z)};
184  EXPECT_EQ(Utils::ItemsFromIndex(index_list), expected);
185  EXPECT_EQ(Utils::UniqueItemsFromIndex(index_list), expected);
186 }
View model to show content of SessionModel in Qt widgets: all item properties as a table row.
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
Vector item with three x,y,z property items.
Definition: vectoritem.h:24
static const QString P_Z
Definition: VectorItem.h:26
static const QString P_Y
Definition: VectorItem.h:25
static const QString P_X
Definition: VectorItem.h:24
MVVM_VIEWMODEL_EXPORT std::vector< SessionItem * > ItemsFromIndex(const QModelIndexList &index_list)
Returns vector of underlying SessionItem's for given index list.
MVVM_VIEWMODEL_EXPORT std::vector< SessionItem * > UniqueItemsFromIndex(const QModelIndexList &index_list)
Returns vector of underlying SessionItem's for given index list. Removes repetitions.

References ModelView::SessionModel::insertItem(), ModelView::Utils::ItemsFromIndex(), ModelView::VectorItem::P_X, ModelView::VectorItem::P_Y, ModelView::VectorItem::P_Z, and ModelView::Utils::UniqueItemsFromIndex().

Here is the call graph for this function:

◆ TEST_F() [5/9]

TEST_F ( ViewModelUtilsTest  ,
itemTextColorRole   
)

Testing color role of item.

Definition at line 99 of file viewmodelutils.test.cpp.

100 {
101  SessionItem item("Something");
102 
103  // no color defined for item by default
104  auto variant = Utils::TextColorRole(item);
105  EXPECT_FALSE(variant.isValid());
106 
107  item.setEnabled(false);
108  variant = Utils::TextColorRole(item);
109  EXPECT_EQ(variant.value<QColor>(), QColor(Qt::gray));
110 }

References ModelView::SessionItem::setEnabled(), and ModelView::Utils::TextColorRole().

Here is the call graph for this function:

◆ TEST_F() [6/9]

TEST_F ( ViewModelUtilsTest  ,
itemToolTipRole   
)

Testing tooltip role of the item.

Definition at line 146 of file viewmodelutils.test.cpp.

147 {
148  SessionItem item("Something");
149 
150  auto variant = Utils::ToolTipRole(item);
151  EXPECT_FALSE(variant.isValid());
152 
153  item.setToolTip("abc");
154  EXPECT_EQ(Utils::ToolTipRole(item).toString(), QString("abc"));
155 }
std::string toString(PyObject *obj)
Converts PyObject into string, if possible, or throws exception.
Definition: PyUtils.cpp:24

References ModelView::SessionItem::setToolTip(), ModelView::Utils::ToolTipRole(), and PyUtils::toString().

Here is the call graph for this function:

◆ TEST_F() [7/9]

TEST_F ( ViewModelUtilsTest  ,
iterate   
)

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

49 {
50  QStandardItemModel model;
51 
52  model.setColumnCount(2);
53  QStandardItem* parentItem = model.invisibleRootItem();
54 
55  auto row1 = get_items({1, 2});
56  parentItem->appendRow(row1);
57  row1.at(0)->appendRow(get_items({3, 4}));
58 
59  auto row2 = get_items({10, 20});
60  parentItem->appendRow(row2);
61 
62  std::vector<int> expected = {1, 2, 3, 4, 10, 20};
63  std::vector<int> result;
64 
65  Utils::iterate_model(&model, QModelIndex(), [&](const QModelIndex& index) {
66  auto item = model.itemFromIndex(index);
67  result.push_back(item->data(Qt::EditRole).value<int>());
68  });
69 
70  EXPECT_EQ(result, expected);
71 }
MVVM_VIEWMODEL_EXPORT void iterate_model(const QAbstractItemModel *model, const QModelIndex &parent, const std::function< void(const QModelIndex &child)> &fun)
Iterates through QAbstractItem model.

References ModelView::Utils::iterate_model().

Here is the call graph for this function:

◆ TEST_F() [8/9]

TEST_F ( ViewModelUtilsTest  ,
parentItemsFromIndex   
)

Check ParentItemsFromIndex in PropertyTableViewModel context.

ViewItem with its three property x, y, z forms one row. All corresponding indices of (x,y,z) should give us pointer to VectorItem.

Definition at line 217 of file viewmodelutils.test.cpp.

218 {
219  // creating VectorItem and viewModel to see it as a table
220  SessionModel model;
221  auto parent = model.insertItem<VectorItem>();
222  PropertyTableViewModel viewModel(&model);
223 
224  // it's a table with one row and x,y,z columns
225  EXPECT_EQ(viewModel.rowCount(), 1);
226  EXPECT_EQ(viewModel.columnCount(), 3);
227 
228  // empty index list doesn't lead to SessionItem's
229  QModelIndexList index_list;
230  EXPECT_EQ(Utils::ParentItemsFromIndex(index_list).size(), 0);
231 
232  std::vector<SessionItem*> expected = {parent};
233 
234  // one cell in a list should give us pointer to original VectorItem
235  index_list.push_back(viewModel.index(0, 1));
236  EXPECT_EQ(Utils::ParentItemsFromIndex(index_list), expected);
237 
238  index_list.clear();
239  index_list.push_back(viewModel.index(0, 1));
240  EXPECT_EQ(Utils::ParentItemsFromIndex(index_list), expected);
241 
242  index_list.clear();
243  index_list.push_back(viewModel.index(0, 2));
244  EXPECT_EQ(Utils::ParentItemsFromIndex(index_list), expected);
245 
246  // tthree cells (x, y, z) in a list should give us pointer to original VectorItem
247  index_list.clear();
248  index_list.push_back(viewModel.index(0, 0));
249  index_list.push_back(viewModel.index(0, 1));
250  index_list.push_back(viewModel.index(0, 2));
251  EXPECT_EQ(Utils::ParentItemsFromIndex(index_list), expected);
252 }
MVVM_VIEWMODEL_EXPORT std::vector< SessionItem * > ParentItemsFromIndex(const QModelIndexList &index_list)
Returns vector of parent items from given index list.

References ModelView::SessionModel::insertItem(), and ModelView::Utils::ParentItemsFromIndex().

Here is the call graph for this function:

◆ TEST_F() [9/9]

TEST_F ( ViewModelUtilsTest  ,
UniqueItemsFromIndex   
)

Check UniqueItemsFromIndex for artificially constructed viewmodel.

Definition at line 190 of file viewmodelutils.test.cpp.

191 {
192  SessionItem item1;
193  item1.setData(42, ItemDataRole::DATA);
194  SessionItem item2;
195  item2.setData(42, ItemDataRole::DATA);
196 
197  ViewModelBase viewmodel;
198  std::vector<std::unique_ptr<ViewItem>> items;
199  items.emplace_back(std::make_unique<ViewLabelItem>(&item1));
200  items.emplace_back(std::make_unique<ViewLabelItem>(&item2));
201  items.emplace_back(std::make_unique<ViewDataItem>(&item1));
202  items.emplace_back(std::make_unique<ViewDataItem>(&item2));
203  viewmodel.insertRow(viewmodel.rootItem(), 0, std::move(items));
204 
205  QModelIndexList index_list = {viewmodel.index(0, 0), viewmodel.index(0, 1),
206  viewmodel.index(0, 2), viewmodel.index(0, 3)};
207 
208  EXPECT_EQ(Utils::ItemsFromIndex(index_list),
209  std::vector<SessionItem*>({&item1, &item2, &item1, &item2}));
210  EXPECT_EQ(Utils::UniqueItemsFromIndex(index_list), std::vector<SessionItem*>({&item1, &item2}));
211 }
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Definition: sessionitem.h:141
Base class for all view models to show content of SessionModel in Qt views.
Definition: viewmodelbase.h:31
ViewItem * rootItem() const
Returns a pointer to invisible root item.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void insertRow(ViewItem *parent, int row, std::vector< std::unique_ptr< ViewItem >> items)
Insert a row of items at index 'row' to given parent.

References ModelView::ItemDataRole::DATA, ModelView::ViewModelBase::index(), ModelView::ViewModelBase::insertRow(), ModelView::Utils::ItemsFromIndex(), ModelView::ViewModelBase::rootItem(), ModelView::SessionItem::setData(), and ModelView::Utils::UniqueItemsFromIndex().

Here is the call graph for this function: