21 #include <QStandardItemModel>
37 using children_t = std::vector<std::unique_ptr<ViewItem>>;
45 std::pair<children_t, expected_t>
test_data(
int ncolumns)
47 auto vector_of_unique = TestUtils::create_row<ViewItem, TestItem>(ncolumns);
49 return std::make_pair(std::move(vector_of_unique), std::move(vector_of_pointers));
60 QStandardItemModel model;
61 auto parent = model.invisibleRootItem();
63 EXPECT_EQ(model.rowCount(), 0);
64 EXPECT_EQ(model.columnCount(), 0);
66 QList<QStandardItem*> children{
new QStandardItem,
new QStandardItem};
67 parent->appendRow(children);
68 auto index = model.index(0, 1, QModelIndex());
69 EXPECT_EQ(model.itemFromIndex(index), children.at(1));
72 auto non_existing_index = model.index(0, 2, QModelIndex());
73 EXPECT_FALSE(non_existing_index.isValid());
74 EXPECT_EQ(non_existing_index, QModelIndex());
77 EXPECT_EQ(model.itemFromIndex(non_existing_index),
nullptr);
80 EXPECT_EQ(model.itemFromIndex(QModelIndex()),
nullptr);
93 EXPECT_TRUE(viewmodel.
rootItem() !=
nullptr);
95 auto non_existing_index = viewmodel.
index(0, 0, QModelIndex());
96 EXPECT_FALSE(non_existing_index.isValid());
97 EXPECT_EQ(viewmodel.
itemFromIndex(non_existing_index),
nullptr);
98 EXPECT_EQ(viewmodel.
parent(QModelIndex()), QModelIndex());
107 auto [children, expected] = test_data( 1);
115 auto child_index = viewmodel.
index(0, 0, QModelIndex());
116 EXPECT_EQ(child_index.row(), 0);
117 EXPECT_EQ(child_index.column(), 0);
118 EXPECT_EQ(child_index.model(), &viewmodel);
121 EXPECT_EQ(viewmodel.
indexFromItem(expected[0]), child_index);
124 EXPECT_EQ(viewmodel.
itemFromIndex(child_index), expected[0]);
127 EXPECT_EQ(viewmodel.
rowCount(child_index), 0);
131 EXPECT_EQ(viewmodel.
parent(child_index), QModelIndex());
141 auto [children_row0, expected_row0] = test_data( 1);
142 auto [children_front, expected_front] = test_data( 1);
151 auto child_index0 = viewmodel.
index(0, 0, QModelIndex());
152 auto child_index1 = viewmodel.
index(1, 0, QModelIndex());
155 EXPECT_EQ(viewmodel.
indexFromItem(expected_row0[0]), child_index1);
156 EXPECT_EQ(viewmodel.
indexFromItem(expected_front[0]), child_index0);
159 EXPECT_EQ(viewmodel.
itemFromIndex(child_index0), expected_front[0]);
160 EXPECT_EQ(viewmodel.
itemFromIndex(child_index1), expected_row0[0]);
168 auto [children, expected] = test_data( 1);
186 auto [children_row0, expected_row0] = test_data( 2);
187 auto [children_row1, expected_row1] = test_data( 2);
192 auto child0_index = viewmodel.
index(0, 0, QModelIndex());
193 auto child1_index = viewmodel.
index(0, 1, QModelIndex());
194 viewmodel.
appendRow(expected_row0[0], std::move(children_row1));
197 EXPECT_EQ(viewmodel.
rowCount(QModelIndex()), 1);
198 EXPECT_EQ(viewmodel.
columnCount(QModelIndex()), 2);
199 EXPECT_EQ(viewmodel.
rowCount(child0_index), 1);
203 auto grandchild0_index = viewmodel.
index(0, 0, child0_index);
204 auto grandchild1_index = viewmodel.
index(0, 1, child0_index);
205 EXPECT_EQ(viewmodel.
parent(grandchild0_index), child0_index);
206 EXPECT_EQ(viewmodel.
parent(grandchild1_index), child0_index);
209 EXPECT_EQ(viewmodel.
indexFromItem(expected_row0[0]), child0_index);
210 EXPECT_EQ(viewmodel.
indexFromItem(expected_row0[1]), child1_index);
211 EXPECT_EQ(viewmodel.
indexFromItem(expected_row1[0]), grandchild0_index);
212 EXPECT_EQ(viewmodel.
indexFromItem(expected_row1[1]), grandchild1_index);
220 auto [children, expected] = test_data( 2);
222 QSignalSpy spyInsert(&viewmodel, &ViewModelBase::rowsInserted);
223 QSignalSpy spyRemove(&viewmodel, &ViewModelBase::rowsRemoved);
231 EXPECT_EQ(spyRemove.count(), 0);
232 EXPECT_EQ(spyInsert.count(), 1);
233 QList<QVariant> arguments = spyInsert.takeFirst();
234 EXPECT_EQ(arguments.size(), 3);
235 EXPECT_EQ(arguments.at(0).value<QModelIndex>(), QModelIndex());
236 EXPECT_EQ(arguments.at(1).value<
int>(), 0);
237 EXPECT_EQ(arguments.at(2).value<
int>(), 0);
240 auto index0 = viewmodel.
index(0, 0, QModelIndex());
241 auto index1 = viewmodel.
index(0, 1, QModelIndex());
251 auto [children_row0, expected_row0] = test_data( 2);
252 auto [children_row1, expected_row1] = test_data( 2);
253 auto [children_row2, expected_row2] = test_data( 2);
255 QSignalSpy spyInsert(&viewmodel, &ViewModelBase::rowsInserted);
256 QSignalSpy spyRemove(&viewmodel, &ViewModelBase::rowsRemoved);
267 EXPECT_EQ(spyRemove.count(), 1);
268 EXPECT_EQ(spyInsert.count(), 3);
269 QList<QVariant> arguments = spyRemove.takeFirst();
270 EXPECT_EQ(arguments.size(), 3);
271 EXPECT_EQ(arguments.at(0).value<QModelIndex>(), QModelIndex());
272 EXPECT_EQ(arguments.at(1).value<
int>(), 1);
273 EXPECT_EQ(arguments.at(2).value<
int>(), 1);
279 QVariant expected(42.0);
283 children.emplace_back(std::make_unique<ViewDataItem>(&item));
288 QModelIndex children_index = viewmodel.
index(0, 0, QModelIndex());
290 EXPECT_EQ(viewmodel.
data(children_index, Qt::EditRole), expected);
297 QVariant expected(42.0);
302 children.emplace_back(std::make_unique<ViewDataItem>(&item));
306 QSignalSpy spyData(&viewmodel, &ViewModelBase::dataChanged);
309 QModelIndex children_index = viewmodel.
index(0, 0, QModelIndex());
310 QVariant new_value(43.0);
311 EXPECT_TRUE(viewmodel.
setData(children_index, new_value, Qt::EditRole));
314 EXPECT_EQ(spyData.count(), 1);
315 QList<QVariant> arguments = spyData.takeFirst();
316 EXPECT_EQ(arguments.size(), 3);
317 EXPECT_EQ(arguments.at(0).value<QModelIndex>(), children_index);
318 EXPECT_EQ(arguments.at(1).value<QModelIndex>(), children_index);
319 QVector<int> expected_roles{Qt::EditRole};
320 EXPECT_EQ(arguments.at(2).value<QVector<int>>(), expected_roles);
326 QVariant expected(42.0);
331 children.emplace_back(std::make_unique<ViewLabelItem>(&item));
332 children.emplace_back(std::make_unique<ViewDataItem>(&item));
337 QModelIndex label_index = viewmodel.
index(0, 0, QModelIndex());
338 QModelIndex data_index = viewmodel.
index(0, 1, QModelIndex());
340 EXPECT_TRUE(viewmodel.
flags(label_index) & Qt::ItemIsSelectable);
341 EXPECT_TRUE(viewmodel.
flags(label_index) & Qt::ItemIsEnabled);
342 EXPECT_FALSE(viewmodel.
flags(label_index) & Qt::ItemIsEditable);
344 EXPECT_TRUE(viewmodel.
flags(data_index) & Qt::ItemIsSelectable);
345 EXPECT_TRUE(viewmodel.
flags(data_index) & Qt::ItemIsEnabled);
346 EXPECT_TRUE(viewmodel.
flags(data_index) & Qt::ItemIsEditable);
354 auto [children_row0, expected_row0] = test_data( 2);
355 auto [children_row1, expected_row1] = test_data( 2);
357 QSignalSpy spyInsert(&viewmodel, &ViewModelBase::rowsInserted);
358 QSignalSpy spyRemove(&viewmodel, &ViewModelBase::rowsRemoved);
370 EXPECT_EQ(spyRemove.count(), 1);
371 EXPECT_EQ(spyInsert.count(), 2);
372 QList<QVariant> arguments = spyRemove.takeFirst();
373 EXPECT_EQ(arguments.size(), 3);
374 EXPECT_EQ(arguments.at(0).value<QModelIndex>(), QModelIndex());
375 EXPECT_EQ(arguments.at(1).value<
int>(), 0);
376 EXPECT_EQ(arguments.at(2).value<
int>(), 1);
The main object representing an editable/displayable/serializable entity.
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
virtual SessionItem * setDisplayName(const std::string &name)
Sets display name (fluent interface).
Represents the view of SessionItem's data in a single cell of ViewModel.
Base class for all view models to show content of SessionModel in Qt views.
void removeRow(ViewItem *parent, int row)
void appendRow(ViewItem *parent, std::vector< std::unique_ptr< ViewItem >> items)
Appends row of items to given parent.
ViewItem * rootItem() const
Returns a pointer to invisible root item.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns the item flags for the given index.
void clearRows(ViewItem *parent)
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QModelIndex indexFromItem(const ViewItem *item) const
Returns the QModelIndex associated with the given item.
void insertRow(ViewItem *parent, int row, std::vector< std::unique_ptr< ViewItem >> items)
Insert a row of items at index 'row' to given parent.
QModelIndex parent(const QModelIndex &child) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
ViewItem * itemFromIndex(const QModelIndex &index) const
Returns a pointer to the RefViewItem associated with the given index.
Tests for ViewModelBase class.
std::pair< children_t, expected_t > test_data(int ncolumns)
Helper function to get two vectors, each ncolumns length, in the form of a pair.
std::vector< ViewItem * > expected_t
std::vector< std::unique_ptr< ViewItem > > children_t
void removeRow(QGridLayout *layout, int row, bool deleteWidgets=true)
Removes row from grid layout (important: doesn't change row count).
materialitems.h Collection of materials to populate MaterialModel.
auto create_pointers(const std::vector< std::unique_ptr< T >> &vec)
Creates vector of pointers from vector of unique_ptr.
TEST_F(ViewModelBaseTest, standardItemModel)
Checking behaviour of QStandardItemModel for reference.