41 EXPECT_EQ(item.
model(),
nullptr);
42 EXPECT_EQ(item.
parent(),
nullptr);
44 EXPECT_FALSE(item.
data<QVariant>(role).isValid());
45 EXPECT_TRUE(item.
children().empty());
70 EXPECT_FALSE(item.
data<QVariant>(role).isValid());
72 QVariant expected(42.0);
73 EXPECT_TRUE(item.
setData(expected, role));
78 EXPECT_EQ(item.
data<QVariant>(role), expected);
81 EXPECT_TRUE(item.
setData(43.0, role));
83 EXPECT_EQ(item.
data<QVariant>(role), QVariant::fromValue(43.0));
86 EXPECT_FALSE(item.
setData(43.0, role));
88 EXPECT_EQ(item.
data<QVariant>(role), QVariant::fromValue(43.0));
130 const double expected = 42.0;
131 EXPECT_TRUE(item.
setData(expected));
132 EXPECT_EQ(item.
data<
double>(), expected);
138 const int expected = 42;
139 EXPECT_TRUE(item.
setData(expected));
140 EXPECT_EQ(item.
data<
int>(), expected);
146 const bool expected_true =
true;
147 EXPECT_TRUE(item.
setData(expected_true));
148 EXPECT_EQ(item.
data<
bool>(), expected_true);
149 const bool expected_false =
false;
150 EXPECT_TRUE(item.
setData(expected_false));
151 EXPECT_EQ(item.
data<
bool>(), expected_false);
157 const std::string expected{
"abc"};
158 EXPECT_TRUE(item.
setData(expected));
159 EXPECT_EQ(item.
data<std::string>(), expected);
168 EXPECT_TRUE(item.
setData(data));
176 EXPECT_EQ(item.
data<
double>(), 42.0);
185 QVariant expected(42.0);
188 EXPECT_TRUE(item.
setData(expected, role));
193 EXPECT_EQ(item.
data<QVariant>(role), expected);
196 EXPECT_THROW(item.
setData(std::string(
"abc"), role), std::runtime_error);
199 EXPECT_NO_THROW(item.
setData(QVariant(), role));
207 auto item = std::make_unique<SessionItem>();
208 auto item_id = item->identifier();
209 EXPECT_EQ(item->itemData()->roles().size(), 2u);
211 std::shared_ptr<ItemPool> pool;
215 pool->register_item(item.get(), item_id);
217 EXPECT_EQ(item->identifier(), item_id);
220 auto key = pool->key_for_item(item.get());
222 EXPECT_EQ(item->itemData()->roles(), expected_roles);
223 EXPECT_EQ(item_id, key);
263 auto parent = std::make_unique<SessionItem>();
266 std::unique_ptr<SessionItem> child(
new SessionItem);
269 EXPECT_EQ(parent->childrenCount(), 0);
272 EXPECT_EQ(parent->getItem(
"", 0),
nullptr);
273 EXPECT_EQ(parent->getItem(
"", -1),
nullptr);
274 EXPECT_EQ(parent->getItem(
"", 10),
nullptr);
277 auto p_child = child.release();
278 parent->insertItem(p_child, {
"", 0});
279 EXPECT_EQ(parent->childrenCount(), 1);
281 EXPECT_EQ(parent->children()[0], p_child);
282 EXPECT_EQ(parent->getItem(
"", 0), p_child);
283 EXPECT_EQ(p_child->parent(), parent.get());
290 auto parent = std::make_unique<SessionItem>();
303 EXPECT_EQ(parent->getItem(
"", 0), child1);
304 EXPECT_EQ(parent->getItem(
"", 1), child2);
305 std::vector<SessionItem*> expected = {child1, child2};
306 EXPECT_EQ(parent->children(), expected);
309 parent->insertItem(child3, {
"", 1});
310 expected = {child1, child3, child2};
311 EXPECT_EQ(parent->children(), expected);
315 EXPECT_EQ(parent->getItem(
"", 0), child1);
316 EXPECT_EQ(parent->getItem(
"", 1), child3);
317 EXPECT_EQ(parent->getItem(
"", 2), child2);
318 EXPECT_EQ(parent->getItem(
"", 3),
nullptr);
321 parent->insertItem(child4, {
"", parent->childrenCount()});
324 EXPECT_EQ(child1->parent(), parent.get());
325 EXPECT_EQ(child2->parent(), parent.get());
326 EXPECT_EQ(child3->parent(), parent.get());
327 EXPECT_EQ(child4->parent(), parent.get());
330 EXPECT_THROW(parent->insertItem(child2,
TagRow::append()), std::runtime_error);
333 auto child5 = std::make_unique<SessionItem>();
334 EXPECT_FALSE(parent->insertItem(child5.get(), {
"", parent->childrenCount() + 1}));
341 auto parent = std::make_unique<SessionItem>();
353 EXPECT_EQ(parent->childrenCount(), 3);
356 EXPECT_EQ(parent->takeItem({
"", -1}),
nullptr);
357 EXPECT_EQ(parent->takeItem({
"", parent->childrenCount()}),
nullptr);
360 auto taken = parent->takeItem({
"", 0});
361 EXPECT_EQ(taken->parent(),
nullptr);
362 std::vector<SessionItem*> expected = {child2, child3};
363 EXPECT_EQ(parent->children(), expected);
372 const std::string tag1 =
"tag1";
375 auto parent = std::make_unique<SessionItem>();
383 parent->insertItem(child2, {tag1, -1});
386 std::vector<SessionItem*> expected = {child1, child2};
387 EXPECT_EQ(parent->children(), expected);
392 EXPECT_EQ(parent->getItem(tag1), child1);
393 EXPECT_EQ(parent->getItem(tag1, 0), child1);
394 EXPECT_EQ(parent->getItem(tag1, 1), child2);
395 EXPECT_EQ(parent->getItem(tag1, 2),
nullptr);
398 EXPECT_EQ(parent->getItems(tag1), expected);
401 delete parent->takeItem({tag1, 0});
402 EXPECT_EQ(parent->getItems(tag1), std::vector<SessionItem*>() = {child2});
404 delete parent->takeItem({tag1, 0});
405 EXPECT_EQ(parent->getItems(tag1), std::vector<SessionItem*>() = {});
408 EXPECT_EQ(parent->takeItem({tag1, 0}),
nullptr);
415 const std::string tag1 =
"tag1";
416 const std::string tag2 =
"tag2";
419 auto parent = std::make_unique<SessionItem>();
432 parent->insertItem(child_t2_c, {tag2, -1});
434 parent->insertItem(child_t1_a, {tag1, -1});
435 parent->insertItem(child_t1_b, {tag1, -1});
437 parent->insertItem(child_t2_b, {tag2, 1});
440 std::vector<SessionItem*> expected = {child_t1_a, child_t1_b, child_t2_a, child_t2_b,
442 EXPECT_EQ(parent->children(), expected);
447 EXPECT_EQ(parent->getItem(tag1), child_t1_a);
448 EXPECT_EQ(parent->getItem(tag1, 0), child_t1_a);
449 EXPECT_EQ(parent->getItem(tag1, 1), child_t1_b);
450 EXPECT_EQ(parent->getItem(tag2, 0), child_t2_a);
451 EXPECT_EQ(parent->getItem(tag2, 1), child_t2_b);
452 EXPECT_EQ(parent->getItem(tag2, 2), child_t2_c);
453 EXPECT_EQ(parent->getItem(tag2, 3),
nullptr);
456 expected = {child_t1_a, child_t1_b};
457 EXPECT_EQ(parent->getItems(tag1), expected);
458 expected = {child_t2_a, child_t2_b, child_t2_c};
459 EXPECT_EQ(parent->getItems(tag2), expected);
462 delete parent->takeItem({tag2, 1});
463 expected = {child_t1_a, child_t1_b};
464 EXPECT_EQ(parent->getItems(tag1), expected);
465 expected = {child_t2_a, child_t2_c};
466 EXPECT_EQ(parent->getItems(tag2), expected);
473 const std::string tag1 =
"tag1";
474 const int maxItems = 3;
475 auto parent = std::make_unique<SessionItem>();
476 parent->registerTag(
TagInfo(tag1, 0, maxItems, std::vector<std::string>() = {}));
479 std::vector<SessionItem*> expected;
480 for (
int i = 0; i < maxItems; ++i) {
482 expected.push_back(child);
483 EXPECT_TRUE(parent->insertItem(child, {tag1, -1}));
485 EXPECT_EQ(parent->getItems(tag1), expected);
489 EXPECT_FALSE(parent->insertItem(extra, {tag1, -1}));
492 delete parent->takeItem({tag1, 0});
493 expected.erase(expected.begin());
494 EXPECT_EQ(parent->getItems(tag1), expected);
497 parent->insertItem(extra, {tag1, -1});
498 expected.push_back(extra);
499 EXPECT_EQ(parent->getItems(tag1), expected);
506 const std::string tag1 =
"tag1";
507 const std::string tag2 =
"tag2";
508 const std::string modelType1 =
"ModelType1";
509 const std::string modelType2 =
"ModelType2";
510 const std::string modelType3 =
"ModelType3";
511 const std::string modelType4 =
"ModelType4";
513 auto parent = std::make_unique<SessionItem>();
515 TagInfo(tag1, 0, -1, std::vector<std::string>() = {modelType1, modelType2}));
516 parent->registerTag(
TagInfo(tag2, 0, -1, std::vector<std::string>() = {modelType3}));
523 EXPECT_FALSE(parent->insertItem(item1, {tag2, -1}));
524 EXPECT_FALSE(parent->insertItem(item3, {tag1, -1}));
527 parent->insertItem(item3, {tag2, -1});
528 parent->insertItem(item1, {tag1, -1});
529 parent->insertItem(item2, {tag1, -1});
531 std::vector<SessionItem*> expected = {item1, item2};
532 EXPECT_EQ(parent->getItems(tag1), expected);
534 EXPECT_EQ(parent->getItems(tag2), expected);
541 const std::string tag1 =
"tag1";
542 const std::string tag2 =
"tag2";
545 auto parent = std::make_unique<SessionItem>();
556 parent->insertItem(child_t2_c, {tag2, -1});
557 parent->insertItem(child_t1_a, {tag1, -1});
558 parent->insertItem(child_t1_b, {tag1, -1});
559 parent->insertItem(child_t2_b, {tag2, 1});
561 EXPECT_EQ(child_t1_a->tagRow().tag,
"tag1");
562 EXPECT_EQ(child_t1_b->tagRow().tag,
"tag1");
563 EXPECT_EQ(child_t2_a->tagRow().tag,
"tag2");
564 EXPECT_EQ(child_t2_b->tagRow().tag,
"tag2");
565 EXPECT_EQ(child_t2_c->tagRow().tag,
"tag2");
568 EXPECT_EQ(parentless_item.
tagRow().
tag,
"");
575 const std::string tag1 =
"tag1";
576 const std::string tag2 =
"tag2";
579 auto parent = std::make_unique<SessionItem>();
590 parent->insertItem(child_t2_c, {tag2, -1});
591 parent->insertItem(child_t1_a, {tag1, -1});
592 parent->insertItem(child_t1_b, {tag1, -1});
593 parent->insertItem(child_t2_b, {tag2, 1});
595 EXPECT_EQ(child_t1_a->tagRow().row, 0);
596 EXPECT_EQ(child_t1_b->tagRow().row, 1);
597 EXPECT_EQ(child_t2_a->tagRow().row, 0);
598 EXPECT_EQ(child_t2_b->tagRow().row, 1);
599 EXPECT_EQ(child_t2_c->tagRow().row, 2);
601 EXPECT_EQ(child_t1_a->tagRow().tag,
"tag1");
602 EXPECT_EQ(child_t1_b->tagRow().tag,
"tag1");
603 EXPECT_EQ(child_t2_a->tagRow().tag,
"tag2");
604 EXPECT_EQ(child_t2_b->tagRow().tag,
"tag2");
605 EXPECT_EQ(child_t2_c->tagRow().tag,
"tag2");
612 const std::string tag1 =
"tag1";
613 const std::string tag2 =
"tag2";
616 auto parent = std::make_unique<SessionItem>();
627 parent->insertItem(child_t2_c, {tag2, -1});
628 parent->insertItem(child_t1_a, {tag1, -1});
629 parent->insertItem(child_t1_b, {tag1, -1});
630 parent->insertItem(child_t2_b, {tag2, 1});
632 EXPECT_EQ(parent->tagRowOfItem(child_t1_a).row, 0);
633 EXPECT_EQ(parent->tagRowOfItem(child_t1_b).row, 1);
634 EXPECT_EQ(parent->tagRowOfItem(child_t2_a).row, 0);
635 EXPECT_EQ(parent->tagRowOfItem(child_t2_b).row, 1);
636 EXPECT_EQ(parent->tagRowOfItem(child_t2_c).row, 2);
638 EXPECT_EQ(parent->tagRowOfItem(child_t1_a).tag,
"tag1");
639 EXPECT_EQ(parent->tagRowOfItem(child_t1_b).tag,
"tag1");
640 EXPECT_EQ(parent->tagRowOfItem(child_t2_a).tag,
"tag2");
641 EXPECT_EQ(parent->tagRowOfItem(child_t2_b).tag,
"tag2");
642 EXPECT_EQ(parent->tagRowOfItem(child_t2_c).tag,
"tag2");
653 EXPECT_FALSE(variant.isValid());
666 EXPECT_TRUE(variant.isValid());
685 EXPECT_EQ(item.
toolTip(),
"abc");
704 const std::string tag1 =
"tag1";
705 const std::string tag2 =
"tag2";
708 auto parent = std::make_unique<SessionItem>();
717 parent->insertItem(child_t2_a, {tag2, -1});
718 parent->insertItem(child_t2_b, {tag2, -1});
720 EXPECT_EQ(parent->itemCount(tag1), 1);
721 EXPECT_EQ(parent->itemCount(tag2), 2);
Provides registration of SessionItem pointers and their unique identifiers in global memory pool.
std::vector< int > roles() const
The main object representing an editable/displayable/serializable entity.
SessionItemTags * itemTags()
Returns pointer to internal collection of tag-registered items (non-const version).
std::string identifier() const
Returns unique identifier.
SessionItem * setEditable(bool value)
Sets editable flag to given value (fluent interface).
SessionItem * setEnabled(bool value)
Sets enabled flag to given value (fluent interface).
SessionItem * setToolTip(const std::string &tooltip)
Sets item tooltip (fluent interface).
bool hasData(int role=ItemDataRole::DATA) const
Returns true if item has data on board with given role.
std::string editorType() const
Returns editor type.
bool isEnabled() const
Returns true if this item has enabled flag set.
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.
std::string toolTip() const
Returns item tooltip, if exists.
bool isEditable() const
Returns true if this item has editable flag set.
SessionItem * setEditorType(const std::string &editor_type)
Sets editor type (fluent interface).
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.
T data(int role=ItemDataRole::DATA) const
Returns data of given type T for given role.
std::vector< SessionItem * > children() const
Returns vector of children formed from all chidlren from all tags.
SessionItemData * itemData()
Returns pointer to item's data container (non-const version).
virtual std::string displayName() const
Returns display name.
bool insertItem(SessionItem *item, const TagRow &tagrow)
Insert item into given tag under the given row.
TagRow tagRow() const
Returns TagRow of this item under which it is accessible through its parent.
virtual SessionItem * setDisplayName(const std::string &name)
Sets display name (fluent interface).
model_type modelType() const
Returns item's model type.
Holds info about single tag for SessionItem.
static TagInfo universalTag(std::string name, std::vector< std::string > modelTypes={})
Constructs universal tag intended for unlimited amount of various items.
static TagRow append(const std::string &tag_name={})
Returns TagRow corresponding to the append to tag_name.
const model_type BaseType
const std::string double_type_name
const std::string int_type_name
const int TOOLTIP
tooltip for item's data
const int EDITORTYPE
type of custom editor for the data role
const int DATA
main data role
const int APPEARANCE
appearance flag
const int LIMITS
possibly limits on item's data
const int DISPLAY
display name
const int IDENTIFIER
unique identifier
MVVM_MODEL_EXPORT int IndexOfChild(const SessionItem *parent, const SessionItem *child)
Returns index in children array corresponding to given child.
MVVM_MODEL_EXPORT bool HasTag(const SessionItem &item, const std::string &tag)
Returns true if given item has registered tag.
materialitems.h Collection of materials to populate MaterialModel.
TEST_F(SessionItemTest, initialState)