File indexing completed on 2024-04-28 07:39:46

0001 /*
0002     SPDX-FileCopyrightText: 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
0003     SPDX-FileCopyrightText: 2014 Inge Wallin <inge@lysator.liu.se>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "itemgroup.h"
0009 #include "world.h"  // FIXME: This is ugly
0010 
0011 namespace StepCore
0012 {
0013 
0014 
0015 STEPCORE_META_OBJECT(ItemGroup, QT_TRANSLATE_NOOP("ObjectClass", "ItemGroup"), QT_TRANSLATE_NOOP("ObjectDescription", "ItemGroup"), 
0016              0, STEPCORE_SUPER_CLASS(Item),)
0017 
0018 
0019 ItemGroup::ItemGroup(const ItemGroup& group)
0020     : Item()
0021 {
0022     *this = group;
0023 }
0024 
0025 void ItemGroup::setWorld(World* world)
0026 {
0027     ItemList::const_iterator end = _items.end();
0028     for(ItemList::const_iterator it = _items.begin(); it != end; ++it)
0029         (*it)->setWorld(world);
0030     Item::setWorld(world);
0031 }
0032 
0033 void ItemGroup::worldItemRemoved(Item* item)
0034 {
0035     ItemList::const_iterator end = _items.end();
0036     for(ItemList::const_iterator it = _items.begin(); it != end; ++it)
0037         (*it)->worldItemRemoved(item);
0038 }
0039 
0040 void ItemGroup::addItem(Item* item)
0041 {
0042     _items.push_back(item);
0043 
0044     if(world()) world()->worldItemAdded(item);
0045 
0046     item->setGroup(this);
0047     item->setWorld(this->world());
0048 }
0049 
0050 void ItemGroup::removeItem(Item* item)
0051 {
0052     item->setWorld(nullptr);
0053     item->setGroup(nullptr);
0054 
0055     if(world()) world()->worldItemRemoved(item);
0056 
0057     ItemList::iterator i = std::find(_items.begin(), _items.end(), item);
0058     STEPCORE_ASSERT_NOABORT(i != _items.end());
0059     _items.erase(i);
0060 }
0061 
0062 void ItemGroup::clear()
0063 {
0064     ItemList::const_iterator end = _items.end();
0065     for(ItemList::const_iterator it = _items.begin(); it != end; ++it) {
0066         (*it)->setWorld(nullptr);
0067         (*it)->setGroup(nullptr);
0068         if(world()) world()->worldItemRemoved(*it);
0069     }
0070 
0071     for(ItemList::const_iterator it = _items.begin(); it != end; ++it) {
0072         delete *it;
0073     }
0074 
0075     _items.clear();
0076 }
0077 
0078 ItemGroup::~ItemGroup()
0079 {
0080     clear();
0081 }
0082 
0083 ItemGroup& ItemGroup::operator=(const ItemGroup& group)
0084 {
0085 
0086     /*
0087     item->setGroup(this);
0088     item->setWorld(this->world());
0089     _items.push_back(item);
0090 
0091     if(world()) {
0092         //world()->worldItemAdded(item);
0093         ItemGroup* gr = dynamic_cast<ItemGroup*>(item);
0094         if(gr) gr->groupItemsAdded();
0095     }
0096     */
0097 
0098     if(this == &group) return *this;
0099 
0100     clear();
0101 
0102     _items.reserve(group._items.size());
0103 
0104     const ItemList::const_iterator gr_end = group._items.end();
0105     for(ItemList::const_iterator it = group._items.begin(); it != gr_end; ++it) {
0106         StepCore::Item* item = static_cast<Item*>( (*it)->metaObject()->cloneObject(*(*it)) );
0107         _items.push_back(item);
0108     }
0109 
0110     const ItemList::const_iterator end = _items.end();
0111     for(ItemList::const_iterator it = _items.begin(); it != end; ++it) {
0112         (*it)->setGroup(this);
0113     }
0114 
0115     Item::operator=(group);
0116 
0117     // NOTE: We don't change world() here
0118 
0119     return *this;
0120 }
0121 
0122 int ItemGroup::childItemIndex(const Item* item) const
0123 {
0124     ItemList::const_iterator o = std::find(_items.begin(), _items.end(), item);
0125     STEPCORE_ASSERT_NOABORT(o != _items.end());
0126     return std::distance(_items.begin(), o);
0127 }
0128 
0129 Item* ItemGroup::childItem(const QString& name) const
0130 {
0131     ItemList::const_iterator end = _items.end();
0132     for(ItemList::const_iterator it = _items.begin(); it != end; ++it)
0133         if((*it)->name() == name) return *it;
0134     return nullptr;
0135 }
0136 
0137 Item* ItemGroup::item(const QString& name) const
0138 {
0139     if(name.isEmpty()) return nullptr;
0140     ItemList::const_iterator end = _items.end();
0141     for(ItemList::const_iterator it = _items.begin(); it != end; ++it) {
0142         if((*it)->name() == name) return *it;
0143         if((*it)->metaObject()->inherits<ItemGroup>()) {
0144             Item* ret = static_cast<ItemGroup*>(*it)->item(name);
0145             if(ret) return ret;
0146         }
0147     }
0148     return nullptr;
0149 }
0150 
0151 void ItemGroup::allItems(ItemList* items) const
0152 {
0153     items->reserve(_items.size());
0154     ItemList::const_iterator end = _items.end();
0155     for(ItemList::const_iterator it = _items.begin(); it != end; ++it) {
0156         items->push_back(*it);
0157         if((*it)->metaObject()->inherits<ItemGroup>())
0158             static_cast<ItemGroup*>(*it)->allItems(items);
0159     }
0160 }
0161 
0162 
0163 } // namespace StepCore