49 auto pool = std::make_shared<ItemPool>();
56 EXPECT_TRUE(item !=
nullptr);
57 EXPECT_EQ(item->parent(), model.
rootItem());
58 EXPECT_EQ(item->model(), &model);
59 EXPECT_EQ(item->modelType(), modelType);
62 auto item_key = item->identifier();
63 EXPECT_EQ(pool->item_for_key(item_key), item);
71 EXPECT_EQ(pool->item_for_key(child_key), child);
73 EXPECT_TRUE(child !=
nullptr);
74 EXPECT_EQ(child->parent(), item);
75 EXPECT_EQ(child->model(), &model);
76 EXPECT_EQ(child->modelType(), modelType);
79 auto taken = item->takeItem({
"", 0});
80 EXPECT_EQ(taken, child);
81 EXPECT_EQ(child->model(),
nullptr);
84 EXPECT_EQ(pool->item_for_key(child_key),
nullptr);
91 auto pool = std::make_shared<ItemPool>();
98 EXPECT_TRUE(item !=
nullptr);
99 EXPECT_EQ(item->parent(), model.
rootItem());
100 EXPECT_EQ(item->model(), &model);
101 EXPECT_EQ(item->modelType(), modelType);
104 auto item_key = item->identifier();
105 EXPECT_EQ(pool->item_for_key(item_key), item);
113 EXPECT_EQ(pool->item_for_key(child_key), child);
115 EXPECT_TRUE(child !=
nullptr);
116 EXPECT_EQ(child->parent(), item);
117 EXPECT_EQ(child->model(), &model);
118 EXPECT_EQ(child->modelType(), modelType);
121 auto taken = item->takeItem({
"", 0});
122 EXPECT_EQ(taken, child);
123 EXPECT_EQ(child->model(),
nullptr);
126 EXPECT_EQ(pool->item_for_key(child_key),
nullptr);
133 const std::string tag1(
"tag1");
159 QVariant value(42.0);
172 auto pool = std::make_shared<ItemPool>();
188 EXPECT_EQ(pool->key_for_item(child2),
"");
193 auto pool = std::make_shared<ItemPool>();
200 EXPECT_NO_THROW(model.
removeItem(parent, {
"", 0}));
205 auto pool = std::make_shared<ItemPool>();
210 auto parent_key = parent->identifier();
215 EXPECT_EQ(pool->item_for_key(parent_key), parent);
216 EXPECT_EQ(pool->item_for_key(child_key), child);
220 EXPECT_EQ(pool->item_for_key(parent_key),
nullptr);
221 EXPECT_EQ(pool->item_for_key(child_key),
nullptr);
240 model.
moveItem(child0, parent1, {
"", 0});
242 std::vector<SessionItem*> expected = {child0, child1};
243 EXPECT_EQ(parent1->
children(), expected);
244 EXPECT_EQ(parent0->children().size(), 0);
249 auto pool = std::make_shared<ItemPool>();
252 EXPECT_EQ(pool->size(), 1);
263 EXPECT_FALSE(model.
rootItem() == first_root);
264 EXPECT_EQ(pool->key_for_item(first_root),
"");
265 EXPECT_EQ(pool->size(), 1);
270 auto pool = std::make_shared<ItemPool>();
273 EXPECT_EQ(pool->size(), 1);
285 model.
clear(rebuild);
287 EXPECT_FALSE(model.
rootItem() == first_root);
288 EXPECT_EQ(pool->key_for_item(first_root),
"");
289 EXPECT_EQ(pool->size(), 2);
290 EXPECT_EQ(pool->key_for_item(new_item), new_item->identifier());
307 ASSERT_TRUE(
copy !=
nullptr);
308 ASSERT_TRUE(
copy != item);
309 EXPECT_FALSE(
copy->identifier().empty());
310 EXPECT_TRUE(
copy->identifier() != item->identifier());
311 EXPECT_EQ(
copy->data<
double>(), 42.0);
313 EXPECT_TRUE(item !=
copy);
314 std::vector<SessionItem*> expected = {item,
copy};
332 auto copy_child =
copy->getItem(
"defaultTag");
334 ASSERT_TRUE(
copy !=
nullptr);
335 ASSERT_TRUE(copy_child !=
nullptr);
336 EXPECT_FALSE(
copy->identifier().empty());
337 EXPECT_TRUE(
copy->identifier() != parent0->identifier());
338 EXPECT_EQ(copy_child->data<
double>(), 42.0);
352 auto item = std::make_unique<PropertyItem>();
357 EXPECT_EQ(
copy->data<
double>(), 42.0);
372 auto copy = model.
copyItem(property, parent0, {
"property", -1});
374 EXPECT_EQ(
copy,
nullptr);
386 EXPECT_EQ(model.
findItem(
id), parent);
390 EXPECT_EQ(model.
findItem(
id),
nullptr);
398 auto pool = std::make_shared<ItemPool>();
409 EXPECT_EQ(model1.
findItem(id1), parent1);
410 EXPECT_EQ(model2.
findItem(id1), parent1);
411 EXPECT_EQ(model1.
findItem(id2), parent2);
412 EXPECT_EQ(model2.
findItem(id2), parent2);
416 EXPECT_EQ(model1.
findItem(id1),
nullptr);
417 EXPECT_EQ(model2.
findItem(id1),
nullptr);
418 EXPECT_EQ(model1.
findItem(id2), parent2);
419 EXPECT_EQ(model2.
findItem(id2), parent2);
425 EXPECT_EQ(model.
topItem<>(),
nullptr);
426 EXPECT_EQ(model.
topItem(),
nullptr);
430 EXPECT_EQ(model.
topItem<>(), property);
431 EXPECT_EQ(model.
topItem(), property);
437 std::vector<SessionItem*> expected;
440 EXPECT_EQ(model.
topItems<>(), expected);
441 EXPECT_EQ(model.
topItems(), expected);
448 expected = {property1, compound1, property2, compound2};
449 EXPECT_EQ(model.
topItems<>(), expected);
450 EXPECT_EQ(model.
topItems(), expected);
452 std::vector<CompoundItem*> expected2 = {compound1, compound2};
458 const std::string expectedModelType(
"TestItemType");
464 ASSERT_TRUE(item !=
nullptr);
465 ASSERT_TRUE(
dynamic_cast<TestItem*
>(item) !=
nullptr);
466 EXPECT_EQ(item->modelType(), expectedModelType);
Complex item holding mixed SessionItem types (single properties and other CompountItems).
Item to carry concrete editable entity (e.g.
The main object representing an editable/displayable/serializable entity.
std::string identifier() const
Returns unique identifier.
TagRow tagRowOfItem(const SessionItem *item) const
Returns pair of tag and row corresponding to given item.
SessionItem * takeItem(const TagRow &tagrow)
Removes item from given row from given tag, returns it to the caller.
void registerTag(const TagInfo &tagInfo, bool set_as_default=false)
Registers tag to hold items under given name.
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
SessionItem * parent() const
Returns parent item. Will return nullptr if item doesn't have a parent.
SessionModel * model() const
Returns the model to which given item belongs to.
int childrenCount() const
Returns total number of children in all tags.
std::vector< SessionItem * > children() const
Returns vector of children formed from all chidlren from all tags.
bool insertItem(SessionItem *item, const TagRow &tagrow)
Insert item into given tag under the given row.
Main class to hold hierarchy of SessionItem objects.
T * topItem() const
Returns top item of the given type.
bool setData(SessionItem *item, const Variant &value, int role)
Sets the data for given item.
std::vector< T * > topItems() const
Returns top items of the given type.
SessionItem * insertNewItem(const model_type &modelType, SessionItem *parent=nullptr, const TagRow &tagrow={})
Insert new item using item's modelType.
void moveItem(SessionItem *item, SessionItem *new_parent, const TagRow &tagrow)
Move item from it's current parent to a new parent under given tag and row.
SessionItem * findItem(const identifier_type &id)
Returns SessionItem for given identifier.
SessionItem * copyItem(const SessionItem *item, SessionItem *parent, const TagRow &tagrow={})
Copy item and insert it in parent's tag and row. Item could belong to any model/parent.
SessionItem * rootItem() const
Returns root item of the model.
void registerItem(const std::string &label={})
Register used defined item to use with the model.
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
Variant data(SessionItem *item, int role) const
Returns the data for given item and role.
void clear(std::function< void(SessionItem *)> callback={})
Removes all items from the model.
void removeItem(SessionItem *parent, const TagRow &tagrow)
Removes given row from parent.
static TagInfo universalTag(std::string name, std::vector< std::string > modelTypes={})
Constructs universal tag intended for unlimited amount of various items.
static TagInfo propertyTag(std::string name, std::string model_type)
Constructs tag intended for single property.
static TagRow append(const std::string &tag_name={})
Returns TagRow corresponding to the append to tag_name.
const model_type BaseType
const int DATA
main data role
const int DISPLAY
display name
MVVM_MODEL_EXPORT int IndexOfChild(const SessionItem *parent, const SessionItem *child)
Returns index in children array corresponding to given child.
MVVM_MODEL_EXPORT SessionItem * ChildAt(const SessionItem *parent, int index)
Returns child at given index of parent.
materialitems.h Collection of materials to populate MaterialModel.
std::string identifier_type
@ copy
full deep copying with item identifiers regenerated
TEST_F(SessionModelTest, initialState)