24 #include <QModelIndexList>
25 #include <QStandardItemModel>
28 QList<QStandardItem*> get_items(std::vector<int> data)
30 QList<QStandardItem*> result;
33 result.append(
new QStandardItem(QString::number(x)));
50 QStandardItemModel model;
52 model.setColumnCount(2);
53 QStandardItem* parentItem = model.invisibleRootItem();
55 auto row1 = get_items({1, 2});
56 parentItem->appendRow(row1);
57 row1.at(0)->appendRow(get_items({3, 4}));
59 auto row2 = get_items({10, 20});
60 parentItem->appendRow(row2);
62 std::vector<int> expected = {1, 2, 3, 4, 10, 20};
63 std::vector<int> result;
66 auto item = model.itemFromIndex(index);
67 result.push_back(item->data(Qt::EditRole).value<
int>());
70 EXPECT_EQ(result, expected);
79 QVector<int> expected = {Qt::DisplayRole, Qt::EditRole};
80 EXPECT_EQ(roles, expected);
84 #if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
89 EXPECT_EQ(roles, expected);
94 EXPECT_EQ(roles, expected);
105 EXPECT_FALSE(variant.isValid());
109 EXPECT_EQ(variant.value<QColor>(), QColor(Qt::gray));
120 EXPECT_FALSE(variant.isValid());
122 item.
setData(QVariant::fromValue(
true));
125 item.
setData(QVariant::fromValue(
false));
137 EXPECT_FALSE(variant.isValid());
139 QColor expected(Qt::green);
151 EXPECT_FALSE(variant.isValid());
169 EXPECT_EQ(viewModel.rowCount(), 1);
170 EXPECT_EQ(viewModel.columnCount(), 3);
173 QModelIndexList index_list;
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));
181 std::vector<SessionItem*> expected = {parent->getItem(
VectorItem::P_X),
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));
205 QModelIndexList index_list = {viewmodel.
index(0, 0), viewmodel.
index(0, 1),
206 viewmodel.
index(0, 2), viewmodel.
index(0, 3)};
209 std::vector<SessionItem*>({&item1, &item2, &item1, &item2}));
225 EXPECT_EQ(viewModel.rowCount(), 1);
226 EXPECT_EQ(viewModel.columnCount(), 3);
229 QModelIndexList index_list;
232 std::vector<SessionItem*> expected = {parent};
235 index_list.push_back(viewModel.index(0, 1));
239 index_list.push_back(viewModel.index(0, 1));
243 index_list.push_back(viewModel.index(0, 2));
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));
View model to show content of SessionModel in Qt widgets: all item properties as a table row.
The main object representing an editable/displayable/serializable entity.
SessionItem * setEnabled(bool value)
Sets enabled flag to given value (fluent interface).
SessionItem * setToolTip(const std::string &tooltip)
Sets item tooltip (fluent interface).
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Main class to hold hierarchy of SessionItem objects.
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
Vector item with three x,y,z property items.
static const std::string P_X
static const std::string P_Z
static const std::string P_Y
Base class for all view models to show content of SessionModel in Qt views.
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.
void iterate(const QModelIndex &index, const QAbstractItemModel *model, const std::function< void(const QModelIndex &)> &fun)
Iterates through all model indices and calls user function.
const int TOOLTIP
tooltip for item's data
const int DATA
main data role
const int APPEARANCE
appearance flag
MVVM_VIEWMODEL_EXPORT QVariant TextColorRole(const SessionItem &item)
Returns text color for given item.
MVVM_VIEWMODEL_EXPORT QVariant DecorationRole(const SessionItem &item)
Returns decoration role for given item.
MVVM_VIEWMODEL_EXPORT QVariant ToolTipRole(const SessionItem &item)
Returns tooltip role for given item.
MVVM_VIEWMODEL_EXPORT QVector< int > ItemRoleToQtRole(int role)
Returns vector of Qt roles corresponding to given ItemDataRole.
MVVM_VIEWMODEL_EXPORT std::vector< SessionItem * > ItemsFromIndex(const QModelIndexList &index_list)
Returns vector of underlying SessionItem's for given index list.
MVVM_VIEWMODEL_EXPORT void iterate_model(const QAbstractItemModel *model, const QModelIndex &parent, const std::function< void(const QModelIndex &child)> &fun)
Iterates through QAbstractItem model.
MVVM_VIEWMODEL_EXPORT QVariant CheckStateRole(const SessionItem &item)
Returns check state role of given item.
MVVM_VIEWMODEL_EXPORT std::vector< SessionItem * > UniqueItemsFromIndex(const QModelIndexList &index_list)
Returns vector of underlying SessionItem's for given index list. Removes repetitions.
MVVM_VIEWMODEL_EXPORT std::vector< SessionItem * > ParentItemsFromIndex(const QModelIndexList &index_list)
Returns vector of parent items from given index list.
materialitems.h Collection of materials to populate MaterialModel.
std::string toString(PyObject *obj)
Converts PyObject into string, if possible, or throws exception.
QVariant ToolTipRole(const SessionItem &item, int ncol=0)
Returns tooltip for given item.
QVariant ForegroundRole(const SessionItem &item)
Returns text color for given item.
TEST_F(ViewModelUtilsTest, iterate)