File indexing completed on 2024-10-06 06:37:54

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 /** \file itemgroup.h
0009  *  \brief Contains the ItemGroup object.
0010  */
0011 
0012 #ifndef STEPCORE_ITEMGROUP_H
0013 #define STEPCORE_ITEMGROUP_H
0014 
0015 
0016 #include <vector> // XXX: Replace if Qt is enabled.
0017 
0018 #include "types.h"
0019 #include "item.h"
0020 
0021 
0022 namespace StepCore
0023 {
0024 
0025 
0026 /** \ingroup world
0027  *  \brief Groups several items together
0028  */
0029 class ItemGroup : public Item
0030 {
0031     STEPCORE_OBJECT(ItemGroup)
0032 
0033 public:
0034     /** Constructs empty group */
0035     explicit ItemGroup(const QString& name = QString()) : Item(name) {}
0036     /** Constructs a copy of the group (deep copy) */
0037     ItemGroup(const ItemGroup& group);
0038     /** Destroys the group and all its subitems */
0039     ~ItemGroup();
0040 
0041     /** Assignment operator (deep copy)
0042      *  \warning Do not call this on groups already attached to the world */
0043     ItemGroup& operator=(const ItemGroup& group);
0044 
0045     /** Get list of all direct child items in the ItemGroup */
0046     const ItemList& items() const  { return _items; }
0047 
0048     bool contains(const Item* item) const { return std::find(_items.begin(), _items.end(), item) != _items.end(); }
0049 
0050     /** Get list of all items in the ItemGroup
0051      *  \note This operation takes long time since it
0052      *        recursively traverses all child groups */
0053     ItemList allItems() const { ItemList l; allItems(&l); return l; }
0054     /** Get list of all items in the ItemGroup
0055      *  \param items Array to store items
0056      *  \note This operation takes long time since it
0057      *        recursively traverses all child groups */
0058     void allItems(ItemList* items) const;
0059 
0060     /** Add new item to the group */
0061     virtual void addItem(Item* item);
0062     /** Remove item from the group (you should delete item yourself) */
0063     virtual void removeItem(Item* item);
0064     /** Delete item from the group (it actually deletes item) */
0065     virtual void deleteItem(Item* item) { removeItem(item); delete item; }
0066 
0067     /** Deletes all items */
0068     void clear();
0069 
0070     /** Finds direct child item in items() */
0071     int  childItemIndex(const Item* item) const;
0072     /** Get direct child count */
0073     int  childItemCount() const { return _items.size(); }
0074     /** Get direct child item by its index */
0075     Item* childItem(int index) const { return _items[index]; }
0076     /** Get direct child item by its name */
0077     Item* childItem(const QString& name) const;
0078     /** Get any descendant item by its name */
0079     Item* item(const QString& name) const;
0080 
0081     /** Recursively call setWorld for all children objects */
0082     void setWorld(World* world) override;
0083     /** Recursively call worldItemRemoved for all children objects */
0084     void worldItemRemoved(Item* item) override;
0085     
0086 private:
0087     ItemList  _items;
0088 };
0089 
0090 
0091 } // namespace StepCore
0092 
0093 
0094 #endif